Skip to content
Open
Changes from 2 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
35 changes: 33 additions & 2 deletions data/summary/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
"fmt"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
Expand All @@ -15,6 +18,17 @@ import (
storeStructuredMongo "github.com/tidepool-org/platform/store/structured/mongo"
)

var (
QueueLag = promauto.NewHistogramVec(prometheus.HistogramOpts{
Comment thread
Roukoswarf marked this conversation as resolved.
Name: "tidepool_summary_queue_lag",
Help: "The current queue lag in minutes",
}, []string{"type"})
QueueLength = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "tidepool_summary_queue_length",
Help: "The current queue length in number of summaries",
}, []string{"type"})
)

type Repo[A types.StatsPt[T], T types.Stats] struct {
*storeStructuredMongo.Repository
}
Expand Down Expand Up @@ -213,6 +227,14 @@ func (r *Repo[A, T]) SetOutdated(ctx context.Context, userId, reason string) (*t
return userSummary.Dates.OutdatedSince, nil
}

func (r *Repo[T, A]) GetSummaryQueueLength(ctx context.Context) (int64, error) {
selector := bson.M{
"type": types.GetTypeString[T, A](),
"dates.outdatedSince": bson.M{"$lte": time.Now().UTC()},
}
return r.CountDocuments(ctx, selector)
}

func (r *Repo[T, A]) GetOutdatedUserIDs(ctx context.Context, page *page.Pagination) (*types.OutdatedSummariesResponse, error) {
if ctx == nil {
return nil, errors.New("context is missing")
Expand All @@ -221,8 +243,10 @@ func (r *Repo[T, A]) GetOutdatedUserIDs(ctx context.Context, page *page.Paginati
return nil, errors.New("pagination is missing")
}

typ := types.GetTypeString[T, A]()

selector := bson.M{
"type": types.GetTypeString[T, A](),
"type": typ,
"dates.outdatedSince": bson.M{"$lte": time.Now().UTC()},
}

Expand All @@ -231,7 +255,7 @@ func (r *Repo[T, A]) GetOutdatedUserIDs(ctx context.Context, page *page.Paginati
{Key: "dates.outdatedSince", Value: 1},
})
opts.SetLimit(int64(page.Size))
opts.SetProjection(bson.M{"stats": 0})
opts.SetProjection(bson.M{"userId": 1, "dates": 1})

cursor, err := r.Find(ctx, selector, opts)
if err != nil {
Expand Down Expand Up @@ -260,6 +284,13 @@ func (r *Repo[T, A]) GetOutdatedUserIDs(ctx context.Context, page *page.Paginati
response.End = *userSummary.Dates.OutdatedSince
}

QueueLag.WithLabelValues(typ).Observe(time.Now().UTC().Sub(response.Start).Minutes())
Comment thread
Roukoswarf marked this conversation as resolved.
Outdated
count, err := r.GetSummaryQueueLength(ctx)
if err != nil {
return nil, fmt.Errorf("unable to get summary queue length: %w", err)
}
QueueLength.WithLabelValues(typ).Set(float64(count))

return response, nil
}

Expand Down