Skip to content

Commit

Permalink
refactor: update metric api to cumulate task count after db purge
Browse files Browse the repository at this point in the history
- increment the task result count everytime the api is called instead of fetch from TaskResult table
- use Redis INCR for atomic increments that work across all instances
  • Loading branch information
codebender37 committed Dec 3, 2024
1 parent 2bf5131 commit 080de18
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
46 changes: 39 additions & 7 deletions pkg/metric/metrics_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package metric

import (
"context"
"encoding/json"

"dojo-api/db"
"dojo-api/pkg/cache"
"dojo-api/pkg/event"
"dojo-api/pkg/orm"
"encoding/json"

"github.com/redis/go-redis/v9"
"github.com/rs/zerolog/log"
)

Expand Down Expand Up @@ -53,19 +56,48 @@ func (metricService *MetricService) UpdateCompletedTaskCount(ctx context.Context
}

func (metricService *MetricService) UpdateTotalTaskResultsCount(ctx context.Context) error {
taskResultORM := orm.NewTaskResultORM()
cache := cache.GetCacheInstance()
metricORM := orm.NewMetricsORM()

completedTResultCount, err := taskResultORM.GetCompletedTResultCount(ctx)
cacheKey := "metrics:task_results:total"

// Try to get current count from Redis
_, err := cache.Redis.Get(ctx, cacheKey).Int64()
if err == redis.Nil { // Key doesn't exist (e.g., after Redis restart)
// Get the last metric from database
lastMetric, err := metricORM.GetMetricsDataByMetricType(ctx, db.MetricsTypeTotalNumTaskResults)
if err != nil && !db.IsErrNotFound(err) {
return err
}

// Initialize Redis with the last known count from database
if lastMetric != nil {
var lastMetricData MetricTaskResultsCount
if err := json.Unmarshal(lastMetric.MetricsData, &lastMetricData); err != nil {
return err
}
currentCount := int64(lastMetricData.TotalNumTasksResults)
// Set the Redis counter to last known value
if err := cache.Redis.Set(ctx, cacheKey, currentCount, 0).Err(); err != nil {
return err
}
}
} else if err != nil {
return err
}

// Increment the counter
count, err := cache.Redis.Incr(ctx, cacheKey).Result()
if err != nil {
log.Error().Err(err).Msg("Failed to get completed task result count")
log.Error().Err(err).Msg("Failed to increment task results count")
return err
}
newMetricData := MetricTaskResultsCount{TotalNumTasksResults: completedTResultCount}

// Store in database
newMetricData := MetricTaskResultsCount{TotalNumTasksResults: int(count)}
log.Info().Interface("TotalTaskResultsCount", newMetricData).Msg("Updating total task results count metric")

err = metricORM.CreateNewMetric(ctx, db.MetricsTypeTotalNumTaskResults, newMetricData)
return err
return metricORM.CreateNewMetric(ctx, db.MetricsTypeTotalNumTaskResults, newMetricData)
}

func (metricService *MetricService) UpdateAvgTaskCompletionTime(ctx context.Context) error {
Expand Down
3 changes: 2 additions & 1 deletion pkg/orm/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package orm

import (
"context"
"dojo-api/db"
"encoding/json"
"time"

"dojo-api/db"

"github.com/rs/zerolog/log"
)

Expand Down

0 comments on commit 080de18

Please sign in to comment.