|
38 | 38 | # REMOVE_END
|
39 | 39 |
|
40 | 40 | # 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. |
44 | 41 | res4 = r.ts().add("thermometer:2", 1, 10.8, retention_msecs=100)
|
45 | 42 | print(res4) # >>> 1
|
46 | 43 |
|
|
83 | 80 |
|
84 | 81 | # STEP_START get
|
85 | 82 | # The last recorded temperature for thermometer:2
|
86 |
| -# was 10.3 on day 2. |
| 83 | +# was 10.3 at time 2. |
87 | 84 | res9 = r.ts().get("thermometer:2")
|
88 | 85 | print(res9) # >>> (2, 10.3)
|
89 | 86 | # STEP_END
|
|
92 | 89 | # REMOVE_END
|
93 | 90 |
|
94 | 91 | # 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". |
96 | 93 | res10 = r.ts().create("rg:1")
|
97 | 94 | print(res10) # >>> True
|
98 | 95 |
|
|
109 | 106 | res12 = r.ts().range("rg:1", "-", "+")
|
110 | 107 | print(res12) # >>> [(0, 18.0), (1, 14.0), (2, 22.0), (3, 18.0), (4, 24.0)]
|
111 | 108 |
|
112 |
| -# Retrieve data points up to day 1 (inclusive). |
| 109 | +# Retrieve data points up to time 1 (inclusive). |
113 | 110 | res13 = r.ts().range("rg:1", "-", 1)
|
114 | 111 | print(res13) # >>> [(0, 18.0), (1, 14.0)]
|
115 | 112 |
|
116 |
| - |
117 |
| -# Retrieve data points from day 3 onwards. |
| 113 | +# Retrieve data points from time 3 onwards. |
118 | 114 | res14 = r.ts().range("rg:1", 3, "+")
|
119 | 115 | print(res14) # >>> [(3, 18.0), (4, 24.0)]
|
120 | 116 |
|
121 | 117 | # Retrieve all the data points in descending order.
|
122 | 118 | res15 = r.ts().revrange("rg:1", "-", "+")
|
123 | 119 | print(res15) # >>> [(4, 24.0), (3, 18.0), (2, 22.0), (1, 14.0), (0, 18.0)]
|
124 | 120 |
|
125 |
| -# Retrieve data points up to day 1 (inclusive), but return them |
| 121 | +# Retrieve data points up to time 1 (inclusive), but return them |
126 | 122 | # in descending order.
|
127 | 123 | res16 = r.ts().revrange("rg:1", "-", 1)
|
128 | 124 | print(res16) # >>> [(1, 14.0), (0, 18.0)]
|
|
165 | 161 | # REMOVE_END
|
166 | 162 |
|
167 | 163 | # 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 |
170 | 166 | # data points.
|
171 | 167 | res20 = r.ts().create(
|
172 | 168 | "rg:2",
|
|
189 | 185 | res23 = r.ts().madd([
|
190 | 186 | ("rg:2", 0, 1.8),
|
191 | 187 | ("rg:3", 0, 0.9),
|
192 |
| - ("rg:4", 0, 25.4), |
| 188 | + ("rg:4", 0, 25), |
193 | 189 | ])
|
194 | 190 | print(res23) # >>> [0, 0, 0]
|
195 | 191 |
|
|
221 | 217 | ])
|
222 | 218 | print(res27) # >>> [4, 4, 4]
|
223 | 219 |
|
224 |
| -# Retrieve the last data point from each US rain gauge. If |
| 220 | +# Retrieve the last data point from each US time series. If |
225 | 221 | # you don't specify any labels, an empty array is returned
|
226 | 222 | # for the labels.
|
227 | 223 | res28 = r.ts().mget(["location=us"])
|
|
232 | 228 | res29 = r.ts().mget(["location=us"], select_labels=["unit"])
|
233 | 229 | print(res29) # >>> [{'unit': 'cm'}, (4, 1.78), {'unit': 'in'}, (4, 0.74)]
|
234 | 230 |
|
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 |
237 | 233 | # labels in the results.
|
238 | 234 | res30 = r.ts().mrange(
|
239 | 235 | "-", 2, filters=["unit=mm"], with_labels=True
|
240 | 236 | )
|
241 | 237 | print(res30)
|
242 | 238 | # >>> [{'rg:4': [{'location': 'uk', 'unit': 'mm'}, [(0, 25.4),...
|
243 | 239 |
|
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, |
246 | 242 | # but only return the `location` label. Return the results
|
247 | 243 | # in descending order of timestamp.
|
248 | 244 | res31 = r.ts().mrevrange(
|
|
269 | 265 | {
|
270 | 266 | 'rg:4': [
|
271 | 267 | {'location': 'uk', 'unit': 'mm'},
|
272 |
| - [(0, 25.4), (1, 18.0), (2, 21.0)] |
| 268 | + [(0, 25), (1, 18.0), (2, 21.0)] |
273 | 269 | ]
|
274 | 270 | }
|
275 | 271 | ]
|
|
387 | 383 | ])
|
388 | 384 | print(res43) # >>> [3, 3, 3, 3]
|
389 | 385 |
|
390 |
| -# The result pairs contain the timestamp and the maximum wind speed |
| 386 | +# The result pairs contain the timestamp and the maximum sample value |
391 | 387 | # for the country at that timestamp.
|
392 | 388 | res44 = r.ts().mrange(
|
393 | 389 | "-", "+",
|
|
396 | 392 | reduce="max"
|
397 | 393 | )
|
398 | 394 | 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)]]}, ... |
400 | 396 |
|
401 |
| -# The result pairs contain the timestamp and the average wind speed |
| 397 | +# The result pairs contain the timestamp and the average sample value |
402 | 398 | # for the country at that timestamp.
|
403 | 399 | res45 = r.ts().mrange(
|
404 | 400 | "-", "+",
|
|
407 | 403 | reduce="avg"
|
408 | 404 | )
|
409 | 405 | 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)]]}, ... |
411 | 407 | # STEP_END
|
412 | 408 | # REMOVE_START
|
413 | 409 | assert res37 is True
|
|
426 | 422 | {'country=us': [{}, [(1, 12.5), (2, 14.5), (3, 13.0)]]}
|
427 | 423 | ]
|
428 | 424 | # 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