1
+ const eng = require ( '../../plugins/engine' )
2
+ const { parseCliQL } = require ( '../cliql' )
3
+ const { Transform } = require ( 'stream' )
4
+ const { scanClickhouse, scanFingerprints } = require ( '../db/clickhouse' )
5
+
6
+ module . exports . checkCustomPlugins = async ( options ) => {
7
+ options . API = options . API || {
8
+ logql : async ( query , start , end , limit ) => {
9
+ const params = {
10
+ query,
11
+ start,
12
+ end,
13
+ limit,
14
+ direction : 'backward' ,
15
+ step : '60s'
16
+ }
17
+ const req = {
18
+ query : params
19
+ }
20
+ const res = new Transform ( {
21
+ transform ( chunk , encoding , callback ) {
22
+ callback ( null , chunk )
23
+ }
24
+ } )
25
+ res . writeHead = ( ) => { }
26
+ const cliqlParams = parseCliQL ( req . query . query )
27
+ if ( cliqlParams ) {
28
+ scanClickhouse ( cliqlParams , { res } , params )
29
+ } else {
30
+ await scanFingerprints (
31
+ req . query ,
32
+ { res : res }
33
+ )
34
+ }
35
+ let str = ''
36
+ res . on ( 'data' , ( d ) => {
37
+ str += d
38
+ } )
39
+ await new Promise ( ( resolve , reject ) => {
40
+ res . once ( 'error' , reject )
41
+ res . once ( 'close' , resolve )
42
+ res . once ( 'end' , resolve )
43
+ } )
44
+ return JSON . parse ( str )
45
+ } /* ,
46
+ promql: async () => {
47
+
48
+ } */
49
+ }
50
+ const plugins = eng . getPlg ( { type : 'custom_processor' } )
51
+ for ( const plugin of Object . values ( plugins ) ) {
52
+ for ( const e of Object . entries ( options ) ) {
53
+ plugin [ e [ 0 ] ] = e [ 1 ]
54
+ }
55
+ if ( plugin . check ( ) ) {
56
+ return await plugin . process ( )
57
+ }
58
+ }
59
+ }
0 commit comments