From 9e809768d0f802ec245701531de8c9af1dcedaf1 Mon Sep 17 00:00:00 2001 From: Zhongnan Su Date: Tue, 30 Nov 2021 12:00:14 -0800 Subject: [PATCH] remove poller code from frontend plugin (#235) Signed-off-by: Zhongnan Su --- dashboards-reports/common/index.ts | 7 +-- .../backend/opensearch-reports-plugin.ts | 10 --- .../server/clusters/notificationsPlugin.ts | 4 +- .../server/executor/createScheduledReport.ts | 62 ------------------- .../server/executor/executor.ts | 54 ---------------- dashboards-reports/server/plugin.ts | 28 --------- .../server/routes/lib/createReport.ts | 4 -- dashboards-reports/server/utils/constants.ts | 6 -- 8 files changed, 5 insertions(+), 170 deletions(-) delete mode 100644 dashboards-reports/server/executor/createScheduledReport.ts delete mode 100644 dashboards-reports/server/executor/executor.ts delete mode 100644 dashboards-reports/server/utils/constants.ts diff --git a/dashboards-reports/common/index.ts b/dashboards-reports/common/index.ts index b980455c..ce180a09 100644 --- a/dashboards-reports/common/index.ts +++ b/dashboards-reports/common/index.ts @@ -16,19 +16,18 @@ export const OPENSEARCH_REPORTS_API = { LIST_REPORT_INSTANCES: `${BASE_REPORTS_URI}/instances`, REPORT_DEFINITION: `${BASE_REPORTS_URI}/definition`, LIST_REPORT_DEFINITIONS: `${BASE_REPORTS_URI}/definitions`, - POLL_REPORT_INSTANCE: `${BASE_REPORTS_URI}/poll_instance`, }; const REPORTING_NOTIFICATIONS_API_PREFIX = '/api/reporting_notifications'; export const REPORTING_NOTIFICATIONS_DASHBOARDS_API = Object.freeze({ GET_CONFIGS: `${REPORTING_NOTIFICATIONS_API_PREFIX}/get_configs`, GET_EVENT: `${REPORTING_NOTIFICATIONS_API_PREFIX}/get_event`, - SEND_TEST_MESSAGE: `${REPORTING_NOTIFICATIONS_API_PREFIX}/test_message` + SEND_TEST_MESSAGE: `${REPORTING_NOTIFICATIONS_API_PREFIX}/test_message`, }); const NOTIFICATIONS_API_BASE_PATH = '/_plugins/_notifications'; export const NOTIFICATIONS_API = Object.freeze({ CONFIGS: `${NOTIFICATIONS_API_BASE_PATH}/configs`, EVENTS: `${NOTIFICATIONS_API_BASE_PATH}/events`, - TEST_MESSAGE: `${NOTIFICATIONS_API_BASE_PATH}/feature/test` -}); \ No newline at end of file + TEST_MESSAGE: `${NOTIFICATIONS_API_BASE_PATH}/feature/test`, +}); diff --git a/dashboards-reports/server/backend/opensearch-reports-plugin.ts b/dashboards-reports/server/backend/opensearch-reports-plugin.ts index 473a6ccc..5c0c943e 100644 --- a/dashboards-reports/server/backend/opensearch-reports-plugin.ts +++ b/dashboards-reports/server/backend/opensearch-reports-plugin.ts @@ -143,14 +143,4 @@ export default function (Client: any, config: any, components: any) { }, method: 'DELETE', }); - - /** - * Poller API - */ - opensearchReports.pollReportInstance = clientAction({ - url: { - fmt: `${OPENSEARCH_REPORTS_API.POLL_REPORT_INSTANCE}`, - }, - method: 'GET', - }); } diff --git a/dashboards-reports/server/clusters/notificationsPlugin.ts b/dashboards-reports/server/clusters/notificationsPlugin.ts index b3f85c81..4f48cb7d 100644 --- a/dashboards-reports/server/clusters/notificationsPlugin.ts +++ b/dashboards-reports/server/clusters/notificationsPlugin.ts @@ -29,7 +29,7 @@ export function NotificationsPlugin(Client: any, config: any, components: any) { }, }, method: 'GET', - }) + }); notifications.sendTestMessage = clientAction({ url: { @@ -43,4 +43,4 @@ export function NotificationsPlugin(Client: any, config: any, components: any) { }, method: 'GET', }); -} \ No newline at end of file +} diff --git a/dashboards-reports/server/executor/createScheduledReport.ts b/dashboards-reports/server/executor/createScheduledReport.ts deleted file mode 100644 index d3abfb58..00000000 --- a/dashboards-reports/server/executor/createScheduledReport.ts +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -import { - REPORT_TYPE, - REPORT_STATE, - LOCAL_HOST, - DELIVERY_TYPE, -} from '../routes/utils/constants'; -import { ILegacyClusterClient, Logger } from '../../../../src/core/server'; -import { createSavedSearchReport } from '../routes/utils/savedSearchReportHelper'; -import { ReportSchemaType } from '../model'; -import { CreateReportResultType } from '../routes/utils/types'; -import { createVisualReport } from '../routes/utils/visual_report/visualReportHelper'; -import { updateReportState } from '../routes/lib/updateReportState'; - -export const createScheduledReport = async ( - reportId: string, - report: ReportSchemaType, - opensearchClient: ILegacyClusterClient, - opensearchReportsClient: ILegacyClusterClient, - logger: Logger -) => { - let createReportResult: CreateReportResultType; - - const { - report_definition: { - report_params: reportParams, - delivery: { delivery_type: deliveryType }, - }, - } = report; - const { report_source: reportSource } = reportParams; - - try { - // TODO: generate report logic will be added back once we have the user impersonation - // if (reportSource === REPORT_TYPE.savedSearch) { - // createReportResult = await createSavedSearchReport( - // report, - // opensearchClient, - // isScheduledTask - // ); - // } else { - // // report source can only be one of [saved search, visualization, dashboard] - // // compose url with localhost - // const completeQueryUrl = `${LOCAL_HOST}${report.query_url}`; - // createReportResult = await createVisualReport( - // reportParams, - // completeQueryUrl, - // logger - // ); - // } - - await updateReportState(reportId, opensearchReportsClient, REPORT_STATE.created); - } catch (error) { - // update report instance with "error" state - //TODO: save error detail and display on UI - logger.error(`Failed to create scheduled report ${error}`); - await updateReportState(reportId, opensearchReportsClient, REPORT_STATE.error); - } -}; diff --git a/dashboards-reports/server/executor/executor.ts b/dashboards-reports/server/executor/executor.ts deleted file mode 100644 index 034011f3..00000000 --- a/dashboards-reports/server/executor/executor.ts +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -import { ILegacyClusterClient, Logger } from '../../../../src/core/server'; -import { createScheduledReport } from './createScheduledReport'; -import { POLL_INTERVAL } from '../utils/constants'; -import { parseOpenSearchErrorResponse } from '../routes/utils/helpers'; -import { backendToUiReport } from '../routes/utils/converters/backendToUi'; -import { BackendReportInstanceType } from 'server/model/backendModel'; - -async function pollAndExecuteJob( - opensearchReportsClient: ILegacyClusterClient, - opensearchClient: ILegacyClusterClient, - logger: Logger -) { - logger.info( - `start polling at time: ${new Date().toISOString()} with fixed interval: ${POLL_INTERVAL} milliseconds` - ); - try { - // poll job - const opensearchResp = await opensearchReportsClient.callAsInternalUser( - 'opensearch_reports.pollReportInstance' - ); - const job: BackendReportInstanceType = opensearchResp.reportInstance; - - // job retrieved, otherwise will be undefined because 204 No-content is returned - if (job) { - const reportMetadata = backendToUiReport(job); - const reportId = job.id; - logger.info( - `scheduled job sent from scheduler with report id: ${reportId}` - ); - - await createScheduledReport( - reportId, - reportMetadata, - opensearchClient, - opensearchReportsClient, - logger - ); - } else { - // 204 no content is returned, 204 doesn't have response body - logger.info(`No scheduled job to execute ${JSON.stringify(opensearchResp)}`); - } - } catch (error) { - logger.error( - `Failed to poll job ${error.statusCode} ${parseOpenSearchErrorResponse(error)}` - ); - } -} - -export { pollAndExecuteJob }; diff --git a/dashboards-reports/server/plugin.ts b/dashboards-reports/server/plugin.ts index f541be2c..ac2ed152 100644 --- a/dashboards-reports/server/plugin.ts +++ b/dashboards-reports/server/plugin.ts @@ -11,7 +11,6 @@ import { Logger, ILegacyClusterClient, } from '../../../src/core/server'; -import { setIntervalAsync } from 'set-interval-async/dynamic'; import { Semaphore, SemaphoreInterface, withTimeout } from 'async-mutex'; import opensearchReportsPlugin from './backend/opensearch-reports-plugin'; import { @@ -19,8 +18,6 @@ import { ReportsDashboardsPluginStart, } from './types'; import registerRoutes from './routes'; -import { pollAndExecuteJob } from './executor/executor'; -import { POLL_INTERVAL } from './utils/constants'; import { NotificationsPlugin } from './clusters/notificationsPlugin'; import { buildConfig, ReportingConfigType } from './config'; import { ReportingConfig } from './config/config'; @@ -114,31 +111,6 @@ export class ReportsDashboardsPlugin public start(core: CoreStart) { this.logger.debug('reports-dashboards: Started'); - const opensearchReportsClient: ILegacyClusterClient = core.opensearch.legacy.createClient( - 'opensearch_reports', - { - plugins: [opensearchReportsPlugin], - } - ); - - const opensearchClient: ILegacyClusterClient = - core.opensearch.legacy.client; - /* - setIntervalAsync provides the same familiar interface as built-in setInterval for asynchronous functions, - while preventing multiple executions from overlapping in time. - Polling at at a 5 min fixed interval - - TODO: need further optimization polling with a mix approach of - random delay and dynamic delay based on the amount of jobs. - */ - // setIntervalAsync( - // pollAndExecuteJob, - // POLL_INTERVAL, - // opensearchReportsClient, - // notificationClient, - // opensearchClient, - // this.logger - // ); return {}; } diff --git a/dashboards-reports/server/routes/lib/createReport.ts b/dashboards-reports/server/routes/lib/createReport.ts index 40a073e2..57f2c5cd 100644 --- a/dashboards-reports/server/routes/lib/createReport.ts +++ b/dashboards-reports/server/routes/lib/createReport.ts @@ -5,8 +5,6 @@ import { REPORT_TYPE, - REPORT_STATE, - DELIVERY_TYPE, DATA_REPORT_CONFIG, EXTRA_HEADERS, } from '../utils/constants'; @@ -21,8 +19,6 @@ import { createSavedSearchReport } from '../utils/savedSearchReportHelper'; import { ReportSchemaType } from '../../model'; import { CreateReportResultType } from '../utils/types'; import { createVisualReport } from '../utils/visual_report/visualReportHelper'; -import { SetCookie, Headers } from 'puppeteer-core'; -import { updateReportState } from './updateReportState'; import { saveReport } from './saveReport'; import { SemaphoreInterface } from 'async-mutex'; import { ReportingConfig } from 'server'; diff --git a/dashboards-reports/server/utils/constants.ts b/dashboards-reports/server/utils/constants.ts deleted file mode 100644 index aee16f23..00000000 --- a/dashboards-reports/server/utils/constants.ts +++ /dev/null @@ -1,6 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -export const POLL_INTERVAL = 1000 * 60 * 5; // in milliseconds (5 min)