1919 wait_chain ,
2020 wait_fixed ,
2121)
22+ from tqdm .auto import tqdm
2223
2324from ._logging import log_decorator
2425from ._utils import function_translation , period_translation
3940
4041_TIMEOUT_DEAULT = 120
4142
43+ _DEFAULT_MAX_PAGE_SIZE = 30000
44+
4245
4346class Client :
4447 """
@@ -424,7 +427,7 @@ def get_samples_aggregate(
424427 end = None ,
425428 aggregation_period = None ,
426429 aggregation_function = None ,
427- max_page_size = None ,
430+ max_page_size = _DEFAULT_MAX_PAGE_SIZE ,
428431 ):
429432 """
430433 Retrieve a series from DataReservoir.io using the samples/aggregate endpoint.
@@ -440,7 +443,7 @@ def get_samples_aggregate(
440443 Stop time (exclusive) of the aggregated series given as anything
441444 pandas.to_datetime is able to parse. Date must be within the past 90 days.
442445 aggregation_function : str
443- One of "Avg ", "Min ", "Max ", "Stdev ".
446+ One of "mean ", "min ", "max ", "std ".
444447 aggregation_period : str
445448 Used in combination with aggregation function to specify the period for aggregation.
446449 Aggregation period is maximum 24 hours. Values can be in units of h, m, s, ms,
@@ -482,6 +485,9 @@ def get_samples_aggregate(
482485 if aggregation_function in function_translation :
483486 aggregation_function = function_translation [aggregation_function ]
484487
488+ if not aggregation_period [0 ].isnumeric ():
489+ aggregation_period = "1" + aggregation_period
490+
485491 for period_unit in period_translation :
486492 if (
487493 aggregation_period .endswith (period_unit )
@@ -493,18 +499,19 @@ def get_samples_aggregate(
493499 )
494500 break
495501
496- start = pd .to_datetime (start , dayfirst = True , unit = "ns" , utc = True ). isoformat ()
497- end = pd .to_datetime (end , dayfirst = True , unit = "ns" , utc = True ). isoformat ()
502+ start = pd .to_datetime (start , dayfirst = True , unit = "ns" , utc = True )
503+ end = pd .to_datetime (end , dayfirst = True , unit = "ns" , utc = True )
498504
499- params = {}
505+ if start .value >= end .value :
506+ raise ValueError ("Start must be before end." )
500507
501- if max_page_size :
502- params ["maxPageSize" ] = max_page_size
508+ params = {}
503509
510+ params ["maxPageSize" ] = max_page_size
504511 params ["aggregationPeriod" ] = aggregation_period
505512 params ["aggregationFunction" ] = aggregation_function
506- params ["start" ] = start
507- params ["end" ] = end
513+ params ["start" ] = start . isoformat ()
514+ params ["end" ] = end . isoformat ()
508515
509516 next_page_link = f"{ environment .api_base_url } reservoir/timeseries/{ series_id } /samples/aggregate?{ urlencode (params )} "
510517
@@ -535,23 +542,32 @@ def get_samples_aggregate_page(url):
535542 timeout = _TIMEOUT_DEAULT ,
536543 )
537544
545+ progress_bar = tqdm (unit = " pages" , desc = "Downloading aggregate data" )
538546 while next_page_link :
539547 response = get_samples_aggregate_page (next_page_link )
540548 response .raise_for_status ()
541549 response_json = response .json ()
542550 next_page_link = response_json .get ("@odata.nextLink" , None )
543551
544552 content = [
545- (pd .to_datetime (sample ["Timestamp" ], utc = True ), sample ["Value" ])
553+ (
554+ pd .to_datetime (sample ["Timestamp" ], unit = "ns" , utc = True ),
555+ sample ["Value" ],
556+ )
546557 for sample in response_json ["value" ]
547558 ]
548559
560+ # update the progress bar
561+ if content :
562+ progress_bar .update (1 )
563+
549564 new_df = pd .DataFrame (
550565 content , columns = ("index" , "values" ), copy = False
551566 ).astype ({"values" : "float64" }, errors = "ignore" )
552567
553568 df = pd .concat ([df , new_df ])
554569
570+ progress_bar .close ()
555571 series = df .set_index ("index" ).squeeze ("columns" ).copy (deep = True )
556572
557573 return series
0 commit comments