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.
69 lines
1.5 KiB
Go
69 lines
1.5 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/LazyBachelor/LazyPM/internal/models"
|
|
"github.com/LazyBachelor/LazyPM/internal/storage"
|
|
"github.com/steveyegge/beads"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
)
|
|
|
|
type App = models.App
|
|
type Config = models.Config
|
|
|
|
func New(ctx context.Context, config Config, opts ...Option) (*App, func(), error) {
|
|
b := defaultBuilder(ctx, config)
|
|
|
|
for _, opt := range opts {
|
|
if err := opt(b); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
}
|
|
|
|
if !config.AutoInit {
|
|
if err := b.initializer.Init(config.AppDir + "db.db"); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
}
|
|
|
|
if b.issueService == nil {
|
|
sqliteStore, err := beads.NewSQLiteStorage(b.ctx, config.AppDir+"db.db")
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
b.lifecycle.Add(func() { sqliteStore.Close() })
|
|
|
|
b.issueService, err = storage.NewBeadsIssueStorage(b.ctx, sqliteStore, config.IssuePrefix)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
}
|
|
|
|
if b.statsService == nil {
|
|
statStore := storage.NewJsonStorage(config.StatisticsStoragePath, &models.Statistics{
|
|
ID: primitive.NewObjectID(),
|
|
StartTime: time.Now(),
|
|
})
|
|
|
|
jsonStatService, err := NewStatisticsService(statStore, b.logger)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
b.statsService = jsonStatService
|
|
}
|
|
|
|
app := &App{
|
|
Config: config,
|
|
Logger: b.logger,
|
|
|
|
Issues: b.issueService,
|
|
Stats: b.statsService,
|
|
ActionLogger: config.ActionLogger,
|
|
}
|
|
|
|
return app, b.lifecycle.Close, nil
|
|
}
|