diff --git a/src/model/stat.rs b/src/model/stat.rs index dad5ca0..6ec6b16 100644 --- a/src/model/stat.rs +++ b/src/model/stat.rs @@ -98,6 +98,7 @@ ORDER BY #[derive(FromRow, Serialize, Clone)] pub struct Stat { name: String, + pub(crate) amount_trips: i32, pub(crate) rowed_km: i32, } @@ -108,9 +109,11 @@ impl Stat { None => chrono::Local::now().year(), }; //TODO: switch to query! macro again (once upgraded to sqlite 3.42 on server) - let rowed_km = sqlx::query(&format!( + // proper guests + let guests = sqlx::query(&format!( " -SELECT SUM((b.amount_seats - COALESCE(m.member_count, 0)) * l.distance_in_km) as total_guest_km +SELECT SUM((b.amount_seats - COALESCE(m.member_count, 0)) * l.distance_in_km) as total_guest_km, + SUM(b.amount_seats - COALESCE(m.member_count, 0)) AS amount_trips FROM logbook l JOIN boat b ON l.boat_id = b.id LEFT JOIN ( @@ -123,12 +126,15 @@ WHERE l.distance_in_km IS NOT NULL AND l.arrival LIKE '{year}-%' AND not b.exter )) .fetch_one(db) .await - .unwrap() - .get::(0) as i32; + .unwrap(); - let rowed_km_guests = sqlx::query(&format!( + let guest_km: i32 = guests.get(0); + let guest_amount_trips: i32 = guests.get(1); + + // e.g. scheckbücher + let guest_user = sqlx::query(&format!( " -SELECT CAST(SUM(l.distance_in_km) AS INTEGER) AS rowed_km +SELECT CAST(SUM(l.distance_in_km) AS INTEGER) AS rowed_km, COUNT(*) AS amount_trips FROM user u INNER JOIN rower r ON u.id = r.rower_id INNER JOIN logbook l ON r.logbook_id = l.id @@ -145,15 +151,27 @@ AND u.name != 'Externe Steuerperson'; )) .fetch_one(db) .await - .unwrap() - .get::(0) as i32; + .unwrap(); + + let guest_user_km: i32 = guest_user.get(0); + let guest_user_amount_trips: i32 = guest_user.get(1); Stat { name: "Gäste".into(), - rowed_km: rowed_km + rowed_km_guests, + amount_trips: guest_amount_trips + guest_user_amount_trips, + rowed_km: guest_km + guest_user_km, } } + pub async fn trips_people(db: &SqlitePool, year: Option) -> i32 { + let stats = Self::people(db, year).await; + let mut sum = 0; + for stat in stats { + sum += stat.amount_trips; + } + + sum + } pub async fn sum_people(db: &SqlitePool, year: Option) -> i32 { let stats = Self::people(db, year).await; let mut sum = 0; @@ -172,7 +190,7 @@ AND u.name != 'Externe Steuerperson'; //TODO: switch to query! macro again (once upgraded to sqlite 3.42 on server) sqlx::query(&format!( " -SELECT u.name, CAST(SUM(l.distance_in_km) AS INTEGER) AS rowed_km +SELECT u.name, CAST(SUM(l.distance_in_km) AS INTEGER) AS rowed_km, COUNT(*) AS amount_trips FROM ( SELECT * FROM user WHERE id IN ( @@ -194,6 +212,7 @@ ORDER BY rowed_km DESC, u.name; .into_iter() .map(|row| Stat { name: row.get("name"), + amount_trips: row.get("amount_trips"), rowed_km: row.get("rowed_km"), }) .collect() @@ -203,7 +222,7 @@ ORDER BY rowed_km DESC, u.name; //TODO: switch to query! macro again (once upgraded to sqlite 3.42 on server) let row = sqlx::query(&format!( " -SELECT u.name, CAST(SUM(l.distance_in_km) AS INTEGER) AS rowed_km +SELECT u.name, CAST(SUM(l.distance_in_km) AS INTEGER) AS rowed_km, COUNT(*) AS amount_trips FROM ( SELECT * FROM user WHERE id={} @@ -220,6 +239,7 @@ WHERE l.distance_in_km IS NOT NULL; Stat { name: row.get("name"), + amount_trips: row.get("amount_trips"), rowed_km: row.get("rowed_km"), } } @@ -232,7 +252,7 @@ WHERE l.distance_in_km IS NOT NULL; //TODO: switch to query! macro again (once upgraded to sqlite 3.42 on server) let row = sqlx::query(&format!( " -SELECT u.name, CAST(SUM(l.distance_in_km) AS INTEGER) AS rowed_km +SELECT u.name, CAST(SUM(l.distance_in_km) AS INTEGER) AS rowed_km, COUNT(*) AS amount_trips FROM ( SELECT * FROM user WHERE id={} @@ -249,6 +269,7 @@ WHERE l.distance_in_km IS NOT NULL AND l.arrival LIKE '{year}-%'; Stat { name: row.get("name"), + amount_trips: row.get("amount_trips"), rowed_km: row.get("rowed_km"), } } diff --git a/src/tera/stat.rs b/src/tera/stat.rs index 5c7217c..f1823bf 100644 --- a/src/tera/stat.rs +++ b/src/tera/stat.rs @@ -32,13 +32,14 @@ async fn index_boat_kiosk(db: &State, _kiosk: KioskCookie) -> Templa async fn index(db: &State, user: DonauLinzUser, year: Option) -> Template { let stat = Stat::people(db, year).await; let club_km = Stat::sum_people(db, year).await; + let club_trips = Stat::trips_people(db, year).await; let guest_km = Stat::guest(db, year).await; let personal = stat::get_personal(db, &user).await; let kiosk = false; Template::render( "stat.people", - context!(loggedin_user: &UserWithDetails::from_user(user.into_inner(), db).await, stat, personal, kiosk, guest_km, club_km), + context!(loggedin_user: &UserWithDetails::from_user(user.into_inner(), db).await, stat, personal, kiosk, guest_km, club_km, club_trips), ) } diff --git a/templates/stat.people.html.tera b/templates/stat.people.html.tera index 3c8e361..9856dd6 100644 --- a/templates/stat.people.html.tera +++ b/templates/stat.people.html.tera @@ -20,7 +20,7 @@
- {% set_global km = 0 %} {% set_global index = 1 %} + {% set_global km = 0 %} {% set_global km = 0 %} {% set_global index = 1 %} {% for s in stat %}
{{ s.name }} {{ s.rowed_km }} km + {{ s.amount_trips }} Fahrt{{ s.amount_trips | pluralize(plural="en") }} {% set_global km = s.rowed_km %}
{% endfor %} @@ -44,6 +45,7 @@ Summe Vereinsmitglieder {{ club_km }} km + {{ club_trips }} Fahrt{{ club_trips | pluralize(plural="en") }}
Summe {{ guest_km.name }} {{ guest_km.rowed_km }} km + {{ guest_km.amount_trips }} Fahrt{{ guest_km.amount_trips | pluralize(plural="en") }}
Gesamtsumme {{ club_km + guest_km.rowed_km }} km + {{ guest_km.amount_trips + club_trips }} Fahrt{{ guest_km.amount_trips + club_trips | pluralize(plural="en") }}