Files
grafana/public/app/plugins/datasource/loki/migrations/variableQueryMigrations.ts
Matias Chomicki 6e6069a2ba Loki: Create Variable Query Editor for Loki. (#54102)
* feat(loki-query-editor): create base editor component

* feat(loki-query-editor): update editor to use loki query type

* feat(loki-query-editor): add custom variable support to datasource

* feat(loki-query-editor): prevent errors when no label is present

* Add unit test for LokiMetricFindQuery

* Update datasource test

* Add component test

* Add variable query migration support

* feat(loki-query-editor): add migration support for older-format variables

* Fix enum capitalization for consistency

* Move attribute to the top of the class

* Remove unnecessary from()

* Update capitalization of new enum

* Fix enum capitalization in component

* feat(loki-query-editor): replace unnecessary class with class method
2022-08-30 18:18:51 +02:00

37 lines
1.0 KiB
TypeScript

import { LokiVariableQuery, LokiVariableQueryType } from '../types';
export const labelNamesRegex = /^label_names\(\)\s*$/;
export const labelValuesRegex = /^label_values\((?:(.+),\s*)?([a-zA-Z_][a-zA-Z0-9_]*)\)\s*$/;
export function migrateVariableQuery(rawQuery: string | LokiVariableQuery): LokiVariableQuery {
// If not string, we assume LokiVariableQuery
if (typeof rawQuery !== 'string') {
return rawQuery;
}
const queryBase = {
refId: 'LokiVariableQueryEditor-VariableQuery',
type: LokiVariableQueryType.LabelNames,
};
const labelNames = rawQuery.match(labelNamesRegex);
if (labelNames) {
return {
...queryBase,
type: LokiVariableQueryType.LabelNames,
};
}
const labelValues = rawQuery.match(labelValuesRegex);
if (labelValues) {
return {
...queryBase,
type: LokiVariableQueryType.LabelValues,
label: labelValues[2] ? labelValues[2] : labelValues[1],
stream: labelValues[2] ? labelValues[1] : undefined,
};
}
return queryBase;
}