I am currently extracting CloudWatch metrics statistics with the aws php sdk.
Unfortunately the getMetricStatistics method is returning unsorted results. I wonder why it is not sorted by Timestamp ASC. Does anyone know how to get sorted results from Cloudwatch?
my code
$result = $this->client->getMetricStatistics(array(
'Namespace' => 'My.Namespace',
'MetricName' => 'my-metric',
'StartTime' => strtotime('-1 days'),
'EndTime' => strtotime('now'),
'Period' => 300,
'Statistics' => array('Average'),
));
and the result
array (size=7)
0 =>
array (size=3)
'Timestamp' => string '2014-04-16T10:53:00Z' (length=20)
'Unit' => string 'Count' (length=5)
'Average' => string '9.998594711316002' (length=17)
1 =>
array (size=3)
'Timestamp' => string '2014-04-16T11:43:00Z' (length=20)
'Unit' => string 'Count' (length=5)
'Average' => string '0.7908148450858722' (length=18)
2 =>
array (size=3)
'Timestamp' => string '2014-04-16T11:08:00Z' (length=20)
'Unit' => string 'Count' (length=5)
'Average' => string '5.402251656252796' (length=17)
3 =>
array (size=3)
'Timestamp' => string '2014-04-16T11:03:00Z' (length=20)
'Unit' => string 'Count' (length=5)
'Average' => string '8.958888493053081' (length=17)
Thanks for your help!
J.
I still did not manage to get sorted results directly from the method call. So I used usort as suggested by @Svenskunganka
usort($result['Datapoints'], function($a, $b) {
if($a['Timestamp'] == $b['Timestamp']) {
return 0;
}
return ($a['Timestamp'] < $b['Timestamp']) ? -1 : 1;
});
I am still looking for the perfect way to get sorted results. If anyone has a clue, I would be really thankful.
There is no way to get sorted results from GetMetricStatistics, you have to sort the datapoints yourself.
"GetMetricStatistics does not return the data in chronological order."
Source: http://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_GetMetricStatistics.html