seher/opencode_go/
types.rs1use chrono::{DateTime, Utc};
2
3const LIMIT_EPSILON: f64 = 1e-9;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum OpencodeGoUsageSource {
7 LocalDatabase,
8}
9
10#[derive(Debug, Clone, PartialEq)]
11pub struct OpencodeGoUsageWindow {
12 pub entry_type: &'static str,
13 pub spent_usd: f64,
14 pub limit_usd: f64,
15 pub resets_at: Option<DateTime<Utc>>,
16}
17
18impl OpencodeGoUsageWindow {
19 #[must_use]
20 pub fn is_limited(&self) -> bool {
21 self.spent_usd + LIMIT_EPSILON >= self.limit_usd
22 }
23
24 #[must_use]
25 pub fn utilization(&self) -> f64 {
26 if self.limit_usd <= 0.0 {
27 100.0
28 } else {
29 self.spent_usd / self.limit_usd * 100.0
30 }
31 }
32}
33
34#[derive(Debug, Clone, PartialEq)]
35pub struct OpencodeGoUsageSnapshot {
36 pub source: OpencodeGoUsageSource,
37 pub credentials_available: bool,
38 pub total_messages: usize,
39 pub windows: Vec<OpencodeGoUsageWindow>,
40}
41
42impl OpencodeGoUsageSnapshot {
43 #[must_use]
44 pub fn reset_time(&self) -> Option<DateTime<Utc>> {
45 self.windows
46 .iter()
47 .filter(|window| window.is_limited())
48 .filter_map(|window| window.resets_at)
49 .max()
50 }
51}