import { HybridComplete } from './hybrid' ; import { CachedPrometheusClient , HTTPPrometheusClient , PrometheusClient , PrometheusConfig } from '../client/prometheus' ; import { CompletionContext , CompletionResult } from '@codemirror/autocomplete' ; export interface CompleteStrategy { promQL ( context ) : Promise < CompletionResult | null > | CompletionResult | null ; } export interface CompleteConfiguration { remote ? : PrometheusConfig | PrometheusClient ; maxMetricsMetadata ? : number ; completeStrategy ? : CompleteStrategy ; } function isPrometheusConfig ( remoteConfig ) { 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 ? ) { if ( conf ?. completeStrategy ) { return conf . completeStrategy ; } if ( conf ?. remote ) { if ( ! isPrometheusConfig ( conf . remote ) ) { return new HybridComplete ( conf . remote , conf . maxMetricsMetadata ) ; } return new HybridComplete ( new CachedPrometheusClient ( new HTTPPrometheusClient ( conf . remote ) , conf . remote . cache ) , conf . maxMetricsMetadata ) ; } return new HybridComplete ( ) ; }