Skip to content
This repository has been archived by the owner on Jul 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #110 from prometheus-community/feature/remove-enri…
Browse files Browse the repository at this point in the history
…cher

Remove enricher + bump lezer-promql
  • Loading branch information
juliusv authored Mar 22, 2021
2 parents daf0559 + e86606e commit 0950344
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 87 deletions.
32 changes: 0 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,38 +102,6 @@ Use it cautiously. A high value of this limit can cause a crash of your browser
const promQL = new PromQLExtension().setComplete({ maxMetricsMetadata: 10000 })
```

### enricher

enricher is a function that will allow user to enrich the current completion by adding a custom one. It takes two
parameters:

* `trigger` should be used to decide whenever you would like to trigger your custom completion. It's better to not
trigger the same completion for multiple different ContextKind. For example, the autocompletion of the metricName /
the function / the aggregation happens at the same time. So if you want to trigger your custom completion for
metricName / function / aggregation, you should just choose to trigger it for the function for example. Otherwise, you
will end up to have the same completion result multiple time.

* `result` is the current result of the completion. Usually you don't want to override it but instead to concat your own
completion with this one.

```typescript
import { Completion } from '@codemirror/autocomplete';
import { ContextKind } from 'codemirror-promql';

function myCustomEnricher(trigger: ContextKind, result: Completion[]): Completion[] | Promise<Completion[]> {
switch (trigger) {
case ContextKind.Aggregation:
// custom completion
// ...
// return result.concat( myCustomCompletionArray )
default:
return result;
}
}

const promQL = new PromQLExtension().setComplete({ enricher: myCustomEnricher })
```

### Connect the autocompletion extension to a remote Prometheus server

Connecting the autocompletion extension to a remote Prometheus server will provide autocompletion of metric names, label
Expand Down
26 changes: 13 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"homepage": "https://github.com/prometheus-community/codemirror-promql/blob/master/README.md",
"dependencies": {
"lezer-promql": "^0.16.0",
"lezer-promql": "^0.17.0",
"lru-cache": "^6.0.0"
},
"devDependencies": {
Expand Down
31 changes: 1 addition & 30 deletions src/lang-promql/complete/hybrid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,30 +119,6 @@ export interface Context {
matchers?: Matcher[];
}

// EnrichCompletionHandler defines a function that will be called during the completion calculation.
// * `trigger` should be used to decide whenever you would like to trigger your custom completion.
// It's better to not trigger the same completion for multiple different ContextKind.
// For example, the autocompletion of the metricName / the function / the aggregation happens at the same time.
// So if you want to trigger your custom completion for metricName / function / aggregation, you should just choose to trigger it for the function for example.
// Otherwise, you will end up to have the same completion result multiple time.
// * result is the current result of the completion. Usually you don't want to override it but instead to concat your own completion with this one.
// Typical implementation snippet:
// function myCustomEnricher(trigger: ContextKind, result: Completion[]): Completion[] | Promise<Completion[]> {
// switch (trigger) {
// case ContextKind.Aggregation:
// // custom completion
// // ...
// // return result.concat( myCustomCompletionArray )
// default:
// return result;
// }
// }
export type EnrichCompletionHandler = (trigger: ContextKind, result: Completion[]) => Completion[] | Promise<Completion[]>;

function defaultEnricher(trigger: ContextKind, result: Completion[]): Completion[] | Promise<Completion[]> {
return result;
}

function getMetricNameInVectorSelector(tree: SyntaxNode, state: EditorState): string {
// Find if there is a defined metric name. Should be used to autocomplete a labelValue or a labelName
// First find the parent "VectorSelector" to be able to find then the subChild "MetricIdentifier" if it exists.
Expand Down Expand Up @@ -424,12 +400,10 @@ export function analyzeCompletion(state: EditorState, node: SyntaxNode): Context
export class HybridComplete implements CompleteStrategy {
private readonly prometheusClient: PrometheusClient | undefined;
private readonly maxMetricsMetadata: number;
private readonly enricher: EnrichCompletionHandler;

constructor(prometheusClient?: PrometheusClient, maxMetricsMetadata = 10000, enricher = defaultEnricher) {
constructor(prometheusClient?: PrometheusClient, maxMetricsMetadata = 10000) {
this.prometheusClient = prometheusClient;
this.maxMetricsMetadata = maxMetricsMetadata;
this.enricher = enricher;
}

promQL(context: CompletionContext): Promise<CompletionResult | null> | CompletionResult | null {
Expand Down Expand Up @@ -508,9 +482,6 @@ export class HybridComplete implements CompleteStrategy {
return this.autocompleteLabelValue(result, context);
});
}
asyncResult = asyncResult.then((result) => {
return this.enricher(context.kind, result);
});
}
return asyncResult.then((result) => {
return arrayToCompletionResult(result, computeStartCompletePosition(tree, pos), pos, completeSnippet, span);
Expand Down
13 changes: 3 additions & 10 deletions src/lang-promql/complete/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import { EnrichCompletionHandler, HybridComplete } from './hybrid';
import { HybridComplete } from './hybrid';
import { CachedPrometheusClient, HTTPPrometheusClient, PrometheusClient, PrometheusConfig } from '../client/prometheus';
import { CompletionContext, CompletionResult } from '@codemirror/autocomplete';

export { EnrichCompletionHandler, ContextKind } from './hybrid';
// Complete is the interface that defines the simple method that returns a CompletionResult.
// Every different completion mode must implement this interface.
export interface CompleteStrategy {
Expand All @@ -37,8 +36,6 @@ export interface CompleteConfiguration {
// maxMetricsMetadata is the maximum number of metrics in Prometheus for which metadata is fetched.
// If the number of metrics exceeds this limit, no metric metadata is fetched at all.
maxMetricsMetadata?: number;
// enricher is a function that will allow user to enrich the current completion by adding a custom one
enricher?: EnrichCompletionHandler;
// When providing this custom CompleteStrategy, the settings above will not be used.
completeStrategy?: CompleteStrategy;
}
Expand All @@ -53,13 +50,9 @@ export function newCompleteStrategy(conf?: CompleteConfiguration): CompleteStrat
}
if (conf?.remote) {
if (!isPrometheusConfig(conf.remote)) {
return new HybridComplete(conf.remote, conf.maxMetricsMetadata, conf.enricher);
return new HybridComplete(conf.remote, conf.maxMetricsMetadata);
}
return new HybridComplete(
new CachedPrometheusClient(new HTTPPrometheusClient(conf.remote), conf.remote.cache?.maxAge),
conf.maxMetricsMetadata,
conf.enricher
);
return new HybridComplete(new CachedPrometheusClient(new HTTPPrometheusClient(conf.remote), conf.remote.cache?.maxAge), conf.maxMetricsMetadata);
}
return new HybridComplete();
}
2 changes: 1 addition & 1 deletion src/lang-promql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
// SOFTWARE.

export { PrometheusClient } from './client';
export { CompleteConfiguration, ContextKind, EnrichCompletionHandler } from './complete';
export { CompleteConfiguration } from './complete';
export { PromQLExtension, promQLLanguage } from './promql';

0 comments on commit 0950344

Please sign in to comment.