Skip to content

Commit

Permalink
fixing timestamp offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Jul 5, 2024
1 parent 08e72e8 commit b861285
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions pkg/cmd/spks.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ func SpksCMD(allMetrics map[string]map[string]prometheus.Counter, ctx context.Co

ticker := time.NewTicker(24 * time.Hour)

runSPKSBilling(prometheusURL, prometheusQueryArr, logger, allMetrics, salesOrder, UnitID, c.Context)
daysChannel := make(chan int, 1)
if days != 0 {
daysChannel <- days
} else {
runSPKSBilling(prometheusURL, prometheusQueryArr, logger, allMetrics, salesOrder, UnitID, c.Context)
}

for {
select {
Expand All @@ -74,32 +79,33 @@ func SpksCMD(allMetrics map[string]map[string]prometheus.Counter, ctx context.Co
case <-ticker.C:
// this runs every 24 hours after program start
runSPKSBilling(prometheusURL, prometheusQueryArr, logger, allMetrics, salesOrder, UnitID, c.Context)
case <-daysChannel:
runSPKSBilling(prometheusURL, prometheusQueryArr, logger, allMetrics, salesOrder, UnitID, c.Context)
if days > 0 {
days--
daysChannel <- days
}
}
}
},
}
}

func runSPKSBilling(prometheusURL string, prometheusQueryArr [4]string, logger logr.Logger, allMetrics map[string]map[string]prometheus.Counter, salesOrder string, UnitID string, c context.Context) {
var startYesterdayAbsolute time.Time
// var startYesterdayAbsolute time.Time
location, err := time.LoadLocation("Europe/Zurich")
if err != nil {
allMetrics["odooMetrics"]["odooFailed"].Inc()
}
now := time.Now().In(location)
// this variable is necessary to query Prometheus, with timerange [1d:1d] it returns data from 1 day up to midnight
startOfToday := time.Date(now.Year(), now.Month(), now.Day()-days, 0, 0, 0, 0, location)

if days != 0 {
startYesterdayAbsolute = time.Date(now.Year(), now.Month(), now.Day()-days, 0, 0, 0, 0, location).In(time.UTC)
} else {
startYesterdayAbsolute = time.Date(now.Year(), now.Month(), now.Day()-1, 0, 0, 0, 0, location).In(time.UTC)
}
startYesterdayAbsolute := time.Date(now.Year(), now.Month(), now.Day()-days-1, 0, 0, 0, 0, location).In(time.UTC)

endYesterdayAbsolute := startYesterdayAbsolute.Add(24 * time.Hour)

logger.Info("Running SPKS billing with such timeranges: ", "startOfToday", startOfToday, "startYesterdayAbsolute", startYesterdayAbsolute.Local(), "endYesterdayAbsolute", endYesterdayAbsolute.Local())
return

odooClient := odoo.NewOdooAPIClient(c, odooURL, odooOauthTokenURL, odooClientId, odooClientSecret, logger, allMetrics["odooMetrics"])

mariadbStandard, mariadbPremium, redisStandard, redisPremium, err := getDatabasesCounts(prometheusURL, prometheusQueryArr, logger, startOfToday, allMetrics)
Expand Down

0 comments on commit b861285

Please sign in to comment.