Skip to content

Commit

Permalink
add apply query trigger
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Li <[email protected]>
  • Loading branch information
joshuali925 committed Jun 4, 2024
1 parent 2f8e559 commit ee51bce
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 4 deletions.
45 changes: 45 additions & 0 deletions src/plugins/data/public/actions/apply_query_action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { i18n } from '@osd/i18n';
import { ActionByType, createAction } from '../../../ui_actions/public';
import { Query, TimefilterContract, TimeRange } from '..';
import { QueryStringContract } from '../query/query_string';

export const ACTION_GLOBAL_APPLY_QUERY = 'ACTION_GLOBAL_APPLY_QUERY';

export interface ApplyGlobalQueryActionContext {
query: Query;
dateRange?: TimeRange;
timeFieldName?: string;
}

async function isCompatible(context: ApplyGlobalQueryActionContext) {
return context.query !== undefined;
}

export function createQueryAction(
queryString: QueryStringContract,
timeFilter: TimefilterContract
): ActionByType<typeof ACTION_GLOBAL_APPLY_QUERY> {
return createAction<typeof ACTION_GLOBAL_APPLY_QUERY>({
type: ACTION_GLOBAL_APPLY_QUERY,
id: ACTION_GLOBAL_APPLY_QUERY,
order: 100,
getIconType: () => 'search',
getDisplayName: () => {
return i18n.translate('data.filter.applyFilterActionTitle', {
defaultMessage: 'Apply query to current view',
});
},
isCompatible,
execute: async (context: ApplyGlobalQueryActionContext) => {
queryString.setQuery(context.query);
if (context.dateRange) {
timeFilter.setTime(context.dateRange);
}
},
});
}
5 changes: 5 additions & 0 deletions src/plugins/data/public/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ export {
createFilterAction,
ApplyGlobalFilterActionContext,
} from './apply_filter_action';
export {
ACTION_GLOBAL_APPLY_QUERY,
createQueryAction,
ApplyGlobalQueryActionContext,
} from './apply_query_action';
export { createFiltersFromValueClickAction } from './filters/create_filters_from_value_click';
export { createFiltersFromRangeSelectAction } from './filters/create_filters_from_range_select';
export * from './select_range_action';
Expand Down
6 changes: 5 additions & 1 deletion src/plugins/data/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,11 @@ export {

export { isTimeRange, isQuery, isFilter, isFilters } from '../common';

export { ACTION_GLOBAL_APPLY_FILTER, ApplyGlobalFilterActionContext } from './actions';
export {
ACTION_GLOBAL_APPLY_FILTER,
ApplyGlobalFilterActionContext,
ApplyGlobalQueryActionContext,
} from './actions';

export * from '../common/field_mapping';

Expand Down
10 changes: 10 additions & 0 deletions src/plugins/data/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import {
SELECT_RANGE_TRIGGER,
VALUE_CLICK_TRIGGER,
APPLY_FILTER_TRIGGER,
APPLY_QUERY_TRIGGER,
} from '../../ui_actions/public';
import {
ACTION_GLOBAL_APPLY_FILTER,
Expand All @@ -84,6 +85,9 @@ import {
ValueClickActionContext,
createValueClickAction,
createSelectRangeAction,
ACTION_GLOBAL_APPLY_QUERY,
ApplyGlobalQueryActionContext,
createQueryAction,
} from './actions';

import { SavedObjectsClientPublicToCommon } from './index_patterns';
Expand All @@ -96,6 +100,7 @@ import { DEFAULT_DATA_SOURCE_TYPE } from './data_sources/constants';

declare module '../../ui_actions/public' {
export interface ActionContextMapping {
[ACTION_GLOBAL_APPLY_QUERY]: ApplyGlobalQueryActionContext;
[ACTION_GLOBAL_APPLY_FILTER]: ApplyGlobalFilterActionContext;
[ACTION_SELECT_RANGE]: SelectRangeActionContext;
[ACTION_VALUE_CLICK]: ValueClickActionContext;
Expand Down Expand Up @@ -144,6 +149,11 @@ export class DataPublicPlugin
createFilterAction(queryService.filterManager, queryService.timefilter.timefilter)
);

uiActions.addTriggerAction(
APPLY_QUERY_TRIGGER,
createQueryAction(queryService.queryString, queryService.timefilter.timefilter)
);

uiActions.addTriggerAction(
SELECT_RANGE_TRIGGER,
createSelectRangeAction(() => ({
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/ui_actions/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export {
selectRangeTrigger,
VALUE_CLICK_TRIGGER,
valueClickTrigger,
APPLY_QUERY_TRIGGER,
applyQueryTrigger,
APPLY_FILTER_TRIGGER,
applyFilterTrigger,
VISUALIZE_FIELD_TRIGGER,
Expand Down
6 changes: 4 additions & 2 deletions src/plugins/ui_actions/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@
* under the License.
*/

import { CoreStart, CoreSetup, Plugin, PluginInitializerContext } from 'src/core/public';
import { CoreSetup, CoreStart, Plugin, PluginInitializerContext } from 'src/core/public';
import { UiActionsService } from './service';
import {
applyFilterTrigger,
applyQueryTrigger,
selectRangeTrigger,
valueClickTrigger,
applyFilterTrigger,
visualizeFieldTrigger,
visualizeGeoFieldTrigger,
} from './triggers';
Expand All @@ -58,6 +59,7 @@ export class UiActionsPlugin implements Plugin<UiActionsSetup, UiActionsStart> {
public setup(core: CoreSetup): UiActionsSetup {
this.service.registerTrigger(selectRangeTrigger);
this.service.registerTrigger(valueClickTrigger);
this.service.registerTrigger(applyQueryTrigger);
this.service.registerTrigger(applyFilterTrigger);
this.service.registerTrigger(visualizeFieldTrigger);
this.service.registerTrigger(visualizeGeoFieldTrigger);
Expand Down
43 changes: 43 additions & 0 deletions src/plugins/ui_actions/public/triggers/apply_query_trigger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Any modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { i18n } from '@osd/i18n';
import { Trigger } from '.';

export const APPLY_QUERY_TRIGGER = 'QUERY_TRIGGER';
export const applyQueryTrigger: Trigger<'QUERY_TRIGGER'> = {
id: APPLY_QUERY_TRIGGER,
title: i18n.translate('uiActions.triggers.changeQueryTitle', {
defaultMessage: 'Change query',
}),
description: i18n.translate('uiActions.triggers.changeQueryDescription', {
defaultMessage: 'When OpenSearch Dashboards query is changed.',
}),
};
1 change: 1 addition & 0 deletions src/plugins/ui_actions/public/triggers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export * from './trigger_contract';
export * from './trigger_internal';
export * from './select_range_trigger';
export * from './value_click_trigger';
export * from './apply_query_trigger';
export * from './apply_filter_trigger';
export * from './visualize_field_trigger';
export * from './visualize_geo_field_trigger';
Expand Down
7 changes: 6 additions & 1 deletion src/plugins/ui_actions/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ import {
VISUALIZE_FIELD_TRIGGER,
VISUALIZE_GEO_FIELD_TRIGGER,
DEFAULT_TRIGGER,
APPLY_QUERY_TRIGGER,
} from './triggers';
import type { RangeSelectContext, ValueClickContext } from '../../embeddable/public';
import type { ApplyGlobalFilterActionContext } from '../../data/public';
import type {
ApplyGlobalQueryActionContext,
ApplyGlobalFilterActionContext,
} from '../../data/public';

export type TriggerRegistry = Map<TriggerId, TriggerInternal<any>>;
export type ActionRegistry = Map<string, ActionInternal>;
Expand All @@ -60,6 +64,7 @@ export interface TriggerContextMapping {
[DEFAULT_TRIGGER]: TriggerContext;
[SELECT_RANGE_TRIGGER]: RangeSelectContext;
[VALUE_CLICK_TRIGGER]: ValueClickContext;
[APPLY_QUERY_TRIGGER]: ApplyGlobalQueryActionContext;
[APPLY_FILTER_TRIGGER]: ApplyGlobalFilterActionContext;
[VISUALIZE_FIELD_TRIGGER]: VisualizeFieldContext;
[VISUALIZE_GEO_FIELD_TRIGGER]: VisualizeFieldContext;
Expand Down

0 comments on commit ee51bce

Please sign in to comment.