Move darkstorm_backend to just backend
Added MongoDB instances of DB tables Updated some DB interfaces Added logging to count cleanup
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/CalebQ42/darkstorm-server/internal/backend"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
type MongoTable[T backend.IDStruct] struct {
|
||||
col *mongo.Collection
|
||||
}
|
||||
|
||||
func NewMongoTable[T backend.IDStruct](col *mongo.Collection) *MongoTable[T] {
|
||||
return &MongoTable[T]{
|
||||
col: col,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MongoTable[T]) Get(ID string) (data *T, err error) {
|
||||
res := m.col.FindOne(context.Background(), bson.M{"_id": ID})
|
||||
if res.Err() == mongo.ErrNoDocuments {
|
||||
return nil, backend.ErrNotFound
|
||||
} else if res.Err() != nil {
|
||||
return nil, res.Err()
|
||||
}
|
||||
var out T
|
||||
err = res.Decode(&out)
|
||||
return &out, err
|
||||
}
|
||||
|
||||
func (m *MongoTable[T]) Find(values map[string]any) ([]T, error) {
|
||||
res, err := m.col.Find(context.Background(), values)
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return nil, backend.ErrNotFound
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var out []T
|
||||
err = res.All(context.Background(), &out)
|
||||
return out, err
|
||||
}
|
||||
|
||||
func (m *MongoTable[T]) Insert(data T) error {
|
||||
_, err := m.col.InsertOne(context.Background(), data)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *MongoTable[T]) Remove(ID string) error {
|
||||
res := m.col.FindOneAndDelete(context.Background(), bson.M{"_id": ID})
|
||||
return res.Err()
|
||||
}
|
||||
|
||||
func (m *MongoTable[T]) FullUpdate(ID string, data T) error {
|
||||
res := m.col.FindOneAndReplace(context.Background(), bson.M{"_id": ID}, data)
|
||||
if res.Err() == mongo.ErrNoDocuments {
|
||||
return backend.ErrNotFound
|
||||
}
|
||||
return res.Err()
|
||||
}
|
||||
|
||||
func (m *MongoTable[T]) PartUpdate(ID string, update map[string]any) error {
|
||||
res := m.col.FindOneAndUpdate(context.Background(), bson.M{"_id": ID}, update)
|
||||
if res.Err() == mongo.ErrNoDocuments {
|
||||
return backend.ErrNotFound
|
||||
}
|
||||
return res.Err()
|
||||
}
|
||||
|
||||
func (m *MongoTable[CountLog]) RemoveOldLogs(date int) {
|
||||
m.col.DeleteMany(context.Background(), bson.M{"date": bson.M{"$lt": date}})
|
||||
}
|
||||
func (m *MongoTable[CountLog]) Count(platform string) (int, error) {
|
||||
var filter bson.M
|
||||
if platform == "" || platform == "all" {
|
||||
filter = bson.M{}
|
||||
} else {
|
||||
filter = bson.M{"platform": platform}
|
||||
}
|
||||
out, err := m.col.CountDocuments(context.Background(), filter)
|
||||
return int(out), err
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/CalebQ42/darkstorm-server/internal/backend"
|
||||
"github.com/google/uuid"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
type MongoCrashTable struct {
|
||||
*MongoTable[backend.CrashReport]
|
||||
archiveCol *mongo.Collection
|
||||
}
|
||||
|
||||
func NewMongoCrashTable(crashCol *mongo.Collection, archiveCol *mongo.Collection) *MongoCrashTable {
|
||||
return &MongoCrashTable{
|
||||
MongoTable: NewMongoTable[backend.CrashReport](crashCol),
|
||||
archiveCol: archiveCol,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MongoCrashTable) Archive(toArchive backend.ArchivedCrash) error {
|
||||
if toArchive.Platform == "" {
|
||||
toArchive.Platform = "all"
|
||||
}
|
||||
_, err := m.archiveCol.InsertOne(context.Background(), toArchive)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *MongoCrashTable) IsArchived(ind backend.IndividualCrash) bool {
|
||||
res := m.archiveCol.FindOne(context.Background(),
|
||||
bson.M{"error": ind.Error, "stack": ind.Stack, "platform": bson.M{"$in": []string{ind.Platform, "all"}}},
|
||||
)
|
||||
return res.Err() == nil
|
||||
}
|
||||
|
||||
func (m *MongoCrashTable) InsertCrash(ind backend.IndividualCrash) error {
|
||||
first, _, _ := strings.Cut(ind.Stack, "\n")
|
||||
_, err := m.col.UpdateOne(context.Background(),
|
||||
bson.M{"error": ind.Error, "firstLine": first, //filter main report
|
||||
"individual.stack": ind.Stack, "individual.platform": ind.Platform}, //filter individual
|
||||
bson.M{"$inc": bson.M{"individual.count": 1}}, //increment count
|
||||
)
|
||||
if err == mongo.ErrNoDocuments {
|
||||
ind.Count = 1
|
||||
_, err = m.col.UpdateOne(context.Background(),
|
||||
bson.M{"error": ind.Error, "firstLine": first}, //filter
|
||||
bson.M{"$push": bson.M{"individual": ind}}, //Add new individual report
|
||||
)
|
||||
if err == mongo.ErrNoDocuments {
|
||||
var id uuid.UUID
|
||||
id, err = uuid.NewV7()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = m.col.InsertOne(context.Background(),
|
||||
backend.CrashReport{
|
||||
ID: id.String(),
|
||||
Error: ind.Error,
|
||||
FirstLine: first,
|
||||
Individual: []backend.IndividualCrash{ind},
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
package db
|
||||
Reference in New Issue
Block a user