@fc-components/monaco-editor 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,35 @@
1
+ export type LabelOperator = '=' | '!=' | '=~' | '!~';
2
+
3
+ export type Label = {
4
+ name: string;
5
+ value: string;
6
+ op: LabelOperator;
7
+ };
8
+
9
+ export interface Metric {
10
+ name: string;
11
+ help: string;
12
+ type: string;
13
+ }
14
+
15
+ export interface PromMetricsMetadataItem {
16
+ type: string;
17
+ help: string;
18
+ unit?: string;
19
+ }
20
+
21
+ export interface PromMetricsMetadata {
22
+ [metric: string]: PromMetricsMetadataItem;
23
+ }
24
+
25
+ type FetchFn = (input: RequestInfo, init?: RequestInit) => Promise<Response>;
26
+
27
+ export interface DataProviderParams {
28
+ url?: string;
29
+ lookbackInterval?: number;
30
+ variablesNames?: string[];
31
+ request: FetchFn;
32
+ httpMethod?: 'POST' | 'GET';
33
+ apiPrefix?: string;
34
+ httpErrorHandler?: (error: any) => void;
35
+ }
@@ -0,0 +1,29 @@
1
+ import { Label } from './types';
2
+
3
+ export class NeverCaseError extends Error {
4
+ constructor(_value: never) {
5
+ super('should never happen');
6
+ }
7
+ }
8
+
9
+ // based on the openmetrics-documentation, the 3 symbols we have to handle are:
10
+ // - \n ... the newline character
11
+ // - \ ... the backslash character
12
+ // - " ... the double-quote character
13
+ export function escapeLabelValueInExactSelector(labelValue: string): string {
14
+ return labelValue.replace(/\\/g, '\\\\').replace(/\n/g, '\\n').replace(/"/g, '\\"');
15
+ }
16
+
17
+ export function makeSelector(metricName: string, labels?: Label[], labelName?: string): string {
18
+ if (!labels || labels.length === 0) {
19
+ return metricName;
20
+ }
21
+
22
+ const allLabels = [...labels].filter((label) => label.name !== labelName && label.value !== '');
23
+
24
+ const allLabelTexts = allLabels.map((label) => {
25
+ return `${label.name}${label.op}"${escapeLabelValueInExactSelector(label.value)}"`;
26
+ });
27
+
28
+ return `{${allLabelTexts.join(',')}}`;
29
+ }
@@ -0,0 +1,93 @@
1
+ import { SyntaxNode } from '@lezer/common';
2
+ import { LRParser } from '@lezer/lr';
3
+
4
+ export const ErrorId = 0;
5
+
6
+ interface ParserErrorBoundary {
7
+ startLineNumber: number;
8
+ startColumn: number;
9
+ endLineNumber: number;
10
+ endColumn: number;
11
+ error: string;
12
+ }
13
+
14
+ interface ParseError {
15
+ text: string;
16
+ node: SyntaxNode;
17
+ }
18
+
19
+ export function validateQuery(query: string, interpolatedQuery: string, queryLines: string[], parser: LRParser): ParserErrorBoundary[] | false {
20
+ if (!query) {
21
+ return false;
22
+ }
23
+
24
+ const interpolatedErrors: ParseError[] = parseQuery(interpolatedQuery, parser);
25
+ if (!interpolatedErrors.length) {
26
+ return false;
27
+ }
28
+
29
+ let parseErrors: ParseError[] = interpolatedErrors;
30
+ if (query !== interpolatedQuery) {
31
+ const queryErrors: ParseError[] = parseQuery(query, parser);
32
+ parseErrors = interpolatedErrors.flatMap((interpolatedError) => queryErrors.filter((queryError) => interpolatedError.text === queryError.text) || interpolatedError);
33
+ }
34
+
35
+ return parseErrors.map((parseError) => findErrorBoundary(query, queryLines, parseError)).filter(isErrorBoundary);
36
+ }
37
+
38
+ function parseQuery(query: string, parser: LRParser) {
39
+ const parseErrors: ParseError[] = [];
40
+ const tree = parser.parse(query);
41
+ tree.iterate({
42
+ enter: (nodeRef): false | void => {
43
+ if (nodeRef.type.id === ErrorId) {
44
+ const node = nodeRef.node;
45
+ parseErrors.push({
46
+ node: node,
47
+ text: query.substring(node.from, node.to),
48
+ });
49
+ }
50
+ },
51
+ });
52
+ return parseErrors;
53
+ }
54
+
55
+ function findErrorBoundary(query: string, queryLines: string[], parseError: ParseError): ParserErrorBoundary | null {
56
+ if (queryLines.length === 1) {
57
+ const isEmptyString = parseError.node.from === parseError.node.to;
58
+ const errorNode = isEmptyString && parseError.node.parent ? parseError.node.parent : parseError.node;
59
+ const error = isEmptyString ? query.substring(errorNode.from, errorNode.to) : parseError.text;
60
+ return {
61
+ startLineNumber: 1,
62
+ startColumn: errorNode.from + 1,
63
+ endLineNumber: 1,
64
+ endColumn: errorNode.to + 1,
65
+ error,
66
+ };
67
+ }
68
+
69
+ let startPos = 0,
70
+ endPos = 0;
71
+ for (let line = 0; line < queryLines.length; line++) {
72
+ endPos = startPos + queryLines[line].length;
73
+
74
+ if (parseError.node.from > endPos) {
75
+ startPos += queryLines[line].length + 1;
76
+ continue;
77
+ }
78
+
79
+ return {
80
+ startLineNumber: line + 1,
81
+ startColumn: parseError.node.from - startPos + 1,
82
+ endLineNumber: line + 1,
83
+ endColumn: parseError.node.to - startPos + 1,
84
+ error: parseError.text,
85
+ };
86
+ }
87
+
88
+ return null;
89
+ }
90
+
91
+ function isErrorBoundary(boundary: ParserErrorBoundary | null): boundary is ParserErrorBoundary {
92
+ return boundary !== null;
93
+ }