dashboard: Group messages by service
This commit is contained in:
@ -1,6 +1,9 @@
|
||||
use std::{cmp::max, collections::BTreeMap};
|
||||
|
||||
use rocket::{http::Status, response::content::RawHtml, State};
|
||||
use rocket_dyn_templates::{context, Template};
|
||||
use serde::Serialize;
|
||||
use snitch_lib::Severity;
|
||||
|
||||
use crate::database::Database;
|
||||
|
||||
@ -9,25 +12,33 @@ pub async fn index(db: &State<Database>) -> Result<RawHtml<Template>, Status> {
|
||||
// TODO:
|
||||
// maybe only show most recent error for each service in dashboard view, i.e.:
|
||||
// select distinct on (service) time, message from (select * from record order by time desc) as foo;
|
||||
let messages = db.get_all_log_messages().map_err(|e| {
|
||||
let records = db.get_all_log_messages().map_err(|e| {
|
||||
log::error!("failed to query database: {e}");
|
||||
Status::InternalServerError
|
||||
})?;
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct ServiceRecords<'a> {
|
||||
first_record: &'a Record,
|
||||
last_record: &'a Record,
|
||||
severity: Severity,
|
||||
all_records: Vec<&'a Record>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct Record {
|
||||
time: String,
|
||||
severity: String,
|
||||
severity: Severity,
|
||||
service: String,
|
||||
message: String,
|
||||
location: String,
|
||||
}
|
||||
|
||||
let messages: Vec<_> = messages
|
||||
let records: Vec<_> = records
|
||||
.into_iter()
|
||||
.map(|(_id, record)| Record {
|
||||
time: record.time.map(|time| time.to_string()).unwrap_or_default(),
|
||||
severity: record.severity.as_str().to_string(),
|
||||
severity: record.severity,
|
||||
service: record.service,
|
||||
message: record.message,
|
||||
location: record
|
||||
@ -38,10 +49,28 @@ pub async fn index(db: &State<Database>) -> Result<RawHtml<Template>, Status> {
|
||||
})
|
||||
.collect();
|
||||
|
||||
let mut records_by_service = BTreeMap::new();
|
||||
|
||||
for record in &records {
|
||||
let entry = records_by_service
|
||||
.entry(record.service.as_str())
|
||||
.or_insert_with(|| ServiceRecords {
|
||||
first_record: record,
|
||||
last_record: record,
|
||||
all_records: vec![],
|
||||
severity: record.severity,
|
||||
});
|
||||
|
||||
entry.last_record = record;
|
||||
entry.all_records.push(record);
|
||||
entry.severity = max(record.severity, entry.severity);
|
||||
}
|
||||
|
||||
Ok(RawHtml(Template::render(
|
||||
"index",
|
||||
context! {
|
||||
messages: &messages
|
||||
records: &records,
|
||||
records_by_service: &records_by_service,
|
||||
},
|
||||
)))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user