-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from Holo-Host/new-endpoint-208-b
Endpoint 208 - /holoport/usage
- Loading branch information
Showing
4 changed files
with
77 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,7 +66,7 @@ pub async fn rocket() -> Rocket<Build> { | |
.mount( | ||
"/holoport", | ||
routes![ | ||
usage, // TODO!! | ||
usage, // done | ||
], | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,75 @@ | ||
use anyhow::Result; | ||
use rocket::{ | ||
http::Status, | ||
serde::json::Json, | ||
serde::{json::Json, Deserialize, Serialize}, | ||
{get, State}, | ||
}; | ||
|
||
use crate::hpos::WsMutex; | ||
use crate::hpos::{Ws, WsMutex}; | ||
|
||
/// Returns list of all host invoices as needed for the host-console-ui invoice page | ||
/// -- includes optional invoice_set {all, unpaid, paid} param to allow querying the invoices by their status | ||
#[get("/usage")] | ||
pub async fn usage(wsm: &State<WsMutex>) -> Result<Json<()>, (Status, String)> { | ||
#[get("/usage?<usage_interval>")] | ||
pub async fn usage( | ||
wsm: &State<WsMutex>, | ||
usage_interval: i64, | ||
) -> Result<Json<UsageResponse>, (Status, String)> { | ||
let mut ws = wsm.lock().await; | ||
|
||
Ok(Json(())) | ||
Ok(Json(handle_usage(&mut ws, usage_interval).await.map_err( | ||
|e| (Status::InternalServerError, e.to_string()), | ||
)?)) | ||
} | ||
|
||
async fn handle_usage(ws: &mut Ws, usage_interval: i64) -> Result<UsageResponse> { | ||
let all_hosted_happs = | ||
crate::handlers::hosted_happs::handle_get_all(usage_interval, None, ws).await?; | ||
|
||
Ok(all_hosted_happs | ||
.into_iter() | ||
.fold(UsageResponse::default(), |acc, happ| { | ||
if !happ.enabled { | ||
// is this logic right? Isn't it possible for a happ to have some usage but now be disabled? | ||
return acc; | ||
} | ||
|
||
let UsageResponse { | ||
total_hosted_agents, | ||
current_total_storage, | ||
total_hosted_happs, | ||
total_usage, | ||
} = acc; | ||
|
||
let TotalUsage { cpu, bandwidth } = total_usage; | ||
|
||
let happ_usage = happ.usage.unwrap_or_default(); | ||
|
||
UsageResponse { | ||
total_hosted_agents: total_hosted_agents + happ.source_chains.unwrap_or_default(), | ||
current_total_storage: current_total_storage + happ_usage.disk_usage, | ||
total_hosted_happs: total_hosted_happs + 1, | ||
total_usage: TotalUsage { | ||
cpu: cpu + happ_usage.cpu, | ||
bandwidth: bandwidth + happ_usage.bandwidth, | ||
}, | ||
} | ||
})) | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Default)] | ||
#[serde(crate = "rocket::serde")] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct UsageResponse { | ||
total_hosted_agents: u16, | ||
current_total_storage: u64, | ||
total_hosted_happs: u16, | ||
total_usage: TotalUsage, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Default)] | ||
#[serde(crate = "rocket::serde")] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct TotalUsage { | ||
cpu: u64, | ||
bandwidth: u64, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters