variables.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. import { from, Observable } from 'rxjs';
  2. import { map, mergeMap } from 'rxjs/operators';
  3. import { CustomVariableSupport, DataQueryRequest, DataQueryResponse } from '@grafana/data';
  4. import CloudMonitoringMetricFindQuery from './CloudMonitoringMetricFindQuery';
  5. import { CloudMonitoringVariableQueryEditor } from './components/VariableQueryEditor';
  6. import CloudMonitoringDatasource from './datasource';
  7. import { CloudMonitoringVariableQuery } from './types';
  8. export class CloudMonitoringVariableSupport extends CustomVariableSupport<
  9. CloudMonitoringDatasource,
  10. CloudMonitoringVariableQuery
  11. > {
  12. private readonly metricFindQuery: CloudMonitoringMetricFindQuery;
  13. constructor(private readonly datasource: CloudMonitoringDatasource) {
  14. super();
  15. this.metricFindQuery = new CloudMonitoringMetricFindQuery(datasource);
  16. this.query = this.query.bind(this);
  17. }
  18. editor = CloudMonitoringVariableQueryEditor;
  19. query(request: DataQueryRequest<CloudMonitoringVariableQuery>): Observable<DataQueryResponse> {
  20. const executeObservable = from(this.metricFindQuery.execute(request.targets[0]));
  21. return from(this.datasource.ensureGCEDefaultProject()).pipe(
  22. mergeMap(() => executeObservable),
  23. map((data) => ({ data }))
  24. );
  25. }
  26. }