123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- import { isGUIDish } from './components/ResourcePicker/utils';
- import DataSource from './datasource';
- import { AzureMonitorQuery, AzureQueryType } from './types';
- import {
- AppInsightsGroupByQuery,
- AppInsightsMetricNameQuery,
- GrafanaTemplateVariableQuery,
- MetricDefinitionsQuery,
- MetricNamespaceQuery,
- MetricNamesQuery,
- ResourceGroupsQuery,
- ResourceNamesQuery,
- SubscriptionsQuery,
- WorkspacesQuery,
- } from './types/templateVariables';
- /*
- Grafana Template Variable Functions
- ex: Subscriptions()
- These are helper functions we have created and exposed to users to make the writing of template variables easier.
- Due to legacy reasons, we still need to parse strings to determine if a query is a Grafana Template Variable Function
- or if it's a KQL-type query
- */
- export const grafanaTemplateVariableFnMatches = (query: string) => {
- return {
- subscriptions: query.match(/^Subscriptions\(\)/i),
- resourceGroups: query.match(/^ResourceGroups\(\)/i),
- resourceGroupsWithSub: query.match(/^ResourceGroups\(([^\)]+?)(,\s?([^,]+?))?\)/i),
- metricDefinitions: query.match(/^Namespaces\(([^\)]+?)(,\s?([^,]+?))?\)/i),
- metricDefinitionsWithSub: query.match(/^Namespaces\(([^,]+?),\s?([^,]+?)\)/i),
- resourceNames: query.match(/^ResourceNames\(([^,]+?),\s?([^,]+?)\)/i),
- resourceNamesWithSub: query.match(/^ResourceNames\(([^,]+?),\s?([^,]+?),\s?(.+?)\)/i),
- metricNamespace: query.match(/^MetricNamespace\(([^,]+?),\s?([^,]+?),\s?([^,]+?)\)/i),
- metricNamespaceWithSub: query.match(/^metricnamespace\(([^,]+?),\s?([^,]+?),\s?([^,]+?),\s?([^,]+?)\)/i),
- metricNames: query.match(/^MetricNames\(([^,]+?),\s?([^,]+?),\s?([^,]+?),\s?([^,]+?)\)/i),
- metricNamesWithSub: query.match(/^MetricNames\(([^,]+?),\s?([^,]+?),\s?([^,]+?),\s?([^,]+?),\s?(.+?)\)/i),
- appInsightsMetricNameQuery: query.match(/^AppInsightsMetricNames\(\)/i),
- appInsightsGroupByQuery: query.match(/^AppInsightsGroupBys\(([^\)]+?)(,\s?([^,]+?))?\)/i),
- workspacesQuery: query.match(/^workspaces\(\)/i),
- workspacesQueryWithSub: query.match(/^workspaces\(["']?([^\)]+?)["']?\)/i),
- };
- };
- const isGrafanaTemplateVariableFnQuery = (query: string) => {
- const matches: Record<string, RegExpMatchArray | null> = grafanaTemplateVariableFnMatches(query);
- return Object.keys(matches).some((key) => !!matches[key]);
- };
- const createGrafanaTemplateVariableQuery = (rawQuery: string, datasource: DataSource): AzureMonitorQuery => {
- const matchesForQuery = grafanaTemplateVariableFnMatches(rawQuery);
- const defaultSubscriptionId = datasource.azureMonitorDatasource.defaultSubscriptionId;
- const createGrafanaTemplateVariableDetails = (): GrafanaTemplateVariableQuery => {
- // deprecated app insights template variables (will most likely remove in grafana 9)
- if (matchesForQuery.appInsightsMetricNameQuery) {
- const queryDetails: AppInsightsMetricNameQuery = { rawQuery, kind: 'AppInsightsMetricNameQuery' };
- return queryDetails;
- }
- if (matchesForQuery.appInsightsGroupByQuery) {
- const queryDetails: AppInsightsGroupByQuery = {
- kind: 'AppInsightsGroupByQuery',
- rawQuery,
- metricName: matchesForQuery.appInsightsGroupByQuery[1],
- };
- return queryDetails;
- }
- if (matchesForQuery.subscriptions) {
- const queryDetails: SubscriptionsQuery = {
- kind: 'SubscriptionsQuery',
- rawQuery,
- };
- return queryDetails;
- }
- if (matchesForQuery.resourceGroupsWithSub) {
- const queryDetails: ResourceGroupsQuery = {
- kind: 'ResourceGroupsQuery',
- rawQuery,
- subscription: matchesForQuery.resourceGroupsWithSub[1],
- };
- return queryDetails;
- }
- if (matchesForQuery.resourceGroups && defaultSubscriptionId) {
- const queryDetails: ResourceGroupsQuery = {
- kind: 'ResourceGroupsQuery',
- rawQuery,
- subscription: defaultSubscriptionId,
- };
- return queryDetails;
- }
- if (matchesForQuery.metricDefinitionsWithSub) {
- const queryDetails: MetricDefinitionsQuery = {
- kind: 'MetricDefinitionsQuery',
- rawQuery,
- subscription: matchesForQuery.metricDefinitionsWithSub[1],
- resourceGroup: matchesForQuery.metricDefinitionsWithSub[2],
- };
- return queryDetails;
- }
- if (matchesForQuery.metricDefinitions && defaultSubscriptionId) {
- const queryDetails: MetricDefinitionsQuery = {
- kind: 'MetricDefinitionsQuery',
- rawQuery,
- subscription: defaultSubscriptionId,
- resourceGroup: matchesForQuery.metricDefinitions[1],
- };
- return queryDetails;
- }
- if (matchesForQuery.resourceNamesWithSub) {
- const queryDetails: ResourceNamesQuery = {
- kind: 'ResourceNamesQuery',
- rawQuery,
- subscription: matchesForQuery.resourceNamesWithSub[1],
- resourceGroup: matchesForQuery.resourceNamesWithSub[2],
- metricDefinition: matchesForQuery.resourceNamesWithSub[3],
- };
- return queryDetails;
- }
- if (matchesForQuery.resourceNames && defaultSubscriptionId) {
- const queryDetails: ResourceNamesQuery = {
- kind: 'ResourceNamesQuery',
- rawQuery,
- subscription: defaultSubscriptionId,
- resourceGroup: matchesForQuery.resourceNames[1],
- metricDefinition: matchesForQuery.resourceNames[2],
- };
- return queryDetails;
- }
- if (matchesForQuery.metricNamespaceWithSub) {
- const queryDetails: MetricNamespaceQuery = {
- kind: 'MetricNamespaceQuery',
- rawQuery,
- subscription: matchesForQuery.metricNamespaceWithSub[1],
- resourceGroup: matchesForQuery.metricNamespaceWithSub[2],
- metricDefinition: matchesForQuery.metricNamespaceWithSub[3],
- resourceName: matchesForQuery.metricNamespaceWithSub[4],
- };
- return queryDetails;
- }
- if (matchesForQuery.metricNamespace && defaultSubscriptionId) {
- const queryDetails: MetricNamespaceQuery = {
- kind: 'MetricNamespaceQuery',
- rawQuery,
- subscription: defaultSubscriptionId,
- resourceGroup: matchesForQuery.metricNamespace[1],
- metricDefinition: matchesForQuery.metricNamespace[2],
- resourceName: matchesForQuery.metricNamespace[3],
- };
- return queryDetails;
- }
- if (matchesForQuery.metricNames && defaultSubscriptionId) {
- if (matchesForQuery.metricNames[3].indexOf(',') === -1) {
- const queryDetails: MetricNamesQuery = {
- kind: 'MetricNamesQuery',
- rawQuery,
- subscription: defaultSubscriptionId,
- resourceGroup: matchesForQuery.metricNames[1],
- metricDefinition: matchesForQuery.metricNames[2],
- resourceName: matchesForQuery.metricNames[3],
- metricNamespace: matchesForQuery.metricNames[4],
- };
- return queryDetails;
- }
- }
- if (matchesForQuery.metricNamesWithSub) {
- const queryDetails: MetricNamesQuery = {
- kind: 'MetricNamesQuery',
- rawQuery,
- subscription: matchesForQuery.metricNamesWithSub[1],
- resourceGroup: matchesForQuery.metricNamesWithSub[2],
- metricDefinition: matchesForQuery.metricNamesWithSub[3],
- resourceName: matchesForQuery.metricNamesWithSub[4],
- metricNamespace: matchesForQuery.metricNamesWithSub[5],
- };
- return queryDetails;
- }
- if (matchesForQuery.workspacesQueryWithSub) {
- const queryDetails: WorkspacesQuery = {
- kind: 'WorkspacesQuery',
- rawQuery,
- subscription: (matchesForQuery.workspacesQueryWithSub[1] || '').trim(),
- };
- return queryDetails;
- }
- if (matchesForQuery.workspacesQuery && defaultSubscriptionId) {
- const queryDetails: WorkspacesQuery = {
- kind: 'WorkspacesQuery',
- rawQuery,
- subscription: defaultSubscriptionId,
- };
- return queryDetails;
- }
- // fallback
- const queryDetails: SubscriptionsQuery = { kind: 'SubscriptionsQuery', rawQuery };
- return queryDetails;
- };
- const query: AzureMonitorQuery = {
- refId: 'A',
- queryType: AzureQueryType.GrafanaTemplateVariableFn,
- grafanaTemplateVariableFn: createGrafanaTemplateVariableDetails(),
- subscription: defaultSubscriptionId,
- };
- return query;
- };
- const createLogAnalyticsTemplateVariableQuery = async (
- rawQuery: string,
- datasource: DataSource
- ): Promise<AzureMonitorQuery> => {
- const defaultSubscriptionId = datasource.azureMonitorDatasource.defaultSubscriptionId;
- let resource = '';
- // if there's an existing query, we try to get the resourcesuri from a deprecated default workspace
- // a note this is very similar logic to what is used in useMigrations but moved out of the react-hook land
- if (rawQuery) {
- const defaultWorkspaceId = datasource.azureLogAnalyticsDatasource.getDeprecatedDefaultWorkSpace();
- if (defaultWorkspaceId) {
- const isWorkspaceGUID = isGUIDish(defaultWorkspaceId);
- if (isWorkspaceGUID) {
- resource = await datasource.resourcePickerData.getResourceURIFromWorkspace(defaultWorkspaceId);
- } else {
- resource = defaultWorkspaceId;
- }
- } else {
- const maybeFirstWorkspace = await datasource.azureLogAnalyticsDatasource.getFirstWorkspace();
- resource = maybeFirstWorkspace || '';
- }
- }
- return {
- refId: 'A',
- queryType: AzureQueryType.LogAnalytics,
- azureLogAnalytics: {
- query: rawQuery,
- resource,
- },
- subscription: defaultSubscriptionId,
- };
- };
- export const migrateStringQueriesToObjectQueries = async (
- rawQuery: string | AzureMonitorQuery,
- options: { datasource: DataSource }
- ): Promise<AzureMonitorQuery> => {
- // no need to migrate already migrated queries
- if (typeof rawQuery !== 'string') {
- return rawQuery;
- }
- return isGrafanaTemplateVariableFnQuery(rawQuery)
- ? createGrafanaTemplateVariableQuery(rawQuery, options.datasource)
- : createLogAnalyticsTemplateVariableQuery(rawQuery, options.datasource);
- };
|