Additional, more informative routes (#1204)
* Have reward set updater run its own timer (#1200) * Have reward set updater run its own timer * Filter rocket log spam * Take last day of uptime for rewarding (#1202) * Take last day of uptime for rewarding * Rejigger calculations * Blacklist based on last 24 hr * Cleanup * Clippy * Additional, more informative routes * Improve blacklist updates * Fix rewards estimation
This commit is contained in:
@@ -11,8 +11,97 @@ pub(crate) struct StorageManager {
|
||||
pub(crate) connection_pool: sqlx::SqlitePool,
|
||||
}
|
||||
|
||||
pub struct AvgReliability {
|
||||
identity: String,
|
||||
value: Option<f32>,
|
||||
}
|
||||
|
||||
impl AvgReliability {
|
||||
pub fn identity(&self) -> &str {
|
||||
&self.identity
|
||||
}
|
||||
|
||||
pub fn value(&self) -> f32 {
|
||||
self.value.unwrap_or_default()
|
||||
}
|
||||
}
|
||||
|
||||
// all SQL goes here
|
||||
impl StorageManager {
|
||||
pub(super) async fn get_all_avg_mix_reliability_in_last_24hr(
|
||||
&self,
|
||||
end_ts_secs: i64,
|
||||
) -> Result<Vec<AvgReliability>, sqlx::Error> {
|
||||
let start_ts_secs = end_ts_secs - 86400;
|
||||
self.get_all_avg_mix_reliability_in_interval(start_ts_secs, end_ts_secs)
|
||||
.await
|
||||
}
|
||||
|
||||
pub(super) async fn get_all_avg_gateway_reliability_in_last_24hr(
|
||||
&self,
|
||||
end_ts_secs: i64,
|
||||
) -> Result<Vec<AvgReliability>, sqlx::Error> {
|
||||
let start_ts_secs = end_ts_secs - 86400;
|
||||
self.get_all_avg_gateway_reliability_in_interval(start_ts_secs, end_ts_secs)
|
||||
.await
|
||||
}
|
||||
|
||||
pub(super) async fn get_all_avg_mix_reliability_in_interval(
|
||||
&self,
|
||||
start_ts_secs: i64,
|
||||
end_ts_secs: i64,
|
||||
) -> Result<Vec<AvgReliability>, sqlx::Error> {
|
||||
let result = sqlx::query_as!(
|
||||
AvgReliability,
|
||||
r#"
|
||||
SELECT
|
||||
d.identity as "identity: String",
|
||||
AVG(s.reliability) as "value: f32"
|
||||
FROM
|
||||
mixnode_details d
|
||||
JOIN
|
||||
mixnode_status s on d.id = s.mixnode_details_id
|
||||
WHERE
|
||||
timestamp >= ? AND
|
||||
timestamp <= ?
|
||||
GROUP BY 1
|
||||
"#,
|
||||
start_ts_secs,
|
||||
end_ts_secs
|
||||
)
|
||||
.fetch_all(&self.connection_pool)
|
||||
.await?;
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub(super) async fn get_all_avg_gateway_reliability_in_interval(
|
||||
&self,
|
||||
start_ts_secs: i64,
|
||||
end_ts_secs: i64,
|
||||
) -> Result<Vec<AvgReliability>, sqlx::Error> {
|
||||
let result = sqlx::query_as!(
|
||||
AvgReliability,
|
||||
r#"
|
||||
SELECT
|
||||
d.identity as "identity: String",
|
||||
AVG(reliability) as "value: f32"
|
||||
FROM
|
||||
gateway_details d
|
||||
JOIN
|
||||
gateway_status s on d.id = s.gateway_details_id
|
||||
WHERE
|
||||
timestamp >= ? AND
|
||||
timestamp <= ?
|
||||
GROUP BY 1
|
||||
"#,
|
||||
start_ts_secs,
|
||||
end_ts_secs
|
||||
)
|
||||
.fetch_all(&self.connection_pool)
|
||||
.await?;
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Tries to obtain row id of given mixnode given its identity.
|
||||
///
|
||||
/// # Arguments
|
||||
@@ -247,6 +336,26 @@ impl StorageManager {
|
||||
.await
|
||||
}
|
||||
|
||||
pub(super) async fn get_average_reliability_in_interval(
|
||||
&self,
|
||||
id: i64,
|
||||
start: i64,
|
||||
end: i64,
|
||||
) -> Result<Option<f32>, sqlx::Error> {
|
||||
let result = sqlx::query!(
|
||||
r#"
|
||||
SELECT AVG(reliability) as "reliability: f32" FROM mixnode_status
|
||||
WHERE mixnode_details_id= ? AND timestamp >= ? AND timestamp <= ?
|
||||
"#,
|
||||
id,
|
||||
start,
|
||||
end
|
||||
)
|
||||
.fetch_one(&self.connection_pool)
|
||||
.await?;
|
||||
Ok(result.reliability)
|
||||
}
|
||||
|
||||
/// Gets all reliability statuses for gateway with particular id that were inserted
|
||||
/// into the database within the specified time interval.
|
||||
///
|
||||
|
||||
@@ -15,6 +15,8 @@ use sqlx::ConnectOptions;
|
||||
use std::path::PathBuf;
|
||||
use time::OffsetDateTime;
|
||||
|
||||
use self::manager::AvgReliability;
|
||||
|
||||
pub(crate) mod manager;
|
||||
pub(crate) mod models;
|
||||
|
||||
@@ -68,6 +70,32 @@ impl ValidatorApiStorage {
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) async fn get_all_avg_gateway_reliability_in_last_24hr(
|
||||
&self,
|
||||
end_ts_secs: i64,
|
||||
) -> Result<Vec<AvgReliability>, ValidatorApiStorageError> {
|
||||
let result = self
|
||||
.manager
|
||||
.get_all_avg_gateway_reliability_in_last_24hr(end_ts_secs)
|
||||
.await
|
||||
.map_err(|e| ValidatorApiStorageError::InternalDatabaseError(format!("{}", e)))?;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub(crate) async fn get_all_avg_mix_reliability_in_last_24hr(
|
||||
&self,
|
||||
end_ts_secs: i64,
|
||||
) -> Result<Vec<AvgReliability>, ValidatorApiStorageError> {
|
||||
let result = self
|
||||
.manager
|
||||
.get_all_avg_mix_reliability_in_last_24hr(end_ts_secs)
|
||||
.await
|
||||
.map_err(|e| ValidatorApiStorageError::InternalDatabaseError(format!("{}", e)))?;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Gets all statuses for particular mixnode that were inserted
|
||||
/// since the provided timestamp.
|
||||
///
|
||||
@@ -261,6 +289,16 @@ impl ValidatorApiStorage {
|
||||
))
|
||||
}
|
||||
|
||||
pub(crate) async fn get_average_mixnode_uptime_in_the_last_24hrs(
|
||||
&self,
|
||||
identity: &str,
|
||||
end_ts_secs: i64,
|
||||
) -> Result<Uptime, ValidatorApiStorageError> {
|
||||
let start = end_ts_secs - 86400;
|
||||
self.get_average_mixnode_uptime_in_interval(identity, start, end_ts_secs)
|
||||
.await
|
||||
}
|
||||
|
||||
/// Based on the data available in the validator API, determines the average uptime of particular
|
||||
/// mixnode during the specified time interval.
|
||||
///
|
||||
@@ -285,28 +323,17 @@ impl ValidatorApiStorage {
|
||||
None => return Ok(Uptime::zero()),
|
||||
};
|
||||
|
||||
let monitor_runs = self.get_monitor_runs_count(start, end).await?;
|
||||
let mixnode_statuses = self
|
||||
let reliability = self
|
||||
.manager
|
||||
.get_mixnode_statuses_by_id(mixnode_database_id, start, end)
|
||||
.get_average_reliability_in_interval(mixnode_database_id, start, end)
|
||||
.await
|
||||
.map_err(|e| ValidatorApiStorageError::InternalDatabaseError(format!("{}", e)))?;
|
||||
|
||||
let mut total: f32 = 0.0;
|
||||
for mixnode_status in mixnode_statuses {
|
||||
total += mixnode_status.reliability() as f32;
|
||||
if let Some(reliability) = reliability {
|
||||
Ok(Uptime::new(reliability))
|
||||
} else {
|
||||
Ok(Uptime::zero())
|
||||
}
|
||||
|
||||
let uptime = match Uptime::from_uptime_sum(total, monitor_runs) {
|
||||
Ok(uptime) => uptime,
|
||||
Err(_) => {
|
||||
// this should really ever happen...
|
||||
error!("mixnode {} has uptime > 100!", identity);
|
||||
Uptime::default()
|
||||
}
|
||||
};
|
||||
|
||||
Ok(uptime)
|
||||
}
|
||||
|
||||
/// Obtain status reports of mixnodes that were active in the specified time interval.
|
||||
|
||||
Reference in New Issue
Block a user