From 79b9da9f0c788960958f598251a86b7c5682a481 Mon Sep 17 00:00:00 2001 From: Kira Sotnikov Date: Tue, 29 Oct 2024 19:32:28 +0300 Subject: [PATCH] Fix bps to mbps; 0.0.17 --- .gitignore | 1 + Cargo.lock | 2 +- Cargo.toml | 2 +- query.txt | 101 ---------------------------------------------------- src/main.rs | 4 +-- src/web.rs | 43 +++++++++++----------- 6 files changed, 26 insertions(+), 127 deletions(-) delete mode 100644 query.txt diff --git a/.gitignore b/.gitignore index de45b0b..14ec505 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ config.toml config-api.toml deploy/servers query.txt +query2.txt diff --git a/Cargo.lock b/Cargo.lock index 151b7f3..5a2135f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1620,7 +1620,7 @@ checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" [[package]] name = "pony" -version = "0.0.16" +version = "0.0.17" dependencies = [ "actix-web", "actix-web-httpauth", diff --git a/Cargo.toml b/Cargo.toml index 9213e69..a01adc2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pony" -version = "0.0.16" +version = "0.0.17" edition = "2021" [dependencies] diff --git a/query.txt b/query.txt deleted file mode 100644 index 755f8b0..0000000 --- a/query.txt +++ /dev/null @@ -1,101 +0,0 @@ -SELECT - fromUnixTimestamp(Timestamp) AS t, - Path AS metric, - Value as value -FROM - default.graphite_data -WHERE - Path LIKE 'prod.%' - -ORDER BY - Timestamp ASC; - - ---- -SELECT - toStartOfInterval(toDateTime(Timestamp), INTERVAL 1 minute) AS time, - sum(Value) AS total_connections -FROM - default.graphite_data -WHERE - Path LIKE 'prod.%.network.%' - AND toDateTime(Timestamp) >= now() - INTERVAL 1 hour -GROUP BY - time -ORDER BY - time - - -SELECT anyLast(Date) AS latest_t, Path -FROM graphite_index -GROUP BY Path; - - -SELECT anyLast(Level) as level, anyLast(Date) AS latest_t, Path as metric FROM graphite_index WHERE metric like 'dev%connections%' and Level = '4' GROUP BY metric; - - -SELECT anyLast(Level) as level, anyLast(Date) AS time, Path as metric FROM graphite_index WHERE metric like 'prod%connections%' and Level = '4' GROUP BY metric ORDER BY time ASC; - -SELECT - toInt64(toUnixTimestamp(toDateTime(anyLast(Date)))) AS latest, - Path AS metric, - toFloat64(anyLast(Value)) AS value - FROM default.graphite_data - WHERE metric LIKE 'prod.mk2%.connections%' - GROUP BY metric - - -"SELECT toInt64(toUnixTimestamp(toDateTime(anyLast(Date)))) AS latest, - Path as metric, - toFloat64(anyLast(Value)) as value - FROM default.graphite_data - WHERE metric - LIKE 'dev.mk2%connections.%' - GROUP BY metric"; - - - SELECT - toInt64(toUnixTimestamp(toDateTime(anyLast(Date)))) AS latest, - Path AS metric, - toFloat64(anyLast(Value)) AS value - FROM default.graphite_data - WHERE metric LIKE 'dev.mk2%bps' - GROUP BY metric - - - curl -X POST http://127.0.0.1:5005/webhook \ - -H "Content-Type: application/json" \ - -d '{ - "product": { - "id": "d31384b8-e412-4be5-a2ec-297ae6666c8f", - "title": "Тестовый продукт для cs 1.3" - }, - "buyer": { - "email": "emwbwj@lava.top" - }, - "contractId": "7ea82675-4ded-4133-95a7-a6efbaf165cc", - "amount": 40254.19, - "currency": "RUB", - "timestamp": "2024-02-05T09:38:27.33277Z", - "status": "failed", - "errorMessage": "Payment window is opened but not completed" - }' - - curl -X POST http://127.0.0.1:5005/webhook \ - -H "Content-Type: application/json" \ - -d '{ - "product": { - "id": "72d53efb-3696-469f-b856-f0d815748dd6", - "title": "Subscription 3085107274" - }, - "buyer": { - "email": "emwbwj@lava.top" - }, - "contractId": "c5a0cacc-3453-44b0-9532-aa492f1ba191", - "amount": 2049.85, - "currency": "RUB", - "timestamp": "2024-02-05T08:44:32.42176Z", - "status": "subscription-active", - "errorMessage": "" - }' - diff --git a/src/main.rs b/src/main.rs index bd7cbbf..5a77875 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,7 +34,7 @@ use crate::web::not_found; #[derive(Parser)] #[command( - version = "0.0.16", + version = "0.0.17", about = "Pony - montiroing tool for Xray/Wireguard" )] struct Cli { @@ -77,7 +77,7 @@ async fn main() -> std::io::Result<()> { std::process::exit(1); } else { info!(">>> Settings: {:?}", settings); - info!(">>> Version: 0.0.16"); + info!(">>> Version: 0.0.17"); } let carbon_server = settings.carbon.address.clone(); diff --git a/src/web.rs b/src/web.rs index d4c07f0..9cd83f6 100644 --- a/src/web.rs +++ b/src/web.rs @@ -33,27 +33,26 @@ struct ConnectionsByType { #[derive(Serialize)] struct StatusResponse { connections: ConnectionsByServer, - bps: BpsByServer, + mbps: MbpsByServer, } -type Bps = Vec>; -type BpsByServer = Vec>>; - -fn convert_bps(rx: Bps, tx: Bps) -> BpsByServer { - let mut server_list: BpsByServer = Vec::new(); - - for (index, rx_map) in rx.iter().enumerate() { - if let Some(tx_map) = tx.get(index) { - for (server, &rx_value) in rx_map { - let tx_value = *tx_map.get(server).unwrap_or(&0.0); - let mut server_entry = HashMap::new(); - server_entry.insert( - server.clone(), - HashMap::from([("rx".to_string(), rx_value), ("tx".to_string(), tx_value)]), - ); - server_list.push(server_entry); - } - } +type Bps = HashMap; +type MbpsByServer = Vec>>; + +fn convert_bps_to_mbps(rx: Bps, tx: Bps) -> MbpsByServer { + let mut server_list: MbpsByServer = Vec::new(); + + for (server, &rx_value) in &rx { + let tx_value = tx.get(server).copied().unwrap_or(0.0); + let mut server_entry = HashMap::new(); + server_entry.insert( + server.clone(), + HashMap::from([ + ("rx".to_string(), (rx_value * 1500.0 / 1024.0 / 1024.0)), + ("tx".to_string(), (tx_value * 1500.0 / 1024.0 / 1024.0)), + ]), + ); + server_list.push(server_entry); } server_list @@ -175,14 +174,14 @@ pub async fn status(req: Path, ch_client: Data>) -> impl Res } let connections_by_server = convert_connections(connections_by_type.clone()); - let bps_by_server = convert_bps(vec![rx.clone()], vec![tx.clone()]); + let mbps_by_server = convert_bps_to_mbps(rx.clone(), tx.clone()); debug!("Connections {:?}", connections_by_server); - debug!("Bps {:?} {:?}", rx, tx); + debug!("Mbps {:?}", mbps_by_server); let response = StatusResponse { connections: connections_by_server, - bps: bps_by_server, + mbps: mbps_by_server, }; HttpResponse::Ok().json(response)