@fc-components/monaco-editor 0.1.15 → 0.1.17

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.
@@ -9,6 +9,7 @@ export declare class DataProvider {
9
9
  private readonly errorHandler?;
10
10
  private readonly httpMethod;
11
11
  private readonly apiPrefix;
12
+ private enableRequests;
12
13
  private readonly customRequest;
13
14
  metrics: string[];
14
15
  labelKeys: string[];
@@ -19,12 +20,12 @@ export declare class DataProvider {
19
20
  setVariablesNames(variablesNames: string[]): void;
20
21
  private buildRequest;
21
22
  private request;
22
- fetchSeries: (selector: string, withLimit?: string | undefined) => Promise<Record<string, string>[]>;
23
+ fetchSeries: (selector: string, withLimit?: string | undefined) => Promise<Record<string, string | undefined>[]>;
23
24
  fetchLabels: (selector: string) => Promise<string[]>;
24
25
  fetchLabelValues: (labelName: string, selector: string) => Promise<string[]>;
25
26
  getAllMetricNames(): string[];
26
- start: () => Promise<void[]>;
27
- loadMetricsMetadata(): Promise<void>;
27
+ start: () => Promise<PromMetricsMetadata[]>;
28
+ loadMetricsMetadata(): Promise<PromMetricsMetadata>;
28
29
  metricNamesToMetrics(metricNames: string[]): Metric[];
29
30
  private setInputInRange;
30
31
  private enableAutocompleteSuggestionsUpdate;
@@ -8,6 +8,7 @@ interface PromQLEditorProps {
8
8
  placeholder?: string;
9
9
  enableAutocomplete?: boolean;
10
10
  durationVariablesCompletion?: boolean;
11
+ enableRequests?: boolean;
11
12
  readOnly?: boolean;
12
13
  disabled?: boolean;
13
14
  interpolateString?: (query: string) => string;
@@ -27,5 +27,6 @@ export interface DataProviderParams {
27
27
  httpMethod?: 'POST' | 'GET';
28
28
  apiPrefix?: string;
29
29
  httpErrorHandler?: (error: any) => void;
30
+ enableRequests?: boolean;
30
31
  }
31
32
  export {};
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.1.15",
6
+ "version": "0.1.17",
7
7
  "license": "MIT",
8
8
  "main": "dist/index.js",
9
9
  "module": "dist/monaco-editor.esm.js",
@@ -26,6 +26,7 @@ export class DataProvider {
26
26
  private readonly errorHandler?: (error: any) => void;
27
27
  private readonly httpMethod: 'POST' | 'GET' = 'GET';
28
28
  private readonly apiPrefix: string = '/api/v1';
29
+ private enableRequests: boolean = true;
29
30
  private readonly customRequest: CustomRequest = (input: RequestInfo, init?: RequestInit): Promise<Response> => fetch(input, init);
30
31
  metrics: string[];
31
32
  labelKeys: string[];
@@ -56,6 +57,11 @@ export class DataProvider {
56
57
  this.apiPrefix = params.apiPrefix;
57
58
  }
58
59
 
60
+ // control whether DataProvider should actually send network requests
61
+ if (typeof params.enableRequests !== 'undefined') {
62
+ this.enableRequests = !!params.enableRequests;
63
+ }
64
+
59
65
  this.metrics = [];
60
66
  this.labelKeys = [];
61
67
  }
@@ -112,7 +118,10 @@ export class DataProvider {
112
118
  });
113
119
  }
114
120
 
115
- fetchSeries = async (selector: string, withLimit?: string): Promise<Record<string, string>[]> => {
121
+ fetchSeries = async (selector: string, withLimit?: string): Promise<Record<string, string | undefined>[]> => {
122
+ if (!this.enableRequests) {
123
+ return [] as Record<string, string | undefined>[];
124
+ }
116
125
  const end = new Date();
117
126
  const start = new Date(end.getTime() - this.lookbackInterval);
118
127
  const url = `${this.apiPrefix}/series`;
@@ -138,6 +147,9 @@ export class DataProvider {
138
147
  };
139
148
 
140
149
  fetchLabels = async (selector: string): Promise<string[]> => {
150
+ if (!this.enableRequests) {
151
+ return [] as string[];
152
+ }
141
153
  const end = new Date();
142
154
  const start = new Date(end.getTime() - this.lookbackInterval);
143
155
  const url = `${this.apiPrefix}/labels`;
@@ -164,6 +176,9 @@ export class DataProvider {
164
176
  };
165
177
 
166
178
  fetchLabelValues = async (labelName: string, selector: string): Promise<string[]> => {
179
+ if (!this.enableRequests) {
180
+ return [] as string[];
181
+ }
167
182
  const end = new Date();
168
183
  const start = new Date(end.getTime() - this.lookbackInterval);
169
184
  const url = `${this.apiPrefix}/label/${labelName}/values`;
@@ -174,6 +189,7 @@ export class DataProvider {
174
189
  if (selector) {
175
190
  urlParams['match[]'] = selector;
176
191
  }
192
+
177
193
  const request = this.buildRequest(url, new URLSearchParams(urlParams));
178
194
 
179
195
  return await this.request<string[]>(request.uri, {
@@ -197,7 +213,11 @@ export class DataProvider {
197
213
  ]);
198
214
  };
199
215
 
200
- async loadMetricsMetadata() {
216
+ async loadMetricsMetadata(): Promise<PromMetricsMetadata> {
217
+ if (!this.enableRequests) {
218
+ this.metricsMetadata = {} as PromMetricsMetadata;
219
+ return this.metricsMetadata;
220
+ }
201
221
  const request = this.buildRequest(`${this.apiPrefix}/metadata`, new URLSearchParams({}));
202
222
  this.metricsMetadata = await this.request<PromMetricsMetadata>(request.uri, {
203
223
  method: this.httpMethod,
@@ -205,6 +225,8 @@ export class DataProvider {
205
225
  }).catch(() => {
206
226
  return {} as PromMetricsMetadata;
207
227
  });
228
+
229
+ return this.metricsMetadata || ({} as PromMetricsMetadata);
208
230
  }
209
231
 
210
232
  metricNamesToMetrics(metricNames: string[]): Metric[] {
@@ -1,4 +1,4 @@
1
- import UFuzzy from '@leeoniya/ufuzzy';
1
+ // import UFuzzy from '@leeoniya/ufuzzy';
2
2
 
3
3
  import { FUNCTIONS } from '../promql';
4
4
  import { makeSelector, NeverCaseError } from '../util';
@@ -17,26 +17,26 @@ type Completion = {
17
17
  triggerOnInsert?: boolean;
18
18
  };
19
19
 
20
- const metricNamesSearchClient = new UFuzzy({ intraMode: 1 });
20
+ // const metricNamesSearchClient = new UFuzzy({ intraMode: 1 });
21
21
 
22
22
  // we order items like: history, functions, metrics
23
23
  function getAllMetricNamesCompletions(dataProvider: DataProvider): Completion[] {
24
24
  let metricNames = dataProvider.getAllMetricNames();
25
25
 
26
- if (metricNames.length > dataProvider.metricNamesSuggestionLimit) {
27
- const { monacoSettings } = dataProvider;
28
- monacoSettings.enableAutocompleteSuggestionsUpdate();
29
-
30
- if (monacoSettings.inputInRange) {
31
- metricNames =
32
- metricNamesSearchClient
33
- .filter(metricNames, monacoSettings.inputInRange)
34
- ?.slice(0, dataProvider.metricNamesSuggestionLimit)
35
- .map((idx) => metricNames[idx]) ?? [];
36
- } else {
37
- metricNames = metricNames.slice(0, dataProvider.metricNamesSuggestionLimit);
38
- }
39
- }
26
+ // if (metricNames.length > dataProvider.metricNamesSuggestionLimit) {
27
+ // const { monacoSettings } = dataProvider;
28
+ // monacoSettings.enableAutocompleteSuggestionsUpdate();
29
+
30
+ // if (monacoSettings.inputInRange) {
31
+ // metricNames =
32
+ // metricNamesSearchClient
33
+ // .filter(metricNames, monacoSettings.inputInRange)
34
+ // ?.slice(0, dataProvider.metricNamesSuggestionLimit)
35
+ // .map((idx) => metricNames[idx]) ?? [];
36
+ // } else {
37
+ // metricNames = metricNames.slice(0, dataProvider.metricNamesSuggestionLimit);
38
+ // }
39
+ // }
40
40
 
41
41
  return dataProvider.metricNamesToMetrics(metricNames).map((metric) => ({
42
42
  type: 'METRIC_NAME',
@@ -143,7 +143,7 @@ async function getLabelValues(metric: string | undefined, labelName: string, oth
143
143
  if (key === '__name__') {
144
144
  continue;
145
145
  }
146
- if (key === labelName) {
146
+ if (key === labelName && value) {
147
147
  labelValues.add(value);
148
148
  }
149
149
  }
@@ -20,6 +20,8 @@ interface PromQLEditorProps {
20
20
  placeholder?: string;
21
21
  enableAutocomplete?: boolean;
22
22
  durationVariablesCompletion?: boolean;
23
+ // When false, DataProvider will not send network requests. Default: true
24
+ enableRequests?: boolean;
23
25
  readOnly?: boolean;
24
26
  disabled?: boolean;
25
27
  interpolateString?: (query: string) => string;
@@ -92,6 +94,7 @@ export default function PromQLEditor(props: PromQLEditorProps & DataProviderPara
92
94
  placeholder,
93
95
  interpolateString,
94
96
  enableAutocomplete = true,
97
+ enableRequests = true,
95
98
  readOnly = false,
96
99
  disabled = false,
97
100
  onChange,
@@ -266,6 +269,7 @@ export default function PromQLEditor(props: PromQLEditorProps & DataProviderPara
266
269
  httpMethod: props.httpMethod,
267
270
  apiPrefix: props.apiPrefix,
268
271
  httpErrorHandler: props.httpErrorHandler,
272
+ enableRequests: enableRequests,
269
273
  });
270
274
  dataProviderRef.current = dataProvider;
271
275
  dataProvider.start();
@@ -331,6 +335,7 @@ export default function PromQLEditor(props: PromQLEditorProps & DataProviderPara
331
335
  props.httpMethod,
332
336
  props.apiPrefix,
333
337
  placeholder,
338
+ enableRequests,
334
339
  ]);
335
340
 
336
341
  return (
@@ -33,4 +33,6 @@ export interface DataProviderParams {
33
33
  httpMethod?: 'POST' | 'GET';
34
34
  apiPrefix?: string;
35
35
  httpErrorHandler?: (error: any) => void;
36
+ // When false, DataProvider will not send any network requests.
37
+ enableRequests?: boolean;
36
38
  }