grafana/public/app/plugins/datasource/grafana-azure-monitor-datasource/datasource.ts
kay delaney cf2cc71393 Chore: Remove angular dependency from backendSrv (#20999)
* Chore: Remove angular dependency from backendSrv

* Refactor: Naive soultion for logging out unauthorized users

* Refactor: Restructures to different streams

* Refactor: Restructures datasourceRequest

* Refactor: Flipped back if statement

* Refactor: Extracted getFromFetchStream

* Refactor: Extracts toFailureStream operation

* Refactor: Fixes issue when options.params contains arrays

* Refactor: Fixes broken test (but we need a lot more)

* Refactor: Adds explaining comments

* Refactor: Adds latest RxJs version so cancellations work

* Refactor: Cleans up the takeUntil code

* Refactor: Adds tests for request function

* Refactor: Separates into smaller functions

* Refactor: Adds last error tests

* Started to changed so we require getBackendSrv from the @grafana-runtime when applicable.

* Using the getBackendSrv from @grafana/runtime.

* Changed so we use the getBackendSrv from the @grafana-runtime when possible.

* Fixed so Server Admin -> Orgs works again.

* Removed unused dependency.

* Fixed digest issues on the Server Admin -> Users page.

* Fix: Fixes digest problems in Playlists

* Fix: Fixes digest issues in VersionHistory

* Tests: Fixes broken tests

* Fix: Fixes digest issues in Alerting => Notification channels

* Fixed digest issues on the Intive page.

* Fixed so we run digest after password reset email sent.

* Fixed digest issue when trying to sign up account.

* Fixed so the Server Admin -> Edit Org works with backendSrv

* Fixed so Server Admin -> Users works with backend srv.

* Fixed digest issues in Server Admin -> Orgs

* Fix: Fixes digest issues in DashList plugin

* Fixed digest issues on Server Admin -> users.

* Fix: Fixes digest issues with Snapshots

* Fixed digest issue when deleting a user.

* Fix: Fixes digest issues with dashLink

* Chore: Changes RxJs version to 6.5.4 which includes the same cancellation fix

* Fix: Fixes digest issue when toggling folder in manage dashboards

* Fix: Fixes bug in executeInOrder

* Fix: Fixes digest issue with CreateFolderCtrl and FolderDashboardsCtrl

* Fix: Fixes tslint error in test

* Refactor: Changes default behaviour for emitted messages as before migration

* Fix: Fixes various digest issues when saving, starring or deleting dashboards

* Fix: Fixes digest issues with FolderPickerCtrl

* Fixed digest issue.

* Fixed digest issues.

* Fixed issues with angular digest.

* Removed the this.digest pattern.

Co-authored-by: Hugo Häggmark <hugo.haggmark@gmail.com>
Co-authored-by: Marcus Andersson <systemvetaren@gmail.com>
2020-01-21 10:08:07 +01:00

210 lines
6.4 KiB
TypeScript

import _ from 'lodash';
import AzureMonitorDatasource from './azure_monitor/azure_monitor_datasource';
import AppInsightsDatasource from './app_insights/app_insights_datasource';
import AzureLogAnalyticsDatasource from './azure_log_analytics/azure_log_analytics_datasource';
import { AzureMonitorQuery, AzureDataSourceJsonData } from './types';
import { DataSourceApi, DataQueryRequest, DataSourceInstanceSettings } from '@grafana/data';
import { TemplateSrv } from 'app/features/templating/template_srv';
export default class Datasource extends DataSourceApi<AzureMonitorQuery, AzureDataSourceJsonData> {
azureMonitorDatasource: AzureMonitorDatasource;
appInsightsDatasource: AppInsightsDatasource;
azureLogAnalyticsDatasource: AzureLogAnalyticsDatasource;
/** @ngInject */
constructor(instanceSettings: DataSourceInstanceSettings<AzureDataSourceJsonData>, private templateSrv: TemplateSrv) {
super(instanceSettings);
this.azureMonitorDatasource = new AzureMonitorDatasource(instanceSettings, this.templateSrv);
this.appInsightsDatasource = new AppInsightsDatasource(instanceSettings, this.templateSrv);
this.azureLogAnalyticsDatasource = new AzureLogAnalyticsDatasource(instanceSettings, this.templateSrv);
}
async query(options: DataQueryRequest<AzureMonitorQuery>) {
const promises: any[] = [];
const azureMonitorOptions = _.cloneDeep(options);
const appInsightsOptions = _.cloneDeep(options);
const azureLogAnalyticsOptions = _.cloneDeep(options);
azureMonitorOptions.targets = _.filter(azureMonitorOptions.targets, ['queryType', 'Azure Monitor']);
appInsightsOptions.targets = _.filter(appInsightsOptions.targets, ['queryType', 'Application Insights']);
azureLogAnalyticsOptions.targets = _.filter(azureLogAnalyticsOptions.targets, ['queryType', 'Azure Log Analytics']);
if (azureMonitorOptions.targets.length > 0) {
const amPromise = this.azureMonitorDatasource.query(azureMonitorOptions);
if (amPromise) {
promises.push(amPromise);
}
}
if (appInsightsOptions.targets.length > 0) {
const aiPromise = this.appInsightsDatasource.query(appInsightsOptions);
if (aiPromise) {
promises.push(aiPromise);
}
}
if (azureLogAnalyticsOptions.targets.length > 0) {
const alaPromise = this.azureLogAnalyticsDatasource.query(azureLogAnalyticsOptions);
if (alaPromise) {
promises.push(alaPromise);
}
}
if (promises.length === 0) {
return Promise.resolve({ data: [] });
}
return Promise.all(promises).then(results => {
return { data: _.flatten(results) };
});
}
async annotationQuery(options: any) {
return this.azureLogAnalyticsDatasource.annotationQuery(options);
}
async metricFindQuery(query: string) {
if (!query) {
return Promise.resolve([]);
}
const aiResult = this.appInsightsDatasource.metricFindQuery(query);
if (aiResult) {
return aiResult;
}
const amResult = this.azureMonitorDatasource.metricFindQuery(query);
if (amResult) {
return amResult;
}
const alaResult = this.azureLogAnalyticsDatasource.metricFindQuery(query);
if (alaResult) {
return alaResult;
}
return Promise.resolve([]);
}
async testDatasource() {
const promises: any[] = [];
if (this.azureMonitorDatasource.isConfigured()) {
promises.push(this.azureMonitorDatasource.testDatasource());
}
if (this.appInsightsDatasource.isConfigured()) {
promises.push(this.appInsightsDatasource.testDatasource());
}
if (this.azureLogAnalyticsDatasource.isConfigured()) {
promises.push(this.azureLogAnalyticsDatasource.testDatasource());
}
if (promises.length === 0) {
return {
status: 'error',
message: `Nothing configured. At least one of the API's must be configured.`,
title: 'Error',
};
}
return Promise.all(promises).then(results => {
let status = 'success';
let message = '';
for (let i = 0; i < results.length; i++) {
if (results[i].status !== 'success') {
status = results[i].status;
}
message += `${i + 1}. ${results[i].message} `;
}
return {
status: status,
message: message,
title: _.upperFirst(status),
};
});
}
/* Azure Monitor REST API methods */
getResourceGroups(subscriptionId: string) {
return this.azureMonitorDatasource.getResourceGroups(subscriptionId);
}
getMetricDefinitions(subscriptionId: string, resourceGroup: string) {
return this.azureMonitorDatasource.getMetricDefinitions(subscriptionId, resourceGroup);
}
getResourceNames(subscriptionId: string, resourceGroup: string, metricDefinition: string) {
return this.azureMonitorDatasource.getResourceNames(subscriptionId, resourceGroup, metricDefinition);
}
getMetricNames(
subscriptionId: string,
resourceGroup: string,
metricDefinition: string,
resourceName: string,
metricNamespace: string
) {
return this.azureMonitorDatasource.getMetricNames(
subscriptionId,
resourceGroup,
metricDefinition,
resourceName,
metricNamespace
);
}
getMetricNamespaces(subscriptionId: string, resourceGroup: string, metricDefinition: string, resourceName: string) {
return this.azureMonitorDatasource.getMetricNamespaces(
subscriptionId,
resourceGroup,
metricDefinition,
resourceName
);
}
getMetricMetadata(
subscriptionId: string,
resourceGroup: string,
metricDefinition: string,
resourceName: string,
metricNamespace: string,
metricName: string
) {
return this.azureMonitorDatasource.getMetricMetadata(
subscriptionId,
resourceGroup,
metricDefinition,
resourceName,
metricNamespace,
metricName
);
}
/* Application Insights API method */
getAppInsightsMetricNames() {
return this.appInsightsDatasource.getMetricNames();
}
getAppInsightsMetricMetadata(metricName: string) {
return this.appInsightsDatasource.getMetricMetadata(metricName);
}
getAppInsightsColumns(refId: string | number) {
return this.appInsightsDatasource.logAnalyticsColumns[refId];
}
/*Azure Log Analytics */
getAzureLogAnalyticsWorkspaces(subscriptionId: string) {
return this.azureLogAnalyticsDatasource.getWorkspaces(subscriptionId);
}
getSubscriptions() {
return this.azureMonitorDatasource.getSubscriptions();
}
}