Proper context.Context usage
This commit is contained in:
@@ -18,8 +18,8 @@ func NewMongoTable[T backend.IDStruct](col *mongo.Collection) *MongoTable[T] {
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MongoTable[T]) Get(ID string) (data *T, err error) {
|
||||
res := m.col.FindOne(context.Background(), bson.M{"_id": ID})
|
||||
func (m *MongoTable[T]) Get(ctx context.Context, ID string) (data *T, err error) {
|
||||
res := m.col.FindOne(ctx, bson.M{"_id": ID})
|
||||
if res.Err() == mongo.ErrNoDocuments {
|
||||
return nil, backend.ErrNotFound
|
||||
} else if res.Err() != nil {
|
||||
@@ -30,58 +30,58 @@ func (m *MongoTable[T]) Get(ID string) (data *T, err error) {
|
||||
return &out, err
|
||||
}
|
||||
|
||||
func (m *MongoTable[T]) Find(values map[string]any) ([]T, error) {
|
||||
res, err := m.col.Find(context.Background(), values)
|
||||
func (m *MongoTable[T]) Find(ctx context.Context, values map[string]any) ([]T, error) {
|
||||
res, err := m.col.Find(ctx, 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)
|
||||
err = res.All(ctx, &out)
|
||||
return out, err
|
||||
}
|
||||
|
||||
func (m *MongoTable[T]) Insert(data T) error {
|
||||
_, err := m.col.InsertOne(context.Background(), data)
|
||||
func (m *MongoTable[T]) Insert(ctx context.Context, data T) error {
|
||||
_, err := m.col.InsertOne(ctx, data)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *MongoTable[T]) Remove(ID string) error {
|
||||
res := m.col.FindOneAndDelete(context.Background(), bson.M{"_id": ID})
|
||||
func (m *MongoTable[T]) Remove(ctx context.Context, ID string) error {
|
||||
res := m.col.FindOneAndDelete(ctx, 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)
|
||||
func (m *MongoTable[T]) FullUpdate(ctx context.Context, ID string, data T) error {
|
||||
res := m.col.FindOneAndReplace(ctx, 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}, bson.M{"$set": update})
|
||||
func (m *MongoTable[T]) PartUpdate(ctx context.Context, ID string, update map[string]any) error {
|
||||
res := m.col.FindOneAndUpdate(ctx, bson.M{"_id": ID}, bson.M{"$set": update})
|
||||
if res.Err() == mongo.ErrNoDocuments {
|
||||
return backend.ErrNotFound
|
||||
}
|
||||
return res.Err()
|
||||
}
|
||||
|
||||
func (m *MongoTable[CountLog]) RemoveOldLogs(date int) error {
|
||||
_, err := m.col.DeleteMany(context.Background(), bson.M{"date": bson.M{"$lt": date}})
|
||||
func (m *MongoTable[CountLog]) RemoveOldLogs(ctx context.Context, date int) error {
|
||||
_, err := m.col.DeleteMany(ctx, bson.M{"date": bson.M{"$lt": date}})
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
func (m *MongoTable[CountLog]) Count(platform string) (int, error) {
|
||||
func (m *MongoTable[CountLog]) Count(ctx context.Context, 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)
|
||||
out, err := m.col.CountDocuments(ctx, filter)
|
||||
return int(out), err
|
||||
}
|
||||
|
||||
@@ -22,24 +22,24 @@ func NewMongoCrashTable(crashCol *mongo.Collection, archiveCol *mongo.Collection
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MongoCrashTable) Archive(toArchive backend.ArchivedCrash) error {
|
||||
func (m *MongoCrashTable) Archive(ctx context.Context, toArchive backend.ArchivedCrash) error {
|
||||
if toArchive.Platform == "" {
|
||||
toArchive.Platform = "all"
|
||||
}
|
||||
_, err := m.archiveCol.InsertOne(context.Background(), toArchive)
|
||||
_, err := m.archiveCol.InsertOne(ctx, toArchive)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *MongoCrashTable) IsArchived(ind backend.IndividualCrash) bool {
|
||||
res := m.archiveCol.FindOne(context.Background(),
|
||||
func (m *MongoCrashTable) IsArchived(ctx context.Context, ind backend.IndividualCrash) bool {
|
||||
res := m.archiveCol.FindOne(ctx,
|
||||
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 {
|
||||
func (m *MongoCrashTable) InsertCrash(ctx context.Context, ind backend.IndividualCrash) error {
|
||||
first, _, _ := strings.Cut(ind.Stack, "\n")
|
||||
res, err := m.col.UpdateOne(context.Background(),
|
||||
res, err := m.col.UpdateOne(ctx,
|
||||
bson.M{"error": ind.Error, "firstLine": first, //filter main report
|
||||
"individual": bson.M{"$elemMatch": bson.M{"stack": ind.Stack, "platform": ind.Platform}}}, //filter individual
|
||||
bson.M{"$inc": bson.M{"individual.$.count": 1}}, //increment count
|
||||
@@ -49,7 +49,7 @@ func (m *MongoCrashTable) InsertCrash(ind backend.IndividualCrash) error {
|
||||
}
|
||||
if err == mongo.ErrNoDocuments || res.MatchedCount == 0 {
|
||||
ind.Count = 1
|
||||
res, err = m.col.UpdateMany(context.Background(),
|
||||
res, err = m.col.UpdateMany(ctx,
|
||||
bson.M{"error": ind.Error, "firstLine": first}, //filter
|
||||
bson.M{"$push": bson.M{"individual": ind}}, //Add new individual report
|
||||
)
|
||||
@@ -63,7 +63,7 @@ func (m *MongoCrashTable) InsertCrash(ind backend.IndividualCrash) error {
|
||||
return err
|
||||
}
|
||||
ind.Count = 1
|
||||
_, err = m.col.InsertOne(context.Background(),
|
||||
_, err = m.col.InsertOne(ctx,
|
||||
backend.CrashReport{
|
||||
ID: id.String(),
|
||||
Error: ind.Error,
|
||||
|
||||
Reference in New Issue
Block a user