mirror of
https://github.com/Noratrieb/uptime.git
synced 2026-01-14 16:45:06 +01:00
correctly calculate percentegates
This commit is contained in:
parent
8b2ec568c3
commit
50d15bc4e7
3 changed files with 20 additions and 13 deletions
13
src/db.rs
13
src/db.rs
|
|
@ -197,25 +197,30 @@ pub async fn migrate_checks(db: &Pool<Sqlite>, interval_seconds: u64) -> Result<
|
|||
}
|
||||
|
||||
info!("Inserting checks");
|
||||
let mut db = db.begin().await.wrap_err("starting transaction")?;
|
||||
let mut db_trans = db.begin().await.wrap_err("starting transaction")?;
|
||||
for check in table.iter() {
|
||||
sqlx::query("INSERT INTO checks_series (request_time_range_start, request_time_range_end, website, result) VALUES (?, ?, ?, ?);")
|
||||
.bind(check.request_time_range_start)
|
||||
.bind(check.request_time_range_end)
|
||||
.bind(&check.website)
|
||||
.bind(&check.result)
|
||||
.execute(&mut *db)
|
||||
.execute(&mut *db_trans)
|
||||
.await
|
||||
.wrap_err_with(|| format!("inserting new series record for {}", check.website))?;
|
||||
}
|
||||
info!("Dropping old table");
|
||||
|
||||
sqlx::query("DROP TABLE checks")
|
||||
.execute(&mut *db)
|
||||
.execute(&mut *db_trans)
|
||||
.await
|
||||
.wrap_err("dropping table checks")?;
|
||||
|
||||
db.commit().await.wrap_err("committing transaction")?;
|
||||
db_trans.commit().await.wrap_err("committing transaction")?;
|
||||
|
||||
sqlx::query("VACUUM")
|
||||
.execute(db)
|
||||
.await
|
||||
.wrap_err("running vacuum")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
17
src/web.rs
17
src/web.rs
|
|
@ -81,15 +81,22 @@ fn compute_status(checks: Vec<CheckSeries>) -> Vec<WebsiteStatus> {
|
|||
const BAR_ELEMS: usize = 100;
|
||||
let bar_info = checks_to_classes(&checks, BAR_ELEMS);
|
||||
|
||||
let len = checks.len();
|
||||
let total_duration_millis = checks
|
||||
.first()
|
||||
.map(|first| {
|
||||
checks.last().unwrap().0.end.timestamp_millis()
|
||||
- first.0.start.timestamp_millis()
|
||||
})
|
||||
.unwrap_or_default();
|
||||
|
||||
checks.into_iter().for_each(|(time, result)| {
|
||||
if let CheckState::Ok = result {
|
||||
last_ok = std::cmp::max(last_ok, Some(time.end));
|
||||
count_ok += 1;
|
||||
count_ok += time.end.timestamp_millis() - time.start.timestamp_millis();
|
||||
}
|
||||
});
|
||||
|
||||
let ok_ratio = (count_ok as f32) / (len as f32);
|
||||
let ok_ratio = (count_ok as f32) / (total_duration_millis as f32);
|
||||
let ok_ratio = format!("{:.2}%", ok_ratio * 100.0);
|
||||
|
||||
let last_ok = last_ok.map(|utc| utc.render_nicely());
|
||||
|
|
@ -97,8 +104,6 @@ fn compute_status(checks: Vec<CheckSeries>) -> Vec<WebsiteStatus> {
|
|||
website,
|
||||
last_ok,
|
||||
ok_ratio,
|
||||
count_ok,
|
||||
total_requests: len,
|
||||
bar_info,
|
||||
}
|
||||
})
|
||||
|
|
@ -221,8 +226,6 @@ struct WebsiteStatus {
|
|||
website: String,
|
||||
last_ok: Option<String>,
|
||||
ok_ratio: String,
|
||||
total_requests: usize,
|
||||
count_ok: usize,
|
||||
bar_info: BarInfo,
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -68,8 +68,7 @@
|
|||
|
||||
<h2>{{ check.website }}</h2>
|
||||
<p>
|
||||
Uptime: {{ check.ok_ratio }} ({{ check.count_ok }}/{{
|
||||
check.total_requests }})
|
||||
Uptime: {{ check.ok_ratio }}
|
||||
</p>
|
||||
{% if check.last_ok.is_some() %}
|
||||
<p>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue