diff --git a/README.md b/README.md index f0a53f9..2878e6b 100644 --- a/README.md +++ b/README.md @@ -3,16 +3,21 @@ Public API for collecting and reading statistics on holoports and holo network Endpoints: -#### GET `/` +### GET `/` + +Gets status of connection to db. #### `200 OK` -Text: status of connection to db +### DELETE `/cleanup` + +Deletes from host_statistics.holoport_status documents with timestamp field older than 30 days. + +#### `200 OK` -GET -`/hosts/list-available?days=7` +### GET `/hosts/list-available?hours=7` -days = Cut off time. Records older than this will be ignored. +hours = Cut off time. Records older than this will be ignored. This endpoint returns all the holoports on the holo network as seen by both zerotier network controller and Holoport's netstatsd. Data from both sources is merged and analyzed for possible errors. All the errors are reported in form of an array under field `errors`. @@ -39,24 +44,7 @@ This endpoint returns all the holoports on the holo network as seen by both zero ] ``` -GET -`/hosts/registered?days=7` - -days = Cut off time. Records older than this will be ignored. - -#### `200 OK` - -```json -[ - "holoport_id_1", - "holoport_id_2", - "holoport_id_3", - "holoport_id_4" -] -``` - -GET -`/hosts//uptime` +### GET `/hosts//uptime` #### `200 OK` @@ -66,8 +54,7 @@ GET } ``` -GET -`/network/capacity` +### GET `/network/capacity` #### `200 OK` @@ -79,8 +66,7 @@ GET } ``` -POST -`/hosts/stats` +### POST `/hosts/stats` payload: ```json diff --git a/src/db.rs b/src/db.rs index 726274c..eef08ce 100644 --- a/src/db.rs +++ b/src/db.rs @@ -16,8 +16,8 @@ use crate::types::{ Uptime, ZerotierMember, }; -const DAYS_TOO_LARGE: Error400 = - Error400::Message("Days specified is too large. Cutoff is earlier than start of unix epoch"); +const HOURS_TOO_LARGE: Error400 = + Error400::Message("Hours specified is too large. Cutoff is earlier than start of unix epoch"); // AppDbPool is managed by Rocket as a State, which means it is available across threads. // Type mongodb::Database (starting v2.0.0 of mongodb driver) represents a connection pool to db, @@ -50,9 +50,9 @@ pub async fn cleanup_database(db: &Client) -> Result { let hp_status: Collection = db.database("host_statistics").collection("holoport_status"); - let cutoff_ms = match get_cutoff_timestamp(30) { + let cutoff_ms = match get_cutoff_timestamp(30 * 24) { Some(x) => x, - None => return Err(ApiError::BadRequest(DAYS_TOO_LARGE)), + None => return Err(ApiError::BadRequest(HOURS_TOO_LARGE)), }; let val = doc! { @@ -120,7 +120,6 @@ pub async fn get_zerotier_members(db: &Client) -> Result, Ap // Use aggregation pipeline to extract only relevant fields from database let pipeline = vec![ doc! { - // get entries within last days "$match": { "config.authorized": true } @@ -154,11 +153,11 @@ pub async fn get_zerotier_members(db: &Client) -> Result, Ap } // Return the most recent record for hosts stored in `holoport_status` collection -// Ignores records older than days +// Ignores records older than hours pub async fn get_hosts_stats(db: &Client, cutoff: u64) -> Result, ApiError> { let cutoff_ms = match get_cutoff_timestamp(cutoff) { Some(x) => x, - None => return Err(ApiError::BadRequest(DAYS_TOO_LARGE)), + None => return Err(ApiError::BadRequest(HOURS_TOO_LARGE)), }; let hp_status: Collection = @@ -166,7 +165,7 @@ pub async fn get_hosts_stats(db: &Client, cutoff: u64) -> Result, let pipeline = vec![ doc! { - // get entries within last days + // get entries within last hours "$match": { "timestamp": {"$gte": cutoff_ms} } @@ -229,13 +228,13 @@ pub async fn get_hosts_stats(db: &Client, cutoff: u64) -> Result, } // Helper function to get cutoff timestamp for filter -// We use u64 for days because otherwise we have to recast as u64 in the function, and 4 bytes isn't a big deal here -// Returns None if days is too large and causes negative timestamp (propagates .checked_sub() which does the same) -fn get_cutoff_timestamp(days: u64) -> Option { +// We use u64 for hours because otherwise we have to recast as u64 in the function, and 4 bytes isn't a big deal here +// Returns None if hours is too large and causes negative timestamp (propagates .checked_sub() which does the same) +fn get_cutoff_timestamp(hours: u64) -> Option { let current_timestamp = SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH) .expect("SystemTime should be after unix epoch"); - let valid_duration = Duration::from_secs(60 * 60 * 24 * days); + let valid_duration = Duration::from_secs(60 * 60 * hours); let cutoff_timestamp = current_timestamp.checked_sub(valid_duration)?; use std::convert::TryInto; diff --git a/src/main.rs b/src/main.rs index 775dba3..3684d15 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,13 +30,13 @@ async fn uptime(name: String, pool: &State) -> Result")] +#[get("/list-available?")] async fn list_available( - days: u64, + hours: u64, pool: &State, ) -> Result>, ApiError> { - // TODO: return BAD_REQUEST if days not passed - let hosts = db::get_hosts_stats(&pool.mongo, days).await?; + // TODO: return BAD_REQUEST if hours not passed + let hosts = db::get_hosts_stats(&pool.mongo, hours).await?; let members = db::get_zerotier_members(&pool.mongo).await?; Ok(Json(list_available_hosts(hosts, members).await?))