view src/template_types.rs @ 61:51ea67f319e3

Improve recent sessions formatting and fix time bug
author Lewin Bormann <lbo@spheniscida.de>
date Sat, 23 Jul 2022 07:56:46 -0700
parents
children fd0237049be0
line wrap: on
line source

use crate::logsdb::RecentSessionsRow;
use rocket::serde::Serialize;
use std::collections::BTreeSet;

#[derive(Serialize)]
#[serde(crate = "rocket::serde")]
pub struct RecentSessionsTableRow {
    start: String,
    duration: String,
    count: i64,
    refer: Option<String>,
    origin_country: Option<String>,
    origin_city: Option<String>,
    ua: String,
    alltags: String,
}

impl RecentSessionsTableRow {
    pub fn from_row<F: time::formatting::Formattable>(
        r: RecentSessionsRow,
        off: time::UtcOffset,
        timeformat: &F,
    ) -> RecentSessionsTableRow {
        RecentSessionsTableRow {
            start: time::OffsetDateTime::from_unix_timestamp(r.start)
                .unwrap_or(time::OffsetDateTime::UNIX_EPOCH)
                .to_offset(off)
                .format(&timeformat)
                .unwrap_or(String::new()),
            duration: format!("{:.0}", time::Duration::seconds(r.duration)),
            count: r.count,
            refer: r.refer,
            origin_country: r.origin_country,
            origin_city: r.origin_city,
            ua: r.ua,
            alltags: BTreeSet::from_iter(r.alltags.split(","))
                .into_iter()
                .collect::<Vec<&str>>()
                .join(", "),
        }
    }
}