Merge pull request 'show-amount-trips' (#833) from show-amount-trips into staging
All checks were successful
CI/CD Pipeline / test (push) Successful in 12m47s
CI/CD Pipeline / deploy-staging (push) Successful in 7m0s
CI/CD Pipeline / deploy-main (push) Has been skipped

Reviewed-on: #833
This commit is contained in:
philipp 2025-01-06 13:14:55 +01:00
commit 5448558085
3 changed files with 40 additions and 14 deletions

View File

@ -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::<i64, usize>(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::<i64, usize>(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>) -> 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>) -> 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"),
}
}

View File

@ -32,13 +32,14 @@ async fn index_boat_kiosk(db: &State<SqlitePool>, _kiosk: KioskCookie) -> Templa
async fn index(db: &State<SqlitePool>, user: DonauLinzUser, year: Option<i32>) -> 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),
)
}

View File

@ -20,7 +20,7 @@
</div>
<div id="filter-result-js" class="search-result"></div>
<div class="border-r border-l border-gray-200 dark:border-primary-600">
{% set_global km = 0 %} {% set_global index = 1 %}
{% set_global km = 0 %} {% set_global km = 0 %} {% set_global index = 1 %}
{% for s in stat %}
<div class="border-t border-gray-200 dark:border-primary-600 bg-white dark:bg-primary-900 text-black dark:text-white flex justify-between items-center px-3 py-1"
data-filterable="true"
@ -35,6 +35,7 @@
</span>
<span class="grow">{{ s.name }}</span>
<span>{{ s.rowed_km }} km</span>
<span class="pl-3">{{ s.amount_trips }} Fahrt{{ s.amount_trips | pluralize(plural="en") }}</span>
{% set_global km = s.rowed_km %}
</div>
{% endfor %}
@ -44,6 +45,7 @@
<span class="text-sm text-gray-600 dark:text-gray-100 w-10"></span>
<span class="grow"><b>Summe Vereinsmitglieder</b></span>
<span><b>{{ club_km }} km</b></span>
<span class="pl-3"><b>{{ club_trips }} Fahrt{{ club_trips | pluralize(plural="en") }}</b></span>
</div>
<div class="border-t border-gray-200 dark:border-primary-600 bg-white dark:bg-primary-900 text-black dark:text-white flex justify-between items-center px-3 py-1"
data-filterable="false"
@ -51,6 +53,7 @@
<span class="text-sm text-gray-600 dark:text-gray-100 w-10"></span>
<span class="grow"><b>Summe {{ guest_km.name }}</b></span>
<span><b>{{ guest_km.rowed_km }} km</b></span>
<span class="pl-3"><b>{{ guest_km.amount_trips }} Fahrt{{ guest_km.amount_trips | pluralize(plural="en") }}</b></span>
</div>
<div class="border-t border-gray-200 dark:border-primary-600 border-b bg-white dark:bg-primary-900 text-black dark:text-white flex justify-between items-center px-3 py-1"
data-filterable="false"
@ -58,6 +61,7 @@
<span class="text-sm text-gray-600 dark:text-gray-100 w-10"></span>
<span class="grow"><b>Gesamtsumme</b></span>
<span><b>{{ club_km + guest_km.rowed_km }} km</b></span>
<span class="pl-3"><b>{{ guest_km.amount_trips + club_trips }} Fahrt{{ guest_km.amount_trips + club_trips | pluralize(plural="en") }}</b></span>
</div>
</div>
<div id="container" class="w-full"></div>