display both graph, load on click, warn on the instance site

This commit is contained in:
4cuck committed 2024-02-20 00:17:38 +01:00
1 parent 87230a3703
commit c6028ef899
7 files changed
+413 -254

No files matched your search

+23 -37
View File
@@ -1,7 +1,7 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3
use sea_orm::{entity::prelude::*, FromQueryResult};
use sea_query::{Query, Order, Alias, SimpleExpr};
use sea_query::{Alias, Order, Query, SimpleExpr};
use serde::Serialize;
use crate::check_errors;
@@ -45,27 +45,31 @@ pub struct HealthyAmount {
pub dead: i64,
}
#[derive(Debug, FromQueryResult, Serialize)]
pub struct InstanceGraphEntry {
pub time: i64,
pub alive: i64,
pub dead: i64,
}
impl HealthyAmount {
/// Fetch health check graph data for all or selected hosts in the selected time range.
pub async fn fetch(db: &DatabaseConnection, from: Option<DateTimeUtc>, to: Option<DateTimeUtc>, hosts: Option<&[i32]>) -> Result<Vec<Self>,DbErr> {
/// Fetch health check graph data for all or selected hosts in the selected time range.
pub async fn fetch(
db: &DatabaseConnection,
from: Option<DateTimeUtc>,
to: Option<DateTimeUtc>,
hosts: Option<&[i32]>,
) -> Result<Vec<Self>, DbErr> {
let builder = db.get_database_backend();
let mut stmt: sea_query::SelectStatement = Query::select();
stmt.column(self::Column::Time)
.expr_as(SimpleExpr::Custom("SUM(healthy)".to_string()), Alias::new("alive"))
.expr_as(SimpleExpr::Custom("SUM(1-healthy)".to_string()), Alias::new("dead"))
.expr_as(
SimpleExpr::Custom("SUM(healthy)".to_string()),
Alias::new("alive"),
)
.expr_as(
SimpleExpr::Custom("SUM(1-healthy)".to_string()),
Alias::new("dead"),
)
.group_by_col(self::Column::Time)
.from(self::Entity);
if let (Some(from),Some(to)) = (from,to) {
if let (Some(from), Some(to)) = (from, to) {
stmt.and_where(self::Column::Time.between(from.timestamp(), to.timestamp()));
}
if let Some(hosts) = hosts {
if let Some(hosts) = hosts {
stmt.and_where(self::Column::Host.is_in(hosts.iter().map(|v| *v)));
}
stmt.group_by_col(self::Column::Time)
@@ -74,26 +78,6 @@ impl HealthyAmount {
}
}
impl InstanceGraphEntry {
/// Fetch health check graph data for all or selected hosts in the selected time range.
pub async fn fetch(db: &DatabaseConnection, to: Option<DateTimeUtc>) -> Result<Vec<Self>,DbErr> {
let builder = db.get_database_backend();
let mut stmt: sea_query::SelectStatement = Query::select();
stmt//.expr_as(SimpleExpr::Custom("strftime('%Y/%m/%d %H:%M:%S', datetime(time, 'unixepoch', 'utc'))".to_string()), Alias::new("time"))
.column(self::Column::Time)
.expr_as(SimpleExpr::Custom("SUM(healthy)".to_string()), Alias::new("alive"))
.expr_as(SimpleExpr::Custom("SUM(1-healthy)".to_string()), Alias::new("dead"))
.group_by_col(self::Column::Time)
.from(self::Entity);
if let Some(to) = to {
stmt.and_where(self::Column::Time.lte(to.timestamp()));
}
stmt.group_by_col(self::Column::Time)
.order_by(self::Column::Time, Order::Asc);
Self::find_by_statement(builder.build(&stmt)).all(db).await
}
}
#[cfg(test)]
mod test {
@@ -119,7 +103,9 @@ mod test {
let time_3h = time_now
.checked_sub_signed(chrono::Duration::days(14))
.unwrap();
let hosts = vec![1,2,3];
HealthyAmount::fetch(&db, Some(time_3h), Some(time_now),Some(&hosts)).await.unwrap();
let hosts = vec![1, 2, 3];
HealthyAmount::fetch(&db, Some(time_3h), Some(time_now), Some(&hosts))
.await
.unwrap();
}
}
}
+66 -10
View File
@@ -1,7 +1,7 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3
use sea_orm::{entity::prelude::*, FromQueryResult};
use sea_query::{Query, SimpleExpr, Order, Alias};
use sea_query::{Alias, Order, Query, SimpleExpr};
use serde::Serialize;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize)]
@@ -75,24 +75,80 @@ pub struct StatsAmount {
}
impl StatsAmount {
/// Fetch health check graph data for all or selected hosts in the selected time range.
pub async fn fetch(db: &DatabaseConnection, from: DateTimeUtc, to: DateTimeUtc, hosts: Option<&[i32]>) -> Result<Vec<StatsAmount>,DbErr> {
/// Fetch health check graph data for all or selected hosts in the selected time range.
pub async fn fetch(
db: &DatabaseConnection,
from: DateTimeUtc,
to: DateTimeUtc,
hosts: Option<&[i32]>,
) -> Result<Vec<StatsAmount>, DbErr> {
let builder = db.get_database_backend();
let columns = ["limited_accs","total_accs","total_requests","req_photo_rail","req_user_screen_name","req_search","req_list_tweets","req_user_media","req_tweet_detail","req_list","req_user_tweets","req_user_tweets_and_replies"];
let columns = [
"limited_accs",
"total_accs",
"total_requests",
"req_photo_rail",
"req_user_screen_name",
"req_search",
"req_list_tweets",
"req_user_media",
"req_tweet_detail",
"req_list",
"req_user_tweets",
"req_user_tweets_and_replies",
];
let mut stmt: sea_query::SelectStatement = Query::select();
let col_stmt = stmt.column(self::Column::Time);
for col in columns {
col_stmt.expr_as(SimpleExpr::Custom(format!("MAX({col})")), Alias::new(format!("{col}_max")))
.expr_as(SimpleExpr::Custom(format!("CAST(ifnull(AVG({col}),0) as int)")), Alias::new(format!("{col}_avg")));
col_stmt
.expr_as(
SimpleExpr::Custom(format!("MAX({col})")),
Alias::new(format!("{col}_max")),
)
.expr_as(
SimpleExpr::Custom(format!("CAST(ifnull(AVG({col}),0) as int)")),
Alias::new(format!("{col}_avg")),
);
}
col_stmt.group_by_col(self::Column::Time)
col_stmt
.group_by_col(self::Column::Time)
.from(self::Entity)
.and_where(self::Column::Time.between(from.timestamp(), to.timestamp()));
if let Some(hosts) = hosts {
if let Some(hosts) = hosts {
stmt.and_where(self::Column::Host.is_in(hosts.iter().map(|v| *v)));
}
stmt.group_by_col(self::Column::Time)
.order_by(self::Column::Time, Order::Asc);
StatsAmount::find_by_statement(builder.build(&stmt)).all(db).await
StatsAmount::find_by_statement(builder.build(&stmt))
.all(db)
.await
}
}
}
#[derive(Debug, FromQueryResult, Serialize)]
pub struct StatsCSVEntry {
pub time: i64,
pub limited_accs_avg: i32,
pub total_accs_avg: i32,
pub total_requests_avg: i64,
}
impl StatsCSVEntry {
/// Fetch health check graph data for all or selected hosts in the selected time range.
pub async fn fetch(db: &DatabaseConnection) -> Result<Vec<Self>, DbErr> {
let builder = db.get_database_backend();
let columns = ["limited_accs", "total_accs", "total_requests"];
let mut stmt: sea_query::SelectStatement = Query::select();
stmt.column(self::Column::Time);
for col in columns {
stmt.expr_as(
SimpleExpr::Custom(format!("CAST (AVG({col}) as int)")),
Alias::new(format!("{col}_avg")),
);
}
stmt.group_by_col(self::Column::Time).from(self::Entity);
stmt.group_by_col(self::Column::Time)
.order_by(self::Column::Time, Order::Asc);
Self::find_by_statement(builder.build(&stmt)).all(db).await
}
}