1use crate::{models::InsertQueryBuilder, prelude::*};
2use compact_str::ToCompactString;
3use garde::Validate;
4use serde::{Deserialize, Serialize};
5use sqlx::{Row, postgres::PgRow};
6use std::{
7 collections::BTreeMap,
8 sync::{Arc, LazyLock},
9};
10use utoipa::ToSchema;
11
12#[derive(Serialize, Deserialize)]
13pub struct ServerActivity {
14 pub user: Option<super::user::User>,
15 pub impersonator: Option<Fetchable<super::user::User>>,
16 pub api_key: Option<Fetchable<super::user_api_key::UserApiKey>>,
17 pub schedule: Option<Fetchable<super::server_schedule::ServerSchedule>>,
18
19 pub event: compact_str::CompactString,
20 pub ip: Option<sqlx::types::ipnetwork::IpNetwork>,
21 pub data: serde_json::Value,
22
23 pub created: chrono::NaiveDateTime,
24
25 extension_data: super::ModelExtensionData,
26}
27
28impl BaseModel for ServerActivity {
29 const NAME: &'static str = "server_activity";
30
31 fn get_extension_list() -> &'static super::ModelExtensionList {
32 static EXTENSIONS: LazyLock<super::ModelExtensionList> =
33 LazyLock::new(|| std::sync::RwLock::new(Vec::new()));
34
35 &EXTENSIONS
36 }
37
38 fn get_extension_data(&self) -> &super::ModelExtensionData {
39 &self.extension_data
40 }
41
42 #[inline]
43 fn base_columns(prefix: Option<&str>) -> BTreeMap<&'static str, compact_str::CompactString> {
44 let prefix = prefix.unwrap_or_default();
45
46 let mut columns = BTreeMap::from([
47 (
48 "server_activities.impersonator_uuid",
49 compact_str::format_compact!("{prefix}impersonator_uuid"),
50 ),
51 (
52 "server_activities.api_key_uuid",
53 compact_str::format_compact!("{prefix}api_key_uuid"),
54 ),
55 (
56 "server_activities.schedule_uuid",
57 compact_str::format_compact!("{prefix}schedule_uuid"),
58 ),
59 (
60 "server_activities.event",
61 compact_str::format_compact!("{prefix}event"),
62 ),
63 (
64 "server_activities.ip",
65 compact_str::format_compact!("{prefix}ip"),
66 ),
67 (
68 "server_activities.data",
69 compact_str::format_compact!("{prefix}data"),
70 ),
71 (
72 "server_activities.created",
73 compact_str::format_compact!("{prefix}created"),
74 ),
75 ]);
76
77 columns.extend(super::user::User::base_columns(Some("user_")));
78
79 columns
80 }
81
82 #[inline]
83 fn map(prefix: Option<&str>, row: &PgRow) -> Result<Self, crate::database::DatabaseError> {
84 let prefix = prefix.unwrap_or_default();
85
86 Ok(Self {
87 user: if row
88 .try_get::<uuid::Uuid, _>("user_uuid".to_string().as_str())
89 .is_ok()
90 {
91 Some(super::user::User::map(Some("user_"), row)?)
92 } else {
93 None
94 },
95 impersonator: super::user::User::get_fetchable_from_row(
96 row,
97 compact_str::format_compact!("{prefix}impersonator_uuid"),
98 ),
99 api_key: super::user_api_key::UserApiKey::get_fetchable_from_row(
100 row,
101 compact_str::format_compact!("{prefix}api_key_uuid"),
102 ),
103 schedule: super::server_schedule::ServerSchedule::get_fetchable_from_row(
104 row,
105 compact_str::format_compact!("{prefix}schedule_uuid"),
106 ),
107 event: row.try_get(compact_str::format_compact!("{prefix}event").as_str())?,
108 ip: row.try_get(compact_str::format_compact!("{prefix}ip").as_str())?,
109 data: row.try_get(compact_str::format_compact!("{prefix}data").as_str())?,
110 created: row.try_get(compact_str::format_compact!("{prefix}created").as_str())?,
111 extension_data: Self::map_extensions(prefix, row)?,
112 })
113 }
114}
115
116impl ServerActivity {
117 pub async fn by_server_uuid_with_pagination(
118 database: &crate::database::Database,
119 server_uuid: uuid::Uuid,
120 page: i64,
121 per_page: i64,
122 search: Option<&str>,
123 ) -> Result<super::Pagination<Self>, crate::database::DatabaseError> {
124 let offset = (page - 1) * per_page;
125
126 let rows = sqlx::query(&format!(
127 r#"
128 SELECT {}, COUNT(*) OVER() AS total_count
129 FROM server_activities
130 LEFT JOIN users ON users.uuid = server_activities.user_uuid
131 LEFT JOIN roles ON roles.uuid = users.role_uuid
132 WHERE server_activities.server_uuid = $1 AND ($2 IS NULL OR server_activities.event ILIKE '%' || $2 || '%' OR users.username ILIKE '%' || $2 || '%')
133 ORDER BY server_activities.created DESC
134 LIMIT $3 OFFSET $4
135 "#,
136 Self::columns_sql(None)
137 ))
138 .bind(server_uuid)
139 .bind(search)
140 .bind(per_page)
141 .bind(offset)
142 .fetch_all(database.read())
143 .await?;
144
145 Ok(super::Pagination {
146 total: rows
147 .first()
148 .map_or(Ok(0), |row| row.try_get("total_count"))?,
149 per_page,
150 page,
151 data: rows
152 .into_iter()
153 .map(|row| Self::map(None, &row))
154 .try_collect_vec()?,
155 })
156 }
157
158 pub async fn delete_older_than(
159 database: &crate::database::Database,
160 cutoff: chrono::DateTime<chrono::Utc>,
161 ) -> Result<u64, crate::database::DatabaseError> {
162 let result = sqlx::query(
163 r#"
164 DELETE FROM server_activities
165 WHERE server_activities.created < $1
166 "#,
167 )
168 .bind(cutoff.naive_utc())
169 .execute(database.write())
170 .await?;
171
172 Ok(result.rows_affected())
173 }
174
175 pub async fn retain_latest_logs_per_server(
176 database: &crate::database::Database,
177 keep_count: i64,
178 ) -> Result<u64, crate::database::DatabaseError> {
179 let result = sqlx::query(
180 r#"
181 DELETE FROM server_activities
182 WHERE ctid IN (
183 SELECT ctid
184 FROM (
185 SELECT
186 ctid,
187 ROW_NUMBER() OVER (PARTITION BY server_activities.server_uuid ORDER BY server_activities.created DESC) rn
188 FROM server_activities
189 ) sub
190 WHERE sub.rn > $1
191 )
192 "#,
193 )
194 .bind(keep_count)
195 .execute(database.write())
196 .await?;
197
198 Ok(result.rows_affected())
199 }
200}
201
202#[async_trait::async_trait]
203impl IntoApiObject for ServerActivity {
204 type ApiObject = ApiServerActivity;
205 type ExtraArgs<'a> = (&'a crate::storage::StorageUrlRetriever<'a>, bool);
206
207 async fn into_api_object<'a>(
208 self,
209 state: &crate::State,
210 (storage_url_retriever, show_ip): Self::ExtraArgs<'a>,
211 ) -> Result<Self::ApiObject, crate::database::DatabaseError> {
212 let api_object = ApiServerActivity::init_hooks(&self, state).await?;
213
214 let user = if let Some(user) = self.user {
215 Some(user.into_api_object(state, storage_url_retriever).await?)
216 } else {
217 None
218 };
219
220 let impersonator = if let Some(impersonator) = self.impersonator {
221 Some(
222 impersonator
223 .fetch_cached(&state.database)
224 .await?
225 .into_api_object(state, storage_url_retriever)
226 .await?,
227 )
228 } else {
229 None
230 };
231
232 let api_object = finish_extendible!(
233 ApiServerActivity {
234 user,
235 impersonator,
236 event: self.event,
237 ip: if show_ip {
238 self.ip.map(|ip| ip.ip().to_compact_string())
239 } else {
240 None
241 },
242 data: self.data,
243 is_api: self.api_key.is_some(),
244 is_schedule: self.schedule.is_some(),
245 created: self.created.and_utc(),
246 },
247 api_object,
248 state
249 )?;
250
251 Ok(api_object)
252 }
253}
254
255#[derive(ToSchema, Deserialize, Validate)]
256pub struct CreateServerActivityOptions {
257 #[garde(skip)]
258 pub server_uuid: uuid::Uuid,
259 #[garde(skip)]
260 pub user_uuid: Option<uuid::Uuid>,
261 #[garde(skip)]
262 pub impersonator_uuid: Option<uuid::Uuid>,
263 #[garde(skip)]
264 pub api_key_uuid: Option<uuid::Uuid>,
265 #[garde(skip)]
266 pub schedule_uuid: Option<uuid::Uuid>,
267 #[garde(length(chars, min = 1, max = 255))]
268 #[schema(min_length = 1, max_length = 255)]
269 pub event: compact_str::CompactString,
270 #[garde(skip)]
271 #[schema(value_type = Option<String>)]
272 pub ip: Option<sqlx::types::ipnetwork::IpNetwork>,
273 #[garde(skip)]
274 pub data: serde_json::Value,
275
276 #[garde(skip)]
277 pub created: Option<chrono::NaiveDateTime>,
278}
279
280#[async_trait::async_trait]
281impl CreatableModel for ServerActivity {
282 type CreateOptions<'a> = CreateServerActivityOptions;
283 type CreateResult = ();
284
285 fn get_create_handlers() -> &'static LazyLock<CreateListenerList<Self>> {
286 static CREATE_LISTENERS: LazyLock<CreateListenerList<ServerActivity>> =
287 LazyLock::new(|| Arc::new(ModelHandlerList::default()));
288
289 &CREATE_LISTENERS
290 }
291
292 async fn create_with_transaction(
293 state: &crate::State,
294 mut options: Self::CreateOptions<'_>,
295 transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
296 ) -> Result<Self::CreateResult, crate::database::DatabaseError> {
297 options.validate()?;
298
299 let mut query_builder = InsertQueryBuilder::new("server_activities");
300
301 Self::run_create_handlers(&mut options, &mut query_builder, state, transaction).await?;
302
303 query_builder
304 .set("server_uuid", options.server_uuid)
305 .set("user_uuid", options.user_uuid)
306 .set("impersonator_uuid", options.impersonator_uuid)
307 .set("api_key_uuid", options.api_key_uuid)
308 .set("schedule_uuid", options.schedule_uuid)
309 .set("event", &options.event)
310 .set("ip", options.ip)
311 .set("data", &options.data);
312
313 if let Some(created) = options.created {
314 query_builder.set("created", created);
315 }
316
317 query_builder.execute(&mut **transaction).await?;
318
319 let mut result = ();
320
321 Self::run_after_create_handlers(&mut result, &options, state, transaction).await?;
322
323 Ok(result)
324 }
325}
326
327#[schema_extension_derive::extendible]
328#[init_args(ServerActivity, crate::State)]
329#[hook_args(crate::State)]
330#[derive(ToSchema, Serialize)]
331#[schema(title = "ServerActivity")]
332pub struct ApiServerActivity {
333 pub user: Option<super::user::ApiUser>,
334 pub impersonator: Option<super::user::ApiUser>,
335
336 pub event: compact_str::CompactString,
337 pub ip: Option<compact_str::CompactString>,
338 pub data: serde_json::Value,
339
340 pub is_api: bool,
341 pub is_schedule: bool,
342
343 pub created: chrono::DateTime<chrono::Utc>,
344}