-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics.py
More file actions
177 lines (151 loc) · 4.86 KB
/
Copy pathanalytics.py
File metadata and controls
177 lines (151 loc) · 4.86 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# SPDX-License-Identifier: Elastic-2.0
# Copyright (c) 2026 GovernsAI. All rights reserved.
"""
Analytics data models for dashboard data and usage insights.
"""
from dataclasses import dataclass
from typing import Dict, Any, Optional, List
@dataclass
class AnalyticsStats:
"""Statistics for analytics data."""
total: int
by_decision: Optional[Dict[str, int]] = None
by_tool: Optional[Dict[str, int]] = None
by_user: Optional[Dict[str, int]] = None
by_provider: Optional[Dict[str, int]] = None
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "AnalyticsStats":
"""Create from dictionary."""
return cls(
total=data["total"],
by_decision=data.get("byDecision"),
by_tool=data.get("byTool"),
by_user=data.get("byUser"),
by_provider=data.get("byProvider"),
)
def to_dict(self) -> Dict[str, Any]:
"""Convert to dictionary."""
return {
"total": self.total,
"byDecision": self.by_decision,
"byTool": self.by_tool,
"byUser": self.by_user,
"byProvider": self.by_provider,
}
@dataclass
class DecisionAnalytics:
"""Analytics data for decisions."""
decisions: List[Dict[str, Any]]
stats: AnalyticsStats
time_range: str
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "DecisionAnalytics":
"""Create from dictionary."""
return cls(
decisions=data["decisions"],
stats=AnalyticsStats.from_dict(data["stats"]),
time_range=data["timeRange"],
)
def to_dict(self) -> Dict[str, Any]:
"""Convert to dictionary."""
return {
"decisions": self.decisions,
"stats": self.stats.to_dict(),
"timeRange": self.time_range,
}
@dataclass
class ToolCallAnalytics:
"""Analytics data for tool calls."""
tool_calls: List[Dict[str, Any]]
stats: AnalyticsStats
time_range: str
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "ToolCallAnalytics":
"""Create from dictionary."""
return cls(
tool_calls=data["toolCalls"],
stats=AnalyticsStats.from_dict(data["stats"]),
time_range=data["timeRange"],
)
def to_dict(self) -> Dict[str, Any]:
"""Convert to dictionary."""
return {
"toolCalls": self.tool_calls,
"stats": self.stats.to_dict(),
"timeRange": self.time_range,
}
@dataclass
class SpendAnalytics:
"""Analytics data for spending."""
total_cost: float
by_provider: Dict[str, float]
by_user: Dict[str, float]
by_cost_type: Dict[str, float]
time_range: str
currency: str = "USD"
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "SpendAnalytics":
"""Create from dictionary."""
return cls(
total_cost=data["totalCost"],
by_provider=data["byProvider"],
by_user=data["byUser"],
by_cost_type=data["byCostType"],
time_range=data["timeRange"],
currency=data.get("currency", "USD"),
)
def to_dict(self) -> Dict[str, Any]:
"""Convert to dictionary."""
return {
"totalCost": self.total_cost,
"byProvider": self.by_provider,
"byUser": self.by_user,
"byCostType": self.by_cost_type,
"timeRange": self.time_range,
"currency": self.currency,
}
@dataclass
class UsageRecord:
"""Usage record for analytics."""
user_id: str
org_id: str
provider: str
model: str
input_tokens: int
output_tokens: int
cost: float
cost_type: str
timestamp: str
tool: Optional[str] = None
decision: Optional[str] = None
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "UsageRecord":
"""Create from dictionary."""
return cls(
user_id=data["userId"],
org_id=data["orgId"],
provider=data["provider"],
model=data["model"],
input_tokens=data["inputTokens"],
output_tokens=data["outputTokens"],
cost=data["cost"],
cost_type=data["costType"],
timestamp=data["timestamp"],
tool=data.get("tool"),
decision=data.get("decision"),
)
def to_dict(self) -> Dict[str, Any]:
"""Convert to dictionary."""
return {
"userId": self.user_id,
"orgId": self.org_id,
"provider": self.provider,
"model": self.model,
"inputTokens": self.input_tokens,
"outputTokens": self.output_tokens,
"cost": self.cost,
"costType": self.cost_type,
"timestamp": self.timestamp,
"tool": self.tool,
"decision": self.decision,
}