-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathreducer.ts
175 lines (150 loc) · 4.86 KB
/
reducer.ts
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
import { Action } from '@reduxjs/toolkit';
import { defaultMetricAgg } from '@/queryDef';
import { ElasticsearchQuery, MetricAggregation } from '@/types';
import { removeEmpty } from '@/utils';
import { initExploreQuery, initQuery } from '../../state';
import { isMetricAggregationWithMeta, isMetricAggregationWithSettings, isPipelineAggregation } from '../aggregations';
import { getChildren, metricAggregationConfig } from '../utils';
import {
addMetric,
changeMetricAttribute,
changeMetricField,
changeMetricMeta,
changeMetricSetting,
changeMetricType,
removeMetric,
toggleMetricVisibility,
} from './actions';
import { store } from "@/store"
export const reducer = (state: ElasticsearchQuery['metrics'], action: Action): ElasticsearchQuery['metrics'] => {
const defaultsMetricAggregation = store.getState().defaults.metricAggregation;
if (addMetric.match(action)) {
return [...state!, defaultMetricAgg(action.payload)];
}
if (removeMetric.match(action)) {
const metricToRemove = state!.find((m) => m.id === action.payload)!;
const metricsToRemove = [metricToRemove, ...getChildren(metricToRemove, state!)];
const resultingMetrics = state!.filter((metric) => !metricsToRemove.some((toRemove) => toRemove.id === metric.id));
if (resultingMetrics.length === 0) {
return [defaultMetricAgg('1')];
}
return resultingMetrics;
}
if (changeMetricType.match(action)) {
return state!
.filter((metric) =>
// When the new query type is not `metrics` we remove all other metrics from the query
// leaving only the current one.
metricAggregationConfig[action.payload.type].impliedQueryType === 'metrics'
? true
: metric.id === action.payload.id
)
.map((metric) => {
if (metric.id !== action.payload.id) {
return metric;
}
/*
TODO: The previous version of the query editor was keeping some of the old metric's configurations
in the new selected one (such as field or some settings).
It the future would be nice to have the same behavior but it's hard without a proper definition,
as Elasticsearch will error sometimes if some settings are not compatible.
*/
return {
id: metric.id,
type: action.payload.type,
...defaultsMetricAggregation[action.payload.type as keyof typeof defaultsMetricAggregation],
} as MetricAggregation;
});
}
if (changeMetricField.match(action)) {
return state!.map((metric) => {
if (metric.id !== action.payload.id) {
return metric;
}
const newMetric = {
...metric,
field: action.payload.field,
};
if (isPipelineAggregation(metric)) {
return { ...newMetric, pipelineAgg: action.payload.field };
}
return newMetric;
});
}
if (toggleMetricVisibility.match(action)) {
return state!.map((metric) => {
if (metric.id !== action.payload) {
return metric;
}
return {
...metric,
hide: !metric.hide,
};
});
}
if (changeMetricSetting.match(action)) {
return state!.map((metric) => {
if (metric.id !== action.payload.metric.id) {
return metric;
}
// TODO: Here, instead of this if statement, we should assert that metric is MetricAggregationWithSettings
if (isMetricAggregationWithSettings(metric)) {
const newSettings = removeEmpty({
...metric.settings,
[action.payload.settingName]: action.payload.newValue,
});
return {
...metric,
settings: {
...newSettings,
},
};
}
// This should never happen.
return metric;
});
}
if (changeMetricMeta.match(action)) {
return state!.map((metric) => {
if (metric.id !== action.payload.metric.id) {
return metric;
}
// TODO: Here, instead of this if statement, we should assert that metric is MetricAggregationWithMeta
if (isMetricAggregationWithMeta(metric)) {
return {
...metric,
meta: {
...metric.meta,
[action.payload.meta]: action.payload.newValue,
},
};
}
// This should never happen.
return metric;
});
}
if (changeMetricAttribute.match(action)) {
return state!.map((metric) => {
if (metric.id !== action.payload.metric.id) {
return metric;
}
return {
...metric,
[action.payload.attribute]: action.payload.newValue,
};
});
}
if (initQuery.match(action)) {
if (state && state.length > 0) {
return state;
}
return [defaultMetricAgg('1')];
}
if (initExploreQuery.match(action)) {
if (state && state.length > 0) {
return state;
}
return [{ type: 'logs', id: '3', ...defaultsMetricAggregation.logs }];
}
return state;
};