-
-
Notifications
You must be signed in to change notification settings - Fork 330
feat: expose self-service quota windows #1111
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
dofastted
wants to merge
2
commits into
ding113:dev
Choose a base branch
from
dofastted:pr/dev-quota-windows-20260426
base: dev
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
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| ({ | ||
| request: { | ||
| url: "{{baseUrl}}/api/actions/my-usage/getMyQuota", | ||
| method: "POST", | ||
| headers: { | ||
| "Authorization": "Bearer {{apiKey}}", | ||
| "Content-Type": "application/json", | ||
| "User-Agent": "cc-switch/1.0" | ||
| }, | ||
| body: "{}" | ||
| }, | ||
|
|
||
| extractor: function(response) { | ||
| const data = response && response.ok === true && response.data && typeof response.data === "object" | ||
| ? response.data | ||
| : {}; | ||
|
|
||
| const toNumber = function(value, fallback) { | ||
| return typeof value === "number" && Number.isFinite(value) ? value : fallback; | ||
| }; | ||
|
|
||
| const formatPercent = function(value) { | ||
| return typeof value === "number" && Number.isFinite(value) ? value + "%" : "-"; | ||
| }; | ||
|
|
||
| const toBoolean = function(value, fallback) { | ||
| return typeof value === "boolean" ? value : fallback; | ||
| }; | ||
|
|
||
| const round2 = function(value) { | ||
| return Math.round(value * 100) / 100; | ||
| }; | ||
|
|
||
| const percent = function(used, total) { | ||
| return total > 0 ? round2((used / total) * 100) : null; | ||
| }; | ||
|
|
||
| const quotaWindows = data.quotaWindows && typeof data.quotaWindows === "object" | ||
| ? data.quotaWindows | ||
| : {}; | ||
| const fiveHour = quotaWindows.fiveHour || {}; | ||
| const daily = quotaWindows.daily || {}; | ||
| const weekly = quotaWindows.weekly || {}; | ||
| const monthly = quotaWindows.monthly || {}; | ||
| const total = quotaWindows.total || {}; | ||
|
|
||
| const limitMonthlyUsd = toNumber(data.limitMonthlyUsd, null); | ||
| const limitTotalUsd = toNumber(data.limitTotalUsd, limitMonthlyUsd); | ||
| const usedTotalUsd = toNumber(data.usedTotalUsd, toNumber(data.usedMonthlyUsd, 0)); | ||
| const remainingTotalUsd = limitTotalUsd === null ? null : round2(Math.max(limitTotalUsd - usedTotalUsd, 0)); | ||
|
|
||
| const limitDailyUsd = toNumber(data.limitDailyUsd, null); | ||
| const usedDailyUsd = toNumber(data.usedDailyUsd, 0); | ||
| const remainingDailyUsd = limitDailyUsd === null ? null : round2(Math.max(limitDailyUsd - usedDailyUsd, 0)); | ||
|
|
||
| const limit5hUsd = toNumber(data.limit5hUsd, null); | ||
| const used5hUsd = toNumber(data.used5hUsd, 0); | ||
| const remaining5hUsd = limit5hUsd === null ? null : round2(Math.max(limit5hUsd - used5hUsd, 0)); | ||
|
|
||
| const isValid = | ||
| response && | ||
| response.ok === true && | ||
| toBoolean(data.keyIsEnabled, true) && | ||
| toBoolean(data.userIsEnabled, true); | ||
|
|
||
| return { | ||
| isValid: !!isValid, | ||
| invalidMessage: isValid ? undefined : "套餐不可用", | ||
| planName: "Total Quota", | ||
| unit: typeof data.unit === "string" ? data.unit : "USD", | ||
| remaining: toNumber(total.remainingUsd, remainingTotalUsd), | ||
| total: toNumber(total.limitUsd, limitTotalUsd), | ||
| used: toNumber(total.usedUsd, usedTotalUsd), | ||
| todayUsed: toNumber(data.todayUsedUsd, toNumber(daily.usedUsd, usedDailyUsd)), | ||
| todayRemaining: toNumber(data.todayRemainingUsd, toNumber(daily.remainingUsd, remainingDailyUsd)), | ||
| todayUsedPercent: toNumber(data.todayUsedPercent, toNumber(daily.usedPercent, percent(usedDailyUsd, limitDailyUsd))), | ||
| todayRemainingPercent: toNumber( | ||
| data.todayRemainingPercent, | ||
| toNumber(daily.remainingPercent, percent(remainingDailyUsd || 0, limitDailyUsd)) | ||
| ), | ||
| remaining5h: toNumber(fiveHour.remainingUsd, remaining5hUsd), | ||
| remainingDaily: toNumber(daily.remainingUsd, remainingDailyUsd), | ||
| remainingWeekly: toNumber(weekly.remainingUsd, toNumber(data.remainingWeeklyUsd, null)), | ||
| remainingMonthly: toNumber(monthly.remainingUsd, toNumber(data.remainingMonthlyUsd, null)), | ||
| remainingTotal: toNumber(total.remainingUsd, remainingTotalUsd), | ||
| remainingPercent: toNumber(total.remainingPercent, data.remainingPercent), | ||
| extra: "5H剩余:" + formatPercent(toNumber(fiveHour.remainingPercent, percent(remaining5hUsd || 0, limit5hUsd))) | ||
| + "/日剩余:" + formatPercent(toNumber(daily.remainingPercent, percent(remainingDailyUsd || 0, limitDailyUsd))) | ||
| + "/周剩余:" + formatPercent(weekly.remainingPercent) | ||
| + "/月剩余:" + formatPercent(monthly.remainingPercent) | ||
| + "/总剩余:" + formatPercent(toNumber(total.remainingPercent, data.remainingPercent)) | ||
| }; | ||
| } | ||
| }) | ||
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,48 @@ | ||
| ({ | ||
| request: { | ||
| url: "{{baseUrl}}/api/actions/my-usage/getMyQuota", | ||
| method: "POST", | ||
| headers: { | ||
| "Authorization": "Bearer {{apiKey}}", | ||
| "Content-Type": "application/json", | ||
| "User-Agent": "cc-switch/1.0" | ||
| }, | ||
| body: "{}" | ||
| }, | ||
|
|
||
| extractor: function(response) { | ||
| const data = response && response.ok === true && response.data && typeof response.data === "object" | ||
| ? response.data | ||
| : {}; | ||
|
|
||
| const toNumber = function(value, fallback) { | ||
| return typeof value === "number" && Number.isFinite(value) ? value : fallback; | ||
| }; | ||
|
|
||
| const toBoolean = function(value, fallback) { | ||
| return typeof value === "boolean" ? value : fallback; | ||
| }; | ||
|
|
||
| const quotaWindows = data.quotaWindows && typeof data.quotaWindows === "object" | ||
| ? data.quotaWindows | ||
| : {}; | ||
| const daily = quotaWindows.daily || {}; | ||
|
|
||
| return { | ||
| ok: response && response.ok === true, | ||
| isValid: toBoolean(data.keyIsEnabled, true) && toBoolean(data.userIsEnabled, true), | ||
| planName: "Daily Quota", | ||
| remaining: toNumber(daily.remainingUsd, toNumber(data.remainingDailyUsd, null)), | ||
| total: toNumber(daily.limitUsd, toNumber(data.limitDailyUsd, null)), | ||
| used: toNumber(daily.usedUsd, toNumber(data.usedDailyUsd, 0)), | ||
| usedPercent: toNumber(daily.usedPercent, toNumber(data.todayUsedPercent, null)), | ||
| remainingPercent: toNumber(daily.remainingPercent, toNumber(data.todayRemainingPercent, null)), | ||
| unit: typeof data.unit === "string" ? data.unit : "USD", | ||
| keyName: typeof data.keyName === "string" ? data.keyName : null, | ||
| userName: typeof data.userName === "string" ? data.userName : null, | ||
| providerGroup: typeof data.providerGroup === "string" ? data.providerGroup : null, | ||
| resetMode: typeof data.resetMode === "string" ? data.resetMode : null, | ||
| resetTime: typeof data.resetTime === "string" ? data.resetTime : null | ||
| }; | ||
| } | ||
| }) |
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,64 @@ | ||
| ({ | ||
| request: { | ||
| url: "{{baseUrl}}/api/actions/my-usage/getMyQuota", | ||
| method: "POST", | ||
| headers: { | ||
| "Authorization": "Bearer {{apiKey}}", | ||
| "Content-Type": "application/json", | ||
| "User-Agent": "cc-switch/1.0" | ||
| }, | ||
| body: "{}" | ||
| }, | ||
|
|
||
| extractor: function(response) { | ||
| const data = response && response.ok === true && response.data && typeof response.data === "object" | ||
| ? response.data | ||
| : {}; | ||
| const quotaWindows = data.quotaWindows && typeof data.quotaWindows === "object" | ||
| ? data.quotaWindows | ||
| : {}; | ||
| const fiveHour = quotaWindows.fiveHour || {}; | ||
| const daily = quotaWindows.daily || {}; | ||
| const weekly = quotaWindows.weekly || {}; | ||
| const monthly = quotaWindows.monthly || {}; | ||
| const total = quotaWindows.total || {}; | ||
| const formatPercent = function(value) { | ||
| return typeof value === "number" && Number.isFinite(value) ? value + "%" : "-"; | ||
| }; | ||
|
|
||
| const toBoolean = function(value, fallback) { | ||
| return typeof value === "boolean" ? value : fallback; | ||
| }; | ||
|
|
||
| const isValid = | ||
| response && | ||
| response.ok === true && | ||
| toBoolean(data.keyIsEnabled, true) && | ||
| toBoolean(data.userIsEnabled, true); | ||
|
|
||
| return { | ||
| isValid: !!isValid, | ||
| invalidMessage: isValid ? undefined : "套餐不可用", | ||
| planName: "Total Quota", | ||
| unit: typeof data.unit === "string" ? data.unit : "USD", | ||
| remaining: total.remainingUsd, | ||
| total: total.limitUsd, | ||
| used: total.usedUsd, | ||
| todayUsed: data.todayUsedUsd, | ||
| todayRemaining: data.todayRemainingUsd, | ||
| todayUsedPercent: data.todayUsedPercent, | ||
| todayRemainingPercent: data.todayRemainingPercent, | ||
| remaining5h: fiveHour.remainingUsd, | ||
| remainingDaily: daily.remainingUsd, | ||
| remainingWeekly: weekly.remainingUsd, | ||
| remainingMonthly: monthly.remainingUsd, | ||
| remainingTotal: total.remainingUsd, | ||
| remainingPercent: data.remainingPercent, | ||
| extra: "5H剩余:" + formatPercent(fiveHour.remainingPercent) | ||
| + "/日剩余:" + formatPercent(daily.remainingPercent) | ||
| + "/周剩余:" + formatPercent(weekly.remainingPercent) | ||
| + "/月剩余:" + formatPercent(monthly.remainingPercent) | ||
| + "/总剩余:" + formatPercent(total.remainingPercent) | ||
| }; | ||
| } | ||
| }) |
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,48 @@ | ||
| ({ | ||
| request: { | ||
| url: "{{baseUrl}}/api/actions/my-usage/getMyQuota", | ||
| method: "POST", | ||
| headers: { | ||
| "Authorization": "Bearer {{apiKey}}", | ||
| "Content-Type": "application/json", | ||
| "User-Agent": "cc-switch/1.0" | ||
| }, | ||
| body: "{}" | ||
| }, | ||
|
|
||
| extractor: function(response) { | ||
| const data = response && response.ok === true && response.data && typeof response.data === "object" | ||
| ? response.data | ||
| : {}; | ||
|
|
||
| const toNumber = function(value, fallback) { | ||
| return typeof value === "number" && Number.isFinite(value) ? value : fallback; | ||
| }; | ||
|
|
||
| const toBoolean = function(value, fallback) { | ||
| return typeof value === "boolean" ? value : fallback; | ||
| }; | ||
|
|
||
| const quotaWindows = data.quotaWindows && typeof data.quotaWindows === "object" | ||
| ? data.quotaWindows | ||
| : {}; | ||
| const total = quotaWindows.total || {}; | ||
|
|
||
| return { | ||
| ok: response && response.ok === true, | ||
| isValid: toBoolean(data.keyIsEnabled, true) && toBoolean(data.userIsEnabled, true), | ||
| planName: "Total Quota", | ||
| remaining: toNumber(total.remainingUsd, toNumber(data.remainingTotalUsd, null)), | ||
| total: toNumber(total.limitUsd, toNumber(data.limitTotalUsd, null)), | ||
| used: toNumber(total.usedUsd, toNumber(data.usedTotalUsd, 0)), | ||
| usedPercent: toNumber(total.usedPercent, null), | ||
| remainingPercent: toNumber(total.remainingPercent, toNumber(data.remainingPercent, null)), | ||
| unit: typeof data.unit === "string" ? data.unit : "USD", | ||
| keyName: typeof data.keyName === "string" ? data.keyName : null, | ||
| userName: typeof data.userName === "string" ? data.userName : null, | ||
| providerGroup: typeof data.providerGroup === "string" ? data.providerGroup : null, | ||
| resetMode: typeof data.resetMode === "string" ? data.resetMode : null, | ||
| resetTime: typeof data.resetTime === "string" ? data.resetTime : null | ||
| }; | ||
| } | ||
| }) |
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,52 @@ | ||
| ({ | ||
| request: { | ||
| url: "{{baseUrl}}/api/actions/my-usage/getMyQuota", | ||
| method: "POST", | ||
| headers: { | ||
| "Authorization": "Bearer {{apiKey}}", | ||
| "Content-Type": "application/json", | ||
| "User-Agent": "cc-switch/1.0" | ||
| }, | ||
| body: "{}" | ||
| }, | ||
|
|
||
| extractor: function(response) { | ||
| const data = response && response.ok === true && response.data && typeof response.data === "object" | ||
| ? response.data | ||
| : {}; | ||
|
|
||
| const toNumber = function(value, fallback) { | ||
| return typeof value === "number" && Number.isFinite(value) ? value : fallback; | ||
| }; | ||
|
|
||
| const toBoolean = function(value, fallback) { | ||
| return typeof value === "boolean" ? value : fallback; | ||
| }; | ||
|
|
||
| const quotaWindows = data.quotaWindows && typeof data.quotaWindows === "object" | ||
| ? data.quotaWindows | ||
| : {}; | ||
| const weekly = quotaWindows.weekly || {}; | ||
|
|
||
| return { | ||
| isValid: !!( | ||
| response && | ||
| response.ok === true && | ||
| toBoolean(data.keyIsEnabled, true) && | ||
| toBoolean(data.userIsEnabled, true) | ||
| ), | ||
| planName: "Weekly Quota", | ||
| remaining: toNumber(weekly.remainingUsd, toNumber(data.remainingWeeklyUsd, null)), | ||
| total: toNumber(weekly.limitUsd, toNumber(data.limitWeeklyUsd, null)), | ||
| used: toNumber(weekly.usedUsd, toNumber(data.usedWeeklyUsd, 0)), | ||
| usedPercent: toNumber(weekly.usedPercent, null), | ||
| remainingPercent: toNumber(weekly.remainingPercent, null), | ||
| unit: typeof data.unit === "string" ? data.unit : "USD", | ||
| keyName: typeof data.keyName === "string" ? data.keyName : null, | ||
| userName: typeof data.userName === "string" ? data.userName : null, | ||
| providerGroup: typeof data.providerGroup === "string" ? data.providerGroup : null, | ||
| resetMode: typeof data.resetMode === "string" ? data.resetMode : null, | ||
| resetTime: typeof data.resetTime === "string" ? data.resetTime : null | ||
| }; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
| }) | ||
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,67 @@ | ||
| ({ | ||
| request: { | ||
| url: "{{baseUrl}}/api/actions/my-usage/getMyQuota", | ||
| method: "POST", | ||
| headers: { | ||
| "Authorization": "Bearer {{apiKey}}", | ||
| "Content-Type": "application/json", | ||
| "User-Agent": "cc-switch/1.0" | ||
| }, | ||
| body: "{}" | ||
| }, | ||
|
|
||
| extractor: function(response) { | ||
| const data = response && response.ok === true && response.data && typeof response.data === "object" | ||
| ? response.data | ||
| : {}; | ||
|
|
||
| const toNumber = function(value, fallback) { | ||
| return typeof value === "number" && Number.isFinite(value) ? value : fallback; | ||
| }; | ||
|
|
||
| const formatPercent = function(value) { | ||
| return typeof value === "number" && Number.isFinite(value) ? value + "%" : "-"; | ||
| }; | ||
|
|
||
| const toBoolean = function(value, fallback) { | ||
| return typeof value === "boolean" ? value : fallback; | ||
| }; | ||
|
|
||
| const quotaWindows = data.quotaWindows && typeof data.quotaWindows === "object" | ||
| ? data.quotaWindows | ||
| : {}; | ||
| const fiveHour = quotaWindows.fiveHour || {}; | ||
| const daily = quotaWindows.daily || {}; | ||
| const weekly = quotaWindows.weekly || {}; | ||
| const monthly = quotaWindows.monthly || {}; | ||
| const total = quotaWindows.total || {}; | ||
|
|
||
| const isValid = | ||
| response && | ||
| response.ok === true && | ||
| toBoolean(data.keyIsEnabled, true) && | ||
| toBoolean(data.userIsEnabled, true); | ||
|
|
||
| return { | ||
| isValid: !!isValid, | ||
| invalidMessage: isValid ? undefined : "套餐不可用", | ||
| remaining: toNumber(total.remainingUsd, toNumber(data.remainingTotalUsd, null)), | ||
| unit: typeof data.unit === "string" ? data.unit : "USD", | ||
| planName: "Total Quota", | ||
| total: toNumber(total.limitUsd, toNumber(data.limitTotalUsd, null)), | ||
| used: toNumber(total.usedUsd, toNumber(data.usedTotalUsd, 0)), | ||
| todayUsed: toNumber(data.todayUsedUsd, toNumber(daily.usedUsd, 0)), | ||
| todayRemaining: toNumber(data.todayRemainingUsd, toNumber(daily.remainingUsd, null)), | ||
| remainingWeekly: toNumber(weekly.remainingUsd, toNumber(data.remainingWeeklyUsd, null)), | ||
| remainingMonthly: toNumber(monthly.remainingUsd, toNumber(data.remainingMonthlyUsd, null)), | ||
| remainingTotal: toNumber(total.remainingUsd, toNumber(data.remainingTotalUsd, null)), | ||
| remaining5h: toNumber(fiveHour.remainingUsd, toNumber(data.remaining5hUsd, null)), | ||
| remainingDaily: toNumber(daily.remainingUsd, toNumber(data.remainingDailyUsd, null)), | ||
| extra: "5H剩余:" + formatPercent(fiveHour.remainingPercent) | ||
| + "/日剩余:" + formatPercent(toNumber(daily.remainingPercent, data.todayRemainingPercent)) | ||
| + "/周剩余:" + formatPercent(weekly.remainingPercent) | ||
| + "/月剩余:" + formatPercent(monthly.remainingPercent) | ||
| + "/总剩余:" + formatPercent(toNumber(total.remainingPercent, data.remainingPercent)) | ||
| }; | ||
| } | ||
| }) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
docs/examplesandpublic/examplesdocs/exampleshas 6 extractor variants whilepublic/examplescarries only 3 of them (compatible, direct, main), with identical content. Any future update that touches one location must also be manually applied to the other, which is an easy source of drift. Consider keeping a single source of truth (e.g., onlypublic/examples) and generating or symlinking thedocscopies, or at minimum document why two locations are needed.Prompt To Fix With AI