-
Notifications
You must be signed in to change notification settings - Fork 8.1k
/
time_series_query.ts
160 lines (137 loc) · 4.06 KB
/
time_series_query.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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { DEFAULT_GROUPS } from '../index';
import { getDateRangeInfo } from './date_range_info';
import { Logger, CallCluster } from '../../../types';
import { TimeSeriesQuery, TimeSeriesResult, TimeSeriesResultRow } from './time_series_types';
export { TimeSeriesQuery, TimeSeriesResult } from './time_series_types';
export interface TimeSeriesQueryParameters {
logger: Logger;
callCluster: CallCluster;
query: TimeSeriesQuery;
}
export async function timeSeriesQuery(
params: TimeSeriesQueryParameters
): Promise<TimeSeriesResult> {
const { logger, callCluster, query: queryParams } = params;
const {
index,
timeWindowSize,
timeWindowUnit,
interval,
timeField,
dateStart,
dateEnd,
} = queryParams;
const window = `${timeWindowSize}${timeWindowUnit}`;
const dateRangeInfo = getDateRangeInfo({ dateStart, dateEnd, window, interval });
// core query
const esQuery: any = {
index,
body: {
size: 0,
query: {
bool: {
filter: {
range: {
[timeField]: {
gte: dateRangeInfo.dateStart,
lt: dateRangeInfo.dateEnd,
format: 'strict_date_time',
},
},
},
},
},
// aggs: {...}, filled in below
},
ignoreUnavailable: true,
allowNoIndices: true,
ignore: [404],
};
// add the aggregations
const { aggType, aggField, termField, termSize } = queryParams;
const isCountAgg = aggType === 'count';
const isGroupAgg = !!termField;
let aggParent = esQuery.body;
// first, add a group aggregation, if requested
if (isGroupAgg) {
aggParent.aggs = {
groupAgg: {
terms: {
field: termField,
size: termSize || DEFAULT_GROUPS,
},
},
};
aggParent = aggParent.aggs.groupAgg;
}
// next, add the time window aggregation
aggParent.aggs = {
dateAgg: {
date_range: {
field: timeField,
ranges: dateRangeInfo.dateRanges,
},
},
};
aggParent = aggParent.aggs.dateAgg;
// finally, the metric aggregation, if requested
if (!isCountAgg) {
aggParent.aggs = {
metricAgg: {
[aggType]: {
field: aggField,
},
},
};
}
let esResult: any;
const logPrefix = 'indexThreshold timeSeriesQuery: callCluster';
logger.debug(`${logPrefix} call: ${JSON.stringify(esQuery)}`);
try {
esResult = await callCluster('search', esQuery);
} catch (err) {
logger.warn(`${logPrefix} error: ${JSON.stringify(err.message)}`);
throw new Error('error running search');
}
logger.debug(`${logPrefix} result: ${JSON.stringify(esResult)}`);
return getResultFromEs(isCountAgg, isGroupAgg, esResult);
}
function getResultFromEs(
isCountAgg: boolean,
isGroupAgg: boolean,
esResult: Record<string, any>
): TimeSeriesResult {
const aggregations = esResult?.aggregations || {};
// add a fake 'all documents' group aggregation, if a group aggregation wasn't used
if (!isGroupAgg) {
const dateAgg = aggregations.dateAgg || {};
aggregations.groupAgg = {
buckets: [{ key: 'all documents', dateAgg }],
};
delete aggregations.dateAgg;
}
const groupBuckets = aggregations.groupAgg?.buckets || [];
const result: TimeSeriesResult = {
results: [],
};
for (const groupBucket of groupBuckets) {
const groupName: string = `${groupBucket?.key}`;
const dateBuckets = groupBucket?.dateAgg?.buckets || [];
const groupResult: TimeSeriesResultRow = {
group: groupName,
metrics: [],
};
result.results.push(groupResult);
for (const dateBucket of dateBuckets) {
const date: string = dateBucket.to_as_string;
const value: number = isCountAgg ? dateBucket.doc_count : dateBucket.metricAgg.value;
groupResult.metrics.push([date, value]);
}
}
return result;
}