Skip to content
Clone

feat(db): add lprovider db entities (billing aggregate, call log, tenant quota, wallet, freeze log, ledger, admin audit)

zhenyi committed
parent 0404528 commit e05c764 Browse files

8 files changed

+238 −0

File tree 8
Added lib/db/lprovider/llm_billing_aggregate.rs View lib/db/lprovider/llm_billing_aggregate.rs
@@ -0,0 +1,36 @@
1use sea_orm::entity::prelude::*;
2use time::OffsetDateTime;
3
4#[sea_orm::model]
5#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
6#[sea_orm(table_name = "llm_billing_aggregate")]
7pub struct Model {
8 #[sea_orm(primary_key)]
9 pub id: i64,
10 pub tenant_id: i64,
11 pub subject_type: String,
12 pub subject_id: i64,
13 pub period_start: OffsetDateTime,
14 pub period_end: OffsetDateTime,
15 pub period_kind: String,
16 pub model_alias: String,
17 pub upstream_code: String,
18 #[sea_orm(default_value = "0")]
19 pub call_count: i64,
20 #[sea_orm(default_value = "0")]
21 pub success_count: i64,
22 #[sea_orm(default_value = "0")]
23 pub error_count: i64,
24 #[sea_orm(default_value = "0")]
25 pub total_input_tokens: i64,
26 #[sea_orm(default_value = "0")]
27 pub total_output_tokens: i64,
28 #[sea_orm(default_value = "0")]
29 pub total_cache_hit_tokens: i64,
30 #[sea_orm(default_value = "0")]
31 pub total_points: i64,
32 pub created_at: OffsetDateTime,
33 pub updated_at: OffsetDateTime,
34}
35
36impl ActiveModelBehavior for ActiveModel {}
Added lib/db/lprovider/llm_call_log.rs View lib/db/lprovider/llm_call_log.rs
@@ -0,0 +1,77 @@
1use sea_orm::entity::prelude::*;
2use time::OffsetDateTime;
3
4#[sea_orm::model]
5#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
6#[sea_orm(table_name = "llm_call_log")]
7pub struct Model {
8 #[sea_orm(primary_key)]
9 pub id: i64,
10 #[sea_orm(indexed, unique)]
11 pub request_id: Uuid,
12 pub trace_id: Option<Uuid>,
13 pub tenant_id: i64,
14 pub api_key_id: Option<i64>,
15 pub subject_type: Option<String>,
16 pub subject_id: Option<i64>,
17 pub wallet_id: Option<i64>,
18 #[sea_orm(default_value = "false")]
19 pub anonymous: bool,
20 pub subject_path: Option<String>,
21 pub session_key: Option<String>,
22 pub model_alias: String,
23 pub upstream_code: String,
24 pub requested_model: Option<String>,
25 pub chosen_strategy: Option<String>,
26 #[sea_orm(default_value = "0")]
27 pub tier_cost: i32,
28 #[sea_orm(default_value = "0")]
29 pub tier_quality: i32,
30 #[sea_orm(default_value = "0")]
31 pub tier_latency: i32,
32 pub key_label: Option<String>,
33 pub caller: Option<String>,
34 pub caller_service: Option<String>,
35 #[sea_orm(default_value = "0")]
36 pub priority: i32,
37 #[sea_orm(default_value = "false")]
38 pub streamed: bool,
39 pub status: String,
40 pub error_kind: Option<String>,
41 pub error_message: Option<String>,
42 #[sea_orm(default_value = "0")]
43 pub prompt_tokens: i32,
44 #[sea_orm(default_value = "0")]
45 pub completion_tokens: i32,
46 #[sea_orm(default_value = "0")]
47 pub cache_hit_tokens: i32,
48 #[sea_orm(default_value = "0")]
49 pub cache_miss_tokens: i32,
50 #[sea_orm(default_value = "0")]
51 pub billable_input_tokens: i32,
52 #[sea_orm(default_value = "0")]
53 pub billable_output_tokens: i32,
54 #[sea_orm(default_value = "0")]
55 pub points_input: i64,
56 #[sea_orm(default_value = "0")]
57 pub points_output: i64,
58 #[sea_orm(default_value = "0")]
59 pub points_cache_hit: i64,
60 #[sea_orm(default_value = "0")]
61 pub points_total: i64,
62 #[sea_orm(default_value = "0")]
63 pub points_rate_input: i64,
64 #[sea_orm(default_value = "0")]
65 pub points_rate_output: i64,
66 #[sea_orm(default_value = "0")]
67 pub points_rate_cache_hit: i64,
68 pub request_started_at: OffsetDateTime,
69 pub response_completed_at: Option<OffsetDateTime>,
70 #[sea_orm(default_value = "0")]
71 pub latency_ms: i32,
72 pub client_ip: Option<String>,
73 pub user_agent: Option<String>,
74 pub created_at: OffsetDateTime,
75}
76
77impl ActiveModelBehavior for ActiveModel {}
Added lib/db/lprovider/llm_tenant_quota.rs View lib/db/lprovider/llm_tenant_quota.rs
@@ -0,0 +1,27 @@
1use sea_orm::entity::prelude::*;
2use time::OffsetDateTime;
3
4#[sea_orm::model]
5#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
6#[sea_orm(table_name = "llm_tenant_quota")]
7pub struct Model {
8 #[sea_orm(primary_key)]
9 pub id: i64,
10 pub tenant_id: i64,
11 pub subject_type: String,
12 pub subject_id: i64,
13 #[sea_orm(default_value = "0")]
14 pub monthly_quota_points: i64,
15 #[sea_orm(default_value = "0")]
16 pub monthly_quota_input_tokens: i64,
17 #[sea_orm(default_value = "0")]
18 pub monthly_quota_output_tokens: i64,
19 #[sea_orm(default_value = "false")]
20 pub hard_limit: bool,
21 #[sea_orm(default_value = "true")]
22 pub enabled: bool,
23 pub created_at: OffsetDateTime,
24 pub updated_at: OffsetDateTime,
25}
26
27impl ActiveModelBehavior for ActiveModel {}
Added lib/db/lprovider/llm_wallet.rs View lib/db/lprovider/llm_wallet.rs
@@ -0,0 +1,28 @@
1use sea_orm::entity::prelude::*;
2use time::OffsetDateTime;
3
4#[sea_orm::model]
5#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
6#[sea_orm(table_name = "llm_wallet")]
7pub struct Model {
8 #[sea_orm(primary_key, auto_increment = false)]
9 pub wallet_id: i64,
10 pub owner_type: String,
11 pub owner_id: i64,
12 pub tenant_id: i64,
13 pub status: String,
14 #[sea_orm(default_value = "0")]
15 pub balance_points: i64,
16 #[sea_orm(default_value = "0")]
17 pub total_topped_up: i64,
18 #[sea_orm(default_value = "0")]
19 pub total_consumed: i64,
20 #[sea_orm(default_value = "0")]
21 pub total_refunded: i64,
22 pub last_topup_at: Option<OffsetDateTime>,
23 pub last_consume_at: Option<OffsetDateTime>,
24 pub created_at: OffsetDateTime,
25 pub updated_at: OffsetDateTime,
26}
27
28impl ActiveModelBehavior for ActiveModel {}
Added lib/db/lprovider/llm_wallet_freeze_log.rs View lib/db/lprovider/llm_wallet_freeze_log.rs
@@ -0,0 +1,17 @@
1use sea_orm::entity::prelude::*;
2use time::OffsetDateTime;
3
4#[sea_orm::model]
5#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
6#[sea_orm(table_name = "llm_wallet_freeze_log")]
7pub struct Model {
8 #[sea_orm(primary_key)]
9 pub id: i64,
10 pub wallet_id: i64,
11 pub action: String,
12 pub reason: String,
13 pub operator: Option<String>,
14 pub created_at: OffsetDateTime,
15}
16
17impl ActiveModelBehavior for ActiveModel {}
Added lib/db/lprovider/llm_wallet_ledger.rs View lib/db/lprovider/llm_wallet_ledger.rs
@@ -0,0 +1,24 @@
1use sea_orm::entity::prelude::*;
2use time::OffsetDateTime;
3
4#[sea_orm::model]
5#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
6#[sea_orm(table_name = "llm_wallet_ledger")]
7pub struct Model {
8 #[sea_orm(primary_key)]
9 pub id: i64,
10 pub wallet_id: i64,
11 #[sea_orm(indexed)]
12 pub request_id: Option<Uuid>,
13 #[sea_orm(default_value = "0")]
14 pub delta_points: i64,
15 #[sea_orm(default_value = "0")]
16 pub balance_after: i64,
17 pub kind: String,
18 pub idempotency_key: Option<String>,
19 pub reason: Option<String>,
20 pub operator: Option<String>,
21 pub created_at: OffsetDateTime,
22}
23
24impl ActiveModelBehavior for ActiveModel {}
Added lib/db/lprovider/lprovider_admin_audit.rs View lib/db/lprovider/lprovider_admin_audit.rs
@@ -0,0 +1,22 @@
1use sea_orm::entity::prelude::*;
2use time::OffsetDateTime;
3
4#[sea_orm::model]
5#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
6#[sea_orm(table_name = "lprovider_admin_audit")]
7pub struct Model {
8 #[sea_orm(primary_key)]
9 pub id: i64,
10 pub operator: String,
11 pub rpc_method: String,
12 pub instance_id: Option<String>,
13 pub target: Option<String>,
14 pub change_summary: Option<String>,
15 pub config_sha256: Option<String>,
16 pub effect_mode: Option<String>,
17 pub result: String,
18 pub error_message: Option<String>,
19 pub created_at: OffsetDateTime,
20}
21
22impl ActiveModelBehavior for ActiveModel {}
Added lib/db/lprovider/mod.rs View lib/db/lprovider/mod.rs
@@ -0,0 +1,7 @@
1pub mod llm_billing_aggregate;
2pub mod llm_call_log;
3pub mod llm_tenant_quota;
4pub mod llm_wallet;
5pub mod llm_wallet_freeze_log;
6pub mod llm_wallet_ledger;
7pub mod lprovider_admin_audit;