From fbb006e1c1251548a048612d74813e7c2babc908 Mon Sep 17 00:00:00 2001 From: tm26a21p Date: Fri, 14 Jun 2024 19:00:37 -0700 Subject: [PATCH] feat: metrics rendering improvement + bug fixing --- src/state.rs | 32 ++++++++++++++++++++++++++++++++ src/templates.rs | 2 +- templates/components/stats.html | 14 +++++++------- templates/htmx/location.html | 7 ++++++- 4 files changed, 46 insertions(+), 9 deletions(-) diff --git a/src/state.rs b/src/state.rs index a0df530..c713b5e 100644 --- a/src/state.rs +++ b/src/state.rs @@ -50,8 +50,12 @@ impl Common #[derive(Debug, Clone)] pub struct Metrics { + pub visited_last_month: usize, pub visited: usize, pub ip: String, + pub likes: usize, + pub issues_open: usize, + pub issues_closed: usize, } impl Metrics @@ -59,8 +63,36 @@ impl Metrics pub fn new() -> Self { Self { + visited_last_month: 6234, visited: 1, ip: "Unknown".to_string(), + likes: 0, + issues_open: 31, + issues_closed: 87, } } + + pub fn get_likes_ratio_over_last_month(&self) -> f64 + { + // 2 decimal places + let ratio = (self.likes as f64 / self.visited as f64) * 100.0; + (ratio * 100.0).round() / 100.0 + } + + pub fn get_percent_issues_closed(&self) -> f64 + { + // 2 decimal places + let percent = (self.issues_closed as f64 + / (self.issues_open + self.issues_closed) as f64) + * 100.0; + (percent * 100.0).round() / 100.0 + } + + pub fn get_percent_views_over_last_month(&self) -> f64 + { + // 2 decimal places + let percent = + (self.visited as f64 / self.visited_last_month as f64) * 100.0; + (percent * 100.0).round() / 100.0 + } } diff --git a/src/templates.rs b/src/templates.rs index 68b1d39..e0baec2 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -1,6 +1,6 @@ use askama_axum::Template; -use crate::{htmx_templates::GeoLocation, project::Project, state::Metrics}; +use crate::{project::Project, state::Metrics}; #[derive(Template)] #[template(path = "base.html")] diff --git a/templates/components/stats.html b/templates/components/stats.html index 661cd8f..dbc2aae 100644 --- a/templates/components/stats.html +++ b/templates/components/stats.html @@ -10,8 +10,8 @@
Total Likes
-
data
-
data more than last month
+
{{ metrics.likes }}
+
{{metrics.get_likes_ratio_over_last_month()}}% compared to last month
@@ -23,8 +23,8 @@
Website Views
-
data
-
data more than last month
+
{{metrics.visited }}
+
{{metrics.get_percent_views_over_last_month()}}% compared to last month
@@ -35,9 +35,9 @@
-
86%
-
Tasks done
-
31 tasks remaining
+
{{ metrics.get_percent_issues_closed()}}%
+
Issues resolved / Issues opened
+
{{ metrics.issues_closed - metrics.issues_open }} issues remaining
\ No newline at end of file diff --git a/templates/htmx/location.html b/templates/htmx/location.html index 1296dd2..4794517 100644 --- a/templates/htmx/location.html +++ b/templates/htmx/location.html @@ -9,4 +9,9 @@ Timezone {{location.timezone}} - \ No newline at end of file + + + + \ No newline at end of file