Skip to main content

hyperinfer_core/traits/
database.rs

1use async_trait::async_trait;
2use chrono::{DateTime, Utc};
3use serde::{Deserialize, Serialize};
4
5use crate::error::DbError;
6use crate::types::{
7    CreateDeploymentRequest, Deployment, RoutingConfig, UpdateRoutingConfigRequest,
8};
9
10#[derive(Debug, Clone, Serialize, Deserialize, Default)]
11pub struct UsageSummary {
12    pub total_requests: i64,
13    pub total_input_tokens: i64,
14    pub total_output_tokens: i64,
15    pub avg_response_time_ms: f64,
16}
17
18#[async_trait]
19#[allow(clippy::double_must_use)]
20pub trait Database: Clone + Send + Sync + 'static {
21    async fn get_team(&self, id: &str) -> Result<Option<Team>, DbError>;
22    async fn create_team(&self, name: &str, budget_cents: i64) -> Result<Team, DbError>;
23    async fn get_user(&self, id: &str) -> Result<Option<User>, DbError>;
24    async fn get_user_by_email(&self, email: &str) -> Result<Option<User>, DbError>;
25    async fn create_user(
26        &self,
27        team_id: &str,
28        email: &str,
29        role: &str,
30        password_hash: Option<String>,
31    ) -> Result<User, DbError>;
32    async fn update_password_hash(&self, user_id: &str, password_hash: &str)
33        -> Result<(), DbError>;
34    async fn count_users_by_role(&self, role: &str) -> Result<i64, DbError>;
35    async fn get_api_key(&self, id: &str) -> Result<Option<ApiKey>, DbError>;
36    async fn get_api_key_by_hash(&self, key_hash: &str) -> Result<Option<ApiKey>, DbError>;
37    async fn create_api_key(
38        &self,
39        key_hash: &str,
40        user_id: &str,
41        team_id: &str,
42        name: Option<String>,
43    ) -> Result<ApiKey, DbError>;
44    async fn deactivate_api_key(&self, id: &str) -> Result<ApiKey, DbError>;
45    async fn get_model_alias(&self, id: &str) -> Result<Option<ModelAlias>, DbError>;
46    async fn create_model_alias(
47        &self,
48        team_id: &str,
49        alias: &str,
50        target_model: &str,
51        provider: &str,
52    ) -> Result<ModelAlias, DbError>;
53    async fn get_quota(&self, team_id: &str) -> Result<Option<Quota>, DbError>;
54    async fn create_quota(
55        &self,
56        team_id: &str,
57        rpm_limit: i32,
58        tpm_limit: i32,
59    ) -> Result<Quota, DbError>;
60    async fn record_usage(
61        &self,
62        team_id: &str,
63        api_key_id: &str,
64        model: &str,
65        input_tokens: i32,
66        output_tokens: i32,
67        response_time_ms: i64,
68    ) -> Result<UsageLog, DbError>;
69
70    async fn record_usage_batch(&self, entries: &[NewUsageLog]) -> Result<(), DbError> {
71        for entry in entries {
72            self.record_usage(
73                &entry.team_id,
74                &entry.api_key_id,
75                &entry.model,
76                entry.input_tokens,
77                entry.output_tokens,
78                entry.response_time_ms,
79            )
80            .await?;
81        }
82        Ok(())
83    }
84
85    // --- Deployment CRUD ---
86    async fn list_deployments(
87        &self,
88        model: &str,
89        is_active: Option<bool>,
90    ) -> Result<Vec<Deployment>, DbError>;
91    async fn get_deployment(&self, id: &str) -> Result<Option<Deployment>, DbError>;
92    async fn create_deployment(&self, req: CreateDeploymentRequest) -> Result<Deployment, DbError>;
93    async fn update_deployment(
94        &self,
95        id: &str,
96        req: CreateDeploymentRequest,
97    ) -> Result<Deployment, DbError>;
98    async fn delete_deployment(&self, id: &str) -> Result<(), DbError>;
99
100    // --- Routing Config ---
101    async fn get_routing_config(&self) -> Result<Option<RoutingConfig>, DbError>;
102    async fn update_routing_config(
103        &self,
104        req: UpdateRoutingConfigRequest,
105    ) -> Result<RoutingConfig, DbError>;
106
107    async fn ping(&self) -> Result<(), DbError>;
108
109    // --- List operations ---
110    async fn list_teams(&self) -> Result<Vec<Team>, DbError> {
111        Ok(vec![])
112    }
113
114    async fn list_api_keys(&self, team_id: Option<String>) -> Result<Vec<ApiKey>, DbError> {
115        let _ = team_id;
116        Ok(vec![])
117    }
118
119    async fn get_usage_summary(&self, team_id: Option<String>) -> Result<UsageSummary, DbError> {
120        let _ = team_id;
121        Ok(UsageSummary::default())
122    }
123}
124
125#[derive(Debug, Clone, Serialize, Deserialize)]
126pub struct Team {
127    pub id: String,
128    pub name: String,
129    pub budget_cents: i64,
130    pub created_at: DateTime<Utc>,
131    pub updated_at: DateTime<Utc>,
132}
133
134#[derive(Debug, Clone, Serialize, Deserialize)]
135pub struct User {
136    pub id: String,
137    pub team_id: String,
138    pub email: String,
139    pub role: String,
140    #[serde(skip_serializing)]
141    pub password_hash: Option<String>,
142    pub created_at: DateTime<Utc>,
143}
144
145#[derive(Debug, Clone, Serialize, Deserialize)]
146pub struct ApiKey {
147    pub id: String,
148    #[serde(default, skip_serializing)]
149    pub key_hash: String,
150    pub user_id: String,
151    pub team_id: String,
152    pub name: Option<String>,
153    pub is_active: bool,
154    pub created_at: DateTime<Utc>,
155    pub expires_at: Option<DateTime<Utc>>,
156}
157
158#[derive(Debug, Clone, Serialize, Deserialize)]
159pub struct ModelAlias {
160    pub id: String,
161    pub team_id: String,
162    pub alias: String,
163    pub target_model: String,
164    pub provider: String,
165    pub created_at: DateTime<Utc>,
166}
167
168#[derive(Debug, Clone, Serialize, Deserialize)]
169pub struct Quota {
170    pub id: String,
171    pub team_id: String,
172    pub rpm_limit: i32,
173    pub tpm_limit: i32,
174    pub updated_at: DateTime<Utc>,
175}
176
177#[derive(Debug, Clone, Serialize, Deserialize)]
178pub struct NewUsageLog {
179    pub team_id: String,
180    pub api_key_id: String,
181    pub model: String,
182    pub input_tokens: i32,
183    pub output_tokens: i32,
184    pub response_time_ms: i64,
185}
186
187#[derive(Debug, Clone, Serialize, Deserialize)]
188pub struct UsageLog {
189    pub id: String,
190    pub team_id: String,
191    pub api_key_id: String,
192    pub model: String,
193    pub input_tokens: i32,
194    pub output_tokens: i32,
195    pub response_time_ms: i64,
196    pub recorded_at: DateTime<Utc>,
197}