Skip to content

Commit 900cabe

Browse files
DOC-5424 remaining examples
1 parent be32dc3 commit 900cabe

File tree

1 file changed

+111
-22
lines changed

1 file changed

+111
-22
lines changed

doctests/dt_time_series.py

Lines changed: 111 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@
3838
# REMOVE_END
3939

4040
# STEP_START create_retention
41-
# Note: the parameter name is `retention_msecs`, but you
42-
# can interpret the time unit however you like. Here, we
43-
# assume 100 days.
4441
res4 = r.ts().add("thermometer:2", 1, 10.8, retention_msecs=100)
4542
print(res4) # >>> 1
4643

@@ -83,7 +80,7 @@
8380

8481
# STEP_START get
8582
# The last recorded temperature for thermometer:2
86-
# was 10.3 on day 2.
83+
# was 10.3 at time 2.
8784
res9 = r.ts().get("thermometer:2")
8885
print(res9) # >>> (2, 10.3)
8986
# STEP_END
@@ -92,7 +89,7 @@
9289
# REMOVE_END
9390

9491
# STEP_START range
95-
# Add 5 data points to a rain gauge time series.
92+
# Add 5 data points to a time series named "rg:1".
9693
res10 = r.ts().create("rg:1")
9794
print(res10) # >>> True
9895

@@ -109,20 +106,19 @@
109106
res12 = r.ts().range("rg:1", "-", "+")
110107
print(res12) # >>> [(0, 18.0), (1, 14.0), (2, 22.0), (3, 18.0), (4, 24.0)]
111108

112-
# Retrieve data points up to day 1 (inclusive).
109+
# Retrieve data points up to time 1 (inclusive).
113110
res13 = r.ts().range("rg:1", "-", 1)
114111
print(res13) # >>> [(0, 18.0), (1, 14.0)]
115112

116-
117-
# Retrieve data points from day 3 onwards.
113+
# Retrieve data points from time 3 onwards.
118114
res14 = r.ts().range("rg:1", 3, "+")
119115
print(res14) # >>> [(3, 18.0), (4, 24.0)]
120116

121117
# Retrieve all the data points in descending order.
122118
res15 = r.ts().revrange("rg:1", "-", "+")
123119
print(res15) # >>> [(4, 24.0), (3, 18.0), (2, 22.0), (1, 14.0), (0, 18.0)]
124120

125-
# Retrieve data points up to day 1 (inclusive), but return them
121+
# Retrieve data points up to time 1 (inclusive), but return them
126122
# in descending order.
127123
res16 = r.ts().revrange("rg:1", "-", 1)
128124
print(res16) # >>> [(1, 14.0), (0, 18.0)]
@@ -165,8 +161,8 @@
165161
# REMOVE_END
166162

167163
# STEP_START query_multi
168-
# Create three new rain gauge time series, two in the US
169-
# and one in the UK, with different units and add some
164+
# Create three new "rg:" time series (two in the US
165+
# and one in the UK, with different units) and add some
170166
# data points.
171167
res20 = r.ts().create(
172168
"rg:2",
@@ -189,7 +185,7 @@
189185
res23 = r.ts().madd([
190186
("rg:2", 0, 1.8),
191187
("rg:3", 0, 0.9),
192-
("rg:4", 0, 25.4),
188+
("rg:4", 0, 25),
193189
])
194190
print(res23) # >>> [0, 0, 0]
195191

@@ -221,7 +217,7 @@
221217
])
222218
print(res27) # >>> [4, 4, 4]
223219

224-
# Retrieve the last data point from each US rain gauge. If
220+
# Retrieve the last data point from each US time series. If
225221
# you don't specify any labels, an empty array is returned
226222
# for the labels.
227223
res28 = r.ts().mget(["location=us"])
@@ -232,17 +228,17 @@
232228
res29 = r.ts().mget(["location=us"], select_labels=["unit"])
233229
print(res29) # >>> [{'unit': 'cm'}, (4, 1.78), {'unit': 'in'}, (4, 0.74)]
234230

235-
# Retrieve data points up to day 2 (inclusive) from all
236-
# rain gauges that report in millimeters. Include all
231+
# Retrieve data points up to time 2 (inclusive) from all
232+
# time series that use millimeters as the unit. Include all
237233
# labels in the results.
238234
res30 = r.ts().mrange(
239235
"-", 2, filters=["unit=mm"], with_labels=True
240236
)
241237
print(res30)
242238
# >>> [{'rg:4': [{'location': 'uk', 'unit': 'mm'}, [(0, 25.4),...
243239

244-
# Retrieve data points from day 1 to day 3 (inclusive) from
245-
# all rain gauges that report in centimeters or millimeters,
240+
# Retrieve data points from time 1 to time 3 (inclusive) from
241+
# all time series that use centimeters or millimeters as the unit,
246242
# but only return the `location` label. Return the results
247243
# in descending order of timestamp.
248244
res31 = r.ts().mrevrange(
@@ -269,7 +265,7 @@
269265
{
270266
'rg:4': [
271267
{'location': 'uk', 'unit': 'mm'},
272-
[(0, 25.4), (1, 18.0), (2, 21.0)]
268+
[(0, 25), (1, 18.0), (2, 21.0)]
273269
]
274270
}
275271
]
@@ -387,7 +383,7 @@
387383
])
388384
print(res43) # >>> [3, 3, 3, 3]
389385

