-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathusage.go
More file actions
71 lines (61 loc) · 2.48 KB
/
usage.go
File metadata and controls
71 lines (61 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package models
import (
"time"
"go.mongodb.org/mongo-driver/v2/bson"
)
// HourlyUsage tracks cost per user, per project, per hour.
// Each document represents one hour bucket of usage.
type HourlyUsage struct {
ID bson.ObjectID `bson:"_id"`
UserID bson.ObjectID `bson:"user_id"`
ProjectID string `bson:"project_id"`
HourBucket bson.DateTime `bson:"hour_bucket"` // Timestamp truncated to the hour
SuccessCost float64 `bson:"success_cost"` // Cost in USD for successful requests
FailedCost float64 `bson:"failed_cost"` // Cost in USD for failed requests
UpdatedAt bson.DateTime `bson:"updated_at"`
}
func (u HourlyUsage) CollectionName() string {
return "hourly_usages"
}
// WeeklyUsage tracks cost per user, per project, per week.
// Each document represents one week bucket of usage.
type WeeklyUsage struct {
ID bson.ObjectID `bson:"_id"`
UserID bson.ObjectID `bson:"user_id"`
ProjectID string `bson:"project_id"`
WeekBucket bson.DateTime `bson:"week_bucket"` // Timestamp truncated to the week (Monday)
SuccessCost float64 `bson:"success_cost"` // Cost in USD for successful requests
FailedCost float64 `bson:"failed_cost"` // Cost in USD for failed requests
UpdatedAt bson.DateTime `bson:"updated_at"`
}
func (u WeeklyUsage) CollectionName() string {
return "weekly_usages"
}
// LifetimeUsage tracks total cost per user, per project, across all time.
// Each document represents the cumulative usage for a user-project pair.
type LifetimeUsage struct {
ID bson.ObjectID `bson:"_id"`
UserID bson.ObjectID `bson:"user_id"`
ProjectID string `bson:"project_id"`
SuccessCost float64 `bson:"success_cost"` // Total cost in USD for successful requests
FailedCost float64 `bson:"failed_cost"` // Total cost in USD for failed requests
UpdatedAt bson.DateTime `bson:"updated_at"`
}
func (u LifetimeUsage) CollectionName() string {
return "lifetime_usages"
}
// TruncateToHour truncates a time to the start of its hour.
func TruncateToHour(t time.Time) time.Time {
return t.Truncate(time.Hour)
}
// TruncateToWeek truncates a time to the start of its week (Monday 00:00:00 UTC).
func TruncateToWeek(t time.Time) time.Time {
t = t.UTC()
weekday := int(t.Weekday())
if weekday == 0 {
weekday = 7 // Sunday becomes 7
}
// Subtract days to get to Monday
monday := t.AddDate(0, 0, -(weekday - 1))
return time.Date(monday.Year(), monday.Month(), monday.Day(), 0, 0, 0, 0, time.UTC)
}