Skip to content

Commit

Permalink
Simplify area model
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Mar 6, 2025
1 parent d9bddd2 commit ac410d5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
5 changes: 3 additions & 2 deletions src/area/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use rusqlite::{named_params, Connection, Row};
use serde_json::{Map, Value};
use time::{format_description::well_known::Rfc3339, OffsetDateTime};

#[derive(Debug, PartialEq, Eq, Hash)]
const TABLE_NAME: &str = "area";

#[derive(Debug, PartialEq, Eq)]
pub struct Area {
pub id: i64,
pub tags: Map<String, Value>,
Expand All @@ -14,7 +16,6 @@ pub struct Area {
pub deleted_at: Option<OffsetDateTime>,
}

const TABLE_NAME: &str = "area";
const COL_ID: &str = "id";
const COL_TAGS: &str = "tags";
const COL_ALIAS: &str = "alias";
Expand Down
18 changes: 10 additions & 8 deletions src/rpc/generate_reports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ pub struct Res {

pub async fn run(admin: &Admin, pool: &Pool, conf: &Conf) -> Result<Res> {
let started_at = OffsetDateTime::now_utc();
let res = pool.get().await?.interact(generate_reports).await??;
let res = pool
.get()
.await?
.interact(|conn| generate_reports(&conn))
.await??;
let time_s = (OffsetDateTime::now_utc() - started_at).as_seconds_f64();
if res > 0 {
discord::post_message(
Expand All @@ -41,7 +45,7 @@ pub async fn run(admin: &Admin, pool: &Pool, conf: &Conf) -> Result<Res> {
})
}

pub fn generate_reports(conn: &mut Connection) -> Result<usize> {
pub fn generate_reports(conn: &Connection) -> Result<usize> {
let today = OffsetDateTime::now_utc().date();
info!(date = ?today, "Generating report");
let today_reports = Report::select_by_date(&today, None, conn)?;
Expand All @@ -51,18 +55,16 @@ pub fn generate_reports(conn: &mut Connection) -> Result<usize> {
}
let all_areas = Area::select_all_except_deleted(conn)?;
let all_elements = Element::select_all_except_deleted(conn)?;
let mut reports: HashMap<Area, Map<String, Value>> = HashMap::new();
let mut reports: HashMap<i64, Map<String, Value>> = HashMap::new();
for area in all_areas {
let area_elements = crate::element::service::filter_by_area(&all_elements, &area)?;
if let Some(report) = generate_new_report_if_necessary(&area, area_elements, conn)? {
reports.insert(area, report);
reports.insert(area.id, report);
}
}
let sp = conn.savepoint()?;
for (area, report) in &reports {
insert_report(area.id, report, &sp)?;
for (area_id, report) in &reports {
insert_report(*area_id, report, conn)?;
}
sp.commit()?;
Ok(reports.len())
}

Expand Down

0 comments on commit ac410d5

Please sign in to comment.