Skip to content

Commit

Permalink
Clean up files and use helper functions
Browse files Browse the repository at this point in the history
Some clean up to reduce file count
  • Loading branch information
kavilla authored Jun 14, 2024
2 parents c617aa5 + e11a384 commit 8867564
Show file tree
Hide file tree
Showing 30 changed files with 495 additions and 555 deletions.
36 changes: 36 additions & 0 deletions common/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

export const PLUGIN_ID = 'queryEnhancements';
export const PLUGIN_NAME = 'queryEnhancements';

export const BASE_API = '/api/enhancements';

export const SEARCH_STRATEGY = {
PPL: 'ppl',
SQL: 'sql',
};

export const API = {
SEARCH: `${BASE_API}/search`,
PPL_SEARCH: `${BASE_API}/search/${SEARCH_STRATEGY.PPL}`,
SQL_SEARCH: `${BASE_API}/search/${SEARCH_STRATEGY.SQL}`,
};

export const URI = {
PPL: '/_plugins/_ppl',
SQL: '/_plugins/_sql',
ASYNC_QUERY: '/_plugins/_async_query',
ML: '/_plugins/_ml',
OBSERVABILITY: '/_plugins/_observability',
DATA_CONNECTIONS: '/_plugins/_query/_datasources',
};

export const OPENSEARCH_API = {
PANELS: `${URI.OBSERVABILITY}/object`,
DATA_CONNECTIONS: URI.DATA_CONNECTIONS,
};

export const UI_SETTINGS = {};
26 changes: 1 addition & 25 deletions common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,5 @@
* SPDX-License-Identifier: Apache-2.0
*/

export const PLUGIN_ID = 'queryEnhancements';
export const PLUGIN_NAME = 'queryEnhancements';

export const PPL_SEARCH_STRATEGY = 'ppl';
export const SQL_SEARCH_STRATEGY = 'sql';

export const PPL_ENDPOINT = '/_plugins/_ppl';
export const SQL_ENDPOINT = '/_plugins/_sql';

const BASE_OBSERVABILITY_URI = '/_plugins/_observability';
const BASE_DATACONNECTIONS_URI = '/_plugins/_query/_datasources';
export const OPENSEARCH_PANELS_API = {
OBJECT: `${BASE_OBSERVABILITY_URI}/object`,
};
export const OPENSEARCH_DATACONNECTIONS_API = {
DATACONNECTION: `${BASE_DATACONNECTIONS_URI}`,
};

export const JOBS_ENDPOINT_BASE = '/_plugins/_async_query';

export const BASE_ML_COMMONS_URI = '/_plugins/_ml';

// Advanced Settings
export const QUERY_ENABLE_ENHANCEMENTS_SETTING = 'query:enableEnhancements';

export * from './constants';
export * from './utils';
2 changes: 1 addition & 1 deletion opensearch_dashboards.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"id": "queryEnhancements",
"version": "1.0.0",
"version": "3.0.0",
"opensearchDashboardsVersion": "opensearchDashboards",
"server": true,
"ui": true,
Expand Down
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"name": "queryEnhancements",
"version": "0.0.0",
"private": true,
"license": "Apache-2.0",
"version": "3.0.0.0",
"main": "index.ts",
"scripts": {
"build": "yarn plugin-helpers build",
"plugin-helpers": "../../scripts/use_node ../../scripts/plugin_helpers",
"osd": "../../scripts/use_node ../../scripts/osd"
}
"plugin-helpers": "node ../../scripts/plugin_helpers",
"osd": "node ../../scripts/osd"
},
"dependencies": {},
"devDependencies": {}
}
8 changes: 6 additions & 2 deletions public/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import './index.scss';

import { QueryEnhancementsPlugin } from './plugin';

// This exports static code and TypeScript types,
// as well as, OpenSearch Dashboards Platform `plugin()` initializer.
export function plugin() {
return new QueryEnhancementsPlugin();
}

