Files
grafana/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/SubscriptionField.tsx

99 lines
2.9 KiB
TypeScript
Raw Normal View History

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 11:37:39 +00:00
import React, { useCallback, useEffect, useState, useMemo } from 'react';
import { SelectableValue } from '@grafana/data';
import { Select } from '@grafana/ui';
import { AzureMonitorQuery, AzureQueryType, AzureQueryEditorFieldProps, AzureMonitorOption } from '../types';
import { findOption } from './common';
import { Field } from './Field';
interface SubscriptionFieldProps extends AzureQueryEditorFieldProps {
onQueryChange: (newQuery: AzureMonitorQuery) => void;
}
const SubscriptionField: React.FC<SubscriptionFieldProps> = ({
datasource,
query,
variableOptionGroup,
onQueryChange,
}) => {
const [subscriptions, setSubscriptions] = useState<AzureMonitorOption[]>([]);
useEffect(() => {
if (!datasource.azureMonitorDatasource.isConfigured()) {
return;
}
datasource.azureMonitorDatasource.getSubscriptions().then((results) => {
const newSubscriptions = results.map((v) => ({ label: v.text, value: v.value, description: v.value }));
setSubscriptions(newSubscriptions);
// Set a default subscription ID, if we can
let newSubscription = query.subscription;
if (!newSubscription && query.queryType === AzureQueryType.AzureMonitor) {
newSubscription = datasource.azureMonitorDatasource.subscriptionId;
} else if (!query.subscription && query.queryType === AzureQueryType.LogAnalytics) {
newSubscription =
datasource.azureLogAnalyticsDatasource.logAnalyticsSubscriptionId ||
datasource.azureLogAnalyticsDatasource.subscriptionId;
}
if (!newSubscription && newSubscriptions.length > 0) {
newSubscription = newSubscriptions[0].value;
}
newSubscription !== query.subscription &&
onQueryChange({
...query,
subscription: newSubscription,
});
});
}, []);
const handleChange = useCallback(
(change: SelectableValue<string>) => {
if (!change.value) {
return;
}
let newQuery: AzureMonitorQuery = {
...query,
subscription: change.value,
};
if (query.queryType === AzureQueryType.AzureMonitor) {
newQuery.azureMonitor = {
...newQuery.azureMonitor,
resourceGroup: undefined,
metricDefinition: undefined,
metricNamespace: undefined,
resourceName: undefined,
metricName: undefined,
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 11:37:39 +00:00
aggregation: '',
timeGrain: '',
dimensionFilters: [],
};
}
onQueryChange(newQuery);
},
[query, onQueryChange]
);
const options = useMemo(() => [...subscriptions, variableOptionGroup], [subscriptions, variableOptionGroup]);
return (
<Field label="Subscription">
<Select
value={findOption(subscriptions, query.subscription)}
inputId="azure-monitor-subscriptions-field"
onChange={handleChange}
options={options}
width={38}
/>
</Field>
);
};
export default SubscriptionField;