mirror of
https://github.com/tiennm99/miti99bot.git
synced 2026-07-27 10:20:05 +00:00
52 lines
1.4 KiB
Go
52 lines
1.4 KiB
Go
package stats
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
"go.mongodb.org/mongo-driver/v2/mongo"
|
|
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
|
|
|
"github.com/tiennm99/miti99bot/internal/storage"
|
|
)
|
|
|
|
const (
|
|
statsCommandUsersIndexName = "stats_cmd_n_user"
|
|
statsUserCommandsIndexName = "stats_uid_n_cmd"
|
|
statsUsernameLookupIndexName = "stats_user_uid"
|
|
)
|
|
|
|
// InitStore performs stats collection startup maintenance. It is safe to call
|
|
// every boot: MongoDB indexes are created idempotently and memory storage is a
|
|
// no-op.
|
|
func InitStore(ctx context.Context, statsColl storage.Collection) error {
|
|
if mongoColl, ok := storage.MongoCollection(statsColl); ok {
|
|
if err := ensureUsageIndexes(ctx, mongoColl); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ensureUsageIndexes(ctx context.Context, coll *mongo.Collection) error {
|
|
models := []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{bsonField("cmd", 1), bsonField("n", -1), bsonField("user", 1)},
|
|
Options: options.Index().SetName(statsCommandUsersIndexName),
|
|
},
|
|
{
|
|
Keys: bson.D{bsonField("uid", 1), bsonField("n", -1), bsonField("cmd", 1)},
|
|
Options: options.Index().SetName(statsUserCommandsIndexName),
|
|
},
|
|
{
|
|
Keys: bson.D{bsonField("user", 1), bsonField("uid", 1)},
|
|
Options: options.Index().SetName(statsUsernameLookupIndexName),
|
|
},
|
|
}
|
|
if _, err := coll.Indexes().CreateMany(ctx, models); err != nil {
|
|
return fmt.Errorf("stats indexes: %w", err)
|
|
}
|
|
return nil
|
|
}
|