按本机日期计算 Codex 当日 token
This commit is contained in:
parent
6468e91c7a
commit
7a48b50d5b
@ -587,8 +587,9 @@ class CodexRateLimitReader:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _daily_bars_from_buckets(buckets: list[dict]) -> tuple[int, str]:
|
def _daily_bars_from_buckets(buckets: list[dict]) -> tuple[int, str]:
|
||||||
|
today = date.today()
|
||||||
|
wanted_days = [(today - timedelta(days=offset)).isoformat() for offset in range(CODEX_DAILY_BAR_COUNT - 1, -1, -1)]
|
||||||
tokens_by_day: dict[str, int] = {}
|
tokens_by_day: dict[str, int] = {}
|
||||||
latest_day: date | None = None
|
|
||||||
|
|
||||||
for bucket in buckets:
|
for bucket in buckets:
|
||||||
if not isinstance(bucket, dict):
|
if not isinstance(bucket, dict):
|
||||||
@ -597,20 +598,6 @@ class CodexRateLimitReader:
|
|||||||
tokens = bucket.get("tokens")
|
tokens = bucket.get("tokens")
|
||||||
if isinstance(start_date, str) and isinstance(tokens, (int, float)) and tokens >= 0:
|
if isinstance(start_date, str) and isinstance(tokens, (int, float)) and tokens >= 0:
|
||||||
tokens_by_day[start_date] = int(tokens)
|
tokens_by_day[start_date] = int(tokens)
|
||||||
try:
|
|
||||||
bucket_day = date.fromisoformat(start_date)
|
|
||||||
except ValueError:
|
|
||||||
continue
|
|
||||||
if latest_day is None or bucket_day > latest_day:
|
|
||||||
latest_day = bucket_day
|
|
||||||
|
|
||||||
if latest_day is None:
|
|
||||||
return -1, ""
|
|
||||||
|
|
||||||
wanted_days = [
|
|
||||||
(latest_day - timedelta(days=offset)).isoformat()
|
|
||||||
for offset in range(CODEX_DAILY_BAR_COUNT - 1, -1, -1)
|
|
||||||
]
|
|
||||||
|
|
||||||
values = [tokens_by_day.get(day, 0) for day in wanted_days]
|
values = [tokens_by_day.get(day, 0) for day in wanted_days]
|
||||||
max_value = max(values) if values else 0
|
max_value = max(values) if values else 0
|
||||||
@ -619,7 +606,7 @@ class CodexRateLimitReader:
|
|||||||
else:
|
else:
|
||||||
percents = [0 for _ in values]
|
percents = [0 for _ in values]
|
||||||
|
|
||||||
today_tokens_m10 = CodexRateLimitReader._tokens_to_m10(tokens_by_day.get(latest_day.isoformat(), -1))
|
today_tokens_m10 = CodexRateLimitReader._tokens_to_m10(tokens_by_day.get(today.isoformat(), 0))
|
||||||
return today_tokens_m10, ":".join(str(value) for value in percents)
|
return today_tokens_m10, ":".join(str(value) for value in percents)
|
||||||
|
|
||||||
def read_usage_snapshot(self) -> tuple[int, str]:
|
def read_usage_snapshot(self) -> tuple[int, str]:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user