cloudwatch.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:gramola 作者: pfreixes 项目源码 文件源码
def datapoints(self, query, maxdatapoints=None):
        if query.statistics and (query.statistics not in ['Average', 'Sum', 'SampleCount',
                                                          'Maximum', 'Minimum']):
            raise InvalidMetricQuery("Query statistic invalid value `{}`".format(query.statistics))
        elif query.statistics:
            statistics = query.statistics
        else:
            statistics = "Average"

        if maxdatapoints:
            # Calculate the Period where the number of datapoints
            # returned are less than maxdatapoints.

            # Get the first granularity that suits for return the maxdatapoints
            seconds = (query.get_until() - query.get_since()).total_seconds()
            period = next(dropwhile(lambda g: seconds / g > maxdatapoints, count(60, 60)))
        else:
            period = 60

        # get a client using the region given by the query, or if it
        # is None using the one given by the datasource or the profile
        client = self._cw_client(region=query.region)

        kwargs = {
            'Namespace': query.namespace,
            'MetricName': query.metricname,
            'StartTime': query.get_since(),
            'EndTime': query.get_until(),
            'Period': period,
            'Dimensions': [{
                'Name': query.dimension_name,
                'Value': query.dimension_value,
            }],
            'Statistics': [statistics]
        }

        datapoints = self._cw_call(client, "get_metric_statistics", **kwargs)
        return [(point[statistics], time.mktime(point['Timestamp'].timetuple()))
                for point in datapoints['Datapoints']]
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号