-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.tsx
186 lines (160 loc) · 6.96 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { uniqueId } from 'lodash';
import React, { ComponentProps, useRef, useState } from 'react';
import { InlineField, Input, InlineSwitch, Select } from '@grafana/ui';
import { useDispatch } from '@/hooks/useStatelessReducer';
import { extendedStats } from '@/queryDef';
import { MetricAggregation, ExtendedStat } from '@/types';
import { useQuery } from '../../ElasticsearchQueryContext';
import { SettingsEditorContainer } from '../../SettingsEditorContainer';
import { isMetricAggregationWithMissingSupport } from '../aggregations';
import { changeMetricMeta, changeMetricSetting } from '../state/actions';
import { metricAggregationConfig } from '../utils';
import { BucketScriptSettingsEditor } from './BucketScriptSettingsEditor';
import { MovingAverageSettingsEditor } from './MovingAverageSettingsEditor';
import { SettingField } from './SettingField';
import { TopMetricsSettingsEditor } from './TopMetricsSettingsEditor';
import { useDescription } from './useDescription';
// TODO: Move this somewhere and share it with BucketsAggregation Editor
const inlineFieldProps: Partial<ComponentProps<typeof InlineField>> = {
labelWidth: 16,
};
interface Props {
metric: MetricAggregation;
previousMetrics: MetricAggregation[];
}
export const SettingsEditor = ({ metric, previousMetrics }: Props) => {
const { current: baseId } = useRef(uniqueId('es-setting-'));
const dispatch = useDispatch();
const description = useDescription(metric);
const query = useQuery();
const rateAggUnitOptions = [
{ value: 'second', label: 'Second' },
{ value: 'minute', label: 'Minute' },
{ value: 'hour', label: 'Hour' },
{ value: 'day', label: 'Day' },
{ value: 'week', label: 'Week' },
{ value: 'month', label: 'Month' },
{ value: 'quarter', label: 'Quarter' },
{ value: 'Year', label: 'Year' },
];
const rateAggModeOptions = [
{ value: 'sum', label: 'Sum' },
{ value: 'value_count', label: 'Value count' },
];
return (
<SettingsEditorContainer label={description} hidden={metric.hide}>
{metric.type === 'derivative' && <SettingField label="Unit" metric={metric} settingName="unit" />}
{metric.type === 'serial_diff' && <SettingField label="Lag" metric={metric} settingName="lag" placeholder="1" />}
{metric.type === 'cumulative_sum' && <SettingField label="Format" metric={metric} settingName="format" />}
{metric.type === 'moving_avg' && <MovingAverageSettingsEditor metric={metric} />}
{metric.type === 'moving_fn' && (
<>
<SettingField label="Window" metric={metric} settingName="window" />
<SettingField label="Script" metric={metric} settingName="script" />
<SettingField label="Shift" metric={metric} settingName="shift" />
</>
)}
{metric.type === 'top_metrics' && <TopMetricsSettingsEditor metric={metric} />}
{metric.type === 'bucket_script' && (
<BucketScriptSettingsEditor value={metric} previousMetrics={previousMetrics} />
)}
{(metric.type === 'raw_data' || metric.type === 'raw_document') && (
<InlineField label="Size" {...inlineFieldProps}>
<Input
id={`ES-query-${query.refId}_metric-${metric.id}-size`}
onBlur={(e) => dispatch(changeMetricSetting({ metric, settingName: 'size', newValue: e.target.value }))}
defaultValue={metric.settings?.size ?? metricAggregationConfig['raw_data'].defaults.settings?.size}
/>
</InlineField>
)}
{metric.type === 'logs' && <SettingField label="Limit" metric={metric} settingName="limit" placeholder="100" />}
{metric.type === 'cardinality' && (
<SettingField label="Precision Threshold" metric={metric} settingName="precision_threshold" />
)}
{metric.type === 'extended_stats' && (
<>
{extendedStats.map((stat) => (
<ExtendedStatSetting
key={stat.value}
stat={stat}
onChange={(newValue) => dispatch(changeMetricMeta({ metric, meta: stat.value, newValue }))}
value={
metric.meta?.[stat.value] !== undefined
? !!metric.meta?.[stat.value]
: !!metricAggregationConfig['extended_stats'].defaults.meta?.[stat.value]
}
/>
))}
<SettingField label="Sigma" metric={metric} settingName="sigma" placeholder="3" />
</>
)}
{metric.type === 'percentiles' && (
<InlineField label="Percentiles" {...inlineFieldProps}>
<Input
id={`${baseId}-percentiles-percents`}
onBlur={(e) =>
dispatch(
changeMetricSetting({
metric,
settingName: 'percents',
newValue: e.target.value.split(',').filter(Boolean),
})
)
}
defaultValue={
metric.settings?.percents || metricAggregationConfig['percentiles'].defaults.settings?.percents
}
placeholder="1,5,25,50,75,95,99"
/>
</InlineField>
)}
{metric.type === 'rate' && (
<>
<InlineField label="Unit" {...inlineFieldProps} data-testid="unit-select">
<Select
id={`ES-query-${query.refId}_metric-${metric.id}-unit`}
onChange={(e) => dispatch(changeMetricSetting({ metric, settingName: 'unit', newValue: e.value }))}
options={rateAggUnitOptions}
value={metric.settings?.unit}
/>
</InlineField>
<InlineField label="Mode" {...inlineFieldProps} data-testid="mode-select">
<Select
id={`ES-query-${query.refId}_metric-${metric.id}-mode`}
onChange={(e) => dispatch(changeMetricSetting({ metric, settingName: 'mode', newValue: e.value }))}
options={rateAggModeOptions}
value={metric.settings?.unit}
/>
</InlineField>
</>
)}
{isMetricAggregationWithMissingSupport(metric) && (
<SettingField
label="Missing"
metric={metric}
settingName="missing"
tooltip="The missing parameter defines how documents that are missing a value should be treated. By default
they will be ignored but it is also possible to treat them as if they had a value"
/>
)}
</SettingsEditorContainer>
);
};
interface ExtendedStatSettingProps {
stat: ExtendedStat;
onChange: (checked: boolean) => void;
value: boolean;
}
const ExtendedStatSetting = ({ stat, onChange, value }: ExtendedStatSettingProps) => {
// this is needed for the htmlFor prop in the label so that clicking the label will toggle the switch state.
const [id] = useState(uniqueId(`es-field-id-`));
return (
<InlineField label={stat.label} {...inlineFieldProps} key={stat.value}>
<InlineSwitch
id={id}
onChange={(e: React.ChangeEvent<HTMLInputElement>) => onChange(e.target.checked)}
value={value}
/>
</InlineField>
);
};