Metrics and logs - MongoDB Submission (#43)

Adds MongoDB-backed submission for survey statistics/metrics and introduces MongoDB ObjectID-based participant/task identifiers to support upserts and linking metrics to a participant.

It introduces breaking changes to the exsisting json stat and metric files. Delete the old ones

Changes:

Add internal/storage/MongoStorage and wire pm survey submit / pm start to submit local .pm/*.json files to MongoDB.
Extend stats/metrics models to include MongoDB _id / participant_id fields and add participant ID propagation into task metrics.
Update app initialization to generate ObjectIDs for new participants and expose GetParticipantID() via StatsService.
This commit is contained in:
Robin Olsen
2026-03-10 06:09:31 -07:00
committed by GitHub
parent 549415135c
commit 72a2e8acf5
17 changed files with 426 additions and 197 deletions

View File

@@ -4,6 +4,7 @@ import (
"log/slog"
"github.com/LazyBachelor/LazyPM/internal/models"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type RunLifecycle struct {
@@ -23,9 +24,11 @@ func NewRunLifecycle(app *App, config Config, details models.TaskDetails, iType
collector.recordUserAction(action)
})
var participantID primitive.ObjectID
var store MetricsStore
if config.StatisticsStoragePath != "" {
store = NewFileMetricsStore(config.StatisticsStoragePath, logger)
participantID = app.Stats.GetParticipantID()
store = NewFileMetricsStore(config.StatisticsStoragePath, participantID, logger)
}
return &RunLifecycle{

View File

@@ -10,6 +10,7 @@ import (
"time"
"github.com/LazyBachelor/LazyPM/internal/models"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type MetricsStore interface {
@@ -17,14 +18,16 @@ type MetricsStore interface {
}
type FileMetricsStore struct {
path string
logger *slog.Logger
path string
participantID primitive.ObjectID
logger *slog.Logger
}
func NewFileMetricsStore(path string, logger *slog.Logger) *FileMetricsStore {
func NewFileMetricsStore(path string, participantID primitive.ObjectID, logger *slog.Logger) *FileMetricsStore {
return &FileMetricsStore{
path: path,
logger: logger,
path: path,
participantID: participantID,
logger: logger,
}
}
@@ -41,8 +44,10 @@ func (s *FileMetricsStore) Append(ctx context.Context, taskName string, run mode
}
metrics := models.TaskMetricsFile{
TaskName: taskName,
Runs: []models.TaskRunMetrics{},
ID: primitive.NewObjectID(),
ParticipantID: s.participantID,
TaskName: taskName,
Runs: []models.TaskRunMetrics{},
}
if err := readMetrics(&metrics, s.path); err != nil {