From 271b482392f63110a2d6b331cead7766c2aa3d9d Mon Sep 17 00:00:00 2001 From: Nicolas Bigler Date: Tue, 23 Jan 2024 15:45:57 +0100 Subject: [PATCH] Fix wrong timeRange for cloudscale.ch bucket metrics cloudscale.ch counts the day from midnight to midnight in the Europe/Zurich timezone (see: https://github.com/cloudscale-ch/cloudscale-go-sdk/blob/v2.1.0/metrics.go#L15-L19) However, we for us in odoo the day is from midnight to midnight in UTC. We therefore can't use the Start and End times provided in the resulting metric, but instead set it ourselves, to the correct midnight time of UTC for the desired day. Signed-off-by: Nicolas Bigler --- pkg/cloudscale/objectstorage.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/pkg/cloudscale/objectstorage.go b/pkg/cloudscale/objectstorage.go index 6f9467e..b882c4f 100644 --- a/pkg/cloudscale/objectstorage.go +++ b/pkg/cloudscale/objectstorage.go @@ -95,7 +95,7 @@ func (o *ObjectStorage) GetMetrics(ctx context.Context, billingDate time.Time) ( continue } } - records, err := o.createOdooRecord(bucketMetricsData, bd, appuioManaged, salesOrder) + records, err := o.createOdooRecord(bucketMetricsData, bd, appuioManaged, salesOrder, billingDate) if err != nil { logger.Error(err, "unable to create Odoo Record", "namespace", bd.Namespace) continue @@ -107,7 +107,7 @@ func (o *ObjectStorage) GetMetrics(ctx context.Context, billingDate time.Time) ( return allRecords, nil } -func (o *ObjectStorage) createOdooRecord(bucketMetricsData cloudscale.BucketMetricsData, b BucketDetail, appuioManaged bool, salesOrder string) ([]odoo.OdooMeteredBillingRecord, error) { +func (o *ObjectStorage) createOdooRecord(bucketMetricsData cloudscale.BucketMetricsData, b BucketDetail, appuioManaged bool, salesOrder string, billingDate time.Time) ([]odoo.OdooMeteredBillingRecord, error) { if len(bucketMetricsData.TimeSeries) != 1 { return nil, fmt.Errorf("there must be exactly one metrics data point, found %d", len(bucketMetricsData.TimeSeries)) } @@ -134,6 +134,9 @@ func (o *ObjectStorage) createOdooRecord(bucketMetricsData cloudscale.BucketMetr instanceId := fmt.Sprintf("%s/%s", b.Zone, bucketMetricsData.Subject.BucketName) + billingStart := time.Date(billingDate.Year(), billingDate.Month(), billingDate.Day(), 0, 0, 0, 0, time.UTC) + billingEnd := time.Date(billingDate.Year(), billingDate.Month(), billingDate.Day()+1, 0, 0, 0, 0, time.UTC) + return []odoo.OdooMeteredBillingRecord{ { ProductID: productIdStorage, @@ -144,8 +147,8 @@ func (o *ObjectStorage) createOdooRecord(bucketMetricsData cloudscale.BucketMetr UnitID: o.uomMapping[units[productIdStorage]], ConsumedUnits: storageBytesValue, TimeRange: odoo.TimeRange{ - From: bucketMetricsData.TimeSeries[0].Start, - To: bucketMetricsData.TimeSeries[0].End, + From: billingStart, + To: billingEnd, }, }, { @@ -157,8 +160,8 @@ func (o *ObjectStorage) createOdooRecord(bucketMetricsData cloudscale.BucketMetr UnitID: o.uomMapping[units[productIdTrafficOut]], ConsumedUnits: trafficOutValue, TimeRange: odoo.TimeRange{ - From: bucketMetricsData.TimeSeries[0].Start, - To: bucketMetricsData.TimeSeries[0].End, + From: billingStart, + To: billingEnd, }, }, { @@ -170,8 +173,8 @@ func (o *ObjectStorage) createOdooRecord(bucketMetricsData cloudscale.BucketMetr UnitID: o.uomMapping[units[productIdQueryRequests]], ConsumedUnits: queryRequestsValue, TimeRange: odoo.TimeRange{ - From: bucketMetricsData.TimeSeries[0].Start, - To: bucketMetricsData.TimeSeries[0].End, + From: billingStart, + To: billingEnd, }, }, }, nil