Skip to content

Commit

Permalink
feat: metrics rendering improvement + bug fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
tm26a21p committed Jun 15, 2024
1 parent 495e613 commit fbb006e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
32 changes: 32 additions & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,49 @@ 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
{
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
}
}
2 changes: 1 addition & 1 deletion src/templates.rs
Original file line number Diff line number Diff line change
@@ -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")]
Expand Down
14 changes: 7 additions & 7 deletions templates/components/stats.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
</svg>
</div>
<div class="stat-title">Total Likes</div>
<div class="stat-value text-primary"> data </div>
<div class="stat-desc"> data more than last month</div>
<div class="stat-value text-primary"> {{ metrics.likes }} </div>
<div class="stat-desc"> {{metrics.get_likes_ratio_over_last_month()}}% compared to last month</div>
</div>

<div class="stat">
Expand All @@ -23,8 +23,8 @@
</svg>
</div>
<div class="stat-title">Website Views</div>
<div class="stat-value text-secondary">data </div>
<div class="stat-desc"> data more than last month</div>
<div class="stat-value text-secondary"> {{metrics.visited }} </div>
<div class="stat-desc"> {{metrics.get_percent_views_over_last_month()}}% compared to last month</div>
</div>

<div class="stat">
Expand All @@ -35,9 +35,9 @@
</div>
</div>
</div>
<div class="stat-value">86%</div>
<div class="stat-title">Tasks done</div>
<div class="stat-desc text-secondary">31 tasks remaining</div>
<div class="stat-value">{{ metrics.get_percent_issues_closed()}}%</div>
<div class="stat-title">Issues resolved / Issues opened</div>
<div class="stat-desc text-secondary">{{ metrics.issues_closed - metrics.issues_open }} issues remaining</div>
</div>

</div>
7 changes: 6 additions & 1 deletion templates/htmx/location.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@
<tr>
<td class="border px-4 py-2">Timezone</td>
<td class="border px-4 py-2">{{location.timezone}}</td>
</tr>
</tr>
<input type="hidden" id="userAgent" name="userAgent" value="" hx-post="/user-info" hx-trigger="load" hx-swap="none">
</div>
<script>
document.getElementById('userAgent').value = navigator.userAgent;
</script>

0 comments on commit fbb006e

Please sign in to comment.