From ec002ae0dc0352235c86d43e668564fdf0e05127 Mon Sep 17 00:00:00 2001 From: Augustin Husson Date: Wed, 1 Dec 2021 17:05:40 +0100 Subject: [PATCH] extend the number of attribute check to know if it's a prometheusConfig (#9915) Signed-off-by: Augustin Husson --- .../src/client/prometheus.ts | 7 +++-- .../src/complete/hybrid.test.ts | 31 +++++++++++++++++++ .../codemirror-promql/src/complete/index.ts | 11 ++++++- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/web/ui/module/codemirror-promql/src/client/prometheus.ts b/web/ui/module/codemirror-promql/src/client/prometheus.ts index 2a03ec628..5c620faf9 100644 --- a/web/ui/module/codemirror-promql/src/client/prometheus.ts +++ b/web/ui/module/codemirror-promql/src/client/prometheus.ts @@ -47,7 +47,7 @@ export interface CacheConfig { } export interface PrometheusConfig { - url: string; + url?: string; lookbackInterval?: number; // eslint-disable-next-line @typescript-eslint/no-explicit-any httpErrorHandler?: (error: any) => void; @@ -84,7 +84,7 @@ export class HTTPPrometheusClient implements PrometheusClient { private readonly fetchFn: FetchFn = (input: RequestInfo, init?: RequestInit): Promise => fetch(input, init); constructor(config: PrometheusConfig) { - this.url = config.url; + this.url = config.url ? config.url : ''; this.errorHandler = config.httpErrorHandler; if (config.lookbackInterval) { this.lookbackInterval = config.lookbackInterval; @@ -242,12 +242,15 @@ export class HTTPPrometheusClient implements PrometheusClient { private labelsEndpoint(): string { return `${this.apiPrefix}/labels`; } + private labelValuesEndpoint(): string { return `${this.apiPrefix}/label/:name/values`; } + private seriesEndpoint(): string { return `${this.apiPrefix}/series`; } + private metricMetadataEndpoint(): string { return `${this.apiPrefix}/metadata`; } diff --git a/web/ui/module/codemirror-promql/src/complete/hybrid.test.ts b/web/ui/module/codemirror-promql/src/complete/hybrid.test.ts index a1476fa1f..1b4b468c7 100644 --- a/web/ui/module/codemirror-promql/src/complete/hybrid.test.ts +++ b/web/ui/module/codemirror-promql/src/complete/hybrid.test.ts @@ -1247,6 +1247,37 @@ describe('autocomplete promQL test', () => { span: /^[a-zA-Z0-9_:]+$/, }, }, + { + title: 'online autocomplete with initial metric list', + expr: 'rat', + pos: 3, + conf: { remote: { cache: { initialMetricList: ['metric1', 'metric2', 'rat'] } } }, + expectedResult: { + options: ([] as Completion[]).concat( + [ + { + label: 'metric1', + type: 'constant', + }, + { + label: 'metric2', + type: 'constant', + }, + { + label: 'rat', + type: 'constant', + }, + ], + functionIdentifierTerms, + aggregateOpTerms, + numberTerms, + snippets + ), + from: 0, + to: 3, + span: /^[a-zA-Z0-9_:]+$/, + }, + }, ]; testCases.forEach((value) => { it(value.title, async () => { diff --git a/web/ui/module/codemirror-promql/src/complete/index.ts b/web/ui/module/codemirror-promql/src/complete/index.ts index 9cff7165e..a28b7583d 100644 --- a/web/ui/module/codemirror-promql/src/complete/index.ts +++ b/web/ui/module/codemirror-promql/src/complete/index.ts @@ -32,7 +32,16 @@ export interface CompleteConfiguration { } function isPrometheusConfig(remoteConfig: PrometheusConfig | PrometheusClient): remoteConfig is PrometheusConfig { - return (remoteConfig as PrometheusConfig).url !== undefined; + const cfg = remoteConfig as PrometheusConfig; + return ( + cfg.url !== undefined || + cfg.lookbackInterval !== undefined || + cfg.httpErrorHandler !== undefined || + cfg.fetchFn !== undefined || + cfg.cache !== undefined || + cfg.httpMethod !== undefined || + cfg.apiPrefix !== undefined + ); } export function newCompleteStrategy(conf?: CompleteConfiguration): CompleteStrategy {