mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
* Remove unused mocks * Remove time grain converter anys * Improve mocks - Add context mock - Update datasource mock - Add util functions * Remove anys from log_analytics_test * Improve response typing * Remove redundant angular code * Remove more anys - Add Resource type * More type updates * Remove unused code and update arg ds test * Remove old annotations test * Remove unused code and update some more types * Fix lint * Fix lint
76 lines
2.3 KiB
TypeScript
76 lines
2.3 KiB
TypeScript
import { map } from 'lodash';
|
|
|
|
import { SelectableValue } from '@grafana/data';
|
|
import { VariableWithMultiSupport } from 'app/features/variables/types';
|
|
|
|
import { AzureMonitorOption, VariableOptionGroup } from '../types';
|
|
|
|
export const hasOption = (options: AzureMonitorOption[], value: string): boolean =>
|
|
options.some((v) => (v.options ? hasOption(v.options, value) : v.value === value));
|
|
|
|
export const findOptions = (options: AzureMonitorOption[], values: string[] = []) => {
|
|
if (values.length === 0) {
|
|
return [];
|
|
}
|
|
const set = values.reduce((accum, item) => {
|
|
accum.add(item);
|
|
return accum;
|
|
}, new Set());
|
|
return options.filter((option) => set.has(option.value));
|
|
};
|
|
|
|
export const toOption = (v: { text: string; value: string }) => ({ value: v.value, label: v.text });
|
|
|
|
export const addValueToOptions = (
|
|
values: Array<AzureMonitorOption | SelectableValue>,
|
|
variableOptionGroup: VariableOptionGroup,
|
|
value?: string
|
|
) => {
|
|
const options = [...values, variableOptionGroup];
|
|
|
|
const optionValues = values.map((m) => m.value.toLowerCase()).concat(variableOptionGroup.options.map((p) => p.value));
|
|
if (value && !optionValues.includes(value.toLowerCase())) {
|
|
options.push({ label: value, value });
|
|
}
|
|
|
|
return options;
|
|
};
|
|
|
|
// Route definitions shared with the backend.
|
|
// Check: /pkg/tsdb/azuremonitor/azuremonitor-resource-handler.go <registerRoutes>
|
|
export const routeNames = {
|
|
azureMonitor: 'azuremonitor',
|
|
logAnalytics: 'loganalytics',
|
|
appInsights: 'appinsights',
|
|
resourceGraph: 'resourcegraph',
|
|
};
|
|
|
|
export function interpolateVariable(
|
|
value: string | number | Array<string | number>,
|
|
variable: VariableWithMultiSupport
|
|
) {
|
|
if (typeof value === 'string') {
|
|
// When enabling multiple responses, quote the value to mimic the array result below
|
|
// even if only one response is selected. This does not apply if only the "include all"
|
|
// option is enabled but with a custom value.
|
|
if (variable.multi || (variable.includeAll && !variable.allValue)) {
|
|
return "'" + value + "'";
|
|
} else {
|
|
return value;
|
|
}
|
|
}
|
|
|
|
if (typeof value === 'number') {
|
|
return value;
|
|
}
|
|
|
|
const quotedValues = map(value, (val) => {
|
|
if (typeof value === 'number') {
|
|
return value;
|
|
}
|
|
|
|
return "'" + val + "'";
|
|
});
|
|
return quotedValues.join(',');
|
|
}
|