Skip to content

Commit

Permalink
Activity monthly ranks top 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Celeo committed Mar 16, 2024
1 parent 4b12cf8 commit f25e2b1
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 38 deletions.
77 changes: 44 additions & 33 deletions src/endpoints/facility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,15 +239,26 @@ async fn page_activity(
State(state): State<Arc<AppState>>,
session: Session,
) -> Result<Html<String>, AppError> {
#[derive(Debug, Serialize)]
struct ActivityMonth {
value: u32,
position: Option<u8>,
}

impl From<u32> for ActivityMonth {
fn from(value: u32) -> Self {
Self {
value,
position: None,
}
}
}

#[derive(Debug, Serialize)]
struct ControllerActivity {
name: String,
ois: String,
month_0: u32,
month_1: u32,
month_2: u32,
month_3: u32,
month_4: u32,
months: Vec<ActivityMonth>,
}

// this could be a join, but oh well
Expand All @@ -258,6 +269,7 @@ async fn page_activity(
.fetch_all(&state.db)
.await?;

// time ranges
let now = Utc::now();
let months: [String; 5] = [
now.format("%Y-%m").to_string(),
Expand All @@ -279,49 +291,48 @@ async fn page_activity(
.to_string(),
];

let activity_data: Vec<ControllerActivity> = controllers
let mut activity_data: Vec<ControllerActivity> = controllers
.iter()
.map(|controller| {
let this_controller: Vec<_> = activity
.iter()
.filter(|a| a.cid == controller.cid)
.collect();
let months = (0..=4)
.map(|month| {
this_controller
.iter()
.filter(|a| a.month == months[month])
.map(|a| a.minutes)
.sum::<u32>()
.into()
})
.collect();
ControllerActivity {
name: format!("{} {}", controller.first_name, controller.last_name),
ois: match &controller.operating_initials {
Some(ois) => ois.to_owned(),
None => String::new(),
},
month_0: this_controller
.iter()
.filter(|a| a.month == months[0])
.map(|a| a.minutes)
.sum(),
month_1: this_controller
.iter()
.filter(|a| a.month == months[1])
.map(|a| a.minutes)
.sum(),
month_2: this_controller
.iter()
.filter(|a| a.month == months[2])
.map(|a| a.minutes)
.sum(),
month_3: this_controller
.iter()
.filter(|a| a.month == months[3])
.map(|a| a.minutes)
.sum(),
month_4: this_controller
.iter()
.filter(|a| a.month == months[4])
.map(|a| a.minutes)
.sum(),
months,
}
})
.collect();

// TODO top 3 controllers for each month
// top 3 controllers for each month
for month in 0..=4 {
activity_data
.iter()
.enumerate()
.map(|(index, data)| (index, data.months[month].value))
.sorted_by(|a, b| Ord::cmp(&b.1, &a.1))
.map(|(index, _data)| index)
.take(3)
.enumerate()
.for_each(|(rank, controller_index)| {
activity_data[controller_index].months[month].position = Some(rank as u8);
});
}

let user_info: Option<UserInfo> = session.get(SESSION_USER_INFO_KEY).await?;
let template = state.templates.get_template("activity")?;
Expand All @@ -343,7 +354,7 @@ pub fn router(templates: &mut Environment) -> Router<Arc<AppState>> {
let hours = total_minutes / 60;
let minutes = total_minutes % 60;
if hours > 0 || minutes > 0 {
format!("{hours}h{minutes}m")
format!("{hours}h{minutes}m")
} else {
String::new()
}
Expand Down
31 changes: 26 additions & 5 deletions templates/activity.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@

{% block title %}Activity | {{ super() }}{% endblock %}

{% block head_extra %}
<style>
.rank-1 {
font-weight: bolder;
color: gold;
}
.rank-2 {
font-weight: bolder;
color: silver;
}
.rank-3 {
font-weight: bolder;
color: #8C7853;
}
</style>
{% endblock %}

{% block body %}

<h2>Activity</h2>
Expand All @@ -23,11 +40,15 @@
<td>
{{ row.name }} {% if row.ois %}({{ row.ois }}){% endif %}
</td>
<td>{{ row.month_0|minutes_to_hm }}</td>
<td>{{ row.month_1|minutes_to_hm }}</td>
<td>{{ row.month_2|minutes_to_hm }}</td>
<td>{{ row.month_3|minutes_to_hm }}</td>
<td>{{ row.month_4|minutes_to_hm }}</td>
{% for month in row.months %}
<td>
{{ month.value|minutes_to_hm }}
{% if month.position is none %}
{% else %}
<span class="rank-{{ month.position + 1 }}">(#{{ month.position + 1 }})</span>
{% endif %}
</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
Expand Down

0 comments on commit f25e2b1

Please sign in to comment.