【问题标题】:AWS Cloudwatch PHP SDK returning unsorted metrics statisticsAWS Cloudwatch PHP SDK 返回未排序的指标统计信息
【发布时间】:2014-04-17 06:56:20
【问题描述】:

我目前正在使用 aws php sdk 提取 CloudWatch 指标统计信息。

不幸的是,getMetricStatistics 方法正在返回未排序的结果。我想知道为什么它没有按 Timestamp ASC 排序。有谁知道如何从 Cloudwatch 获取排序结果?

我的代码

$result = $this->client->getMetricStatistics(array(
        'Namespace'  => 'My.Namespace',
        'MetricName' => 'my-metric',
        'StartTime'  => strtotime('-1 days'),
        'EndTime'    => strtotime('now'),
        'Period'     => 300,
        'Statistics' => array('Average'),
));

结果

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)

感谢您的帮助!

J.

更新

我仍然没有设法直接从方法调用中获得排序结果。所以我按照@Svenskunganka 的建议使用了 usort

usort($result['Datapoints'], function($a, $b) {
    if($a['Timestamp'] == $b['Timestamp']) {
        return 0;
    }
    return ($a['Timestamp'] < $b['Timestamp']) ? -1 : 1;
});

我仍在寻找获得排序结果的完美方法。如果有人有线索,我将非常感激。

【问题讨论】:

  • 您是否尝试过使用usort() 功能?这是另一个 question 中的一个很好的例子
  • 确定我可以在之后使用 usort 对数组进行排序,但是当有可能直接从方法调用中获得排序结果时,这会好得多
  • 据我所知,getMetricStatistics 函数不带任何其他参数。虽然奇怪的是,当您想要在给定时间范围内获取数据时,它会返回一个未排序的数组。您最好的选择是使用usort 函数。

标签: php amazon-web-services amazon-cloudwatch cloudwatch


【解决方案1】:

无法从 GetMetricStatistics 获得排序结果,您必须自己对数据点进行排序。

“GetMetricStatistics 不按时间顺序返回数据。”

来源:http://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_GetMetricStatistics.html

【讨论】:

    猜你喜欢
    • 2017-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多