variables.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { from, lastValueFrom, Observable } from 'rxjs';
  2. import {
  3. CustomVariableSupport,
  4. DataQueryRequest,
  5. DataQueryResponse,
  6. MetricFindValue,
  7. toDataFrame,
  8. } from '@grafana/data';
  9. import { getTemplateSrv } from '@grafana/runtime';
  10. import VariableEditor from './components/VariableEditor/VariableEditor';
  11. import DataSource from './datasource';
  12. import { migrateStringQueriesToObjectQueries } from './grafanaTemplateVariableFns';
  13. import { AzureMonitorQuery, AzureQueryType } from './types';
  14. import { GrafanaTemplateVariableQuery } from './types/templateVariables';
  15. import messageFromError from './utils/messageFromError';
  16. export class VariableSupport extends CustomVariableSupport<DataSource, AzureMonitorQuery> {
  17. constructor(private readonly datasource: DataSource) {
  18. super();
  19. this.datasource = datasource;
  20. this.query = this.query.bind(this);
  21. }
  22. editor = VariableEditor;
  23. query(request: DataQueryRequest<AzureMonitorQuery>): Observable<DataQueryResponse> {
  24. const promisedResults = async () => {
  25. const queryObj = await migrateStringQueriesToObjectQueries(request.targets[0], { datasource: this.datasource });
  26. if (queryObj.queryType === AzureQueryType.GrafanaTemplateVariableFn && queryObj.grafanaTemplateVariableFn) {
  27. try {
  28. const templateVariablesResults = await this.callGrafanaTemplateVariableFn(queryObj.grafanaTemplateVariableFn);
  29. return {
  30. data: templateVariablesResults?.length ? [toDataFrame(templateVariablesResults)] : [],
  31. };
  32. } catch (err) {
  33. return { data: [], error: { message: messageFromError(err) } };
  34. }
  35. }
  36. request.targets[0] = queryObj;
  37. return lastValueFrom(this.datasource.query(request));
  38. };
  39. return from(promisedResults());
  40. }
  41. callGrafanaTemplateVariableFn(query: GrafanaTemplateVariableQuery): Promise<MetricFindValue[]> | null {
  42. if (query.kind === 'SubscriptionsQuery') {
  43. return this.datasource.getSubscriptions();
  44. }
  45. if (query.kind === 'ResourceGroupsQuery') {
  46. return this.datasource.getResourceGroups(this.replaceVariable(query.subscription));
  47. }
  48. if (query.kind === 'MetricDefinitionsQuery') {
  49. return this.datasource.getMetricDefinitions(
  50. this.replaceVariable(query.subscription),
  51. this.replaceVariable(query.resourceGroup)
  52. );
  53. }
  54. if (query.kind === 'ResourceNamesQuery') {
  55. return this.datasource.getResourceNames(
  56. this.replaceVariable(query.subscription),
  57. this.replaceVariable(query.resourceGroup),
  58. this.replaceVariable(query.metricDefinition)
  59. );
  60. }
  61. if (query.kind === 'MetricNamespaceQuery') {
  62. return this.datasource.azureMonitorDatasource.getMetricNamespaces(query);
  63. }
  64. if (query.kind === 'MetricNamesQuery') {
  65. return this.datasource.azureMonitorDatasource.getMetricNames(query);
  66. }
  67. if (query.kind === 'WorkspacesQuery') {
  68. return this.datasource.azureLogAnalyticsDatasource.getWorkspaces(this.replaceVariable(query.subscription));
  69. }
  70. return null;
  71. }
  72. replaceVariable(metric: string) {
  73. return getTemplateSrv().replace((metric || '').trim());
  74. }
  75. }