1#![allow(clippy::implicit_hasher, clippy::extra_unused_lifetimes)]
2use std::fmt;
3
4use diesel::result::Error;
5use diesel::*;
6
7use crate::concerns::CortexInsertable;
8use crate::schema::log_errors;
9use crate::schema::log_fatals;
10use crate::schema::log_infos;
11use crate::schema::log_invalids;
12use crate::schema::log_warnings;
13use crate::schema::task_runtimes;
14
15use super::tasks::Task;
16
17#[derive(Identifiable, Queryable, AsChangeset, Associations, Clone, Debug)]
20#[diesel(belongs_to(Task))]
21pub struct LogInfo {
23 pub id: i64,
25 pub task_id: i64,
27 pub category: Option<String>,
29 pub what: Option<String>,
31 pub details: Option<String>,
33}
34#[derive(Insertable, Clone, Debug)]
35#[diesel(table_name = log_infos)]
36pub struct NewLogInfo {
38 pub task_id: i64,
40 pub category: String,
42 pub what: String,
44 pub details: String,
46}
47
48#[derive(Insertable, Clone, Debug)]
49#[diesel(table_name = task_runtimes)]
50pub struct NewTaskRuntime {
54 pub task_id: i64,
56 pub service_id: i32,
59 pub runtime_ms: i32,
61}
62
63#[derive(Identifiable, Queryable, AsChangeset, Associations, Clone, Debug)]
64#[diesel(belongs_to(Task))]
65pub struct LogWarning {
67 pub id: i64,
69 pub task_id: i64,
71 pub category: Option<String>,
73 pub what: Option<String>,
75 pub details: Option<String>,
77 pub recorded_at: Option<chrono::DateTime<chrono::Utc>>,
81}
82#[derive(Insertable, Clone, Debug)]
83#[diesel(table_name = log_warnings)]
84pub struct NewLogWarning {
86 pub task_id: i64,
88 pub category: String,
90 pub what: String,
92 pub details: String,
94}
95
96#[derive(Identifiable, Queryable, AsChangeset, Associations, Clone, Debug)]
97#[diesel(belongs_to(Task))]
98pub struct LogError {
100 pub id: i64,
102 pub task_id: i64,
104 pub category: Option<String>,
106 pub what: Option<String>,
108 pub details: Option<String>,
110 pub recorded_at: Option<chrono::DateTime<chrono::Utc>>,
114}
115#[derive(Insertable, Clone, Debug)]
116#[diesel(table_name = log_errors)]
117pub struct NewLogError {
119 pub task_id: i64,
121 pub category: String,
123 pub what: String,
125 pub details: String,
127}
128
129#[derive(Identifiable, Queryable, AsChangeset, Associations, Clone, Debug)]
130#[diesel(belongs_to(Task))]
131pub struct LogFatal {
133 pub id: i64,
135 pub task_id: i64,
137 pub category: Option<String>,
139 pub what: Option<String>,
141 pub details: Option<String>,
143 pub recorded_at: Option<chrono::DateTime<chrono::Utc>>,
147}
148#[derive(Insertable, Clone, Debug)]
149#[diesel(table_name = log_fatals)]
150pub struct NewLogFatal {
152 pub category: String,
154 pub task_id: i64,
156 pub what: String,
158 pub details: String,
160}
161
162#[derive(Identifiable, Queryable, AsChangeset, Clone, Associations, Debug)]
163#[diesel(belongs_to(Task))]
164pub struct LogInvalid {
166 pub id: i64,
168 pub task_id: i64,
170 pub category: Option<String>,
172 pub what: Option<String>,
174 pub details: Option<String>,
176}
177#[derive(Insertable, Clone, Debug)]
178#[diesel(table_name = log_invalids)]
179pub struct NewLogInvalid {
181 pub task_id: i64,
183 pub category: String,
185 pub what: String,
187 pub details: String,
189}
190
191pub trait LogRecord {
193 fn task_id(&self) -> i64;
195 fn category(&self) -> &str;
197 fn what(&self) -> &str;
199 fn details(&self) -> &str;
201 fn set_details(&mut self, new_details: String);
203 fn severity(&self) -> &str;
205 fn debug(&self, f: &mut fmt::Formatter) -> fmt::Result {
207 writeln!(
208 f,
209 "{}(category: {},\n\twhat: {},\n\tdetails: {})",
210 self.severity(),
211 self.category(),
212 self.what(),
213 self.details()
214 )
215 }
216}
217impl fmt::Debug for dyn LogRecord {
218 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.debug(f) }
219}
220impl fmt::Display for dyn LogRecord {
221 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.debug(f) }
222}
223
224impl LogRecord for LogInfo {
225 fn task_id(&self) -> i64 { self.task_id }
226 fn category(&self) -> &str { self.category.as_deref().unwrap_or_default() }
227 fn what(&self) -> &str { self.what.as_deref().unwrap_or_default() }
228 fn details(&self) -> &str { self.details.as_deref().unwrap_or_default() }
229 fn set_details(&mut self, new_details: String) { self.details = Some(new_details); }
230 fn severity(&self) -> &str { "info" }
231}
232impl LogRecord for NewLogInfo {
233 fn task_id(&self) -> i64 { self.task_id }
234 fn category(&self) -> &str { &self.category }
235 fn what(&self) -> &str { &self.what }
236 fn details(&self) -> &str { &self.details }
237 fn set_details(&mut self, new_details: String) { self.details = new_details; }
238 fn severity(&self) -> &str { "info" }
239}
240impl CortexInsertable for NewLogInfo {
241 fn create(&self, connection: &mut PgConnection) -> Result<usize, Error> {
242 insert_into(log_infos::table)
243 .values(self)
244 .execute(connection)
245 }
246}
247impl LogRecord for LogWarning {
248 fn task_id(&self) -> i64 { self.task_id }
249 fn category(&self) -> &str { self.category.as_deref().unwrap_or_default() }
250 fn what(&self) -> &str { self.what.as_deref().unwrap_or_default() }
251 fn details(&self) -> &str { self.details.as_deref().unwrap_or_default() }
252 fn set_details(&mut self, new_details: String) { self.details = Some(new_details); }
253 fn severity(&self) -> &str { "warning" }
254}
255impl LogRecord for NewLogWarning {
256 fn task_id(&self) -> i64 { self.task_id }
257 fn category(&self) -> &str { &self.category }
258 fn what(&self) -> &str { &self.what }
259 fn details(&self) -> &str { &self.details }
260 fn set_details(&mut self, new_details: String) { self.details = new_details; }
261 fn severity(&self) -> &str { "warning" }
262}
263impl CortexInsertable for NewLogWarning {
264 fn create(&self, connection: &mut PgConnection) -> Result<usize, Error> {
265 insert_into(log_warnings::table)
266 .values(self)
267 .execute(connection)
268 }
269}
270impl LogRecord for LogError {
271 fn task_id(&self) -> i64 { self.task_id }
272 fn category(&self) -> &str { self.category.as_deref().unwrap_or_default() }
273 fn what(&self) -> &str { self.what.as_deref().unwrap_or_default() }
274 fn details(&self) -> &str { self.details.as_deref().unwrap_or_default() }
275 fn set_details(&mut self, new_details: String) { self.details = Some(new_details); }
276 fn severity(&self) -> &str { "error" }
277}
278impl LogRecord for NewLogError {
279 fn task_id(&self) -> i64 { self.task_id }
280 fn category(&self) -> &str { &self.category }
281 fn what(&self) -> &str { &self.what }
282 fn details(&self) -> &str { &self.details }
283 fn set_details(&mut self, new_details: String) { self.details = new_details; }
284 fn severity(&self) -> &str { "error" }
285}
286impl CortexInsertable for NewLogError {
287 fn create(&self, connection: &mut PgConnection) -> Result<usize, Error> {
288 insert_into(log_errors::table)
289 .values(self)
290 .execute(connection)
291 }
292}
293impl LogRecord for LogFatal {
294 fn task_id(&self) -> i64 { self.task_id }
295 fn category(&self) -> &str { self.category.as_deref().unwrap_or_default() }
296 fn what(&self) -> &str { self.what.as_deref().unwrap_or_default() }
297 fn details(&self) -> &str { self.details.as_deref().unwrap_or_default() }
298 fn set_details(&mut self, new_details: String) { self.details = Some(new_details); }
299 fn severity(&self) -> &str { "fatal" }
300}
301impl LogRecord for NewLogFatal {
302 fn task_id(&self) -> i64 { self.task_id }
303 fn category(&self) -> &str { &self.category }
304 fn what(&self) -> &str { &self.what }
305 fn details(&self) -> &str { &self.details }
306 fn set_details(&mut self, new_details: String) { self.details = new_details; }
307 fn severity(&self) -> &str { "fatal" }
308}
309impl CortexInsertable for NewLogFatal {
310 fn create(&self, connection: &mut PgConnection) -> Result<usize, Error> {
311 insert_into(log_fatals::table)
312 .values(self)
313 .execute(connection)
314 }
315}
316impl LogRecord for LogInvalid {
317 fn task_id(&self) -> i64 { self.task_id }
318 fn category(&self) -> &str { self.category.as_deref().unwrap_or_default() }
319 fn what(&self) -> &str { self.what.as_deref().unwrap_or_default() }
320 fn details(&self) -> &str { self.details.as_deref().unwrap_or_default() }
321 fn set_details(&mut self, new_details: String) { self.details = Some(new_details); }
322 fn severity(&self) -> &str { "invalid" }
323}
324impl LogRecord for NewLogInvalid {
325 fn task_id(&self) -> i64 { self.task_id }
326 fn category(&self) -> &str { &self.category }
327 fn what(&self) -> &str { &self.what }
328 fn details(&self) -> &str { &self.details }
329 fn set_details(&mut self, new_details: String) { self.details = new_details; }
330 fn severity(&self) -> &str { "invalid" }
331}
332impl CortexInsertable for NewLogInvalid {
333 fn create(&self, connection: &mut PgConnection) -> Result<usize, Error> {
334 insert_into(log_invalids::table)
335 .values(self)
336 .execute(connection)
337 }
338}