def _get_points_proto(points):
    protos = []
    for point in points:
        proto = metrics_pb2.Point(
            timestamp=utils.proto_ts_from_datetime(point.timestamp))

        if isinstance(point.value, value.ValueLong):
            proto.int64_value = int(point.value.value)
        elif isinstance(point.value, value.ValueDouble):
            proto.double_value = float(point.value.value)
        elif isinstance(point.value, value.ValueDistribution):
            proto.distribution_value.MergeFrom(
                metrics_pb2.DistributionValue(
                    sum=point.value.sum,
                    count=point.value.count,
                    sum_of_squared_deviation=point.value.
                    sum_of_squared_deviation,
                    bucket_options=_get_bucket_options_proto(
                        point.value.bucket_options)
                    if point.value.bucket_options else None,
                    buckets=_get_buckets_proto(point.value.buckets)))

        # TODO: handle SUMMARY metrics, #567
        else:  # pragma: NO COVER
            raise TypeError('Unsupported metric type: {}'.format(
                type(point.value)))
        protos.append(proto)
    return protos
    def test_datetime_to_proto_ts_conversion(self):
        now = datetime.utcnow()
        delta = now - datetime(1970, 1, 1)
        expected_seconds = int(delta.total_seconds())
        expected_nanos = delta.microseconds * 1000

        proto_ts = utils.proto_ts_from_datetime(now)
        self.assertEqual(proto_ts.seconds, int(expected_seconds))
        self.assertEqual(proto_ts.nanos, expected_nanos)
    def test_datetime_to_proto_ts_conversion_zero(self):
        zero = datetime(1970, 1, 1)

        proto_ts = utils.proto_ts_from_datetime(zero)
        self.assertEqual(proto_ts.seconds, 0)
        self.assertEqual(proto_ts.nanos, 0)
 def test_datetime_to_proto_ts_conversion_none(self):
     proto_ts = utils.proto_ts_from_datetime(None)
     self.assertEqual(proto_ts.seconds, 0)
     self.assertEqual(proto_ts.nanos, 0)