(ctx context.Context, collection *mongo.Collection, filter bson.M, cost float64, success bool, now time.Time)
| 141 | } |
| 142 | |
| 143 | func (s *UsageService) upsertUsage(ctx context.Context, collection *mongo.Collection, filter bson.M, cost float64, success bool, now time.Time) error { |
| 144 | costField := "failed_cost" |
| 145 | if success { |
| 146 | costField = "success_cost" |
| 147 | } |
| 148 | |
| 149 | update := bson.M{ |
| 150 | "$inc": bson.M{ |
| 151 | costField: cost, |
| 152 | }, |
| 153 | "$set": bson.M{ |
| 154 | "updated_at": bson.NewDateTimeFromTime(now), |
| 155 | }, |
| 156 | "$setOnInsert": bson.M{ |
| 157 | "_id": bson.NewObjectID(), |
| 158 | }, |
| 159 | } |
| 160 | |
| 161 | opts := options.UpdateOne().SetUpsert(true) |
| 162 | _, err := collection.UpdateOne(ctx, filter, update, opts) |
| 163 | return err |
| 164 | } |
| 165 | |
| 166 | func (s *UsageService) trackHourlyUsage(ctx context.Context, userID bson.ObjectID, projectID string, cost float64, success bool, now time.Time) error { |
| 167 | filter := bson.M{ |
no outgoing calls
no test coverage detected