66
77import { flatten } from 'lodash' ;
88import moment from 'moment-timezone' ;
9- import chrome from 'ui/chrome' ;
10- import { npStart } from 'ui/new_platform' ;
119import { TimeRange } from 'src/plugins/data/common' ;
1210import { ExpressionFunctionDefinition , DatatableRow } from 'src/plugins/expressions/public' ;
1311import { fetch } from '../../common/lib/fetch' ;
1412// @ts -ignore untyped local
1513import { buildBoolArray } from '../../server/lib/build_bool_array' ;
1614import { Datatable , Filter } from '../../types' ;
1715import { getFunctionHelp } from '../../i18n' ;
16+ import { InitializeArguments } from './' ;
1817
1918interface Arguments {
2019 query : string ;
@@ -30,110 +29,118 @@ interface Arguments {
3029 * @param timeRange time range to parse
3130 * @param timeZone time zone to do the parsing in
3231 */
33- function parseDateMath ( timeRange : TimeRange , timeZone : string ) {
32+ function parseDateMath (
33+ timeRange : TimeRange ,
34+ timeZone : string ,
35+ timefilter : InitializeArguments [ 'timefilter' ]
36+ ) {
3437 // the datemath plugin always parses dates by using the current default moment time zone.
3538 // to use the configured time zone, we are switching just for the bounds calculation.
3639 const defaultTimezone = moment ( ) . zoneName ( ) ;
3740 moment . tz . setDefault ( timeZone ) ;
3841
39- const parsedRange = npStart . plugins . data . query . timefilter . timefilter . calculateBounds ( timeRange ) ;
42+ const parsedRange = timefilter . calculateBounds ( timeRange ) ;
4043
4144 // reset default moment timezone
4245 moment . tz . setDefault ( defaultTimezone ) ;
4346
4447 return parsedRange ;
4548}
4649
47- export function timelion ( ) : ExpressionFunctionDefinition <
50+ type TimelionFunction = ExpressionFunctionDefinition <
4851 'timelion' ,
4952 Filter ,
5053 Arguments ,
5154 Promise < Datatable >
52- > {
53- const { help, args : argHelp } = getFunctionHelp ( ) . timelion ;
55+ > ;
5456
55- return {
56- name : 'timelion' ,
57- type : 'datatable' ,
58- inputTypes : [ 'filter' ] ,
59- help,
60- args : {
61- query : {
62- types : [ 'string' ] ,
63- aliases : [ '_' , 'q' ] ,
64- help : argHelp . query ,
65- default : '".es(*)"' ,
66- } ,
67- interval : {
68- types : [ 'string' ] ,
69- help : argHelp . interval ,
70- default : 'auto' ,
71- } ,
72- from : {
73- types : [ 'string' ] ,
74- help : argHelp . from ,
75- default : 'now-1y' ,
76- } ,
77- to : {
78- types : [ 'string' ] ,
79- help : argHelp . to ,
80- default : 'now' ,
81- } ,
82- timezone : {
83- types : [ 'string' ] ,
84- help : argHelp . timezone ,
85- default : 'UTC' ,
57+ export function timelionFunctionFactory ( initialize : InitializeArguments ) : ( ) => TimelionFunction {
58+ return ( ) => {
59+ const { help, args : argHelp } = getFunctionHelp ( ) . timelion ;
60+
61+ return {
62+ name : 'timelion' ,
63+ type : 'datatable' ,
64+ inputTypes : [ 'filter' ] ,
65+ help,
66+ args : {
67+ query : {
68+ types : [ 'string' ] ,
69+ aliases : [ '_' , 'q' ] ,
70+ help : argHelp . query ,
71+ default : '".es(*)"' ,
72+ } ,
73+ interval : {
74+ types : [ 'string' ] ,
75+ help : argHelp . interval ,
76+ default : 'auto' ,
77+ } ,
78+ from : {
79+ types : [ 'string' ] ,
80+ help : argHelp . from ,
81+ default : 'now-1y' ,
82+ } ,
83+ to : {
84+ types : [ 'string' ] ,
85+ help : argHelp . to ,
86+ default : 'now' ,
87+ } ,
88+ timezone : {
89+ types : [ 'string' ] ,
90+ help : argHelp . timezone ,
91+ default : 'UTC' ,
92+ } ,
8693 } ,
87- } ,
88- fn : ( input , args ) : Promise < Datatable > => {
89- // Timelion requires a time range. Use the time range from the timefilter element in the
90- // workpad, if it exists. Otherwise fall back on the function args.
91- const timeFilter = input . and . find ( and => and . type === 'time' ) ;
92- const range = timeFilter
93- ? { min : timeFilter . from , max : timeFilter . to }
94- : parseDateMath ( { from : args . from , to : args . to } , args . timezone ) ;
94+ fn : ( input , args ) : Promise < Datatable > => {
95+ // Timelion requires a time range. Use the time range from the timefilter element in the
96+ // workpad, if it exists. Otherwise fall back on the function args.
97+ const timeFilter = input . and . find ( and => and . type === 'time' ) ;
98+ const range = timeFilter
99+ ? { min : timeFilter . from , max : timeFilter . to }
100+ : parseDateMath ( { from : args . from , to : args . to } , args . timezone , initialize . timefilter ) ;
95101
96- const body = {
97- extended : {
98- es : {
99- filter : {
100- bool : {
101- must : buildBoolArray ( input . and ) ,
102+ const body = {
103+ extended : {
104+ es : {
105+ filter : {
106+ bool : {
107+ must : buildBoolArray ( input . and ) ,
108+ } ,
102109 } ,
103110 } ,
104111 } ,
105- } ,
106- sheet : [ args . query ] ,
107- time : {
108- from : range . min ,
109- to : range . max ,
110- interval : args . interval ,
111- timezone : args . timezone ,
112- } ,
113- } ;
112+ sheet : [ args . query ] ,
113+ time : {
114+ from : range . min ,
115+ to : range . max ,
116+ interval : args . interval ,
117+ timezone : args . timezone ,
118+ } ,
119+ } ;
114120
115- return fetch ( chrome . addBasePath ( `/api/timelion/run` ) , {
116- method : 'POST' ,
117- responseType : 'json' ,
118- data : body ,
119- } ) . then ( resp => {
120- const seriesList = resp . data . sheet [ 0 ] . list ;
121- const rows = flatten (
122- seriesList . map ( ( series : { data : any [ ] ; label : string } ) =>
123- series . data . map ( row => ( { '@timestamp' : row [ 0 ] , value : row [ 1 ] , label : series . label } ) )
124- )
125- ) as DatatableRow [ ] ;
121+ return fetch ( initialize . prependBasePath ( `/api/timelion/run` ) , {
122+ method : 'POST' ,
123+ responseType : 'json' ,
124+ data : body ,
125+ } ) . then ( resp => {
126+ const seriesList = resp . data . sheet [ 0 ] . list ;
127+ const rows = flatten (
128+ seriesList . map ( ( series : { data : any [ ] ; label : string } ) =>
129+ series . data . map ( row => ( { '@timestamp' : row [ 0 ] , value : row [ 1 ] , label : series . label } ) )
130+ )
131+ ) as DatatableRow [ ] ;
126132
127- return {
128- type : 'datatable' ,
129- columns : [
130- { name : '@timestamp' , type : 'date' } ,
131- { name : 'value' , type : 'number' } ,
132- { name : 'label' , type : 'string' } ,
133- ] ,
134- rows,
135- } ;
136- } ) ;
137- } ,
133+ return {
134+ type : 'datatable' ,
135+ columns : [
136+ { name : '@timestamp' , type : 'date' } ,
137+ { name : 'value' , type : 'number' } ,
138+ { name : 'label' , type : 'string' } ,
139+ ] ,
140+ rows,
141+ } ;
142+ } ) ;
143+ } ,
144+ } ;
138145 } ;
139146}
0 commit comments