import { uniqBy } from 'lodash' ; import { i18n } from '@osd/i18n' ; import { ExpressionFunctionDefinition } from '../../expression_functions' ; import { OpenSearchDashboardsContext } from '../../expression_types' ; import { Query , uniqFilters } from '../../../../data/common' ; interface Arguments { q ? : string | null ; filters ? : string | null ; timeRange ? : string | null ; savedSearchId ? : string | null ; } export type ExpressionFunctionOpenSearchDashboardsContext = ExpressionFunctionDefinition < 'opensearch_dashboards_context' , OpenSearchDashboardsContext | null , Arguments , Promise < OpenSearchDashboardsContext > > ; const getParsedValue = ( data , defaultValue ) => typeof data === 'string' && data . length ? JSON . parse ( data ) || defaultValue : defaultValue ; const mergeQueries = ( first = [ ] , second ) => uniqBy < Query > ( [ ... ( Array . isArray ( first ) ? first : [ first ] ) , ... ( Array . isArray ( second ) ? second : [ second ] ) ] , ( n ) => JSON . stringify ( n . query ) ) ; export const opensearchDashboardsContextFunction = { name : 'opensearch_dashboards_context' , type : 'opensearch_dashboards_context' , inputTypes : [ 'opensearch_dashboards_context' , 'null' ] , help : i18n . translate ( 'expressions.functions.opensearch_dashboards_context.help' , { defaultMessage : 'Updates opensearch dashboards global context' , } ) , args : { q : { types : [ 'string' , 'null' ] , aliases : [ 'query' , '_' ] , default : null , help : i18n . translate ( 'expressions.functions.opensearch_dashboards_context.q.help' , { defaultMessage : 'Specify OpenSearch Dashboards free form text query' , } ) , } , filters : { types : [ 'string' , 'null' ] , default : '"[]"' , help : i18n . translate ( 'expressions.functions.opensearch_dashboards_context.filters.help' , { defaultMessage : 'Specify OpenSearch Dashboards generic filters' , } ) , } , timeRange : { types : [ 'string' , 'null' ] , default : null , help : i18n . translate ( 'expressions.functions.opensearch_dashboards_context.timeRange.help' , { defaultMessage : 'Specify OpenSearch Dashboards time range filter' , } ) , } , savedSearchId : { types : [ 'string' , 'null' ] , default : null , help : i18n . translate ( 'expressions.functions.opensearch_dashboards_context.savedSearchId.help' , { defaultMessage : 'Specify saved search ID to be used for queries and filters' , } ) , } , } , async fn ( input , args , { getSavedObject } ) { const timeRange = getParsedValue ( args . timeRange , input ?. timeRange ) ; let queries = mergeQueries ( input ?. query , getParsedValue ( args ?. q , [ ] ) ) ; let filters = [ ... ( input ?. filters || [ ] ) , ... getParsedValue ( args ?. filters , [ ] ) ] ; if ( args . savedSearchId ) { if ( typeof getSavedObject !== 'function' ) { throw new Error ( '"getSavedObject" function not available in execution context. ' + 'When you execute expression you need to add extra execution context ' + 'as the third argument and provide "getSavedObject" implementation.' ) ; } const obj = await getSavedObject ( 'search' , args . savedSearchId ) ; const search = obj . attributes . kibanaSavedObjectMeta as { searchSourceJSON : string ; } ; const { query , filter } = getParsedValue ( search . searchSourceJSON , { } ) ; if ( query ) { queries = mergeQueries ( queries , query ) ; } if ( filter ) { filters = [ ... filters , ... ( Array . isArray ( filter ) ? filter : [ filter ] ) ] ; } } return { type : 'opensearch_dashboards_context' , query : queries , filters : uniqFilters ( filters ) . filter ( ( f ) => ! f . meta ?. disabled ) , timeRange , } ; } , } ;