Skip to content

Commit

Permalink
Fix CI
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwizp committed Mar 16, 2020
1 parent b5bdaf5 commit 3665c08
Showing 1 changed file with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
* specific language governing permissions and limitations
* under the License.
*/

import { uniq, isEqual } from 'lodash';
import { i18n } from '@kbn/i18n';
import { ExpressionFunctionDefinition } from '../../expression_functions';
import { KibanaContext } from '../../expression_types';
import { Query, Filter } from '../../../../data/common';

interface Arguments {
q?: string | null;
Expand All @@ -35,6 +36,17 @@ export type ExpressionFunctionKibanaContext = ExpressionFunctionDefinition<
Promise<KibanaContext>
>;

export const mergeInput = <T = any>(input: T | T[] = [], argsValue: string): T[] => {
const parsedArgValue: T | T[] = JSON.parse(argsValue || '[]');
return uniq<T>(
[
...(Array.isArray(parsedArgValue) ? parsedArgValue : [parsedArgValue]),
...(Array.isArray(input) ? input : [input]),
],
isEqual
);
};

export const kibanaContextFunction: ExpressionFunctionKibanaContext = {
name: 'kibana_context',
type: 'kibana_context',
Expand Down Expand Up @@ -75,10 +87,9 @@ export const kibanaContextFunction: ExpressionFunctionKibanaContext = {
},

async fn(input, args, { getSavedObject }) {
const queryArg = args.q ? JSON.parse(args.q) : input?.query || [];
const timeRange = args.timeRange ? JSON.parse(args.timeRange) : input?.timeRange;
const filters = args.filters ? JSON.parse(args.filters) : input?.filters || [];
const queries = Array.isArray(queryArg) ? queryArg : [queryArg];
let queries = mergeInput<Query>(input?.query, args?.q || '[]');
let filters = mergeInput<Filter>(input?.filters, args?.filters || '[]');

if (args.savedSearchId) {
if (typeof getSavedObject !== 'function') {
Expand All @@ -90,10 +101,10 @@ export const kibanaContextFunction: ExpressionFunctionKibanaContext = {
}
const obj = await getSavedObject('search', args.savedSearchId);
const search = obj.attributes.kibanaSavedObjectMeta as { searchSourceJSON: string };
const data = JSON.parse(search.searchSourceJSON) as { query: string; filter: any[] };
const data = JSON.parse(search.searchSourceJSON) as { query: Query[]; filter: Filter[] };

queries.push(...data.query);
filters.push(...data.filter);
queries = queries.concat(data.query);
filters = filters.concat(data.filter);
}

return {
Expand Down

0 comments on commit 3665c08

Please sign in to comment.