Loki: Fix type errors in language_provider (#31902)

* Add initial stat for logLabelOptions

* Refactor, update, remove

* Simplify
pull/31281/head
Ivana Huckova 4 years ago committed by GitHub
parent 57edbb4157
commit 91dde5c980
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      public/app/plugins/datasource/loki/components/LokiQueryField.tsx
  2. 1
      public/app/plugins/datasource/loki/components/__snapshots__/LokiExploreQueryEditor.test.tsx.snap
  3. 52
      public/app/plugins/datasource/loki/language_provider.ts

@ -3,7 +3,7 @@ import { LokiQueryFieldForm, LokiQueryFieldFormProps } from './LokiQueryFieldFor
type LokiQueryFieldProps = Omit<
LokiQueryFieldFormProps,
'labelsLoaded' | 'onLoadOptions' | 'onLabelsRefresh' | 'logLabelOptions' | 'absoluteRange'
'labelsLoaded' | 'onLoadOptions' | 'onLabelsRefresh' | 'absoluteRange'
>;
export const LokiQueryField: FunctionComponent<LokiQueryFieldProps> = (props) => {

@ -40,7 +40,6 @@ exports[`LokiExploreQueryEditor should render component 1`] = `
Object {
"getTimeRangeParams": [Function],
"languageProvider": LokiLanguageProvider {
"addLabelValuesToOptions": [Function],
"cleanText": [Function],
"datasource": [Circular],
"fetchSeries": [Function],

@ -68,7 +68,6 @@ export function addHistoryMetadata(item: CompletionItem, history: LokiHistoryIte
export default class LokiLanguageProvider extends LanguageProvider {
labelKeys: string[];
logLabelOptions: any[];
logLabelFetchTs: number;
started: boolean;
datasource: LokiDatasource;
@ -414,15 +413,14 @@ export default class LokiLanguageProvider extends LanguageProvider {
*/
async fetchLogLabels(): Promise<any> {
const url = '/loki/api/v1/label';
try {
this.logLabelFetchTs = Date.now().valueOf();
const rangeParams = this.datasource.getTimeRangeParams();
const res = await this.request(url, rangeParams);
const timeRange = this.datasource.getTimeRangeParams();
this.logLabelFetchTs = Date.now().valueOf();
const res = await this.request(url, timeRange);
if (Array.isArray(res)) {
this.labelKeys = res.slice().sort();
this.logLabelOptions = this.labelKeys.map((key: string) => ({ label: key, value: key, isLeaf: false }));
} catch (e) {
console.error(e);
}
return [];
}
@ -485,41 +483,23 @@ export default class LokiLanguageProvider extends LanguageProvider {
async fetchLabelValues(key: string): Promise<string[]> {
const url = `/loki/api/v1/label/${key}/values`;
let values: string[] = [];
const rangeParams = this.datasource.getTimeRangeParams();
const { from: start, to: end } = rangeParams;
const cacheKey = this.generateCacheKey(url, start, end, key);
const params = { start, end };
let value = this.labelsCache.get(cacheKey);
if (!value) {
try {
// Clear value when requesting new one. Empty object being truthy also makes sure we don't request twice.
this.labelsCache.set(cacheKey, []);
const res = await this.request(url, params);
values = res.slice().sort();
value = values;
this.labelsCache.set(cacheKey, value);
this.logLabelOptions = this.addLabelValuesToOptions(key, values);
} catch (e) {
console.error(e);
let labelValue = this.labelsCache.get(cacheKey);
if (!labelValue) {
// Clear value when requesting new one. Empty object being truthy also makes sure we don't request twice.
this.labelsCache.set(cacheKey, []);
const res = await this.request(url, params);
if (Array.isArray(res)) {
labelValue = res.slice().sort();
this.labelsCache.set(cacheKey, labelValue);
}
} else {
this.logLabelOptions = this.addLabelValuesToOptions(key, value);
}
return value ?? [];
}
private addLabelValuesToOptions = (labelKey: string, values: string[]) => {
return this.logLabelOptions.map((keyOption) =>
keyOption.value === labelKey
? {
...keyOption,
children: values.map((value) => ({ label: value, value })),
}
: keyOption
);
};
return labelValue ?? [];
}
}

Loading…
Cancel
Save