mirror of
https://github.com/grafana/grafana.git
synced 2025-09-25 23:14:10 +08:00
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
This commit is contained in:
40
public/app/plugins/datasource/loki/variables.ts
Normal file
40
public/app/plugins/datasource/loki/variables.ts
Normal file
@ -0,0 +1,40 @@
|
||||
import { from, Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
|
||||
import { CustomVariableSupport, DataQueryRequest, DataQueryResponse } from '@grafana/data';
|
||||
|
||||
import { LokiVariableQueryEditor } from './components/VariableQueryEditor';
|
||||
import { LokiDatasource } from './datasource';
|
||||
import { LokiVariableQuery, LokiVariableQueryType } from './types';
|
||||
|
||||
export class LokiVariableSupport extends CustomVariableSupport<LokiDatasource, LokiVariableQuery> {
|
||||
editor = LokiVariableQueryEditor;
|
||||
|
||||
constructor(private datasource: LokiDatasource) {
|
||||
super();
|
||||
this.query = this.query.bind(this);
|
||||
}
|
||||
|
||||
async execute(query: LokiVariableQuery) {
|
||||
if (query.type === LokiVariableQueryType.LabelNames) {
|
||||
return this.datasource.labelNamesQuery();
|
||||
}
|
||||
|
||||
if (!query.label) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// If we have query expr, use /series endpoint
|
||||
if (query.stream) {
|
||||
return this.datasource.labelValuesSeriesQuery(query.stream, query.label);
|
||||
}
|
||||
|
||||
return this.datasource.labelValuesQuery(query.label);
|
||||
}
|
||||
|
||||
query(request: DataQueryRequest<LokiVariableQuery>): Observable<DataQueryResponse> {
|
||||
const result = this.execute(request.targets[0]);
|
||||
|
||||
return from(result).pipe(map((data) => ({ data })));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user