Skip to content

Commit

Permalink
Flatten api output, 0.0.16
Browse files Browse the repository at this point in the history
  • Loading branch information
the2pizza committed Oct 29, 2024
1 parent c44d474 commit 2436b03
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pony"
version = "0.0.14"
version = "0.0.16"
edition = "2021"

[dependencies]
Expand Down
7 changes: 5 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ use crate::utils::{current_timestamp, human_readable_date, level_from_settings};
use crate::web::not_found;

#[derive(Parser)]
#[command(version = "0.0.1", about = "Pony - montiroing tool for Xray/Wireguard")]
#[command(
version = "0.0.16",
about = "Pony - montiroing tool for Xray/Wireguard"
)]
struct Cli {
#[arg(short, long, default_value = "config.toml")]
config: String,
Expand Down Expand Up @@ -74,7 +77,7 @@ async fn main() -> std::io::Result<()> {
std::process::exit(1);
} else {
info!(">>> Settings: {:?}", settings);
info!(">>> Version: 0.0.15");
info!(">>> Version: 0.0.16");
}

let carbon_server = settings.carbon.address.clone();
Expand Down
70 changes: 59 additions & 11 deletions src/web.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use actix_web::web::{Data, Path};
use actix_web::{get, HttpResponse, Responder};
use clickhouse::Client;
use log::debug;
use log::error;
use serde::Deserialize;
use serde::Serialize;
Expand All @@ -15,13 +16,13 @@ struct Params {
cluster: String,
}

#[derive(Serialize)]
#[derive(Serialize, Debug, Clone)]
struct Connection {
#[serde(flatten)]
connection: HashMap<String, f64>,
}

#[derive(Serialize)]
#[derive(Serialize, Debug, Clone)]
struct ConnectionsByType {
vless: Vec<Connection>,
vmess: Vec<Connection>,
Expand All @@ -30,15 +31,56 @@ struct ConnectionsByType {
}

#[derive(Serialize)]
struct Bps {
rx: HashMap<String, f64>,
tx: HashMap<String, f64>,
struct StatusResponse {
connections: ConnectionsByServer,
bps: BpsByServer,
}

#[derive(Serialize)]
struct StatusResponse {
connections: ConnectionsByType,
bps: Bps,
type Bps = Vec<HashMap<String, f64>>;
type BpsByServer = Vec<HashMap<String, HashMap<String, f64>>>;

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);
}
}
}

server_list
}

type ConnectionsByServer = HashMap<String, Vec<HashMap<String, f64>>>;

fn convert_connections(connections: ConnectionsByType) -> ConnectionsByServer {
let mut server_map: ConnectionsByServer = HashMap::new();

for (conn_type, conn_list) in [
("vless", connections.vless),
("vmess", connections.vmess),
("ss", connections.ss),
("wg", connections.wg),
] {
for conn in conn_list {
for (server, &value) in &conn.connection {
server_map
.entry(server.clone())
.or_insert_with(Vec::new)
.push(HashMap::from([(conn_type.to_string(), value)]));
}
}
}

server_map
}

pub async fn not_found() -> HttpResponse {
Expand Down Expand Up @@ -132,9 +174,15 @@ pub async fn status(req: Path<Params>, ch_client: Data<Arc<Client>>) -> impl Res
}
}

let connections_by_server = convert_connections(connections_by_type.clone());
let bps_by_server = convert_bps(vec![rx.clone()], vec![tx.clone()]);

debug!("Connections {:?}", connections_by_server);
debug!("Bps {:?} {:?}", rx, tx);

let response = StatusResponse {
connections: connections_by_type,
bps: Bps { rx, tx },
connections: connections_by_server,
bps: bps_by_server,
};

HttpResponse::Ok().json(response)
Expand Down

0 comments on commit 2436b03

Please sign in to comment.