From 37c5ec61e6c79e6ff16bb7a33ec144ac19f0baa3 Mon Sep 17 00:00:00 2001 From: SuZhou-Joe Date: Mon, 18 Dec 2023 10:36:33 +0800 Subject: [PATCH] fix: type declaration --- server/parsers/ppl_parser.ts | 8 +++++--- server/plugin.ts | 14 ++++++-------- server/types.ts | 8 ++++++++ 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/server/parsers/ppl_parser.ts b/server/parsers/ppl_parser.ts index 19460203db..b45a58bcf4 100644 --- a/server/parsers/ppl_parser.ts +++ b/server/parsers/ppl_parser.ts @@ -3,16 +3,18 @@ * SPDX-License-Identifier: Apache-2.0 */ +import { MessageParser } from '../types'; + const extractPPLQueries = (content: string) => { return Array.from(content.matchAll(/(^|[\n\r]|:)\s*(source\s*=\s*.+)/gi)).map( (match) => match[2] ); }; -export const PPLParsers = { +export const PPLParsers: MessageParser = { id: 'ppl_visualization_message', async parserProvider(interaction) { - const ppls: string[] = interaction.additional_info?.["PPLTool.output"]?.flatMap((item: string) => { + const ppls: string[] = (interaction.additional_info?.["PPLTool.output"] as string[] | null)?.flatMap((item: string) => { let ppl: string = "" try { const outputResp = JSON.parse(item); @@ -22,7 +24,7 @@ export const PPLParsers = { } return extractPPLQueries(ppl); - }); + }) || []; if (!ppls.length) return []; diff --git a/server/plugin.ts b/server/plugin.ts index 070b07d898..ea98af80ee 100644 --- a/server/plugin.ts +++ b/server/plugin.ts @@ -19,22 +19,20 @@ import { searchSavedObject, visualizationSavedObject, } from './saved_objects/observability_saved_object'; -import { ObservabilityPluginSetup, ObservabilityPluginStart } from './types'; +import { ObservabilityPluginSetup, ObservabilityPluginStart, AssistantPluginSetup } from './types'; import { PPLParsers } from './parsers/ppl_parser'; export class ObservabilityPlugin - implements Plugin void - } - }> { + implements Plugin { private readonly logger: Logger; constructor(initializerContext: PluginInitializerContext) { this.logger = initializerContext.logger.get(); } - public setup(core: CoreSetup, deps) { + public setup(core: CoreSetup, deps: { + assistantDashboards?: AssistantPluginSetup + }) { const { assistantDashboards } = deps; this.logger.debug('Observability: Setup'); const router = core.http.createRouter(); @@ -127,7 +125,7 @@ export class ObservabilityPlugin }, })); - assistantDashboards.registerMessageParser(PPLParsers); + assistantDashboards?.registerMessageParser(PPLParsers); return {}; } diff --git a/server/types.ts b/server/types.ts index e936737f3b..e70eeb0c44 100644 --- a/server/types.ts +++ b/server/types.ts @@ -7,3 +7,11 @@ export interface ObservabilityPluginSetup {} // eslint-disable-next-line @typescript-eslint/no-empty-interface export interface ObservabilityPluginStart {} + +/** + * Introduce a compile dependency on dashboards-assistant + * as observerability need some types from the plugin. + * It will gives an type error when dashboards-assistant is not installed so add a ts-ignore to suppress the error. + */ +// @ts-ignore +export type { AssistantPluginSetup, MessageParser } from "../../dashboards-assistant/server";