Skip to content

Commit

Permalink
Fix wrong timeRange for cloudscale.ch bucket metrics
Browse files Browse the repository at this point in the history
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 <nicolas.bigler@vshn.ch>
  • Loading branch information
TheBigLee committed Jan 23, 2024
1 parent 76d8969 commit 271b482
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions pkg/cloudscale/objectstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
}
Expand All @@ -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,
Expand All @@ -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,
},
},
{
Expand All @@ -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,
},
},
{
Expand All @@ -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
Expand Down

0 comments on commit 271b482

Please sign in to comment.