Skip to content
This repository has been archived by the owner on Jan 19, 2023. It is now read-only.

Commit

Permalink
Fix: Queries must be product specific, otherwise units don't work
Browse files Browse the repository at this point in the history
  • Loading branch information
David Gubler committed Jul 27, 2022
1 parent 382c8a0 commit 64694da
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var (
Source: sourceQueryRequests + ":" + sourceZones[0],
Target: sql.NullString{String: "1405", Valid: true},
Amount: 0.005,
Unit: "1k Requests",
Unit: "KReq",
During: db.InfiniteRange(),
},
}
Expand All @@ -84,12 +84,26 @@ var (
},
}

queryData = []*db.Query{
queriesData = []*db.Query{
{
Name: "Dummy",
Description: "Dummy query for facts without queries",
Name: sourceQueryStorage + ":" + sourceZones[0],
Description: "S3 Storage",
Query: "",
Unit: "",
Unit: "GB/day",
During: db.InfiniteRange(),
},
{
Name: sourceQueryTrafficOut + ":" + sourceZones[0],
Description: "S3 Traffic Out",
Query: "",
Unit: "GB",
During: db.InfiniteRange(),
},
{
Name: sourceQueryRequests + ":" + sourceZones[0],
Description: "S3 Requests",
Query: "",
Unit: "KReq",
During: db.InfiniteRange(),
},
}
Expand Down Expand Up @@ -126,7 +140,7 @@ func initDb(ctx context.Context, tx *sqlx.Tx) error {
}
}

for _, query := range queryData {
for _, query := range queriesData {
_, err := queriesmodel.Ensure(ctx, tx, query)
if err != nil {
return err
Expand Down Expand Up @@ -215,16 +229,14 @@ func main() {
dateTime, err = datetimesmodel.Ensure(ctx, tx, dateTime)
checkErrExit(err)

// Find the right query. Since we don't actually query prometheus we just fetch a dummy object.
query, err := queriesmodel.GetByName(ctx, tx, "Dummy")
checkErrExit(err)

if bucketMetricsData.TimeSeries[0].Usage.StorageBytes > 0 {
fmt.Printf("syncing %s\n", sourceStorage)
product, err := productsmodel.GetBestMatch(ctx, tx, sourceStorage, bucketMetricsData.TimeSeries[0].Start)
checkErrExit(err)
discount, err := discountsmodel.GetBestMatch(ctx, tx, sourceStorage, bucketMetricsData.TimeSeries[0].Start)
checkErrExit(err)
query, err := queriesmodel.GetByName(ctx, tx, sourceQueryStorage+":"+zone)
checkErrExit(err)
storageQuantity := float64(bucketMetricsData.TimeSeries[0].Usage.StorageBytes) / 1000 / 1000 / 1000
storageFact := factsmodel.New(dateTime, query, tenant, category, product, discount, storageQuantity)
_, err = factsmodel.Ensure(ctx, tx, storageFact)
Expand All @@ -237,6 +249,8 @@ func main() {
checkErrExit(err)
discount, err := discountsmodel.GetBestMatch(ctx, tx, sourceTrafficOut, bucketMetricsData.TimeSeries[0].Start)
checkErrExit(err)
query, err := queriesmodel.GetByName(ctx, tx, sourceQueryTrafficOut+":"+zone)
checkErrExit(err)
trafficOutQuantity := float64(bucketMetricsData.TimeSeries[0].Usage.SentBytes) / 1000 / 1000 / 1000
trafficOutFact := factsmodel.New(dateTime, query, tenant, category, product, discount, trafficOutQuantity)
_, err = factsmodel.Ensure(ctx, tx, trafficOutFact)
Expand All @@ -249,6 +263,8 @@ func main() {
checkErrExit(err)
discount, err := discountsmodel.GetBestMatch(ctx, tx, sourceRequests, bucketMetricsData.TimeSeries[0].Start)
checkErrExit(err)
query, err := queriesmodel.GetByName(ctx, tx, sourceQueryRequests+":"+zone)
checkErrExit(err)
requestsQuantity := float64(bucketMetricsData.TimeSeries[0].Usage.Requests) / 1000
requestsFact := factsmodel.New(dateTime, query, tenant, category, product, discount, requestsQuantity)
_, err = factsmodel.Ensure(ctx, tx, requestsFact)
Expand Down

0 comments on commit 64694da

Please sign in to comment.