export { QueryEnhancementsPluginSetup, QueryEnhancementsPluginStart } from './types';
12 changes: 8 additions & 4 deletions public/plugin.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import moment from 'moment';
import { CoreSetup, CoreStart, Plugin } from '../../../src/core/public';
import { IStorageWrapper, Storage } from '../../../src/plugins/opensearch_dashboards_utils/public';
import { PPLQlSearchInterceptor } from './search/ppl_search_interceptor';
import { SQLQlSearchInterceptor } from './search/sql_search_interceptor';
import { PPLSearchInterceptor, SQLSearchInterceptor } from './search';
import { setData, setStorage } from './services';
import {
QueryEnhancementsPluginSetup,
Expand All @@ -23,15 +27,15 @@ export class QueryEnhancementsPlugin
core: CoreSetup,
{ data }: QueryEnhancementsPluginSetupDependencies
): QueryEnhancementsPluginSetup {
const pplSearchInterceptor = new PPLQlSearchInterceptor({
const pplSearchInterceptor = new PPLSearchInterceptor({
toasts: core.notifications.toasts,
http: core.http,
uiSettings: core.uiSettings,
startServices: core.getStartServices(),
usageCollector: data.search.usageCollector,
});

const sqlSearchInterceptor = new SQLQlSearchInterceptor({
const sqlSearchInterceptor = new SQLSearchInterceptor({
toasts: core.notifications.toasts,
http: core.http,
uiSettings: core.uiSettings,
Expand Down
7 changes: 7 additions & 0 deletions public/search/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

export { PPLSearchInterceptor } from './ppl_search_interceptor';
export { SQLSearchInterceptor } from './sql_search_interceptor';
13 changes: 9 additions & 4 deletions public/search/ppl_search_interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { trimEnd } from 'lodash';
import { Observable, from } from 'rxjs';
import { stringify } from '@osd/std';
Expand All @@ -20,10 +25,10 @@ import {
SearchInterceptor,
SearchInterceptorDeps,
} from '../../../../src/plugins/data/public';
import { formatDate, PPL_SEARCH_STRATEGY, removeKeyword } from '../../common';
import { formatDate, SEARCH_STRATEGY, removeKeyword, API } from '../../common';
import { QueryEnhancementsPluginStartDependencies } from '../types';

export class PPLQlSearchInterceptor extends SearchInterceptor {
export class PPLSearchInterceptor extends SearchInterceptor {
protected queryService!: DataPublicPluginStart['query'];
protected aggsService!: DataPublicPluginStart['search']['aggs'];

Expand All @@ -42,7 +47,7 @@ export class PPLQlSearchInterceptor extends SearchInterceptor {
strategy?: string
): Observable<IOpenSearchDashboardsSearchResponse> {
const { id, ...searchRequest } = request;
const path = trimEnd('/api/pplql/search');
const path = trimEnd(API.PPL_SEARCH);
const { timefilter } = this.queryService;
const dateRange = timefilter.timefilter.getTime();
const { fromDate, toDate } = formatTimePickerDate(dateRange, 'YYYY-MM-DD HH:mm:ss.SSS');
Expand Down Expand Up @@ -186,6 +191,6 @@ export class PPLQlSearchInterceptor extends SearchInterceptor {
}

public search(request: IOpenSearchDashboardsSearchRequest, options: ISearchOptions) {
return this.runSearch(request, options.abortSignal, PPL_SEARCH_STRATEGY);
return this.runSearch(request, options.abortSignal, SEARCH_STRATEGY.PPL);
}
}
15 changes: 10 additions & 5 deletions public/search/sql_search_interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { trimEnd } from 'lodash';
import { Observable, from } from 'rxjs';
import { stringify } from '@osd/std';
Expand All @@ -10,10 +15,10 @@ import {
SearchInterceptor,
SearchInterceptorDeps,
} from '../../../../src/plugins/data/public';
import { SQL_SEARCH_STRATEGY } from '../../common';
import { API, SEARCH_STRATEGY } from '../../common';
import { QueryEnhancementsPluginStartDependencies } from '../types';

export class SQLQlSearchInterceptor extends SearchInterceptor {
export class SQLSearchInterceptor extends SearchInterceptor {
protected queryService!: DataPublicPluginStart['query'];
protected aggsService!: DataPublicPluginStart['search']['aggs'];

Expand All @@ -32,7 +37,7 @@ export class SQLQlSearchInterceptor extends SearchInterceptor {
strategy?: string
): Observable<IOpenSearchDashboardsSearchResponse> {
const { id, ...searchRequest } = request;
const path = trimEnd('/api/sqlql/search');
const path = trimEnd(API.SQL_SEARCH);

const fetchDataFrame = (queryString: string, df = null) => {
const body = stringify({ query: { qs: queryString, format: 'jdbc' }, df });
Expand All @@ -56,7 +61,7 @@ export class SQLQlSearchInterceptor extends SearchInterceptor {
if (!df.body.error) return;
const jsError = new Error(df.body.error.response);
this.deps.toasts.addError(jsError, {
title: i18n.translate('dqlPlugin.sqlQueryError', {
title: i18n.translate('queryEnhancements.sqlQueryError', {
defaultMessage: 'Could not complete the SQL query',
}),
toastMessage: df.body.error.msg,
Expand All @@ -67,6 +72,6 @@ export class SQLQlSearchInterceptor extends SearchInterceptor {
}

public search(request: IOpenSearchDashboardsSearchRequest, options: ISearchOptions) {
return this.runSearch(request, options.abortSignal, SQL_SEARCH_STRATEGY);
return this.runSearch(request, options.abortSignal, SEARCH_STRATEGY.SQL);
}
}
5 changes: 5 additions & 0 deletions public/services.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { createGetterSetter } from '../../../src/plugins/opensearch_dashboards_utils/common';
import { IStorageWrapper } from '../../../src/plugins/opensearch_dashboards_utils/public';
import { DataPublicPluginStart } from '../../../src/plugins/data/public';
Expand Down
5 changes: 5 additions & 0 deletions public/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { DataPublicPluginSetup, DataPublicPluginStart } from 'src/plugins/data/public';

// eslint-disable-next-line @typescript-eslint/no-empty-interface
Expand Down
12 changes: 12 additions & 0 deletions server/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { PluginConfigDescriptor, PluginInitializerContext } from '../../../src/core/server';
import { QueryEnhancementsPlugin } from './plugin';
import { configSchema, ConfigSchema } from '../common/config';
Expand All @@ -11,4 +16,11 @@ export function plugin(initializerContext: PluginInitializerContext) {
return new QueryEnhancementsPlugin(initializerContext);
}

export {
Facet,
OpenSearchPPLPlugin,
OpenSearchObservabilityPlugin,
shimStats,
shimSchemaRow,
} from './utils';
export { QueryEnhancementsPluginSetup, QueryEnhancementsPluginStart } from './types';
22 changes: 11 additions & 11 deletions server/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { Observable } from 'rxjs';
import {
CoreSetup,
Expand All @@ -7,18 +12,15 @@ import {
PluginInitializerContext,
SharedGlobalConfig,
} from '../../../src/core/server';
import { PPL_SEARCH_STRATEGY, SQL_ASYNC_SEARCH_STRATEGY, SQL_SEARCH_STRATEGY } from '../common';
import { SEARCH_STRATEGY } from '../common';
import { defineRoutes } from './routes';
import { EnginePlugin } from './search/engine_plugin';
import { PPLPlugin } from './search/ppl/ppl_plugin';
import { pplSearchStrategyProvider } from './search/ppl/ppl_search_strategy';
import { sqlSearchStrategyProvider } from './search/sql/sql_search_strategy';
import { pplSearchStrategyProvider, sqlSearchStrategyProvider } from './search';
import {
QueryEnhancementsPluginSetup,
QueryEnhancementsPluginSetupDependencies,
QueryEnhancementsPluginStart,
} from './types';
import { uiSettings } from './ui_settings';
import { OpenSearchPPLPlugin, OpenSearchObservabilityPlugin } from './utils';

export class QueryEnhancementsPlugin
implements Plugin<QueryEnhancementsPluginSetup, QueryEnhancementsPluginStart> {
Expand All @@ -34,16 +36,14 @@ export class QueryEnhancementsPlugin
const router = core.http.createRouter();
// Register server side APIs
const client = core.opensearch.legacy.createClient('opensearch_observability', {
plugins: [PPLPlugin, EnginePlugin],
plugins: [OpenSearchPPLPlugin, OpenSearchObservabilityPlugin],
});

core.uiSettings.register(uiSettings);

const pplSearchStrategy = pplSearchStrategyProvider(this.config$, this.logger, client);
const sqlSearchStrategy = sqlSearchStrategyProvider(this.config$, this.logger, client);

data.search.registerSearchStrategy(PPL_SEARCH_STRATEGY, pplSearchStrategy);
data.search.registerSearchStrategy(SQL_SEARCH_STRATEGY, sqlSearchStrategy);
data.search.registerSearchStrategy(SEARCH_STRATEGY.PPL, pplSearchStrategy);
data.search.registerSearchStrategy(SEARCH_STRATEGY.SQL, sqlSearchStrategy);

defineRoutes(this.logger, router, {
ppl: pplSearchStrategy,
Expand Down
Loading

0 comments on commit 8867564

Please sign in to comment.