headless_lms_models/
suotar_endpoint_rate_limits.rs1use utoipa::ToSchema;
6
7use crate::prelude::*;
8use crate::suotar_api_calls::SuotarEndpoint;
9
10#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema)]
12pub struct SuotarEndpointRateLimit {
13 pub endpoint: SuotarEndpoint,
14 pub updated_at: DateTime<Utc>,
16 pub rate_share: f32,
18 pub full_rate_per_minute: i32,
20 pub available: i32,
22}
23
24#[derive(Debug, Clone, PartialEq)]
26pub struct SuotarEndpointRateLimitReport {
27 pub endpoint: SuotarEndpoint,
28 pub rate_share: f32,
29 pub full_rate_per_minute: i32,
30 pub available: i32,
31}
32
33pub async fn upsert(
35 conn: &mut PgConnection,
36 state: &SuotarEndpointRateLimitReport,
37) -> ModelResult<()> {
38 sqlx::query!(
39 r#"
40INSERT INTO suotar_endpoint_rate_limits (
41 endpoint,
42 rate_share,
43 full_rate_per_minute,
44 available
45 )
46VALUES ($1, $2, $3, $4) ON CONFLICT (endpoint) DO
47UPDATE
48SET rate_share = EXCLUDED.rate_share,
49 full_rate_per_minute = EXCLUDED.full_rate_per_minute,
50 available = EXCLUDED.available
51 "#,
52 state.endpoint as SuotarEndpoint,
53 state.rate_share,
54 state.full_rate_per_minute,
55 state.available,
56 )
57 .execute(conn)
58 .await?;
59 Ok(())
60}
61
62pub async fn get_all(conn: &mut PgConnection) -> ModelResult<Vec<SuotarEndpointRateLimit>> {
64 let res = sqlx::query_as!(
65 SuotarEndpointRateLimit,
66 r#"
67SELECT endpoint,
68 updated_at,
69 rate_share,
70 full_rate_per_minute,
71 available
72FROM suotar_endpoint_rate_limits
73ORDER BY endpoint
74 "#,
75 )
76 .fetch_all(conn)
77 .await?;
78 Ok(res)
79}