mirror of
https://github.com/grafana/grafana.git
synced 2025-09-22 20:19:05 +08:00
Elasticsearch: Migrate queryeditor to React (#28033)
Co-authored-by: Zoltán Bedi <zoltan.bedi@gmail.com> Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
This commit is contained in:
54
public/app/plugins/datasource/elasticsearch/utils.ts
Normal file
54
public/app/plugins/datasource/elasticsearch/utils.ts
Normal file
@ -0,0 +1,54 @@
|
||||
import {
|
||||
isMetricAggregationWithField,
|
||||
MetricAggregation,
|
||||
} from './components/QueryEditor/MetricAggregationsEditor/aggregations';
|
||||
import { metricAggregationConfig } from './components/QueryEditor/MetricAggregationsEditor/utils';
|
||||
|
||||
export const describeMetric = (metric: MetricAggregation) => {
|
||||
if (!isMetricAggregationWithField(metric)) {
|
||||
return metricAggregationConfig[metric.type].label;
|
||||
}
|
||||
|
||||
// TODO: field might be undefined
|
||||
return `${metricAggregationConfig[metric.type].label} ${metric.field}`;
|
||||
};
|
||||
|
||||
/**
|
||||
* Utility function to clean up aggregations settings objects.
|
||||
* It removes nullish values and empty strings, array and objects
|
||||
* recursing over nested objects (not arrays).
|
||||
* @param obj
|
||||
*/
|
||||
export const removeEmpty = <T>(obj: T): Partial<T> =>
|
||||
Object.entries(obj).reduce((acc, [key, value]) => {
|
||||
// Removing nullish values (null & undefined)
|
||||
if (value == null) {
|
||||
return { ...acc };
|
||||
}
|
||||
|
||||
// Removing empty arrays (This won't recurse the array)
|
||||
if (Array.isArray(value) && value.length === 0) {
|
||||
return { ...acc };
|
||||
}
|
||||
|
||||
// Removing empty strings
|
||||
if (value?.length === 0) {
|
||||
return { ...acc };
|
||||
}
|
||||
|
||||
// Recursing over nested objects
|
||||
if (!Array.isArray(value) && typeof value === 'object') {
|
||||
const cleanObj = removeEmpty(value);
|
||||
|
||||
if (Object.keys(cleanObj).length === 0) {
|
||||
return { ...acc };
|
||||
}
|
||||
|
||||
return { ...acc, [key]: cleanObj };
|
||||
}
|
||||
|
||||
return {
|
||||
...acc,
|
||||
[key]: value,
|
||||
};
|
||||
}, {});
|
Reference in New Issue
Block a user