-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy patht_disgest_commands.rs
469 lines (450 loc) · 18.2 KB
/
t_disgest_commands.rs
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
use crate::{
client::{prepare_command, PreparedCommand},
resp::{cmd, CollectionResponse, CommandArgs, SingleArg, SingleArgCollection, ToArgs, Value},
};
use serde::Deserialize;
use std::collections::HashMap;
/// A group of Redis commands related to [`T-Digest`](https://redis.io/docs/stack/bloom/)
///
/// # See Also
/// [T-Digest Commands](https://redis.io/commands/?group=tdigest)
pub trait TDigestCommands<'a> {
/// Adds one or more observations to a t-digest sketch.
///
/// # Arguments
/// * `key` - key name for an existing t-digest sketch.
/// * `values` - collection values of an observation (floating-point).
///
/// # See Also
/// * [<https://redis.io/commands/tdigest.add/>](https://redis.io/commands/tdigest.add/)
#[must_use]
fn tdigest_add(
self,
key: impl SingleArg,
values: impl SingleArgCollection<f64>,
) -> PreparedCommand<'a, Self, ()>
where
Self: Sized,
{
prepare_command(self, cmd("TDIGEST.ADD").arg(key).arg(values))
}
/// Returns, for each input rank, an estimation of the value (floating-point) with that rank.
///
/// Multiple estimations can be retrieved in a single call.
///
/// # Arguments
/// * `key` - key name for an existing t-digest sketch.
/// * `ranks` - collection of ranks, for which the value should be retrieved.
/// * `0` - is the rank of the value of the smallest observation.
/// * `n-1` - is the rank of the value of the largest observation; `n` denotes the number of observations added to the sketch.
///
/// # Return
/// a collection of floating-points populated with value_1, value_2, ..., value_R:
/// * Return an accurate result when rank is `0` (the value of the smallest observation)
/// * Return an accurate result when rank is `n-1` (the value of the largest observation), \
/// where n denotes the number of observations added to the sketch.
/// * Return `inf` when rank is equal to n or larger than `n`
///
/// # See Also
/// * [<https://redis.io/commands/tdigest.byrank/>](https://redis.io/commands/tdigest.byrank/)
#[must_use]
fn tdigest_byrank<R: CollectionResponse<f64>>(
self,
key: impl SingleArg,
ranks: impl SingleArgCollection<usize>,
) -> PreparedCommand<'a, Self, R>
where
Self: Sized,
{
prepare_command(self, cmd("TDIGEST.BYRANK").arg(key).arg(ranks))
}
/// Returns, for each input reverse rank, an estimation of the value (floating-point) with that reverse rank.
///
/// Multiple estimations can be retrieved in a single call.
///
/// # Arguments
/// * `key` - key name for an existing t-digest sketch.
/// * `ranks` - collection of reverse ranks, for which the value should be retrieved.
/// * `0` - is the reverse rank of the value of the largest observation.
/// * `n-1` - s the reverse rank of the value of the smallest observation; n denotes the number of observations added to the sketch.
///
/// # Return
/// a collection of floating-points populated with value_1, value_2, ..., value_R:
/// * Return an accurate result when `revrank` is `0` (the value of the largest observation)
/// * Return an accurate result when `revrank` is `n-1` (the value of the smallest observation), \
/// where `n` denotes the number of observations added to the sketch.
/// * Return 'inf' when `revrank` is equal to `n` or larger than `n`
///
/// # See Also
/// * [<https://redis.io/commands/tdigest.byrevrank/>](https://redis.io/commands/tdigest.byrevrank/)
#[must_use]
fn tdigest_byrevrank<R: CollectionResponse<f64>>(
self,
key: impl SingleArg,
ranks: impl SingleArgCollection<usize>,
) -> PreparedCommand<'a, Self, R>
where
Self: Sized,
{
prepare_command(self, cmd("TDIGEST.BYREVRANK").arg(key).arg(ranks))
}
/// Returns, for each input reverse rank, an estimation of the value (floating-point) with that reverse rank.
///
/// Multiple estimations can be retrieved in a single call.
///
/// # Arguments
/// * `key` - key name for an existing t-digest sketch.
/// * `values` - collection values for which the CDF \
/// ([`Cumulative Distribution Function`](https://en.wikipedia.org/wiki/Cumulative_distribution_function)) should be retrieved.
///
/// # Return
/// a collection of floating-points populated with fraction_1, fraction_2, ..., fraction_N.
///
/// All values are `nan` if the sketch is empty.
///
/// # See Also
/// * [<https://redis.io/commands/tdigest.cdf/>](https://redis.io/commands/tdigest.cdf/)
#[must_use]
fn tdigest_cdf<V: SingleArg, R: CollectionResponse<f64>>(
self,
key: impl SingleArg,
values: impl SingleArgCollection<V>,
) -> PreparedCommand<'a, Self, R>
where
Self: Sized,
{
prepare_command(self, cmd("TDIGEST.CDF").arg(key).arg(values))
}
/// Allocates memory and initializes a new t-digest sketch.
///
/// # Arguments
/// * `key` - key name for this new t-digest sketch.
/// * `compression` - controllable tradeoff between accuracy and memory consumption. \
/// 100 is a common value for normal uses. 1000 is more accurate. \
/// If no value is passed by default the compression will be 100. \
/// For more information on scaling of accuracy versus the compression parameter,\
/// see [`The t-digest: Efficient estimates of distributions`](https://www.sciencedirect.com/science/article/pii/S2665963820300403).
///
/// # See Also
/// * [<https://redis.io/commands/tdigest.create/>](https://redis.io/commands/tdigest.create/)
#[must_use]
fn tdigest_create(
self,
key: impl SingleArg,
compression: Option<i64>,
) -> PreparedCommand<'a, Self, ()>
where
Self: Sized,
{
prepare_command(
self,
cmd("TDIGEST.CREATE")
.arg(key)
.arg(compression.map(|c| ("COMPRESSION", c))),
)
}
/// Returns information and statistics about a t-digest sketch
///
/// # Arguments
/// * `key` - key name for an existing t-digest sketch.
///
/// # Return
/// An instance of [`TDigestInfoResult`](TDigestInfoResult)
///
/// # See Also
/// * [<https://redis.io/commands/tdigest.info/>](https://redis.io/commands/tdigest.info/)
#[must_use]
fn tdigest_info(self, key: impl SingleArg) -> PreparedCommand<'a, Self, TDigestInfoResult>
where
Self: Sized,
{
prepare_command(self, cmd("TDIGEST.INFO").arg(key))
}
/// Returns the maximum observation value from a t-digest sketch.
///
/// # Arguments
/// * `key` - key name for an existing t-digest sketch.
///
/// # Return
/// maximum observation value from a sketch. The result is always accurate.
/// `nan` if the sketch is empty.
///
/// # See Also
/// * [<https://redis.io/commands/tdigest.max/>](https://redis.io/commands/tdigest.max/)
#[must_use]
fn tdigest_max(self, key: impl SingleArg) -> PreparedCommand<'a, Self, f64>
where
Self: Sized,
{
prepare_command(self, cmd("TDIGEST.MAX").arg(key))
}
/// Merges multiple t-digest sketches into a single sketch.
///
/// # Arguments
/// * `destination` - key name for a t-digest sketch to merge observation values to.
/// * If `destination` not exist, a new sketch is created.
/// * If `destination` is an existing sketch, its values are merged with the values of the source keys.\
/// To override the destination key contents use [`override`](TDigestMergeOptions::_override).
/// * `sources` - collection of key names for t-digest sketches to merge observation values from.
///
/// # See Also
/// * [<https://redis.io/commands/tdigest.merge/>](https://redis.io/commands/tdigest.merge/)
#[must_use]
fn tdigest_merge<S: SingleArg>(
self,
destination: impl SingleArg,
sources: impl SingleArgCollection<S>,
options: TDigestMergeOptions,
) -> PreparedCommand<'a, Self, ()>
where
Self: Sized,
{
prepare_command(
self,
cmd("TDIGEST.MERGE")
.arg(destination)
.arg(sources.num_args())
.arg(sources)
.arg(options),
)
}
/// Returns the minimum observation value from a t-digest sketch.
///
/// # Arguments
/// * `key` - key name for an existing t-digest sketch.
///
/// # Return
/// minimum observation value from a sketch. The result is always accurate.
/// `nan` if the sketch is empty.
///
/// # See Also
/// * [<https://redis.io/commands/tdigest.min/>](https://redis.io/commands/tdigest.min/)
#[must_use]
fn tdigest_min(self, key: impl SingleArg) -> PreparedCommand<'a, Self, f64>
where
Self: Sized,
{
prepare_command(self, cmd("TDIGEST.MIN").arg(key))
}
/// Returns, for each input fraction, an estimation of the value
/// (floating point) that is smaller than the given fraction of observations.
///
/// Multiple quantiles can be retrieved in a signle call.
///
/// # Arguments
/// * `key` - key name for an existing t-digest sketch.
/// * `quantiles` - collection of quantiles which are input fractions (between 0 and 1 inclusively)
///
/// # Return
/// a collection of estimates (floating-point) populated with value_1, value_2, ..., value_N.
/// * Return an accurate result when quantile is 0 (the value of the smallest observation)
/// * Return an accurate result when quantile is 1 (the value of the largest observation)
///
/// All values are `nan` if the sketch is empty.
///
/// # See Also
/// * [<https://redis.io/commands/tdigest.quantile/>](https://redis.io/commands/tdigest.quantile/)
#[must_use]
fn tdigest_quantile<Q: SingleArg, R: CollectionResponse<f64>>(
self,
key: impl SingleArg,
quantiles: impl SingleArgCollection<Q>,
) -> PreparedCommand<'a, Self, R>
where
Self: Sized,
{
prepare_command(self, cmd("TDIGEST.QUANTILE").arg(key).arg(quantiles))
}
/// Returns, for each input value (floating-point), the estimated rank of the value
/// (the number of observations in the sketch that are smaller than the value + half the number of observations that are equal to the value).
///
/// Multiple ranks can be retrieved in a signle call.
///
/// # Arguments
/// * `key` - key name for an existing t-digest sketch.
/// * `values` - collection of values for which the rank should be estimated.
///
/// # Return
/// a collection of integers populated with rank_1, rank_2, ..., rank_V:
/// * `-1` - when `value` is smaller than the value of the smallest observation.
/// * The number of observations - when `value` is larger than the value of the largest observation.
/// * Otherwise: an estimation of the number of (observations smaller than `value` + half the observations equal to `value`).
///
/// `0` is the rank of the value of the smallest observation.
///
/// `n-1` is the rank of the value of the largest observation; `n` denotes the number of observations added to the sketch.
///
/// All values are `-2` if the sketch is empty.
///
/// # See Also
/// * [<https://redis.io/commands/tdigest.rank/>](https://redis.io/commands/tdigest.rank/)
#[must_use]
fn tdigest_rank<V: SingleArg, R: CollectionResponse<isize>>(
self,
key: impl SingleArg,
values: impl SingleArgCollection<V>,
) -> PreparedCommand<'a, Self, R>
where
Self: Sized,
{
prepare_command(self, cmd("TDIGEST.RANK").arg(key).arg(values))
}
/// Resets a t-digest sketch: empty the sketch and re-initializes it.
///
/// # Arguments
/// * `key` - key name for an existing t-digest sketch.
///
/// # See Also
/// * [<https://redis.io/commands/tdigest.reset/>](https://redis.io/commands/tdigest.reset/)
#[must_use]
fn tdigest_reset(self, key: impl SingleArg) -> PreparedCommand<'a, Self, ()>
where
Self: Sized,
{
prepare_command(self, cmd("TDIGEST.RESET").arg(key))
}
/// Returns, for each input value (floating-point), the estimated reverse rank of the value
/// (the number of observations in the sketch that are smaller than the value + half the number of observations that are equal to the value).
///
/// Multiple reverse ranks can be retrieved in a signle call.
///
/// # Arguments
/// * `key` - key name for an existing t-digest sketch.
/// * `values` - collection of values for which the reverse rank should be estimated.
///
/// # Return
/// a collection of integers populated with revrank_1, revrank_2, ..., revrank_V:
/// * `-1` - when `value` is smaller than the value of the smallest observation.
/// * The number of observations - when `value` is larger than the value of the largest observation.
/// * Otherwise: an estimation of the number of (observations smaller than `value` + half the observations equal to `value`).
///
/// `0` is the reverse rank of the value of the smallest observation.
///
/// `n-1` is the reverse rank of the value of the largest observation; `n` denotes the number of observations added to the sketch.
///
/// All values are `-2` if the sketch is empty.
///
/// # See Also
/// * [<https://redis.io/commands/tdigest.revrank/>](https://redis.io/commands/tdigest.revrank/)
#[must_use]
fn tdigest_revrank<V: SingleArg, R: CollectionResponse<isize>>(
self,
key: impl SingleArg,
values: impl SingleArgCollection<V>,
) -> PreparedCommand<'a, Self, R>
where
Self: Sized,
{
prepare_command(self, cmd("TDIGEST.REVRANK").arg(key).arg(values))
}
/// Returns an estimation of the mean value from the sketch, excluding observation values outside the low and high cutoff quantiles.
///
/// # Arguments
/// * `key` - key name for an existing t-digest sketch.
/// * `low_cut_quantile` - Foating-point value in the range [0..1], should be lower than `high_cut_quantile` \
/// When equal to 0: No low cut. \
/// When higher than 0: Exclude observation values lower than this quantile.
/// * `high_cut_quantile` - Floating-point value in the range [0..1], should be higher than `low_cut_quantile` \
/// When lower than 1: Exclude observation values higher than or equal to this quantile. \
/// When equal to 1: No high cut.
///
/// # Return
/// estimation of the mean value. 'nan' if the sketch is empty.
///
/// # See Also
/// * [<https://redis.io/commands/tdigest.trimmed_mean/>](https://redis.io/commands/tdigest.trimmed_mean/)
#[must_use]
fn tdigest_trimmed_mean(
self,
key: impl SingleArg,
low_cut_quantile: f64,
high_cut_quantile: f64,
) -> PreparedCommand<'a, Self, f64>
where
Self: Sized,
{
prepare_command(
self,
cmd("TDIGEST.TRIMMED_MEAN")
.arg(key)
.arg(low_cut_quantile)
.arg(high_cut_quantile),
)
}
}
/// Result for the [`tdigest_info`](TDigestCommands::tdigest_info) command.
#[derive(Debug, Deserialize)]
pub struct TDigestInfoResult {
/// The compression (controllable trade-off between accuracy and memory consumption) of the sketch
#[serde(rename = "Compression")]
pub compression: usize,
/// Size of the buffer used for storing the centroids and for the incoming unmerged observations
#[serde(rename = "Capacity")]
pub capacity: usize,
/// Number of merged observations
#[serde(rename = "Merged nodes")]
pub merged_nodes: usize,
/// Number of buffered nodes (uncompressed observations)
#[serde(rename = "Unmerged nodes")]
pub unmerged_nodes: usize,
/// Weight of values of the merged nodes
#[serde(rename = "Merged weight")]
pub merged_weight: usize,
/// Weight of values of the unmerged nodes (uncompressed observations)
#[serde(rename = "Unmerged weight")]
pub unmerged_weight: usize,
/// Number of observations added to the sketch
#[serde(rename = "Observations")]
pub observations: usize,
/// Number of times this sketch compressed data together
#[serde(rename = "Total compressions")]
pub total_compressions: usize,
/// Number of bytes allocated for the sketch
#[serde(rename = "Memory usage")]
pub memory_usage: usize,
/// Additional information
#[serde(flatten)]
pub additional_info: HashMap<String, Value>,
}
/// Options for the [`tdigest_merge`](TDigestCommands::tdigest_merge) command.
#[derive(Default)]
pub struct TDigestMergeOptions {
command_args: CommandArgs,
}
impl TDigestMergeOptions {
/// controllable tradeoff between accuracy and memory consumption.
///
/// 100 is a common value for normal uses.
/// 1000 is more accurate.
/// If no value is passed by default the compression will be 100.
/// For more information on scaling of accuracy versus the compression parameter
/// see [`The t-digest: Efficient estimates of distributions`](https://www.sciencedirect.com/science/article/pii/S2665963820300403).
///
/// When COMPRESSION is not specified:
/// * If `destination` does not exist or if [`override`](TDigestMergeOptions::_override) is specified, \
/// the compression is set to the maximal value among all source sketches.
/// * If `destination` already exists and [`override`](TDigestMergeOptions::_override) is not specified, \
/// its compression is not changed.
#[must_use]
pub fn compression(mut self, compression: usize) -> Self {
Self {
command_args: self
.command_args
.arg("COMPRESSION")
.arg(compression)
.build(),
}
}
/// When specified, if `destination` already exists, it is overwritten.
#[must_use]
pub fn _override(mut self) -> Self {
Self {
command_args: self.command_args.arg("OVERRIDE").build(),
}
}
}
impl ToArgs for TDigestMergeOptions {
fn write_args(&self, args: &mut CommandArgs) {
args.arg(&self.command_args);
}
}