Files
nym/validator-api/src/node_status_api/models.rs
T
Jędrzej Stuczyński e5afd54ce0 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
2021-07-19 14:02:47 +01:00

259 lines
6.9 KiB
Rust

// 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;
// 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
}
}
impl From<Uptime> for u8 {
fn from(uptime: Uptime) -> Self {
uptime.0
}
}
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))
}
}
}
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(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,
}
pub(crate) struct ErrorResponse {
error: NodeStatusApiError,
status: Status,
}
impl ErrorResponse {
pub(crate) fn new(error: NodeStatusApiError, status: Status) -> Self {
ErrorResponse { error, status }
}
}
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")
}
}
}
}