grafana/public/app/plugins/datasource/grafana-azure-monitor-datasource/datasource.ts

227 lines
7.4 KiB
TypeScript
Raw Normal View History

import { cloneDeep } from 'lodash';
import { forkJoin, Observable, of } from 'rxjs';
import { map } from 'rxjs/operators';
import {
DataFrame,
DataQueryRequest,
DataQueryResponse,
DataSourceInstanceSettings,
LoadingState,
ScopedVars,
} from '@grafana/data';
import { DataSourceWithBackend } from '@grafana/runtime';
import { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_srv';
import AzureLogAnalyticsDatasource from './azure_log_analytics/azure_log_analytics_datasource';
import AzureMonitorDatasource from './azure_monitor/azure_monitor_datasource';
import AzureResourceGraphDatasource from './azure_resource_graph/azure_resource_graph_datasource';
import ResourcePickerData from './resourcePicker/resourcePickerData';
import { AzureDataSourceJsonData, AzureMonitorQuery, AzureQueryType } from './types';
import migrateAnnotation from './utils/migrateAnnotation';
import migrateQuery from './utils/migrateQuery';
import { VariableSupport } from './variables';
export default class Datasource extends DataSourceWithBackend<AzureMonitorQuery, AzureDataSourceJsonData> {
annotations = {
prepareAnnotation: migrateAnnotation,
};
azureMonitorDatasource: AzureMonitorDatasource;
azureLogAnalyticsDatasource: AzureLogAnalyticsDatasource;
resourcePickerData: ResourcePickerData;
azureResourceGraphDatasource: AzureResourceGraphDatasource;
pseudoDatasource: {
[key in AzureQueryType]?: AzureMonitorDatasource | AzureLogAnalyticsDatasource | AzureResourceGraphDatasource;
} = {};
declare optionsKey: Record<AzureQueryType, string>;
AzureMonitor: Migrate Metrics query editor to React (#30783) * AzureMonitor: Remove anys from datasource to get the inferred type * AzureMonitor: Cast some datasource types TODO: we want proper types for these * AzureMonitor: Initial react Metrics editor components * start dimension fields * replace replaceTemplateVariable with datasource.replace, and rename onQueryChange to onChange * actually just do template variable replacement in the datasource * don't use azureMonitorIsConfigured * Refactors, mainly around the metric metadata - Convert all the metric metadata options for the Select before its set into state - Stop using SelectableValue because it's basically any when all the properties are optional - the onChange function passed to the fields now just accepts the direct value, rather than wrapped in a SelectableValue * added proper fields, and adding and removing for DimensionFields * Update query with Dimension changes * Width * subscription and query type fields * Should be feature complete now, more or less * fix missing import * fix lint issues * set default subscription ID * Starting to write some tests * tests for query editor * Remove subscription ID from the label in Metrics But we keep it there for the angular stuff * MetricsQueryEditor tests * Update index.test.tsx * fix tests * add template variables to dropdowns * clean up * update tests * Reorganise react components * Group query fields into rows * Rename Option type, add Azure response type * Refactor Metrics metric metadata - Types the Azure API - Moves default metadata values into datasource * nit * update test
2021-03-11 05:37:39 -06:00
constructor(
instanceSettings: DataSourceInstanceSettings<AzureDataSourceJsonData>,
private readonly templateSrv: TemplateSrv = getTemplateSrv()
) {
super(instanceSettings);
this.azureMonitorDatasource = new AzureMonitorDatasource(instanceSettings);
this.azureLogAnalyticsDatasource = new AzureLogAnalyticsDatasource(instanceSettings);
this.azureResourceGraphDatasource = new AzureResourceGraphDatasource(instanceSettings);
this.resourcePickerData = new ResourcePickerData(instanceSettings, this.azureMonitorDatasource);
this.pseudoDatasource = {
[AzureQueryType.AzureMonitor]: this.azureMonitorDatasource,
[AzureQueryType.LogAnalytics]: this.azureLogAnalyticsDatasource,
[AzureQueryType.AzureResourceGraph]: this.azureResourceGraphDatasource,
};
this.variables = new VariableSupport(this);
}
filterQuery(item: AzureMonitorQuery): boolean {
if (!item.queryType) {
return false;
}
const ds = this.pseudoDatasource[item.queryType];
return ds?.filterQuery?.(item) ?? true;
}
query(options: DataQueryRequest<AzureMonitorQuery>): Observable<DataQueryResponse> {
const byType = new Map<AzureQueryType, DataQueryRequest<AzureMonitorQuery>>();
for (const baseTarget of options.targets) {
// Migrate old query structures
const target = migrateQuery(baseTarget);
// Skip hidden or invalid queries or ones without properties
if (!target.queryType || target.hide || !hasQueryForType(target)) {
continue;
}
// Initialize the list of queries
if (!byType.has(target.queryType)) {
const queryForType = cloneDeep(options);
queryForType.requestId = `${queryForType.requestId}-${target.refId}`;
queryForType.targets = [];
byType.set(target.queryType, queryForType);
}
const queryForType = byType.get(target.queryType);
queryForType?.targets.push(target);
}
const observables: Array<Observable<DataQueryResponse>> = Array.from(byType.entries()).map(([queryType, req]) => {
const ds = this.pseudoDatasource[queryType];
if (!ds) {
throw new Error('Data source not created for query type ' + queryType);
}
return ds.query(req);
});
// Single query can skip merge
if (observables.length === 1) {
return observables[0];
}
if (observables.length > 1) {
return forkJoin(observables).pipe(
map((results: DataQueryResponse[]) => {
const data: DataFrame[] = [];
for (const result of results) {
for (const frame of result.data) {
data.push(frame);
}
}
return { state: LoadingState.Done, data };
})
);
}
return of({ state: LoadingState.Done, data: [] });
}
targetContainsTemplate(query: AzureMonitorQuery) {
if (query.subscription && this.templateSrv.containsTemplate(query.subscription)) {
return true;
}
let subQuery;
if (query.queryType === AzureQueryType.AzureMonitor) {
subQuery = JSON.stringify(query.azureMonitor);
} else if (query.queryType === AzureQueryType.LogAnalytics) {
subQuery = JSON.stringify(query.azureLogAnalytics);
} else if (query.queryType === AzureQueryType.AzureResourceGraph) {
subQuery = JSON.stringify([query.azureResourceGraph, query.subscriptions]);
}
return !!subQuery && this.templateSrv.containsTemplate(subQuery);
}
2019-07-06 00:01:22 -05:00
async annotationQuery(options: any) {
return this.azureLogAnalyticsDatasource.annotationQuery(options);
}
/* Azure Monitor REST API methods */
AzureMonitor: adds support for multiple subscriptions per datasource (#16922) * chore: AzureMonitor typescript typings Removes some types and using @grafana/ui types instead. Adds some typing for the AzureMonitor query. Also adds a getSubscriptions function that will used in the query editor. * fix: AzureMonitor adds back editor for annotation queries This must have been broken for a month or more. Now possible to edit annotation queries again. * feat: Azure Monitor - support for multiple subscriptions Adds a new dropdown for subscriptions in the query editor. Defaults to the subscription id in jsonData for queries that have no subscription id. * feat: adds Azure Logs multi subscriptions support The subscription id is needed for fetching the list of workspaces. Adds support to the Log Analytics datasource and to the annotations for Log Analytics to be able to choose between multiple subscriptions. * feat: AzureMonitor config page with multiple subs Adds support for multiple subscriptions for the different variations of configuring Azure Monitor and Azure Logs. To be able to show a list of subscriptions, the config has to be saved first - the plugin route fetches the tenant id, client id and client secret from the database so a call to get subscriptions requires that those fields are saved first. If the page has not saved then the use can manually paste in a subscription id. * feat: support for multi subs in Azure Monitor variables Adds an optional subscription parameter to the template variable macros. Also adds a Subscriptions macro. * fix: remove some implicit anys from tests
2019-05-07 08:45:15 -05:00
getResourceGroups(subscriptionId: string) {
return this.azureMonitorDatasource.getResourceGroups(this.templateSrv.replace(subscriptionId));
}
getMetricNamespaces(subscriptionId: string, resourceGroup?: string) {
let url = `/subscriptions/${subscriptionId}`;
if (resourceGroup) {
url += `/resourceGroups/${resourceGroup};`;
}
return this.azureMonitorDatasource.getMetricNamespaces({ resourceUri: url }, true);
}
getResourceNames(subscriptionId: string, resourceGroup?: string, metricNamespace?: string) {
AzureMonitor: Migrate Metrics query editor to React (#30783) * AzureMonitor: Remove anys from datasource to get the inferred type * AzureMonitor: Cast some datasource types TODO: we want proper types for these * AzureMonitor: Initial react Metrics editor components * start dimension fields * replace replaceTemplateVariable with datasource.replace, and rename onQueryChange to onChange * actually just do template variable replacement in the datasource * don't use azureMonitorIsConfigured * Refactors, mainly around the metric metadata - Convert all the metric metadata options for the Select before its set into state - Stop using SelectableValue because it's basically any when all the properties are optional - the onChange function passed to the fields now just accepts the direct value, rather than wrapped in a SelectableValue * added proper fields, and adding and removing for DimensionFields * Update query with Dimension changes * Width * subscription and query type fields * Should be feature complete now, more or less * fix missing import * fix lint issues * set default subscription ID * Starting to write some tests * tests for query editor * Remove subscription ID from the label in Metrics But we keep it there for the angular stuff * MetricsQueryEditor tests * Update index.test.tsx * fix tests * add template variables to dropdowns * clean up * update tests * Reorganise react components * Group query fields into rows * Rename Option type, add Azure response type * Refactor Metrics metric metadata - Types the Azure API - Moves default metadata values into datasource * nit * update test
2021-03-11 05:37:39 -06:00
return this.azureMonitorDatasource.getResourceNames(
this.templateSrv.replace(subscriptionId),
this.templateSrv.replace(resourceGroup),
this.templateSrv.replace(metricNamespace)
AzureMonitor: Migrate Metrics query editor to React (#30783) * AzureMonitor: Remove anys from datasource to get the inferred type * AzureMonitor: Cast some datasource types TODO: we want proper types for these * AzureMonitor: Initial react Metrics editor components * start dimension fields * replace replaceTemplateVariable with datasource.replace, and rename onQueryChange to onChange * actually just do template variable replacement in the datasource * don't use azureMonitorIsConfigured * Refactors, mainly around the metric metadata - Convert all the metric metadata options for the Select before its set into state - Stop using SelectableValue because it's basically any when all the properties are optional - the onChange function passed to the fields now just accepts the direct value, rather than wrapped in a SelectableValue * added proper fields, and adding and removing for DimensionFields * Update query with Dimension changes * Width * subscription and query type fields * Should be feature complete now, more or less * fix missing import * fix lint issues * set default subscription ID * Starting to write some tests * tests for query editor * Remove subscription ID from the label in Metrics But we keep it there for the angular stuff * MetricsQueryEditor tests * Update index.test.tsx * fix tests * add template variables to dropdowns * clean up * update tests * Reorganise react components * Group query fields into rows * Rename Option type, add Azure response type * Refactor Metrics metric metadata - Types the Azure API - Moves default metadata values into datasource * nit * update test
2021-03-11 05:37:39 -06:00
);
}
getMetricNames(subscriptionId: string, resourceGroup: string, metricNamespace: string, resourceName: string) {
return this.azureMonitorDatasource.getMetricNames({
subscription: subscriptionId,
resourceGroup,
metricNamespace,
resourceName,
});
}
/*Azure Log Analytics */
AzureMonitor: adds support for multiple subscriptions per datasource (#16922) * chore: AzureMonitor typescript typings Removes some types and using @grafana/ui types instead. Adds some typing for the AzureMonitor query. Also adds a getSubscriptions function that will used in the query editor. * fix: AzureMonitor adds back editor for annotation queries This must have been broken for a month or more. Now possible to edit annotation queries again. * feat: Azure Monitor - support for multiple subscriptions Adds a new dropdown for subscriptions in the query editor. Defaults to the subscription id in jsonData for queries that have no subscription id. * feat: adds Azure Logs multi subscriptions support The subscription id is needed for fetching the list of workspaces. Adds support to the Log Analytics datasource and to the annotations for Log Analytics to be able to choose between multiple subscriptions. * feat: AzureMonitor config page with multiple subs Adds support for multiple subscriptions for the different variations of configuring Azure Monitor and Azure Logs. To be able to show a list of subscriptions, the config has to be saved first - the plugin route fetches the tenant id, client id and client secret from the database so a call to get subscriptions requires that those fields are saved first. If the page has not saved then the use can manually paste in a subscription id. * feat: support for multi subs in Azure Monitor variables Adds an optional subscription parameter to the template variable macros. Also adds a Subscriptions macro. * fix: remove some implicit anys from tests
2019-05-07 08:45:15 -05:00
getAzureLogAnalyticsWorkspaces(subscriptionId: string) {
return this.azureLogAnalyticsDatasource.getWorkspaces(subscriptionId);
}
getSubscriptions() {
return this.azureMonitorDatasource.getSubscriptions();
}
interpolateVariablesInQueries(queries: AzureMonitorQuery[], scopedVars: ScopedVars): AzureMonitorQuery[] {
const mapped = queries.map((query) => {
if (!query.queryType) {
return query;
}
const ds = this.pseudoDatasource[query.queryType];
return {
datasource: ds?.getRef(),
...(ds?.applyTemplateVariables(query, scopedVars) ?? query),
};
});
return mapped;
}
AzureMonitor: Migrate Metrics query editor to React (#30783) * AzureMonitor: Remove anys from datasource to get the inferred type * AzureMonitor: Cast some datasource types TODO: we want proper types for these * AzureMonitor: Initial react Metrics editor components * start dimension fields * replace replaceTemplateVariable with datasource.replace, and rename onQueryChange to onChange * actually just do template variable replacement in the datasource * don't use azureMonitorIsConfigured * Refactors, mainly around the metric metadata - Convert all the metric metadata options for the Select before its set into state - Stop using SelectableValue because it's basically any when all the properties are optional - the onChange function passed to the fields now just accepts the direct value, rather than wrapped in a SelectableValue * added proper fields, and adding and removing for DimensionFields * Update query with Dimension changes * Width * subscription and query type fields * Should be feature complete now, more or less * fix missing import * fix lint issues * set default subscription ID * Starting to write some tests * tests for query editor * Remove subscription ID from the label in Metrics But we keep it there for the angular stuff * MetricsQueryEditor tests * Update index.test.tsx * fix tests * add template variables to dropdowns * clean up * update tests * Reorganise react components * Group query fields into rows * Rename Option type, add Azure response type * Refactor Metrics metric metadata - Types the Azure API - Moves default metadata values into datasource * nit * update test
2021-03-11 05:37:39 -06:00
getVariables() {
return this.templateSrv.getVariables().map((v) => `$${v.name}`);
}
getVariablesRaw() {
return this.templateSrv.getVariables();
}
}
function hasQueryForType(query: AzureMonitorQuery): boolean {
switch (query.queryType) {
case AzureQueryType.AzureMonitor:
return !!query.azureMonitor;
case AzureQueryType.LogAnalytics:
return !!query.azureLogAnalytics;
case AzureQueryType.AzureResourceGraph:
return !!query.azureResourceGraph;
case AzureQueryType.GrafanaTemplateVariableFn:
return !!query.grafanaTemplateVariableFn;
default:
return false;
}
}