-
Notifications
You must be signed in to change notification settings - Fork 13
/
quantumleap.py
1116 lines (1033 loc) · 48.6 KB
/
quantumleap.py
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
TimeSeries Module for QuantumLeap API Client
"""
import logging
import time
from math import inf
from collections import deque
from itertools import count
from typing import Dict, List, Union, Deque, Optional
from urllib.parse import urljoin
import requests
from pydantic import parse_obj_as, AnyHttpUrl
from filip import settings
from filip.clients.base_http_client import BaseHttpClient
from filip.models.base import FiwareHeader
from filip.models.ngsi_v2.subscriptions import Message
from filip.models.ngsi_v2.timeseries import \
AggrPeriod, \
AggrMethod, \
AggrScope, \
AttributeValues, \
TimeSeries, \
TimeSeriesHeader
from filip.utils.validators import validate_http_url
logger = logging.getLogger(__name__)
class QuantumLeapClient(BaseHttpClient):
"""
Implements functions to use the FIWARE's QuantumLeap, which subscribes to an
Orion Context Broker and stores the subscription data in a time series
database (CrateDB). Further Information:
https://smartsdk.github.io/ngsi-timeseries-api/#quantumleap
https://app.swaggerhub.com/apis/heikkilv/quantumleap-api/
Args:
url: url of the quantumleap service
session (Optional):
fiware_header:
**kwargs:
"""
def __init__(self,
url: str = None,
*,
session: requests.Session = None,
fiware_header: FiwareHeader = None,
**kwargs):
# set service url
url = url or settings.QL_URL
super().__init__(url=url,
session=session,
fiware_header=fiware_header,
**kwargs)
# META API ENDPOINTS
def get_version(self) -> Dict:
"""
Gets version of QuantumLeap-Service.
Returns:
Dictionary with response
"""
url = urljoin(self.base_url, '/version')
try:
res = self.get(url=url)
if res.ok:
return res.json()
res.raise_for_status()
except requests.exceptions.RequestException as err:
self.logger.error(err)
raise
def get_health(self) -> Dict:
"""
This endpoint is intended for administrators of QuantumLeap. Using the
information returned by this endpoint they can diagnose problems in the
service or its dependencies. This information is also useful for cloud
tools such as orchestrators and load balancers with rules based on
health-checks. Due to the lack of a standardized response format, we
base the implementation on the draft of
https://inadarei.github.io/rfc-healthcheck/
Returns:
Dictionary with response
"""
url = urljoin(self.base_url, '/health')
try:
res = self.get(url=url)
if res.ok:
return res.json()
res.raise_for_status()
except requests.exceptions.RequestException as err:
self.logger.error(err)
raise
def post_config(self):
"""
(To Be Implemented) Customize your persistence configuration to
better suit your needs.
"""
raise NotImplementedError("Endpoint to be implemented..")
# INPUT API ENDPOINTS
def post_notification(self, notification: Message):
"""
Notify QuantumLeap the arrival of a new NGSI notification.
Args:
notification: Notification Message Object
"""
url = urljoin(self.base_url, '/v2/notify')
headers = self.headers.copy()
data = []
for entity in notification.data:
data.append(entity.dict(exclude_unset=True,
exclude_defaults=True,
exclude_none=True))
data_set = {
"data": data,
"subscriptionId": notification.subscriptionId
}
try:
res = self.post(
url=url,
headers=headers,
json=data_set)
if res.ok:
self.logger.debug(res.text)
else:
res.raise_for_status()
except requests.exceptions.RequestException as err:
msg = f"Could not post notification for subscription id " \
f"{notification.subscriptionId}"
self.log_error(err=err, msg=msg)
raise
def post_subscription(self,
cb_url: Union[AnyHttpUrl, str],
ql_url: Union[AnyHttpUrl, str],
entity_type: str = None,
entity_id: str = None,
id_pattern: str = None,
attributes: str = None,
observed_attributes: str = None,
notified_attributes: str = None,
throttling: int = None,
time_index_attribute: str = None):
"""
Subscribe QL to process Orion notifications of certain type.
This endpoint simplifies the creation of the subscription in orion
that will generate the notifications to be consumed by QuantumLeap in
order to save historical records. If you want an advanced specification
of the notifications, you can always create the subscription in orion
at your will. This endpoint just aims to simplify the common use case.
Args:
cb_url:
url of the context broker
ql_url:
The url where Orion can reach QuantumLeap. Do not include
specific paths.
entity_type (String):
The type of entities for which to create a
subscription, so as to persist historical data of entities of
this type.
entity_id (String):
Id of the entity to track. If specified, it
takes precedence over the idPattern parameter.
id_pattern (String): The pattern covering the entity ids for which
to subscribe. If not specified, QL will track all entities of
the specified type.
attributes (String): Comma-separated list of attribute names to
track.
observed_attributes (String): Comma-separated list of attribute
names to track.
notified_attributes (String): Comma-separated list of attribute
names to be used to restrict the data of which QL will keep a
history.
throttling (int): Minimal period of time in seconds which must
elapse between two consecutive notifications.
time_index_attribute (String): The name of a custom attribute to be
used as a
time index.
"""
headers = self.headers.copy()
params = {}
url = urljoin(self.base_url, '/v2/subscribe')
validate_http_url(cb_url)
cb_url = urljoin(cb_url, '/v2')
params.update({'orionUrl': cb_url.encode('utf-8')})
validate_http_url(ql_url)
ql_url = urljoin(ql_url, '/v2')
params.update({'quantumleapUrl': ql_url.encode('utf-8')})
if entity_type:
params.update({'entityType': entity_type})
if entity_id:
params.update({'entityId': entity_id})
if id_pattern:
params.update({'idPattern': id_pattern})
if attributes:
params.update({'attributes': attributes})
if observed_attributes:
params.update({'observedAttributes': observed_attributes})
if notified_attributes:
params.update({'notifiedAttributes': notified_attributes})
if throttling:
if throttling < 1:
raise TypeError("Throttling must be a positive integer")
params.update({'throttling': throttling})
if time_index_attribute:
params.update({'timeIndexAttribute': time_index_attribute})
try:
res = self.post(url=url, headers=headers, params=params)
if res.ok:
msg = "Subscription created successfully!"
self.logger.info(msg)
res.raise_for_status()
except requests.exceptions.RequestException as err:
msg = "Could not create subscription."
self.log_error(err=err, msg=msg)
raise
def delete_entity(self, entity_id: str,
entity_type: Optional[str] = None) -> str:
"""
Given an entity (with type and id), delete all its historical records.
Args:
entity_id (String): Entity id is required.
entity_type (Optional[String]): Entity type if entity_id alone
can not uniquely define the entity.
Raises:
RequestException, if entity was not found
Exception, if deleting was not successful
Returns:
The entity_id of entity that is deleted.
"""
url = urljoin(self.base_url, f'/v2/entities/{entity_id}')
headers = self.headers.copy()
if entity_type is not None:
params = {'type': entity_type}
else:
params = {}
# The deletion does not always resolves in a success even if an ok is
# returned.
# Try to delete multiple times with incrementing waits.
# If the entity is no longer found the methode returns with a success
# If the deletion attempt fails after the 10th try, raise an
# Exception: it could not be deleted
counter = 0
while counter < 10:
self.delete(url=url, headers=headers, params=params)
try:
self.get_entity_by_id(entity_id=entity_id,
entity_type=entity_type)
except requests.exceptions.RequestException as err:
self.logger.info("Entity id '%s' successfully deleted!",
entity_id)
return entity_id
time.sleep(counter*5)
counter += 1
msg = f"Could not delete QL entity of id {entity_id}"
logger.error(msg=msg)
raise Exception(msg)
def delete_entity_type(self, entity_type: str) -> str:
"""
Given an entity type, delete all the historical records of all
entities of such type.
Args:
entity_type (String): Type of entities data to be deleted.
Returns:
Entity type of the entities deleted.
"""
url = urljoin(self.base_url, f'/v2/types/{entity_type}')
headers = self.headers.copy()
try:
res = self.delete(url=url, headers=headers)
if res.ok:
self.logger.info("Entities of type '%s' successfully deleted!",
entity_type)
return entity_type
res.raise_for_status()
except requests.exceptions.RequestException as err:
msg = f"Could not delete entities of type {entity_type}"
self.log_error(err=err, msg=msg)
raise
# QUERY API ENDPOINTS
def __query_builder(self,
url,
*,
entity_id: str = None,
options: str = None,
entity_type: str = None,
aggr_method: Union[str, AggrMethod] = None,
aggr_period: Union[str, AggrPeriod] = None,
from_date: str = None,
to_date: str = None,
last_n: int = None,
limit: int = 10000,
offset: int = 0,
georel: str = None,
geometry: str = None,
coords: str = None,
attrs: str = None,
aggr_scope: Union[str, AggrScope] = None
) -> Deque[Dict]:
"""
Private Function to call respective API endpoints, chops large
requests into multiple single requests and merges the
responses
Args:
url:
entity_id:
options:
entity_type:
aggr_method:
aggr_period:
from_date:
to_date:
last_n:
limit:
Maximum number of results to retrieve in a single response.
offset:
Offset to apply to the response results. For example, if the
query was to return 10 results and you use an offset of 1, the
response will return the last 9 values. Make sure you don't
give more offset than the number of results.
georel:
geometry:
coords:
attrs:
aggr_scope:
Returns:
Dict
"""
params = {}
headers = self.headers.copy()
max_records_per_request = 10000
# create a double ending queue
res_q: Deque[Dict] = deque([])
if options:
params.update({'options': options})
if entity_type:
params.update({'type': entity_type})
if aggr_method:
aggr_method = AggrMethod(aggr_method)
params.update({'aggrMethod': aggr_method.value})
if aggr_period:
aggr_period = AggrPeriod(aggr_period)
params.update({'aggrPeriod': aggr_period.value})
if from_date:
params.update({'fromDate': from_date})
if to_date:
params.update({'toDate': to_date})
# These values are required for the integrated pagination mechanism
# maximum items per request
if limit is None:
limit = inf
if offset is None:
offset = 0
if georel:
params.update({'georel': georel})
if coords:
params.update({'coords': coords})
if geometry:
params.update({'geometry': geometry})
if attrs:
params.update({'attrs': attrs})
if aggr_scope:
aggr_scope = AggrScope(aggr_scope)
params.update({'aggr_scope': aggr_scope.value})
if entity_id:
params.update({'id': entity_id})
# This loop will chop large requests into smaller junks.
# The individual functions will then merge the final response models
for i in count(0, max_records_per_request):
try:
params['offset'] = offset + i
params['limit'] = min(limit - i, max_records_per_request)
if params['limit'] <= 0:
break
if last_n:
params['lastN'] = min(last_n - i, max_records_per_request)
if params['lastN'] <= 0:
break
res = self.get(url=url, params=params, headers=headers)
if res.ok:
self.logger.debug('Received: %s', res.json())
# revert append direction when using last_n
if last_n:
res_q.appendleft(res.json())
else:
res_q.append(res.json())
res.raise_for_status()
except requests.exceptions.RequestException as err:
if err.response.status_code == 404 and \
err.response.json().get('error') == 'Not Found' and \
len(res_q) > 0:
break
else:
msg = "Could not load entity data"
self.log_error(err=err, msg=msg)
raise
self.logger.info("Successfully retrieved entity data")
return res_q
# v2/entities
def get_entities(self, *,
entity_type: str = None,
from_date: str = None,
to_date: str = None,
limit: int = 10000,
offset: int = None
) -> List[TimeSeriesHeader]:
"""
Get list of all available entities and their context information
about EntityType and last update date.
Args:
entity_type (str): Comma-separated list of entity types whose data
are to be included in the response. Use only one (no comma)
when required. If used to resolve ambiguity for the given
entityId, make sure the given entityId exists for this
entityType.
from_date (str): The starting date and time (inclusive) from which
the context information is queried. Must be in ISO8601 format
(e.g., 2018-01-05T15:44:34)
to_date (str): The final date and time (inclusive) from which the
context information is queried. Must be in ISO8601 format
(e.g., 2018-01-05T15:44:34).
limit (int): Maximum number of results to be retrieved.
Default value : 10000
offset (int): Offset for the results.
Returns:
List of TimeSeriesHeader
"""
url = urljoin(self.base_url, 'v2/entities')
res = self.__query_builder(url=url,
entity_type=entity_type,
from_date=from_date,
to_date=to_date,
limit=limit,
offset=offset)
return parse_obj_as(List[TimeSeriesHeader], res[0])
# /entities/{entityId}
def get_entity_by_id(self,
entity_id: str,
*,
attrs: str = None,
entity_type: str = None,
aggr_method: Union[str, AggrMethod] = None,
aggr_period: Union[str, AggrPeriod] = None,
from_date: str = None,
to_date: str = None,
last_n: int = None,
limit: int = 10000,
offset: int = None,
georel: str = None,
geometry: str = None,
coords: str = None,
options: str = None
) -> TimeSeries:
"""
History of N attributes of a given entity instance
For example, query max water level of the central tank throughout the
last year. Queries can get more
sophisticated with the use of filters and query attributes.
Args:
entity_id (String): Entity id is required.
attrs (String):
Comma-separated list of attribute names whose data are to be
included in the response. The attributes are retrieved in the
order specified by this parameter. If not specified, all
attributes are included in the response in arbitrary order.
entity_type (String): Comma-separated list of entity types whose
data are to be included in the response.
aggr_method (String):
The function to apply to the raw data filtered by the query
parameters. If not given, the returned data are the same raw
inserted data.
Allowed values: count, sum, avg, min, max
aggr_period (String):
If not defined, the aggregation will apply to all the values
contained in the search result. If defined, the aggregation
function will instead be applied N times, once for each
period, and all those results will be considered for the
response. For example, a query asking for the average
temperature of an attribute will typically return 1 value.
However, with an aggregationPeriod of day, you get the daily
average of the temperature instead (more than one value
assuming you had measurements across many days within the
scope of your search result). aggrPeriod must be accompanied
by an aggrMethod, and the aggrMethod will be applied to all
the numeric attributes specified in attrs; the rest of the
non-numerical attrs will be ignored. By default, the response
is grouped by entity_id. See aggrScope to create aggregation
across entities:
Allowed values: year, month, day, hour, minute, second
from_date (String):
The starting date and time (inclusive) from which the context
information is queried. Must be in ISO8601 format (e.g.,
2018-01-05T15:44:34)
to_date (String):
The final date and time (inclusive) from which the context
information is queried. Must be in ISO8601 format (e.g.,
2018-01-05T15:44:34)
last_n (int):
Used to request only the last N values that satisfy the
request conditions.
limit (int): Maximum number of results to be retrieved.
Default value : 10000
offset (int):
Offset to apply to the response results.
georel (String):
It specifies a spatial relationship between matching entities
and a reference shape (geometry). This parameter is used to
perform geographical queries with the same semantics as in the
FIWARE-NGSI v2 Specification. Full details can be found in the
Geographical Queries section of the specification:
https://fiware.github.io/specifications/ngsiv2/stable/.
geometry (String):
Required if georel is specified. point, line, polygon, box
coords (String):
Optional but required if georel is specified. This parameter
defines the reference shape (geometry) in terms of WGS 84
coordinates and has the same semantics as in the
FIWARE-NGSI v2 Specification, except we only accept coordinates
in decimal degrees---e.g. 40.714,-74.006 is okay, but not
40 42' 51'',74 0' 21''. Full details can be found in the
Geographical Queries section of the specification:
https://fiware.github.io/specifications/ngsiv2/stable/.
options (String): Key value pair options.
Returns:
TimeSeries
"""
url = urljoin(self.base_url, f'/v2/entities/{entity_id}')
res_q = self.__query_builder(url=url,
attrs=attrs,
options=options,
entity_type=entity_type,
aggr_method=aggr_method,
aggr_period=aggr_period,
from_date=from_date,
to_date=to_date,
last_n=last_n,
limit=limit,
offset=offset,
georel=georel,
geometry=geometry,
coords=coords)
# merge response chunks
res = TimeSeries.parse_obj(res_q.popleft())
for item in res_q:
res.extend(TimeSeries.parse_obj(item))
return res
# /entities/{entityId}/value
def get_entity_values_by_id(self,
entity_id: str,
*,
attrs: str = None,
entity_type: str = None,
aggr_method: Union[str, AggrMethod] = None,
aggr_period: Union[str, AggrPeriod] = None,
from_date: str = None,
to_date: str = None,
last_n: int = None,
limit: int = 10000,
offset: int = None,
georel: str = None,
geometry: str = None,
coords: str = None,
options: str = None
) -> TimeSeries:
"""
History of N attributes (values only) of a given entity instance
For example, query the average pressure, temperature and humidity (
values only, no metadata) of this
month in the weather station WS1.
Args:
entity_id (String): Entity id is required.
attrs (String): Comma-separated list of attribute names
entity_type (String): Comma-separated list of entity types whose
data are to be included in the response.
aggr_method (String): The function to apply to the raw data
filtered. count, sum, avg, min, max
aggr_period (String): year, month, day, hour, minute, second
from_date (String): Starting date and time inclusive.
to_date (String): Final date and time inclusive.
last_n (int): Request only the last N values.
limit (int): Maximum number of results to be retrieved.
Default value : 10000
offset (int): Offset for the results.
georel (String): Geographical pattern
geometry (String): Required if georel is specified. point, line,
polygon, box
coords (String): Required if georel is specified.
e.g. 40.714,-74.006
options (String): Key value pair options.
Returns:
Response Model
"""
url = urljoin(self.base_url, f'/v2/entities/{entity_id}/value')
res_q = self.__query_builder(url=url,
attrs=attrs,
options=options,
entity_type=entity_type,
aggr_method=aggr_method,
aggr_period=aggr_period,
from_date=from_date,
to_date=to_date,
last_n=last_n,
limit=limit,
offset=offset,
georel=georel,
geometry=geometry,
coords=coords)
# merge response chunks
res = TimeSeries(entityId=entity_id, **res_q.popleft())
for item in res_q:
res.extend(TimeSeries(entityId=entity_id, **item))
return res
# /entities/{entityId}/attrs/{attrName}
def get_entity_attr_by_id(self,
entity_id: str,
attr_name: str,
*,
entity_type: str = None,
aggr_method: Union[str, AggrMethod] = None,
aggr_period: Union[str, AggrPeriod] = None,
from_date: str = None,
to_date: str = None,
last_n: int = None,
limit: int = 10000,
offset: int = None,
georel: str = None,
geometry: str = None,
coords: str = None,
options: str = None
) -> TimeSeries:
"""
History of an attribute of a given entity instance
For example, query max water level of the central tank throughout the
last year. Queries can get more
sophisticated with the use of filters and query attributes.
Args:
entity_id (String): Entity id is required.
attr_name (String): The attribute name is required.
entity_type (String): Comma-separated list of entity types whose
data are to be included in the response.
aggr_method (String): The function to apply to the raw data
filtered. count, sum, avg, min, max
aggr_period (String): year, month, day, hour, minute, second
from_date (String): Starting date and time inclusive.
to_date (String): Final date and time inclusive.
last_n (int): Request only the last N values.
limit (int): Maximum number of results to be retrieved.
Default value : 10000
offset (int): Offset for the results.
georel (String): Geographical pattern
geometry (String): Required if georel is specified. point, line,
polygon, box
coords (String): Required if georel is specified.
e.g. 40.714,-74.006
options (String): Key value pair options.
Returns:
Response Model
"""
url = urljoin(self.base_url, f'/v2/entities/{entity_id}/attrs'
f'/{attr_name}')
req_q = self.__query_builder(url=url,
entity_id=entity_id,
options=options,
entity_type=entity_type,
aggr_method=aggr_method,
aggr_period=aggr_period,
from_date=from_date,
to_date=to_date,
last_n=last_n,
limit=limit,
offset=offset,
georel=georel,
geometry=geometry,
coords=coords)
# merge response chunks
first = req_q.popleft()
res = TimeSeries(entityId=entity_id,
index=first.get('index'),
attributes=[AttributeValues(**first)])
for item in req_q:
res.extend(TimeSeries(entityId=entity_id,
index=item.get('index'),
attributes=[AttributeValues(**item)]))
return res
# /entities/{entityId}/attrs/{attrName}/value
def get_entity_attr_values_by_id(self,
entity_id: str,
attr_name: str,
*,
entity_type: str = None,
aggr_method: Union[str, AggrMethod] = None,
aggr_period: Union[str, AggrPeriod] = None,
from_date: str = None,
to_date: str = None,
last_n: int = None,
limit: int = 10000,
offset: int = None,
georel: str = None,
geometry: str = None,
coords: str = None,
options: str = None
) -> TimeSeries:
"""
History of an attribute (values only) of a given entity instance
Similar to the previous, but focusing on the values regardless of the
metadata.
Args:
entity_id (String): Entity id is required.
attr_name (String): The attribute name is required.
entity_type (String): Comma-separated list of entity types whose
data are to be included in the response.
aggr_method (String): The function to apply to the raw data
filtered. count, sum, avg, min, max
aggr_period (String): year, month, day, hour, minute, second
from_date (String): Starting date and time inclusive.
to_date (String): Final date and time inclusive.
last_n (int): Request only the last N values.
limit (int): Maximum number of results to be retrieved.
Default value : 10000
offset (int): Offset for the results.
georel (String): Geographical pattern
geometry (String): Required if georel is specified. point, line,
polygon, box
coords (String): Required if georel is specified.
e.g. 40.714,-74.006
options (String): Key value pair options.
Returns:
Response Model
"""
url = urljoin(self.base_url, f'v2/entities/{entity_id}/attrs'
f'/{attr_name}/value')
res_q = self.__query_builder(url=url,
options=options,
entity_type=entity_type,
aggr_method=aggr_method,
aggr_period=aggr_period,
from_date=from_date,
to_date=to_date,
last_n=last_n,
limit=limit,
offset=offset,
georel=georel,
geometry=geometry,
coords=coords)
# merge response chunks
first = res_q.popleft()
res = TimeSeries(
entityId=entity_id,
index=first.get('index'),
attributes=[AttributeValues(attrName=attr_name,
values=first.get('values'))])
for item in res_q:
res.extend(
TimeSeries(
entityId=entity_id,
index=item.get('index'),
attributes=[AttributeValues(attrName=attr_name,
values=item.get('values'))]))
return res
# /types/{entityType}
def get_entity_by_type(self,
entity_type: str,
*,
attrs: str = None,
entity_id: str = None,
aggr_method: Union[str, AggrMethod] = None,
aggr_period: Union[str, AggrPeriod] = None,
from_date: str = None,
to_date: str = None,
last_n: int = None,
limit: int = 10000,
offset: int = None,
georel: str = None,
geometry: str = None,
coords: str = None,
options: str = None,
aggr_scope: Union[str, AggrScope] = None
) -> List[TimeSeries]:
"""
History of N attributes of N entities of the same type.
For example, query the average pressure, temperature and humidity of
this month in all the weather stations.
"""
url = urljoin(self.base_url, f'/v2/types/{entity_type}')
res_q = self.__query_builder(url=url,
entity_id=entity_id,
attrs=attrs,
options=options,
aggr_method=aggr_method,
aggr_period=aggr_period,
from_date=from_date,
to_date=to_date,
last_n=last_n,
limit=limit,
offset=offset,
georel=georel,
geometry=geometry,
coords=coords,
aggr_scope=aggr_scope)
# merge chunks of response
res = [TimeSeries(entityType=entity_type, **item)
for item in res_q.popleft().get('entities')]
for chunk in res_q:
chunk = [TimeSeries(entityType=entity_type, **item)
for item in chunk.get('entities')]
for new, old in zip(chunk, res):
old.extend(new)
return res
# /types/{entityType}/value
def get_entity_values_by_type(self,
entity_type: str,
*,
attrs: str = None,
entity_id: str = None,
aggr_method: Union[str, AggrMethod] = None,
aggr_period: Union[str, AggrPeriod] = None,
from_date: str = None,
to_date: str = None,
last_n: int = None,
limit: int = 10000,
offset: int = None,
georel: str = None,
geometry: str = None,
coords: str = None,
options: str = None,
aggr_scope: Union[str, AggrScope] = None
) -> List[TimeSeries]:
"""
History of N attributes (values only) of N entities of the same type.
For example, query the average pressure, temperature and humidity (
values only, no metadata) of this month in
all the weather stations.
"""
url = urljoin(self.base_url, f'/v2/types/{entity_type}/value')
res_q = self.__query_builder(url=url,
entity_id=entity_id,
attrs=attrs,
options=options,
entity_type=entity_type,
aggr_method=aggr_method,
aggr_period=aggr_period,
from_date=from_date,
to_date=to_date,
last_n=last_n,
limit=limit,
offset=offset,
georel=georel,
geometry=geometry,
coords=coords,
aggr_scope=aggr_scope)
# merge chunks of response
res = [TimeSeries(entityType=entity_type, **item)
for item in res_q.popleft().get('values')]
for chunk in res_q:
chunk = [TimeSeries(entityType=entity_type, **item)
for item in chunk.get('values')]
for new, old in zip(chunk, res):
old.extend(new)
return res
# /types/{entityType}/attrs/{attrName}
def get_entity_attr_by_type(self,
entity_type: str,
attr_name: str,
*,
entity_id: str = None,
aggr_method: Union[str, AggrMethod] = None,
aggr_period: Union[str, AggrPeriod] = None,
from_date: str = None,
to_date: str = None,
last_n: int = None,
limit: int = 10000,
offset: int = None,
georel: str = None,
geometry: str = None,
coords: str = None,
options: str = None,
aggr_scope: Union[str, AggrScope] = None
) -> List[TimeSeries]:
"""
History of an attribute of N entities of the same type.
For example, query the pressure measurements of this month in all the
weather stations. Note in the response,
the index and values arrays are parallel. Also, when using
aggrMethod, the aggregation is done by-entity
instance. In this case, the index array is just the fromDate and
toDate values user specified in the request
(if any).
Args:
entity_type (String): Entity type is required.
attr_name (String): The attribute name is required.
entity_id (String): Comma-separated list of entity ids whose data
are to be included in the response.
aggr_method (String): The function to apply to the raw data
filtered. count, sum, avg, min, max
aggr_period (String): year, month, day, hour, minute, second
aggr_scope (str): Optional. (This parameter is not yet supported).
When the query results cover historical data for multiple
entities instances, you can define the aggregation method to be
applied for each entity instance [entity] or across
them [global]
from_date (String): Starting date and time inclusive.
to_date (String): Final date and time inclusive.
last_n (int): Request only the last N values.
limit (int): Maximum number of results to be retrieved.
Default value : 10000
offset (int): Offset for the results.
georel (String): Geographical pattern
geometry (String): Required if georel is specified. point, line,
polygon, box
coords (String): Required if georel is specified.
e.g. 40.714,-74.006
options (String): Key value pair options.
Returns:
Response Model
"""
url = urljoin(self.base_url, f'/v2/types/{entity_type}/attrs'
f'/{attr_name}')
res_q = self.__query_builder(url=url,
entity_id=entity_id,
options=options,
entity_type=entity_type,
aggr_method=aggr_method,
aggr_period=aggr_period,
from_date=from_date,
to_date=to_date,
last_n=last_n,
limit=limit,
offset=offset,
georel=georel,
geometry=geometry,
coords=coords,
aggr_scope=aggr_scope)
# merge chunks of response