Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reworking metrics query #2004

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 65 additions & 1 deletion backend/services/mgmt/v1alpha1/metrics-service/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
if !ok {
return nil, fmt.Errorf("unable to convert query response to model.Matrix, received type: %T", queryResponse)
}
usage, err := getDailyUsageFromMatrix(matrix)
usage, err := getDailyUsageFromMatrix2(matrix)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -250,7 +250,71 @@
}
}

func getDailyUsageFromMatrix2(matrix model.Matrix) ([]*mgmtv1alpha1.DayResult, error) {
dailyTotals := map[string]float64{}

for _, stream := range matrix {
values := stream.Values
if len(values) == 1 {
ts := values[0].Timestamp.Time()
day := ts.Format("2006-01-02")
dailyTotals[day] += float64(values[0].Value)
} else {
prevValue := float64(values[0].Value)
prevTs := values[0].Timestamp.Time()
prevDay := prevTs.Format("2006-01-02")

for i := 1; i < len(values); i++ {
currentValue := float64(values[i].Value)
currentTs := values[i].Timestamp.Time()
currentDay := currentTs.Format("2006-01-02")

if currentDay == prevDay {
dailyTotals[currentDay] += currentValue - prevValue
} else {
dailyTotals[currentDay] += currentValue
}
prevValue = currentValue
prevDay = currentDay
}
}
}

var dates []string
for day := range dailyTotals {
dates = append(dates, day)
}
sort.Strings(dates)

output := []*mgmtv1alpha1.DayResult{}
for _, day := range dates {
date, err := parseDateString(day)
if err != nil {
return nil, err
}
output = append(output, &mgmtv1alpha1.DayResult{
Date: date,
Count: uint64(dailyTotals[day]),
})
}
return output, nil
}

func parseDateString(dateStr string) (*mgmtv1alpha1.Date, error) {
const layout = "2006-01-02"
t, err := time.Parse(layout, dateStr)
if err != nil {
return nil, err
}

return &mgmtv1alpha1.Date{
Year: uint32(t.Year()),
Month: uint32(t.Month()),
Day: uint32(t.Day()),
}, nil
}

func getDailyUsageFromMatrix(matrix model.Matrix) ([]*mgmtv1alpha1.DayResult, error) {

Check failure on line 317 in backend/services/mgmt/v1alpha1/metrics-service/metrics.go

View workflow job for this annotation

GitHub Actions / golang-lint

func `getDailyUsageFromMatrix` is unused (unused)
output := []*mgmtv1alpha1.DayResult{}
for _, stream := range matrix {
var latest int64
Expand Down Expand Up @@ -292,7 +356,7 @@
}

// combines counts where date is the day and returns a squished list with the original order retained
func squishDayResults(input []*mgmtv1alpha1.DayResult) []*mgmtv1alpha1.DayResult {

Check failure on line 359 in backend/services/mgmt/v1alpha1/metrics-service/metrics.go

View workflow job for this annotation

GitHub Actions / golang-lint

func `squishDayResults` is unused (unused)
dayMap := map[string]uint64{}
for _, result := range input {
dayMap[toDateKey(result.GetDate())] += result.GetCount()
Expand All @@ -308,11 +372,11 @@
return output
}

func ptr[T any](val T) *T {

Check failure on line 375 in backend/services/mgmt/v1alpha1/metrics-service/metrics.go

View workflow job for this annotation

GitHub Actions / golang-lint

func `ptr` is unused (unused)
return &val
}

func timeToDate(t time.Time) mgmtv1alpha1.Date {

Check failure on line 379 in backend/services/mgmt/v1alpha1/metrics-service/metrics.go

View workflow job for this annotation

GitHub Actions / golang-lint

func `timeToDate` is unused (unused)
return mgmtv1alpha1.Date{
Year: uint32(t.Year()),
Month: uint32(t.Month()),
Expand All @@ -320,7 +384,7 @@
}
}

func toDateKey(day *mgmtv1alpha1.Date) string {

Check failure on line 387 in backend/services/mgmt/v1alpha1/metrics-service/metrics.go

View workflow job for this annotation

GitHub Actions / golang-lint

func `toDateKey` is unused (unused)
return fmt.Sprintf("%d_%d_%d", day.Day, day.Month, day.Year)
}

Expand Down
Loading