forked from aqua5230/usage
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathusage_notifications.py
More file actions
96 lines (80 loc) · 3.14 KB
/
usage_notifications.py
File metadata and controls
96 lines (80 loc) · 3.14 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright (C) 2026 lollapalooza <https://github.com/aqua5230>
#
# Part of "usage". Free software licensed under the GNU Affero General Public
# License v3.0 only; see the LICENSE file for full terms and the warranty disclaimer.
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Literal
from burn_rate import RESET_DROP_PERCENT
NotificationKind = Literal["warn", "depleted", "restored"]
VALID_CHANNELS = frozenset(
{
"claude_session",
"claude_weekly",
"codex_session",
"codex_weekly",
}
)
@dataclass(slots=True)
class NotificationEvent:
kind: NotificationKind
channel: str
threshold: float | None
@dataclass(slots=True)
class _ChannelState:
last_percent: float | None = None
warned_thresholds: set[float] = field(default_factory=set)
depleted: bool = False
class QuotaNotifier:
def __init__(self, thresholds: list[float] | None = None) -> None:
values = [90.0] if thresholds is None else thresholds
self.thresholds = sorted({float(value) for value in values})
self._channels = {channel: _ChannelState() for channel in VALID_CHANNELS}
def update(
self,
channels: dict[str, tuple[float | None, bool]],
) -> list[NotificationEvent]:
events: list[NotificationEvent] = []
for channel, (percent, available) in channels.items():
if channel not in VALID_CHANNELS:
continue
events.extend(self._update_channel(channel, percent, available))
return events
def _update_channel(
self,
channel: str,
percent: float | None,
available: bool,
) -> list[NotificationEvent]:
state = self._channels[channel]
events: list[NotificationEvent] = []
current = float(percent) if percent is not None else None
reset = (
current is not None
and state.last_percent is not None
and (state.last_percent - current) > RESET_DROP_PERCENT
)
if reset:
was_depleted = state.depleted
state.warned_thresholds.clear()
state.depleted = False
if was_depleted and available and current is not None and current < 100.0:
events.append(NotificationEvent("restored", channel, None))
depleted = current is not None and current >= 100.0
if depleted:
if not state.depleted:
events.append(NotificationEvent("depleted", channel, None))
state.depleted = True
elif current is not None:
previous = state.last_percent
if previous is not None:
for threshold in self.thresholds:
crossed = previous < threshold <= current
if crossed and threshold not in state.warned_thresholds:
events.append(NotificationEvent("warn", channel, threshold))
state.warned_thresholds.add(threshold)
state.depleted = False
if current is not None:
state.last_percent = current
return events