390-
# The result pairs contain the timestamp and the maximum wind speed
386+
# The result pairs contain the timestamp and the maximum sample value
391387
# for the country at that timestamp.
392388
res44 = r.ts().mrange(
393389
"-", "+",
@@ -396,9 +392,9 @@
396392
reduce="max"
397393
)
398394
print(res44)
399-
# >>> [{'country=uk': [{}, [(1, 18.0), (2, 21.0), (3, 24.0)]]}, {'country=us': [{}, [(1, 20.0), (2, 25.0), (3, 18.0)]]}]
395+
# >>> [{'country=uk': [{}, [(1, 18.0), (2, 21.0), (3, 24.0)]]}, ...
400396

401-
# The result pairs contain the timestamp and the average wind speed
397+
# The result pairs contain the timestamp and the average sample value
402398
# for the country at that timestamp.
403399
res45 = r.ts().mrange(
404400
"-", "+",
@@ -407,7 +403,7 @@
407403
reduce="avg"
408404
)
409405
print(res45)
410-
# >>> [{'country=uk': [{}, [(1, 15.0), (2, 17.5), (3, 17.0)]]}, {'country=us': [{}, [(1, 12.5), (2, 14.5), (3, 11.5)]]}]
406+
# >>> [{'country=uk': [{}, [(1, 15.0), (2, 17.5), (3, 17.0)]]}, ...
411407
# STEP_END
412408
# REMOVE_START
413409
assert res37 is True
@@ -426,3 +422,96 @@
426422
{'country=us': [{}, [(1, 12.5), (2, 14.5), (3, 13.0)]]}
427423
]
428424
# REMOVE_END
425+
426+
# STEP_START create_compaction
427+
res45 = r.ts().create("hyg:1")
428+
print(res45) # >>> True
429+
430+
res46 = r.ts().create("hyg:compacted")
431+
print(res46) # >>> True
432+
433+
res47 = r.ts().createrule("hyg:1", "hyg:compacted", "min", 3)
434+
print(res47) # >>> True
435+
436+
res48 = r.ts().info("hyg:1")
437+
print(res48.rules)
438+
# >>> [['hyg:compacted', 3, 'MIN', 0]]
439+
440+
res49 = r.ts().info("hyg:compacted")
441+
print(res49.source_key) # >>> 'hyg:1'
442+
# STEP_END
443+
# REMOVE_START
444+
assert res45 is True
445+
assert res46 is True
446+
assert res47 is True
447+
assert res48.rules == [['hyg:compacted', 3, 'MIN', 0]]
448+
assert res49.source_key == 'hyg:1'
449+
# REMOVE_END
450+
451+
# STEP_START comp_add
452+
res50 = r.ts().madd([
453+
("hyg:1", 0, 75),
454+
("hyg:1", 1, 77),
455+
("hyg:1", 2, 78),
456+
])
457+
print(res50) # >>> [0, 1, 2]
458+
459+
res51 = r.ts().range("hyg:compacted", "-", "+")
460+
print(res51) # >>> []
461+
462+
res52 = r.ts().add("hyg:1", 3, 79)
463+
print(res52) # >>> 3
464+
465+
res53 = r.ts().range("hyg:compacted", "-", "+")
466+
print(res53) # >>> [(0, 75.0)]
467+
# STEP_END
468+
# REMOVE_START
469+
assert res50 == [0, 1, 2]
470+
assert res51 == []
471+
assert res52 == 3
472+
assert res53 == [(0, 75.0)]
473+
# REMOVE_END
474+
475+
# STEP_START del
476+
res54 = r.ts().info("thermometer:1")
477+
print(res54.total_samples) # >>> 2
478+
print(res54.first_timestamp) # >>> 1
479+
print(res54.last_timestamp) # >>> 2
480+
481+
res55 = r.ts().add("thermometer:1", 3, 9.7)
482+
print(res55) # >>> 3
483+
484+
res56 = r.ts().info("thermometer:1")
485+
print(res56.total_samples) # >>> 3
486+
print(res56.first_timestamp) # >>> 1
487+
print(res56.last_timestamp) # >>> 3
488+
489+
res57 = r.ts().delete("thermometer:1", 1, 2)
490+
print(res57) # >>> 2
491+
492+
res58 = r.ts().info("thermometer:1")
493+
print(res58.total_samples) # >>> 1
494+
print(res58.first_timestamp) # >>> 3
495+
print(res58.last_timestamp) # >>> 3
496+
497+
res59 = r.ts().delete("thermometer:1", 3, 3)
498+
print(res59) # >>> 1
499+
500+
res60 = r.ts().info("thermometer:1")
501+
print(res60.total_samples) # >>> 0
502+
# STEP_END
503+
# REMOVE_START
504+
assert res54.total_samples == 2
505+
assert res54.first_timestamp == 1
506+
assert res54.last_timestamp == 2
507+
assert res55 == 3
508+
assert res56.total_samples == 3
509+
assert res56.first_timestamp == 1
510+
assert res56.last_timestamp == 3
511+
assert res57 == 2
512+
assert res58.total_samples == 1
513+
assert res58.first_timestamp == 3
514+
assert res58.last_timestamp == 3
515+
assert res59 == 1
516+
assert res60.total_samples == 0
517+
# REMOVE_END

0 commit comments

Comments
 (0)