Feature/node status api (#680)
* Basic storage stub * New models for new node status api * Route handling * Mounting new routes * Missing selective commit * Moved network monitor related files to separate package * Starting to see some sqlx action * Schema updates * Log statement upon finished migration * Removed old diesel related imports * Converted mixnode cache initialisation into a fairing * Moved cache related functionalities to separate package Also defined staging there * Created run method for validator cache + removed unwrap * Removed old node-status-api types and left bunch of todo placeholders in their place * Fixed managing validatorcache * Status reports are starting to get constructed * Submitting some dummy results to the database * Removing duplicate code for generating reports * Removed statuses older than 48h * Initial attempt at trying to obtain reports for all active nodes * Removed duplicates from the full report * Grabbing uptime history * Updating historical uptimes of active nodes * Updated sqlx-data.json * Removed all placeholder foomp owner values * Changed Layer serde behaviour for easier usage * Extended validator api config * Initial (seems working !) integration with network monitor * Added database path configuration to config * Using ValidatorCache in NetworkMonitor * Flag indicating whether validator cache has been initialised * Introduced a locla-only route for reward script to perform daily chores * Flag to save config to a file * Moved spawning of receiving future to run method rather than new * Removed arguments that dont make sense to be configured via CLI * Removed dead code from config file * More dead code removal * Added validator API to CI * Corrected manifest-path arguments * Constructing network monitor by passing config * Combined validator API CI with the main CI file * Using query_as for NodeStatus * Checking if historical uptimes were already calculated on particular day * Making id field NOT NULL * More query_as! action * Updated sqlx-data.json * Removed unused chrono feature * Renamed the migration file * Changed default validator endpoint to point to local validator * Removing unnecessary clone * More appropriate naming * Removed dead code * Lock file updates * Updated network monitor address in contract code * Don't stage node status api if network monitor is disabled * cargo fmt * Updated all license notices to SPDX
This commit is contained in:
committed by
GitHub
parent
25d2af3b04
commit
e5afd54ce0
@@ -1,110 +0,0 @@
|
||||
// Copyright 2021 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use crate::node_status_api::models::{BatchGatewayStatus, BatchMixStatus, DefaultRestResponse};
|
||||
use crate::node_status_api::NodeStatusApiClientError;
|
||||
|
||||
pub(crate) struct Config {
|
||||
base_url: String,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub(crate) fn new<S: Into<String>>(base_url: S) -> Self {
|
||||
Config {
|
||||
base_url: base_url.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct Client {
|
||||
config: Config,
|
||||
reqwest_client: reqwest::Client,
|
||||
}
|
||||
|
||||
impl Client {
|
||||
pub(crate) fn new(config: Config) -> Self {
|
||||
let reqwest_client = reqwest::Client::new();
|
||||
Client {
|
||||
config,
|
||||
reqwest_client,
|
||||
}
|
||||
}
|
||||
|
||||
// Potentially, down the line, this could be moved to /common/client-libs
|
||||
// and additional methods could be added like GET for report data, but currently
|
||||
// we have absolutely no use for that in Rust.
|
||||
|
||||
pub(crate) async fn post_batch_mix_status(
|
||||
&self,
|
||||
batch_status: BatchMixStatus,
|
||||
) -> Result<(), NodeStatusApiClientError> {
|
||||
const RELATIVE_PATH: &str = "api/status/mixnode/batch";
|
||||
|
||||
let url = format!("{}/{}", self.config.base_url, RELATIVE_PATH);
|
||||
|
||||
let response = self
|
||||
.reqwest_client
|
||||
.post(url)
|
||||
.json(&batch_status)
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
if response.status().is_success() {
|
||||
let response_content: DefaultRestResponse = response.json().await?;
|
||||
match response_content {
|
||||
DefaultRestResponse::Ok(ok_response) => {
|
||||
if ok_response.ok {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(NodeStatusApiClientError::NodeStatusApiError(
|
||||
"received an ok response with false status".into(),
|
||||
))
|
||||
}
|
||||
}
|
||||
DefaultRestResponse::Error(err_response) => Err(err_response.into()),
|
||||
}
|
||||
} else {
|
||||
Err(NodeStatusApiClientError::NodeStatusApiError(format!(
|
||||
"received response with status {}",
|
||||
response.status()
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn post_batch_gateway_status(
|
||||
&self,
|
||||
batch_status: BatchGatewayStatus,
|
||||
) -> Result<(), NodeStatusApiClientError> {
|
||||
const RELATIVE_PATH: &str = "api/status/gateway/batch";
|
||||
|
||||
let url = format!("{}/{}", self.config.base_url, RELATIVE_PATH);
|
||||
|
||||
let response = self
|
||||
.reqwest_client
|
||||
.post(url)
|
||||
.json(&batch_status)
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
if response.status().is_success() {
|
||||
let response_content: DefaultRestResponse = response.json().await?;
|
||||
match response_content {
|
||||
DefaultRestResponse::Ok(ok_response) => {
|
||||
if ok_response.ok {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(NodeStatusApiClientError::NodeStatusApiError(
|
||||
"received an ok response with false status".into(),
|
||||
))
|
||||
}
|
||||
}
|
||||
DefaultRestResponse::Error(err_response) => Err(err_response.into()),
|
||||
}
|
||||
} else {
|
||||
Err(NodeStatusApiClientError::NodeStatusApiError(format!(
|
||||
"received response with status {}",
|
||||
response.status()
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
// Copyright 2021 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use rocket::http::Status;
|
||||
use rocket::request::{FromRequest, Outcome};
|
||||
use rocket::Request;
|
||||
use std::fmt::Debug;
|
||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct NonLocalRequestError;
|
||||
|
||||
/// Request guard that only allows requests coming from a local address
|
||||
pub(crate) struct LocalRequest;
|
||||
|
||||
fn is_local_address(ip: Option<IpAddr>) -> bool {
|
||||
if let Some(address) = ip {
|
||||
match address {
|
||||
IpAddr::V4(ip) => ip == Ipv4Addr::LOCALHOST,
|
||||
IpAddr::V6(ip) => ip == Ipv6Addr::LOCALHOST,
|
||||
}
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
#[rocket::async_trait]
|
||||
impl<'r> FromRequest<'r> for LocalRequest {
|
||||
type Error = NonLocalRequestError;
|
||||
|
||||
async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> {
|
||||
if is_local_address(request.client_ip()) {
|
||||
Outcome::Success(LocalRequest)
|
||||
} else {
|
||||
warn!(
|
||||
"Received a request from {:?} for a local-only route",
|
||||
request.client_ip()
|
||||
);
|
||||
Outcome::Failure((Status::Unauthorized, NonLocalRequestError))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,73 +1,35 @@
|
||||
// Copyright 2021 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use crate::node_status_api::models::ErrorResponses;
|
||||
use std::fmt::{self, Display, Formatter};
|
||||
use rocket::fairing::AdHoc;
|
||||
use std::path::PathBuf;
|
||||
use std::time::Duration;
|
||||
|
||||
mod client;
|
||||
pub(crate) mod local_guard;
|
||||
pub(crate) mod models;
|
||||
pub(crate) mod routes;
|
||||
pub(crate) mod storage;
|
||||
pub(crate) mod utils;
|
||||
|
||||
pub(crate) use client::{Client, Config};
|
||||
pub(crate) const FIFTEEN_MINUTES: Duration = Duration::from_secs(900);
|
||||
pub(crate) const ONE_HOUR: Duration = Duration::from_secs(3600);
|
||||
pub(crate) const ONE_DAY: Duration = Duration::from_secs(86400);
|
||||
|
||||
const MAX_SANE_UNEXPECTED_PRINT: usize = 100;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum NodeStatusApiClientError {
|
||||
ReqwestClientError(reqwest::Error),
|
||||
NodeStatusApiError(String),
|
||||
UnexpectedResponse(String),
|
||||
}
|
||||
|
||||
impl From<reqwest::Error> for NodeStatusApiClientError {
|
||||
fn from(err: reqwest::Error) -> Self {
|
||||
NodeStatusApiClientError::ReqwestClientError(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ErrorResponses> for NodeStatusApiClientError {
|
||||
fn from(err: ErrorResponses) -> Self {
|
||||
match err {
|
||||
ErrorResponses::Error(err_message) => {
|
||||
NodeStatusApiClientError::NodeStatusApiError(err_message.error)
|
||||
}
|
||||
ErrorResponses::Unexpected(received) => {
|
||||
NodeStatusApiClientError::UnexpectedResponse(received.to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for NodeStatusApiClientError {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
NodeStatusApiClientError::ReqwestClientError(err) => {
|
||||
write!(f, "there was an issue with the REST request - {}", err)
|
||||
}
|
||||
NodeStatusApiClientError::NodeStatusApiError(err) => {
|
||||
write!(
|
||||
f,
|
||||
"there was an issue with the node status api client - {}",
|
||||
err
|
||||
)
|
||||
}
|
||||
NodeStatusApiClientError::UnexpectedResponse(received) => {
|
||||
if received.len() < MAX_SANE_UNEXPECTED_PRINT {
|
||||
write!(
|
||||
f,
|
||||
"received data was completely unexpected. got: {}",
|
||||
received
|
||||
)
|
||||
} else {
|
||||
write!(
|
||||
f,
|
||||
"received data was completely unexpected. got: {}...",
|
||||
received
|
||||
.chars()
|
||||
.take(MAX_SANE_UNEXPECTED_PRINT)
|
||||
.collect::<String>()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
pub(crate) fn stage(database_path: PathBuf) -> AdHoc {
|
||||
AdHoc::on_ignite("SQLx Stage", |rocket| async {
|
||||
rocket
|
||||
.attach(storage::NodeStatusStorage::stage(database_path))
|
||||
.mount(
|
||||
"/v1/status",
|
||||
routes![
|
||||
routes::mixnode_report,
|
||||
routes::gateway_report,
|
||||
routes::mixnode_uptime_history,
|
||||
routes::gateway_uptime_history,
|
||||
routes::mixnodes_full_report,
|
||||
routes::gateways_full_report,
|
||||
routes::rewarding_chores,
|
||||
],
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,68 +1,258 @@
|
||||
// Copyright 2021 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use crate::node_status_api::utils::{NodeStatus, NodeUptimes};
|
||||
use rocket::http::{ContentType, Status};
|
||||
use rocket::response::{self, Responder, Response};
|
||||
use rocket::Request;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::convert::TryFrom;
|
||||
use std::fmt::{self, Display, Formatter};
|
||||
use std::io::Cursor;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
/// A notification sent to the validators to let them know whether a given mix is
|
||||
/// currently up or down (based on whether it's mixing packets)
|
||||
pub struct MixStatus {
|
||||
pub pub_key: String,
|
||||
pub owner: String,
|
||||
pub ip_version: String,
|
||||
pub up: bool,
|
||||
// todo: put into some error enum
|
||||
#[derive(Debug)]
|
||||
pub struct InvalidUptime;
|
||||
|
||||
// value in range 0-100
|
||||
#[derive(Clone, Serialize, Deserialize, Debug)]
|
||||
pub struct Uptime(u8);
|
||||
|
||||
impl Uptime {
|
||||
pub const fn zero() -> Self {
|
||||
Uptime(0)
|
||||
}
|
||||
|
||||
pub fn from_ratio(numerator: usize, denominator: usize) -> Result<Self, InvalidUptime> {
|
||||
if denominator == 0 {
|
||||
return Ok(Self::zero());
|
||||
}
|
||||
|
||||
let uptime = ((numerator as f32 / denominator as f32) * 100.0) as u8;
|
||||
|
||||
if uptime > 100 {
|
||||
Err(InvalidUptime)
|
||||
} else {
|
||||
Ok(Uptime(uptime))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn u8(&self) -> u8 {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
/// A notification sent to the validators to let them know whether a given set of mixes is
|
||||
/// currently up or down (based on whether it's mixing packets)
|
||||
pub struct BatchMixStatus {
|
||||
pub status: Vec<MixStatus>,
|
||||
impl From<Uptime> for u8 {
|
||||
fn from(uptime: Uptime) -> Self {
|
||||
uptime.0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
/// A notification sent to the validators to let them know whether a given gateway is
|
||||
/// currently up or down (based on whether it's mixing packets)
|
||||
pub struct GatewayStatus {
|
||||
pub pub_key: String,
|
||||
pub owner: String,
|
||||
pub ip_version: String,
|
||||
pub up: bool,
|
||||
impl TryFrom<u8> for Uptime {
|
||||
type Error = InvalidUptime;
|
||||
|
||||
fn try_from(value: u8) -> Result<Self, Self::Error> {
|
||||
if value > 100 {
|
||||
Err(InvalidUptime)
|
||||
} else {
|
||||
Ok(Uptime(value))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
/// A notification sent to the validators to let them know whether a given set of gateways is
|
||||
/// currently up or down (based on whether it's mixing packets)
|
||||
pub struct BatchGatewayStatus {
|
||||
pub status: Vec<GatewayStatus>,
|
||||
impl TryFrom<i64> for Uptime {
|
||||
type Error = InvalidUptime;
|
||||
|
||||
fn try_from(value: i64) -> Result<Self, Self::Error> {
|
||||
if !(0..=100).contains(&value) {
|
||||
Err(InvalidUptime)
|
||||
} else {
|
||||
Ok(Uptime(value as u8))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
#[serde(rename_all = "camelCase", untagged)]
|
||||
pub(crate) enum ErrorResponses {
|
||||
Error(ErrorResponse),
|
||||
Unexpected(serde_json::Value),
|
||||
#[derive(Clone, Serialize, Deserialize, Debug)]
|
||||
pub struct MixnodeStatusReport {
|
||||
identity: String,
|
||||
owner: String,
|
||||
|
||||
most_recent_ipv4: bool,
|
||||
most_recent_ipv6: bool,
|
||||
|
||||
last_hour_ipv4: Uptime,
|
||||
last_hour_ipv6: Uptime,
|
||||
|
||||
last_day_ipv4: Uptime,
|
||||
last_day_ipv6: Uptime,
|
||||
}
|
||||
|
||||
impl MixnodeStatusReport {
|
||||
pub(crate) fn construct_from_last_day_reports(
|
||||
identity: String,
|
||||
owner: String,
|
||||
last_day_ipv4: Vec<NodeStatus>,
|
||||
last_day_ipv6: Vec<NodeStatus>,
|
||||
) -> Self {
|
||||
let node_uptimes =
|
||||
NodeUptimes::calculate_from_last_day_reports(last_day_ipv4, last_day_ipv6);
|
||||
|
||||
MixnodeStatusReport {
|
||||
identity,
|
||||
owner,
|
||||
most_recent_ipv4: node_uptimes.most_recent_ipv4,
|
||||
most_recent_ipv6: node_uptimes.most_recent_ipv6,
|
||||
last_hour_ipv4: node_uptimes.last_hour_ipv4,
|
||||
last_hour_ipv6: node_uptimes.last_hour_ipv6,
|
||||
last_day_ipv4: node_uptimes.last_day_ipv4,
|
||||
last_day_ipv6: node_uptimes.last_day_ipv6,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize, Debug)]
|
||||
pub struct GatewayStatusReport {
|
||||
identity: String,
|
||||
owner: String,
|
||||
|
||||
most_recent_ipv4: bool,
|
||||
most_recent_ipv6: bool,
|
||||
|
||||
last_hour_ipv4: Uptime,
|
||||
last_hour_ipv6: Uptime,
|
||||
|
||||
last_day_ipv4: Uptime,
|
||||
last_day_ipv6: Uptime,
|
||||
}
|
||||
|
||||
impl GatewayStatusReport {
|
||||
pub(crate) fn construct_from_last_day_reports(
|
||||
identity: String,
|
||||
owner: String,
|
||||
last_day_ipv4: Vec<NodeStatus>,
|
||||
last_day_ipv6: Vec<NodeStatus>,
|
||||
) -> Self {
|
||||
let node_uptimes =
|
||||
NodeUptimes::calculate_from_last_day_reports(last_day_ipv4, last_day_ipv6);
|
||||
|
||||
GatewayStatusReport {
|
||||
identity,
|
||||
owner,
|
||||
most_recent_ipv4: node_uptimes.most_recent_ipv4,
|
||||
most_recent_ipv6: node_uptimes.most_recent_ipv6,
|
||||
last_hour_ipv4: node_uptimes.last_hour_ipv4,
|
||||
last_hour_ipv6: node_uptimes.last_hour_ipv6,
|
||||
last_day_ipv4: node_uptimes.last_day_ipv4,
|
||||
last_day_ipv6: node_uptimes.last_day_ipv6,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize, Debug)]
|
||||
pub struct MixnodeUptimeHistory {
|
||||
pub(crate) identity: String,
|
||||
pub(crate) owner: String,
|
||||
|
||||
pub(crate) history: Vec<HistoricalUptime>,
|
||||
}
|
||||
|
||||
impl MixnodeUptimeHistory {
|
||||
pub(crate) fn new(identity: String, owner: String, history: Vec<HistoricalUptime>) -> Self {
|
||||
MixnodeUptimeHistory {
|
||||
identity,
|
||||
owner,
|
||||
history,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize, Debug)]
|
||||
pub struct GatewayUptimeHistory {
|
||||
pub(crate) identity: String,
|
||||
pub(crate) owner: String,
|
||||
|
||||
pub(crate) history: Vec<HistoricalUptime>,
|
||||
}
|
||||
|
||||
impl GatewayUptimeHistory {
|
||||
pub(crate) fn new(identity: String, owner: String, history: Vec<HistoricalUptime>) -> Self {
|
||||
GatewayUptimeHistory {
|
||||
identity,
|
||||
owner,
|
||||
history,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize, Debug)]
|
||||
pub struct HistoricalUptime {
|
||||
// ISO 8601 date string
|
||||
// I think this is more than enough, we don't need the uber precision of timezone offsets, etc
|
||||
pub(crate) date: String,
|
||||
|
||||
pub(crate) ipv4_uptime: Uptime,
|
||||
pub(crate) ipv6_uptime: Uptime,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub(crate) struct ErrorResponse {
|
||||
pub(crate) error: String,
|
||||
error: NodeStatusApiError,
|
||||
status: Status,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub(crate) struct OkResponse {
|
||||
pub(crate) ok: bool,
|
||||
impl ErrorResponse {
|
||||
pub(crate) fn new(error: NodeStatusApiError, status: Status) -> Self {
|
||||
ErrorResponse { error, status }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
#[serde(rename_all = "camelCase", untagged)]
|
||||
pub(crate) enum DefaultRestResponse {
|
||||
Ok(OkResponse),
|
||||
Error(ErrorResponses),
|
||||
impl<'r, 'o: 'r> Responder<'r, 'o> for ErrorResponse {
|
||||
fn respond_to(self, _: &'r Request<'_>) -> response::Result<'o> {
|
||||
let message = format!("{}", self.error);
|
||||
Response::build()
|
||||
.header(ContentType::Plain)
|
||||
.sized_body(message.len(), Cursor::new(message))
|
||||
.status(self.status)
|
||||
.ok()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum NodeStatusApiError {
|
||||
MixnodeReportNotFound(String),
|
||||
GatewayReportNotFound(String),
|
||||
MixnodeUptimeHistoryNotFound(String),
|
||||
GatewayUptimeHistoryNotFound(String),
|
||||
|
||||
// I don't think we want to expose errors to the user about what really happened
|
||||
InternalDatabaseError,
|
||||
}
|
||||
|
||||
impl Display for NodeStatusApiError {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
NodeStatusApiError::MixnodeReportNotFound(identity) => write!(
|
||||
f,
|
||||
"Could not find status report associated with mixnode {}",
|
||||
identity
|
||||
),
|
||||
NodeStatusApiError::GatewayReportNotFound(identity) => write!(
|
||||
f,
|
||||
"Could not find status report associated with gateway {}",
|
||||
identity
|
||||
),
|
||||
NodeStatusApiError::MixnodeUptimeHistoryNotFound(identity) => write!(
|
||||
f,
|
||||
"Could not find uptime history associated with mixnode {}",
|
||||
identity
|
||||
),
|
||||
NodeStatusApiError::GatewayUptimeHistoryNotFound(identity) => write!(
|
||||
f,
|
||||
"Could not find uptime history associated with gateway {}",
|
||||
identity
|
||||
),
|
||||
NodeStatusApiError::InternalDatabaseError => {
|
||||
write!(f, "The internal database has experienced an issue")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
// Copyright 2021 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use crate::node_status_api::local_guard::LocalRequest;
|
||||
use crate::node_status_api::models::{
|
||||
ErrorResponse, GatewayStatusReport, GatewayUptimeHistory, MixnodeStatusReport,
|
||||
MixnodeUptimeHistory,
|
||||
};
|
||||
use crate::node_status_api::storage::NodeStatusStorage;
|
||||
use rocket::http::Status;
|
||||
use rocket::serde::json::Json;
|
||||
use rocket::State;
|
||||
|
||||
#[get("/daily-chores")]
|
||||
pub(crate) async fn rewarding_chores(
|
||||
_local_request: LocalRequest,
|
||||
storage: &State<NodeStatusStorage>,
|
||||
) -> Result<&'static str, ErrorResponse> {
|
||||
if storage
|
||||
.daily_chores()
|
||||
.await
|
||||
.map_err(|err| ErrorResponse::new(err, Status::InternalServerError))?
|
||||
{
|
||||
Ok("Updated historical uptimes and purged old reports.")
|
||||
} else {
|
||||
Ok("The historical uptimes were already updated at some point today - nothing was done now.")
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/mixnode/<pubkey>/report")]
|
||||
pub(crate) async fn mixnode_report(
|
||||
storage: &State<NodeStatusStorage>,
|
||||
pubkey: &str,
|
||||
) -> Result<Json<MixnodeStatusReport>, ErrorResponse> {
|
||||
storage
|
||||
.construct_mixnode_report(pubkey)
|
||||
.await
|
||||
.map(Json)
|
||||
.map_err(|err| ErrorResponse::new(err, Status::NotFound))
|
||||
}
|
||||
|
||||
#[get("/gateway/<pubkey>/report")]
|
||||
pub(crate) async fn gateway_report(
|
||||
storage: &State<NodeStatusStorage>,
|
||||
pubkey: &str,
|
||||
) -> Result<Json<GatewayStatusReport>, ErrorResponse> {
|
||||
storage
|
||||
.construct_gateway_report(pubkey)
|
||||
.await
|
||||
.map(Json)
|
||||
.map_err(|err| ErrorResponse::new(err, Status::NotFound))
|
||||
}
|
||||
|
||||
#[get("/mixnode/<pubkey>/history")]
|
||||
pub(crate) async fn mixnode_uptime_history(
|
||||
storage: &State<NodeStatusStorage>,
|
||||
pubkey: &str,
|
||||
) -> Result<Json<MixnodeUptimeHistory>, ErrorResponse> {
|
||||
storage
|
||||
.get_mixnode_uptime_history(pubkey)
|
||||
.await
|
||||
.map(Json)
|
||||
.map_err(|err| ErrorResponse::new(err, Status::NotFound))
|
||||
}
|
||||
|
||||
#[get("/gateway/<pubkey>/history")]
|
||||
pub(crate) async fn gateway_uptime_history(
|
||||
storage: &State<NodeStatusStorage>,
|
||||
pubkey: &str,
|
||||
) -> Result<Json<GatewayUptimeHistory>, ErrorResponse> {
|
||||
storage
|
||||
.get_gateway_uptime_history(pubkey)
|
||||
.await
|
||||
.map(Json)
|
||||
.map_err(|err| ErrorResponse::new(err, Status::NotFound))
|
||||
}
|
||||
|
||||
#[get("/mixnodes/all/report")]
|
||||
pub(crate) async fn mixnodes_full_report(
|
||||
storage: &State<NodeStatusStorage>,
|
||||
) -> Result<Json<Vec<MixnodeStatusReport>>, ErrorResponse> {
|
||||
storage
|
||||
.get_all_mixnode_reports()
|
||||
.await
|
||||
.map(Json)
|
||||
.map_err(|err| ErrorResponse::new(err, Status::InternalServerError))
|
||||
}
|
||||
|
||||
#[get("/gateways/all/report")]
|
||||
pub(crate) async fn gateways_full_report(
|
||||
storage: &State<NodeStatusStorage>,
|
||||
) -> Result<Json<Vec<GatewayStatusReport>>, ErrorResponse> {
|
||||
storage
|
||||
.get_all_gateway_reports()
|
||||
.await
|
||||
.map(Json)
|
||||
.map_err(|err| ErrorResponse::new(err, Status::InternalServerError))
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,99 @@
|
||||
// Copyright 2021 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use crate::node_status_api::models::Uptime;
|
||||
use crate::node_status_api::{FIFTEEN_MINUTES, ONE_HOUR};
|
||||
use sqlx::types::time::OffsetDateTime;
|
||||
|
||||
// Internally used struct to catch results from the database to calculate uptimes for given mixnode/gateway
|
||||
pub(crate) struct NodeStatus {
|
||||
pub(crate) timestamp: i64,
|
||||
pub(crate) up: bool,
|
||||
}
|
||||
|
||||
// Internally used struct to catch results from the database to find active mixnodes/gateways
|
||||
pub(crate) struct ActiveNode {
|
||||
pub(crate) id: i64,
|
||||
pub(crate) pub_key: String,
|
||||
pub(crate) owner: String,
|
||||
}
|
||||
|
||||
// A temporary helper struct used to produce reports for active nodes.
|
||||
pub(crate) struct ActiveNodeDayStatuses {
|
||||
pub(crate) pub_key: String,
|
||||
pub(crate) owner: String,
|
||||
pub(crate) node_id: i64,
|
||||
|
||||
pub(crate) ipv4_statuses: Vec<NodeStatus>,
|
||||
pub(crate) ipv6_statuses: Vec<NodeStatus>,
|
||||
}
|
||||
|
||||
// A helper intermediate struct to remove duplicate code for construction of mixnode and gateway reports
|
||||
pub(crate) struct NodeUptimes {
|
||||
pub(crate) most_recent_ipv4: bool,
|
||||
pub(crate) most_recent_ipv6: bool,
|
||||
|
||||
pub(crate) last_hour_ipv4: Uptime,
|
||||
pub(crate) last_hour_ipv6: Uptime,
|
||||
|
||||
pub(crate) last_day_ipv4: Uptime,
|
||||
pub(crate) last_day_ipv6: Uptime,
|
||||
}
|
||||
|
||||
impl NodeUptimes {
|
||||
pub(crate) fn calculate_from_last_day_reports(
|
||||
last_day_ipv4: Vec<NodeStatus>,
|
||||
last_day_ipv6: Vec<NodeStatus>,
|
||||
) -> Self {
|
||||
let now = OffsetDateTime::now_utc();
|
||||
let hour_ago = (now - ONE_HOUR).unix_timestamp();
|
||||
let fifteen_minutes_ago = (now - FIFTEEN_MINUTES).unix_timestamp();
|
||||
|
||||
let ipv4_day_total = last_day_ipv4.len();
|
||||
let ipv6_day_total = last_day_ipv6.len();
|
||||
|
||||
let ipv4_day_up = last_day_ipv4.iter().filter(|report| report.up).count();
|
||||
let ipv6_day_up = last_day_ipv6.iter().filter(|report| report.up).count();
|
||||
|
||||
let ipv4_hour_total = last_day_ipv4
|
||||
.iter()
|
||||
.filter(|report| report.timestamp >= hour_ago)
|
||||
.count();
|
||||
let ipv6_hour_total = last_day_ipv6
|
||||
.iter()
|
||||
.filter(|report| report.timestamp >= hour_ago)
|
||||
.count();
|
||||
|
||||
let ipv4_hour_up = last_day_ipv4
|
||||
.iter()
|
||||
.filter(|report| report.up && report.timestamp >= hour_ago)
|
||||
.count();
|
||||
let ipv6_hour_up = last_day_ipv6
|
||||
.iter()
|
||||
.filter(|report| report.up && report.timestamp >= hour_ago)
|
||||
.count();
|
||||
|
||||
// most recent status MUST BE within last 15min
|
||||
let most_recent_ipv4 = last_day_ipv4
|
||||
.iter()
|
||||
.max_by_key(|report| report.timestamp) // find the most recent
|
||||
.map(|status| status.timestamp >= fifteen_minutes_ago && status.up) // make sure its within last 15min
|
||||
.unwrap_or_default();
|
||||
let most_recent_ipv6 = last_day_ipv6
|
||||
.iter()
|
||||
.max_by_key(|report| report.timestamp) // find the most recent
|
||||
.map(|status| status.timestamp >= fifteen_minutes_ago && status.up) // make sure its within last 15min
|
||||
.unwrap_or_default();
|
||||
|
||||
// the unwraps in Uptime::from_ratio are fine because it's impossible for us to have more "up" results than all results in total
|
||||
// because both of those values originate from the same vector
|
||||
NodeUptimes {
|
||||
most_recent_ipv4,
|
||||
most_recent_ipv6,
|
||||
last_hour_ipv4: Uptime::from_ratio(ipv4_hour_up, ipv4_hour_total).unwrap(),
|
||||
last_hour_ipv6: Uptime::from_ratio(ipv6_hour_up, ipv6_hour_total).unwrap(),
|
||||
last_day_ipv4: Uptime::from_ratio(ipv4_day_up, ipv4_day_total).unwrap(),
|
||||
last_day_ipv6: Uptime::from_ratio(ipv6_day_up, ipv6_day_total).unwrap(),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user