Debian Codegraph @

packages/time-series/lib/commands/index.ts

raw
1 import * as ADD from './ADD';
2 import * as ALTER from './ALTER';
3 import * as CREATE from './CREATE';
4 import * as CREATERULE from './CREATERULE';
5 import * as DECRBY from './DECRBY';
6 import * as DEL from './DEL';
7 import * as DELETERULE from './DELETERULE';
8 import * as GET from './GET';
9 import * as INCRBY from './INCRBY';
10 import * as INFO_DEBUG from './INFO_DEBUG';
11 import * as INFO from './INFO';
12 import * as MADD from './MADD';
13 import * as MGET from './MGET';
14 import * as MGET_WITHLABELS from './MGET_WITHLABELS';
15 import * as QUERYINDEX from './QUERYINDEX';
16 import * as RANGE from './RANGE';
17 import * as REVRANGE from './REVRANGE';
18 import * as MRANGE from './MRANGE';
19 import * as MRANGE_WITHLABELS from './MRANGE_WITHLABELS';
20 import * as MREVRANGE from './MREVRANGE';
21 import * as MREVRANGE_WITHLABELS from './MREVRANGE_WITHLABELS';
22 import { RedisCommandArguments } from '@redis/client/dist/lib/commands';
23 import { pushVerdictArguments } from '@redis/client/dist/lib/commands/generic-transformers';
24
25 export default {
26 ADD,
27 add: ADD,
28 ALTER,
29 alter: ALTER,
30 CREATE,
31 create: CREATE,
32 CREATERULE,
33 createRule: CREATERULE,
34 DECRBY,
35 decrBy: DECRBY,
36 DEL,
37 del: DEL,
38 DELETERULE,
39 deleteRule: DELETERULE,
40 GET,
41 get: GET,
42 INCRBY,
43 incrBy: INCRBY,
44 INFO_DEBUG,
45 infoDebug: INFO_DEBUG,
46 INFO,
47 info: INFO,
48 MADD,
49 mAdd: MADD,
50 MGET,
51 mGet: MGET,
52 MGET_WITHLABELS,
53 mGetWithLabels: MGET_WITHLABELS,
54 QUERYINDEX,
55 queryIndex: QUERYINDEX,
56 RANGE,
57 range: RANGE,
58 REVRANGE,
59 revRange: REVRANGE,
60 MRANGE,
61 mRange: MRANGE,
62 MRANGE_WITHLABELS,
63 mRangeWithLabels: MRANGE_WITHLABELS,
64 MREVRANGE,
65 mRevRange: MREVRANGE,
66 MREVRANGE_WITHLABELS,
67 mRevRangeWithLabels: MREVRANGE_WITHLABELS
68 };
69
70 export enum TimeSeriesAggregationType {
71 AVG = 'AVG',
72 // @deprecated
73 AVERAGE = 'AVG',
74 FIRST = 'FIRST',
75 LAST = 'LAST',
76 MIN = 'MIN',
77 // @deprecated
78 MINIMUM = 'MIN',
79 MAX = 'MAX',
80 // @deprecated
81 MAXIMUM = 'MAX',
82 SUM = 'SUM',
83 RANGE = 'RANGE',
84 COUNT = 'COUNT',
85 STD_P = 'STD.P',
86 STD_S = 'STD.S',
87 VAR_P = 'VAR.P',
88 VAR_S = 'VAR.S',
89 TWA = 'TWA'
90 }
91
92 export enum TimeSeriesDuplicatePolicies {
93 BLOCK = 'BLOCK',
94 FIRST = 'FIRST',
95 LAST = 'LAST',
96 MIN = 'MIN',
97 MAX = 'MAX',
98 SUM = 'SUM'
99 }
100
101 export enum TimeSeriesReducers {
102 AVG = 'AVG',
103 SUM = 'SUM',
104 MIN = 'MIN',
105 // @deprecated
106 MINIMUM = 'MIN',
107 MAX = 'MAX',
108 // @deprecated
109 MAXIMUM = 'MAX',
110 RANGE = 'range',
111 COUNT = 'COUNT',
112 STD_P = 'STD.P',
113 STD_S = 'STD.S',
114 VAR_P = 'VAR.P',
115 VAR_S = 'VAR.S',
116 }
117
118 export type Timestamp = number | Date | string;
119
120 export function transformTimestampArgument(timestamp: Timestamp): string {
121 if (typeof timestamp === 'string') return timestamp;
122
123 return (
124 typeof timestamp === 'number' ?
125 timestamp :
126 timestamp.getTime()
127 ).toString();
128 }
129
130 export function pushRetentionArgument(args: RedisCommandArguments, retention?: number): RedisCommandArguments {
131 if (retention !== undefined) {
132 args.push(
133 'RETENTION',
134 retention.toString()
135 );
136 }
137
138 return args;
139 }
140
141 export enum TimeSeriesEncoding {
142 COMPRESSED = 'COMPRESSED',
143 UNCOMPRESSED = 'UNCOMPRESSED'
144 }
145
146 export function pushEncodingArgument(args: RedisCommandArguments, encoding?: TimeSeriesEncoding): RedisCommandArguments {
147 if (encoding !== undefined) {
148 args.push(
149 'ENCODING',
150 encoding
151 );
152 }
153
154 return args;
155 }
156
157 export function pushChunkSizeArgument(args: RedisCommandArguments, chunkSize?: number): RedisCommandArguments {
158 if (chunkSize !== undefined) {
159 args.push(
160 'CHUNK_SIZE',
161 chunkSize.toString()
162 );
163 }
164
165 return args;
166 }
167
168 export function pushDuplicatePolicy(args: RedisCommandArguments, duplicatePolicy?: TimeSeriesDuplicatePolicies): RedisCommandArguments {
169 if (duplicatePolicy !== undefined) {
170 args.push(
171 'DUPLICATE_POLICY',
172 duplicatePolicy
173 );
174 }
175
176 return args;
177 }
178
179 export type RawLabels = Array<[label: string, value: string]>;
180
181 export type Labels = {
182 [label: string]: string;
183 };
184
185 export function transformLablesReply(reply: RawLabels): Labels {
186 const labels: Labels = {};
187
188 for (const [key, value] of reply) {
189 labels[key] = value;
190 }
191
192 return labels
193 }
194
195 export function pushLabelsArgument(args: RedisCommandArguments, labels?: Labels): RedisCommandArguments {
196 if (labels) {
197 args.push('LABELS');
198
199 for (const [label, value] of Object.entries(labels)) {
200 args.push(label, value);
201 }
202 }
203
204 return args;
205 }
206
207 export interface IncrDecrOptions {
208 TIMESTAMP?: Timestamp;
209 RETENTION?: number;
210 UNCOMPRESSED?: boolean;
211 CHUNK_SIZE?: number;
212 LABELS?: Labels;
213 }
214
215 export function transformIncrDecrArguments(
216 command: 'TS.INCRBY' | 'TS.DECRBY',
217 key: string,
218 value: number,
219 options?: IncrDecrOptions
220 ): RedisCommandArguments {
221 const args = [
222 command,
223 key,
224 value.toString()
225 ];
226
227 if (options?.TIMESTAMP !== undefined && options?.TIMESTAMP !== null) {
228 args.push('TIMESTAMP', transformTimestampArgument(options.TIMESTAMP));
229 }
230
231 pushRetentionArgument(args, options?.RETENTION);
232
233 if (options?.UNCOMPRESSED) {
234 args.push('UNCOMPRESSED');
235 }
236
237 pushChunkSizeArgument(args, options?.CHUNK_SIZE);
238
239 pushLabelsArgument(args, options?.LABELS);
240
241 return args;
242 }
243
244 export type SampleRawReply = [timestamp: number, value: string];
245
246 export interface SampleReply {
247 timestamp: number;
248 value: number;
249 }
250
251 export function transformSampleReply(reply: SampleRawReply): SampleReply {
252 return {
253 timestamp: reply[0],
254 value: Number(reply[1])
255 };
256 }
257
258 export enum TimeSeriesBucketTimestamp {
259 LOW = '-',
260 HIGH = '+',
261 MID = '~'
262 }
263
264 export interface RangeOptions {
265 LATEST?: boolean;
266 FILTER_BY_TS?: Array<Timestamp>;
267 FILTER_BY_VALUE?: {
268 min: number;
269 max: number;
270 };
271 COUNT?: number;
272 ALIGN?: Timestamp;
273 AGGREGATION?: {
274 type: TimeSeriesAggregationType;
275 timeBucket: Timestamp;
276 BUCKETTIMESTAMP?: TimeSeriesBucketTimestamp;
277 EMPTY?: boolean;
278 };
279 }
280
281 export function pushRangeArguments(
282 args: RedisCommandArguments,
283 fromTimestamp: Timestamp,
284 toTimestamp: Timestamp,
285 options?: RangeOptions
286 ): RedisCommandArguments {
287 args.push(
288 transformTimestampArgument(fromTimestamp),
289 transformTimestampArgument(toTimestamp)
290 );
291
292 pushLatestArgument(args, options?.LATEST);
293
294 if (options?.FILTER_BY_TS) {
295 args.push('FILTER_BY_TS');
296 for (const ts of options.FILTER_BY_TS) {
297 args.push(transformTimestampArgument(ts));
298 }
299 }
300
301 if (options?.FILTER_BY_VALUE) {
302 args.push(
303 'FILTER_BY_VALUE',
304 options.FILTER_BY_VALUE.min.toString(),
305 options.FILTER_BY_VALUE.max.toString()
306 );
307 }
308
309 if (options?.COUNT) {
310 args.push(
311 'COUNT',
312 options.COUNT.toString()
313 );
314 }
315
316 if (options?.ALIGN) {
317 args.push(
318 'ALIGN',
319 transformTimestampArgument(options.ALIGN)
320 );
321 }
322
323 if (options?.AGGREGATION) {
324 args.push(
325 'AGGREGATION',
326 options.AGGREGATION.type,
327 transformTimestampArgument(options.AGGREGATION.timeBucket)
328 );
329
330 if (options.AGGREGATION.BUCKETTIMESTAMP) {
331 args.push(
332 'BUCKETTIMESTAMP',
333 options.AGGREGATION.BUCKETTIMESTAMP
334 );
335 }
336
337 if (options.AGGREGATION.EMPTY) {
338 args.push('EMPTY');
339 }
340 }
341
342 return args;
343 }
344
345 interface MRangeGroupBy {
346 label: string;
347 reducer: TimeSeriesReducers;
348 }
349
350 export function pushMRangeGroupByArguments(args: RedisCommandArguments, groupBy?: MRangeGroupBy): RedisCommandArguments {
351 if (groupBy) {
352 args.push(
353 'GROUPBY',
354 groupBy.label,
355 'REDUCE',
356 groupBy.reducer
357 );
358 }
359
360 return args;
361 }
362
363 export type Filter = string | Array<string>;
364
365 export function pushFilterArgument(args: RedisCommandArguments, filter: string | Array<string>): RedisCommandArguments {
366 args.push('FILTER');
367 return pushVerdictArguments(args, filter);
368 }
369
370 export interface MRangeOptions extends RangeOptions {
371 GROUPBY?: MRangeGroupBy;
372 }
373
374 export function pushMRangeArguments(
375 args: RedisCommandArguments,
376 fromTimestamp: Timestamp,
377 toTimestamp: Timestamp,
378 filter: Filter,
379 options?: MRangeOptions
380 ): RedisCommandArguments {
381 args = pushRangeArguments(args, fromTimestamp, toTimestamp, options);
382 args = pushFilterArgument(args, filter);
383 return pushMRangeGroupByArguments(args, options?.GROUPBY);
384 }
385
386 export type SelectedLabels = string | Array<string>;
387
388 export function pushWithLabelsArgument(args: RedisCommandArguments, selectedLabels?: SelectedLabels): RedisCommandArguments {
389 if (!selectedLabels) {
390 args.push('WITHLABELS');
391 } else {
392 args.push('SELECTED_LABELS');
393 args = pushVerdictArguments(args, selectedLabels);
394 }
395
396 return args;
397 }
398
399 export interface MRangeWithLabelsOptions extends MRangeOptions {
400 SELECTED_LABELS?: SelectedLabels;
401 }
402
403 export function pushMRangeWithLabelsArguments(
404 args: RedisCommandArguments,
405 fromTimestamp: Timestamp,
406 toTimestamp: Timestamp,
407 filter: Filter,
408 options?: MRangeWithLabelsOptions
409 ): RedisCommandArguments {
410 args = pushRangeArguments(args, fromTimestamp, toTimestamp, options);
411 args = pushWithLabelsArgument(args, options?.SELECTED_LABELS);
412 args = pushFilterArgument(args, filter);
413 return pushMRangeGroupByArguments(args, options?.GROUPBY);
414 }
415
416 export function transformRangeReply(reply: Array<SampleRawReply>): Array<SampleReply> {
417 return reply.map(transformSampleReply);
418 }
419
420 type MRangeRawReply = Array<[
421 key: string,
422 labels: RawLabels,
423 samples: Array<SampleRawReply>
424 ]>;
425
426 interface MRangeReplyItem {
427 key: string;
428 samples: Array<SampleReply>;
429 }
430
431 export function transformMRangeReply(reply: MRangeRawReply): Array<MRangeReplyItem> {
432 const args = [];
433
434 for (const [key, _, sample] of reply) {
435 args.push({
436 key,
437 samples: sample.map(transformSampleReply)
438 });
439 }
440
441 return args;
442 }
443 export interface MRangeWithLabelsReplyItem extends MRangeReplyItem {
444 labels: Labels;
445 }
446
447 export function transformMRangeWithLabelsReply(reply: MRangeRawReply): Array<MRangeWithLabelsReplyItem> {
448 const args = [];
449
450 for (const [key, labels, samples] of reply) {
451 args.push({
452 key,
453 labels: transformLablesReply(labels),
454 samples: samples.map(transformSampleReply)
455 });
456 }
457
458 return args;
459 }
460
461 export function pushLatestArgument(args: RedisCommandArguments, latest?: boolean): RedisCommandArguments {
462 if (latest) {
463 args.push('LATEST');
464 }
465
466 return args;
467 }