mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
refactoring dimension values frontend req (#57475)
This commit is contained in:
parent
5d18b41857
commit
c23a7bdcf8
@ -5,7 +5,13 @@ import { getBackendSrv } from '@grafana/runtime';
|
|||||||
import { TemplateSrv } from 'app/features/templating/template_srv';
|
import { TemplateSrv } from 'app/features/templating/template_srv';
|
||||||
|
|
||||||
import { CloudWatchRequest } from './query-runner/CloudWatchRequest';
|
import { CloudWatchRequest } from './query-runner/CloudWatchRequest';
|
||||||
import { CloudWatchJsonData, DescribeLogGroupsRequest, GetDimensionKeysRequest, MultiFilters } from './types';
|
import {
|
||||||
|
CloudWatchJsonData,
|
||||||
|
DescribeLogGroupsRequest,
|
||||||
|
GetDimensionKeysRequest,
|
||||||
|
GetDimensionValuesRequest,
|
||||||
|
MultiFilters,
|
||||||
|
} from './types';
|
||||||
|
|
||||||
export interface SelectableResourceValue extends SelectableValue<string> {
|
export interface SelectableResourceValue extends SelectableValue<string> {
|
||||||
text: string;
|
text: string;
|
||||||
@ -83,13 +89,13 @@ export class CloudWatchAPI extends CloudWatchRequest {
|
|||||||
}).then((dimensionKeys) => dimensionKeys.map(toOption));
|
}).then((dimensionKeys) => dimensionKeys.map(toOption));
|
||||||
}
|
}
|
||||||
|
|
||||||
async getDimensionValues(
|
async getDimensionValues({
|
||||||
region: string,
|
dimensionKey,
|
||||||
namespace: string | undefined,
|
region,
|
||||||
metricName: string | undefined,
|
namespace,
|
||||||
dimensionKey: string,
|
dimensionFilters = {},
|
||||||
dimensionFilters: {}
|
metricName = '',
|
||||||
) {
|
}: GetDimensionValuesRequest) {
|
||||||
if (!namespace || !metricName) {
|
if (!namespace || !metricName) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
@ -199,13 +199,12 @@ export class SQLCompletionItemProvider extends CompletionItemProvider {
|
|||||||
const metricNameToken = getMetricNameToken(currentToken);
|
const metricNameToken = getMetricNameToken(currentToken);
|
||||||
const labelKey = currentToken?.getPreviousNonWhiteSpaceToken()?.getPreviousNonWhiteSpaceToken();
|
const labelKey = currentToken?.getPreviousNonWhiteSpaceToken()?.getPreviousNonWhiteSpaceToken();
|
||||||
if (namespaceToken?.value && labelKey?.value && metricNameToken?.value) {
|
if (namespaceToken?.value && labelKey?.value && metricNameToken?.value) {
|
||||||
const values = await this.api.getDimensionValues(
|
const values = await this.api.getDimensionValues({
|
||||||
this.templateSrv.replace(this.region),
|
region: this.region,
|
||||||
this.templateSrv.replace(namespaceToken.value.replace(/\"/g, '')),
|
namespace: namespaceToken.value.replace(/\"/g, ''),
|
||||||
this.templateSrv.replace(metricNameToken.value),
|
metricName: metricNameToken.value,
|
||||||
this.templateSrv.replace(labelKey.value),
|
dimensionKey: labelKey.value,
|
||||||
{}
|
});
|
||||||
);
|
|
||||||
values.map((o) =>
|
values.map((o) =>
|
||||||
addSuggestion(`'${o.value}'`, { insertText: `'${o.value}' `, command: TRIGGER_SUGGEST })
|
addSuggestion(`'${o.value}'`, { insertText: `'${o.value}' `, command: TRIGGER_SUGGEST })
|
||||||
);
|
);
|
||||||
|
@ -52,7 +52,13 @@ export const FilterItem: FunctionComponent<Props> = ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
return datasource.api
|
return datasource.api
|
||||||
.getDimensionValues(region, namespace, metricName, filter.key, dimensionsExcludingCurrentKey)
|
.getDimensionValues({
|
||||||
|
dimensionKey: filter.key,
|
||||||
|
dimensionFilters: dimensionsExcludingCurrentKey,
|
||||||
|
region,
|
||||||
|
namespace,
|
||||||
|
metricName,
|
||||||
|
})
|
||||||
.then((result: Array<SelectableValue<string>>) => {
|
.then((result: Array<SelectableValue<string>>) => {
|
||||||
if (result.length && !disableExpressions && !result.some((o) => o.value === wildcardOption.value)) {
|
if (result.length && !disableExpressions && !result.some((o) => o.value === wildcardOption.value)) {
|
||||||
result.unshift(wildcardOption);
|
result.unshift(wildcardOption);
|
||||||
|
@ -28,10 +28,10 @@ export function MetricStatEditor({
|
|||||||
onChange,
|
onChange,
|
||||||
onRunQuery,
|
onRunQuery,
|
||||||
}: React.PropsWithChildren<Props>) {
|
}: React.PropsWithChildren<Props>) {
|
||||||
const { region, namespace, metricName, dimensions } = metricStat;
|
const { region, namespace } = metricStat;
|
||||||
const namespaces = useNamespaces(datasource);
|
const namespaces = useNamespaces(datasource);
|
||||||
const metrics = useMetrics(datasource, region, namespace);
|
const metrics = useMetrics(datasource, region, namespace);
|
||||||
const dimensionKeys = useDimensionKeys(datasource, { region, namespace, metricName, dimensionFilters: dimensions });
|
const dimensionKeys = useDimensionKeys(datasource, { ...metricStat, dimensionFilters: metricStat.dimensions });
|
||||||
|
|
||||||
const onMetricStatChange = (metricStat: MetricStat) => {
|
const onMetricStatChange = (metricStat: MetricStat) => {
|
||||||
onChange(metricStat);
|
onChange(metricStat);
|
||||||
|
@ -105,12 +105,12 @@ const FilterItem: React.FC<FilterItemProps> = (props) => {
|
|||||||
const dimensionKeys = useDimensionKeys(datasource, { region: query.region, namespace, metricName });
|
const dimensionKeys = useDimensionKeys(datasource, { region: query.region, namespace, metricName });
|
||||||
|
|
||||||
const loadDimensionValues = async () => {
|
const loadDimensionValues = async () => {
|
||||||
if (!filter.property?.name) {
|
if (!filter.property?.name || !namespace) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
return datasource.api
|
return datasource.api
|
||||||
.getDimensionValues(query.region, namespace, metricName, filter.property.name, {})
|
.getDimensionValues({ region: query.region, namespace, metricName, dimensionKey: filter.property.name })
|
||||||
.then((result: Array<SelectableValue<string>>) => {
|
.then((result: Array<SelectableValue<string>>) => {
|
||||||
return appendTemplateVariables(datasource, result);
|
return appendTemplateVariables(datasource, result);
|
||||||
});
|
});
|
||||||
|
@ -460,3 +460,10 @@ export interface GetDimensionKeysRequest extends ResourceRequest {
|
|||||||
namespace?: string;
|
namespace?: string;
|
||||||
dimensionFilters?: Dimensions;
|
dimensionFilters?: Dimensions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface GetDimensionValuesRequest extends ResourceRequest {
|
||||||
|
dimensionKey: string;
|
||||||
|
namespace: string;
|
||||||
|
metricName?: string;
|
||||||
|
dimensionFilters?: Dimensions;
|
||||||
|
}
|
||||||
|
@ -76,13 +76,13 @@ describe('variables', () => {
|
|||||||
});
|
});
|
||||||
it('should run if values are set', async () => {
|
it('should run if values are set', async () => {
|
||||||
const result = await variables.execute(query);
|
const result = await variables.execute(query);
|
||||||
expect(getDimensionValues).toBeCalledWith(
|
expect(getDimensionValues).toBeCalledWith({
|
||||||
query.region,
|
region: query.region,
|
||||||
query.namespace,
|
namespace: query.namespace,
|
||||||
query.metricName,
|
metricName: query.metricName,
|
||||||
query.dimensionKey,
|
dimensionKey: query.dimensionKey,
|
||||||
query.dimensionFilters
|
dimensionFilters: query.dimensionFilters,
|
||||||
);
|
});
|
||||||
expect(result).toEqual([{ text: 'e', value: 'e', expandable: true }]);
|
expect(result).toEqual([{ text: 'e', value: 'e', expandable: true }]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -105,7 +105,13 @@ export class CloudWatchVariableSupport extends CustomVariableSupport<CloudWatchD
|
|||||||
if (!dimensionKey || !metricName) {
|
if (!dimensionKey || !metricName) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
const keys = await this.api.getDimensionValues(region, namespace, metricName, dimensionKey, dimensionFilters ?? {});
|
const keys = await this.api.getDimensionValues({
|
||||||
|
region,
|
||||||
|
namespace,
|
||||||
|
metricName,
|
||||||
|
dimensionKey,
|
||||||
|
dimensionFilters,
|
||||||
|
});
|
||||||
return keys.map((s) => ({
|
return keys.map((s) => ({
|
||||||
text: s.label,
|
text: s.label,
|
||||||
value: s.value,
|
value: s.value,
|
||||||
|
Loading…
Reference in New Issue
Block a user