Sort by timestamp for get_metrics_statistic boto3 python

I have been trying to get metrics from AWS through boto3 get_metric_statistic, but based on their documentation, it is said that data point will not be output in chronological order.

Therefore, the only way here is to sort by myself, but I am facing some trouble and would like to seek for help. I tried to clean my data as much as I could into a list as shown below.

LatencyList = [[datetime.datetime(2020, 7, 12, 16, 0, tzinfo=tzutc()), 1774.8832250541395, [datetime.datetime(2020, 7, 6, 16, 0, tzinfo=tzutc()), 1636.6231504945638],  [datetime.datetime(2020, 7, 9, 16, 0, tzinfo=tzutc()), 1872.890265292699],  [datetime.datetime(2020, 7, 10, 16, 0, tzinfo=tzutc()), 1993.080265911609],  [datetime.datetime(2020, 7, 7, 16, 0, tzinfo=tzutc()), 1613.9198443579767],  [datetime.datetime(2020, 7, 11, 16, 0, tzinfo=tzutc()), 1785.7875248218666],  [datetime.datetime(2020, 7, 8, 16, 0, tzinfo=tzutc()), 1685.3907645207926]]] 

Is there any way I can sort this list into chronological order? Thank you very much.

I tried using sort() or sorted(), by they didn’t work.

Below is a sample of my code:

LatencyList = [] response = get_metrics_statistic()  ##I didn't paste my full code for metrics here, but I managed to output from this function  for item in response["Datapoints"]:     Average = item["Average"]     Time = item['Timestamp']     Latency = [Time,Average]     LatencyList.append(Latency) 
Add Comment
1 Answer(s)

You can pass a custom key which tells sort what to sort upon. This would be something like: LatencyList.sort(key=itemgetter(0)).

Answered on July 16, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.