Skip to content

Commit

Permalink
[DDO-3272] Move metrics code out of deprecated folders (#343)
Browse files Browse the repository at this point in the history
Co-authored-by: Katie Welch <[email protected]>
  • Loading branch information
jack-r-warren and katiewelch authored Oct 31, 2023
1 parent dcbaed3 commit 7b97747
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 64 deletions.
12 changes: 6 additions & 6 deletions sherlock/internal/boot/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"github.com/broadinstitute/sherlock/sherlock/internal/config"
"github.com/broadinstitute/sherlock/sherlock/internal/db"
"github.com/broadinstitute/sherlock/sherlock/internal/deployhooks"
"github.com/broadinstitute/sherlock/sherlock/internal/deprecated_models/v2models"
"github.com/broadinstitute/sherlock/sherlock/internal/github"
"github.com/broadinstitute/sherlock/sherlock/internal/metrics"
"github.com/broadinstitute/sherlock/sherlock/internal/models"
"github.com/broadinstitute/sherlock/sherlock/internal/slack"
"github.com/rs/zerolog/log"
"gorm.io/gorm"
Expand Down Expand Up @@ -94,15 +94,15 @@ func (a *Application) Start() {
if config.Config.Bool("metrics.v2.enable") {
log.Info().Msgf("BOOT | registering metric views...")
if err = metrics.RegisterViews(); err != nil {
log.Fatal().Msgf("v2metrics.RegisterViews() err: %v", err)
log.Fatal().Msgf("metrics.RegisterViews() err: %v", err)
}

log.Info().Msgf("BOOT | calculating metric values...")
if err = v2models.UpdateMetrics(ctx, a.gormDB); err != nil {
log.Fatal().Msgf("v2models.UpdateMetrics() err: %v", err)
if err = models.UpdateMetrics(ctx, a.gormDB); err != nil {
log.Fatal().Msgf("models.UpdateMetrics() err: %v", err)
}

go v2models.KeepMetricsUpdated(ctx, a.gormDB)
go models.KeepMetricsUpdated(ctx, a.gormDB)
}

if config.Config.Bool("slack.enable") {
Expand All @@ -116,7 +116,7 @@ func (a *Application) Start() {
if config.Config.Bool("github.enable") {
log.Info().Msgf("BOOT | initializing GitHub client...")
if err = github.Init(ctx); err != nil {
log.Fatal().Msgf("githubz.Init() err: %v", err)
log.Fatal().Msgf("github.Init() err: %v", err)
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v2models
package models

import (
"context"
Expand Down Expand Up @@ -189,7 +189,7 @@ group by result_per_version.chart_release_id
}

func reportDataTypeCounts(ctx context.Context, db *gorm.DB) error {
for dataType, model := range map[string]Model{
for dataType, model := range map[string]any{
"chart": Chart{},
"environment": Environment{},
"cluster": Cluster{},
Expand Down Expand Up @@ -359,7 +359,10 @@ func UpdateMetrics(ctx context.Context, db *gorm.DB) error {
return err
}

charts, err := InternalChartStore.ListAllMatchingByUpdated(db, 0, &Chart{})
var charts []Chart
if err = db.Model(&Chart{}).Order("updated_at desc").Find(&charts).Error; err != nil {
return err
}
if err != nil {
return err
}
Expand Down Expand Up @@ -392,7 +395,10 @@ func UpdateMetrics(ctx context.Context, db *gorm.DB) error {
stats.Record(ctx, metrics.AppVersionCountMeasure.M(count))
}

chartReleases, err := InternalChartReleaseStore.ListAllMatchingByUpdated(db, 0, &ChartRelease{ChartID: chart.ID})
var chartReleases []ChartRelease
if err = db.Model(&ChartRelease{}).Where(&ChartRelease{ChartID: chart.ID}).Order("updated_at desc").Find(&chartReleases).Error; err != nil {
return err
}
if err != nil {
return err
}
Expand Down Expand Up @@ -462,7 +468,7 @@ func UpdateMetrics(ctx context.Context, db *gorm.DB) error {
}

lastUpdateTime = time.Now()
log.Info().Msgf("MTRC | v2 metrics updated, took %s", lastUpdateTime.Sub(updateStartTime).String())
log.Info().Msgf("MTRC | metrics updated, took %s", lastUpdateTime.Sub(updateStartTime).String())
return nil
}

Expand All @@ -471,7 +477,7 @@ func KeepMetricsUpdated(ctx context.Context, db *gorm.DB) {
for {
time.Sleep(interval)
if err := UpdateMetrics(ctx, db); err != nil {
log.Warn().Err(err).Msgf("failed to update v2 metrics, now %s stale", time.Since(lastUpdateTime).String())
log.Warn().Err(err).Msgf("MTRC | failed to update metrics, now %s stale", time.Since(lastUpdateTime).String())
}
}
}
11 changes: 11 additions & 0 deletions sherlock/internal/models/metrics_cron_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package models

import (
"context"
)

func (s *modelSuite) TestUpdateMetrics() {
s.Run("doesn't error when running custom SQL", func() {
s.NoError(UpdateMetrics(context.Background(), s.DB))
})
}

0 comments on commit 7b97747

Please sign in to comment.