-
Notifications
You must be signed in to change notification settings - Fork 69
feat: track OpenRouter usage #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wjiayis
wants to merge
13
commits into
staging
Choose a base branch
from
feat/proj-cost-track
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7b39a25
feat: track OpenRouter usage
wjiayis 972c686
Merge branch 'staging' into feat/proj-cost-track
wjiayis d9a41f8
feat: add index and TTL
wjiayis 3d2ce5e
fix: frontend handle missing error response in API client
wjiayis cad2eb3
fix: remove model_slug from usages collection
wjiayis 788c80e
feat: track usage by hourly and weekly
wjiayis bca53b7
fix: track partial usage if error occurs
wjiayis 79678ca
fix: track usage for title and citation key generation
wjiayis a4c645b
feat: track lifetime usage
wjiayis 9c92a9a
Merge branch 'staging' into feat/proj-cost-track
wjiayis 4c14874
fix: IsCustom() -> IsCustomModel
wjiayis 3dd9bd9
Merge branch 'staging' into feat/proj-cost-track
wjiayis 862da76
feat: separate success cost from failed cost
wjiayis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.