Loki: Fix type errors in language_provider (#31902) (#31945)

* Add initial stat for logLabelOptions

* Refactor, update, remove

* Simplify

(cherry picked from commit 91dde5c980)

Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com>
This commit is contained in:
Grot (@grafanabot)
2021-03-15 10:56:01 +01:00
committed by GitHub
co-authored by Ivana Huckova
parent c5ea45ab80
commit 0e9b553daf
3 changed files with 17 additions and 38 deletions
@@ -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],
@@ -56,7 +56,6 @@ export function addHistoryMetadata(item: CompletionItem, history: LokiHistoryIte
export default class LokiLanguageProvider extends LanguageProvider {
labelKeys: string[];
logLabelOptions: any[];
logLabelFetchTs: number;
started: boolean;
datasource: LokiDatasource;
@@ -402,15 +401,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 [];
}
@@ -473,41 +471,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 ?? [];
}
}