1919
2020import moment from 'moment' ;
2121import { i18n } from '@kbn/i18n' ;
22- import { isPlainObject , cloneDeep } from 'lodash' ;
22+ import { cloneDeep , isPlainObject } from 'lodash' ;
23+ import { SearchParams } from 'elasticsearch' ;
24+ import { TimeCache } from './time_cache' ;
25+ import { SearchAPI } from './search_api' ;
26+ import { Opts , Type , Data , UrlObject , Bool , Requests , Query , ContextVarsObject } from './types' ;
2327
24- const TIMEFILTER = '%timefilter%' ;
25- const AUTOINTERVAL = '%autointerval%' ;
26- const MUST_CLAUSE = '%dashboard_context-must_clause%' ;
27- const FILTER_CLAUSE = '%dashboard_context-filter_clause %' ;
28- const MUST_NOT_CLAUSE = '%dashboard_context-must_not_clause %' ;
28+ const TIMEFILTER : string = '%timefilter%' ;
29+ const AUTOINTERVAL : string = '%autointerval%' ;
30+ const MUST_CLAUSE : string = '%dashboard_context-must_clause%' ;
31+ const MUST_NOT_CLAUSE : string = '%dashboard_context-must_not_clause %' ;
32+ const FILTER_CLAUSE : string = '%dashboard_context-filter_clause %' ;
2933
3034// These values may appear in the 'url': { ... } object
31- const LEGACY_CONTEXT = '%context_query%' ;
32- const CONTEXT = '%context%' ;
33- const TIMEFIELD = '%timefield%' ;
35+ const LEGACY_CONTEXT : string = '%context_query%' ;
36+ const CONTEXT : string = '%context%' ;
37+ const TIMEFIELD : string = '%timefield%' ;
3438
3539/**
3640 * This class parses ES requests specified in the data.url objects.
3741 */
3842export class EsQueryParser {
39- constructor ( timeCache , searchAPI , filters , onWarning ) {
43+ _timeCache : TimeCache ;
44+ _searchAPI : SearchAPI ;
45+ _filters : Bool ;
46+ _onWarning : ( ...args : string [ ] ) => void ;
47+
48+ constructor (
49+ timeCache : TimeCache ,
50+ searchAPI : SearchAPI ,
51+ filters : Bool ,
52+ onWarning : ( ...args : string [ ] ) => void
53+ ) {
4054 this . _timeCache = timeCache ;
4155 this . _searchAPI = searchAPI ;
4256 this . _filters = filters ;
@@ -47,7 +61,7 @@ export class EsQueryParser {
4761 /**
4862 * Update request object, expanding any context-aware keywords
4963 */
50- parseUrl ( dataObject , url ) {
64+ parseUrl ( dataObject : Data , url : UrlObject ) {
5165 let body = url . body ;
5266 let context = url [ CONTEXT ] ;
5367 delete url [ CONTEXT ] ;
@@ -167,13 +181,13 @@ export class EsQueryParser {
167181 // Use dashboard context
168182 const newQuery = cloneDeep ( this . _filters ) ;
169183 if ( timefield ) {
170- newQuery . bool . must . push ( body . query ) ;
184+ newQuery . bool ! . must ! . push ( body . query ) ;
171185 }
172186 body . query = newQuery ;
173187 }
174188 }
175189
176- this . _injectContextVars ( body . aggs , false ) ;
190+ this . _injectContextVars ( body . aggs ! , false ) ;
177191 return { dataObject, url } ;
178192 }
179193
@@ -182,8 +196,8 @@ export class EsQueryParser {
182196 * @param {object[] } requests each object is generated by parseUrl()
183197 * @returns {Promise<void> }
184198 */
185- async populateData ( requests ) {
186- const esSearches = requests . map ( ( r ) => r . url ) ;
199+ async populateData ( requests : Requests [ ] ) {
200+ const esSearches = requests . map ( ( r : Requests ) => r . url ) ;
187201 const data$ = this . _searchAPI . search ( esSearches ) ;
188202
189203 const results = await data$ . toPromise ( ) ;
@@ -198,7 +212,7 @@ export class EsQueryParser {
198212 * @param {* } obj
199213 * @param {boolean } isQuery - if true, the `obj` belongs to the req's query portion
200214 */
201- _injectContextVars ( obj , isQuery ) {
215+ _injectContextVars ( obj : Query | SearchParams [ 'body' ] [ 'aggs' ] , isQuery : boolean ) {
202216 if ( obj && typeof obj === 'object' ) {
203217 if ( Array . isArray ( obj ) ) {
204218 // For arrays, replace MUST_CLAUSE and MUST_NOT_CLAUSE string elements
@@ -239,7 +253,7 @@ export class EsQueryParser {
239253 }
240254 } else {
241255 for ( const prop of Object . keys ( obj ) ) {
242- const subObj = obj [ prop ] ;
256+ const subObj = ( obj as ContextVarsObject ) [ prop ] ;
243257 if ( ! subObj || typeof obj !== 'object' ) continue ;
244258
245259 // replace "interval": { "%autointerval%": true|integer } with
@@ -260,7 +274,9 @@ export class EsQueryParser {
260274 ) ;
261275 }
262276 const bounds = this . _timeCache . getTimeBounds ( ) ;
263- obj . interval = EsQueryParser . _roundInterval ( ( bounds . max - bounds . min ) / size ) ;
277+ ( obj as ContextVarsObject ) . interval = EsQueryParser . _roundInterval (
278+ ( bounds . max - bounds . min ) / size
279+ ) ;
264280 continue ;
265281 }
266282
@@ -269,7 +285,7 @@ export class EsQueryParser {
269285 case 'min' :
270286 case 'max' :
271287 // Replace {"%timefilter%": "min|max", ...} object with a timestamp
272- obj [ prop ] = this . _getTimeBound ( subObj , subObj [ TIMEFILTER ] ) ;
288+ ( obj as ContextVarsObject ) [ prop ] = this . _getTimeBound ( subObj , subObj [ TIMEFILTER ] ) ;
273289 continue ;
274290 case true :
275291 // Replace {"%timefilter%": true, ...} object with the "range" object
@@ -302,7 +318,7 @@ export class EsQueryParser {
302318 * @param {object } obj
303319 * @return {object }
304320 */
305- _createRangeFilter ( obj ) {
321+ _createRangeFilter ( obj : Opts ) {
306322 obj . gte = moment ( this . _getTimeBound ( obj , 'min' ) ) . toISOString ( ) ;
307323 obj . lte = moment ( this . _getTimeBound ( obj , 'max' ) ) . toISOString ( ) ;
308324 obj . format = 'strict_date_optional_time' ;
@@ -320,9 +336,9 @@ export class EsQueryParser {
320336 * @param {'min'|'max' } type
321337 * @returns {* }
322338 */
323- _getTimeBound ( opts , type ) {
339+ _getTimeBound ( opts : Opts , type : Type ) : number {
324340 const bounds = this . _timeCache . getTimeBounds ( ) ;
325- let result = bounds [ type ] ;
341+ let result = bounds [ type ] ?. valueOf ( ) || 0 ;
326342
327343 if ( opts . shift ) {
328344 const shift = opts . shift ;
@@ -380,7 +396,7 @@ export class EsQueryParser {
380396 * @param interval (ms)
381397 * @returns {string }
382398 */
383- static _roundInterval ( interval ) {
399+ static _roundInterval ( interval : number ) : string {
384400 switch ( true ) {
385401 case interval <= 500 : // <= 0.5s
386402 return '100ms' ;
0 commit comments