Skip to content

Commit

Permalink
Fix interval as param
Browse files Browse the repository at this point in the history
  • Loading branch information
the2pizza committed Oct 30, 2024
1 parent 4f70cae commit 8b34b46
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 10 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.22"
version = "0.0.23"
edition = "2021"

[dependencies]
Expand Down
1 change: 1 addition & 0 deletions config-example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

[clickhouse]
#address = "http://localhost:8123"
fetch_interval_minute = 10

[app]
#env = "dev"
Expand Down
3 changes: 2 additions & 1 deletion src/clickhouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub async fn fetch_metrics_value(
env: &str,
cluster: &str,
metric_postfix: &str,
fetch_interval: u8,
) -> Result<Vec<MetricValue>, Box<dyn Error>> {
let metric_value_req = format!(
"SELECT
Expand All @@ -23,7 +24,7 @@ pub async fn fetch_metrics_value(
toFloat64(anyLast(Value)) AS value
FROM default.graphite_data
WHERE metric LIKE '{env}.{cluster}%.{metric_postfix}'
AND Timestamp >= now() - INTERVAL 1 MINUTE
AND Timestamp >= now() - INTERVAL {fetch_interval} MINUTE
GROUP BY metric"
);

Expand Down
7 changes: 6 additions & 1 deletion src/config2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ fn default_wg_port() -> u16 {
51820
}

fn default_fetch_interval() -> u8 {
10
}

fn default_api_bind_addr() -> String {
"0.0.0.0".to_string()
}
Expand Down Expand Up @@ -77,6 +81,8 @@ pub struct CarbonConfig {
pub struct ChConfig {
#[serde(default = "default_ch_server")]
pub address: String,
#[serde(default = "default_fetch_interval")]
pub fetch_interval_minute: u8,
}

#[derive(Clone, Debug, Deserialize, Default)]
Expand Down Expand Up @@ -159,7 +165,6 @@ impl Settings {
if self.wg.enabled && self.wg.port == 0 {
return Err("Xray wg порт port coulnd't be 0".into());
}
// Другие проверки
Ok(())
}
}
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use crate::web::not_found;

#[derive(Parser)]
#[command(
version = "0.0.22",
version = "0.0.23",
about = "Pony - montiroing tool for Xray/Wireguard"
)]
struct Cli {
Expand Down Expand Up @@ -77,7 +77,7 @@ async fn main() -> std::io::Result<()> {
std::process::exit(1);
} else {
info!(">>> Settings: {:?}", settings);
info!(">>> Version: 0.0.22");
info!(">>> Version: 0.0.23");
}

let carbon_server = settings.carbon.address.clone();
Expand Down
26 changes: 22 additions & 4 deletions src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::collections::HashMap;
use std::sync::Arc;

use crate::clickhouse::fetch_metrics_value;
use crate::config2::Settings;

#[derive(Deserialize)]
struct Params {
Expand Down Expand Up @@ -149,7 +150,11 @@ pub async fn hello() -> impl Responder {
}

#[get("/status/{env}/{cluster}")]
pub async fn status(req: Path<Params>, ch_client: Data<Arc<Client>>) -> impl Responder {
pub async fn status(
req: Path<Params>,
ch_client: Data<Arc<Client>>,
settings: Data<Arc<Settings>>,
) -> impl Responder {
let mut connections_by_type = ConnectionsByType {
vless: Vec::new(),
vmess: Vec::new(),
Expand All @@ -161,8 +166,14 @@ pub async fn status(req: Path<Params>, ch_client: Data<Arc<Client>>) -> impl Res

for connection_type in connection_types {
let request_postfix = format!("connections.{connection_type}");
let metrics_connections =
fetch_metrics_value(&ch_client, &req.env, &req.cluster, &request_postfix).await;
let metrics_connections = fetch_metrics_value(
&ch_client,
&req.env,
&req.cluster,
&request_postfix,
settings.clickhouse.fetch_interval_minute,
)
.await;

let metrics_value_connections = match metrics_connections {
Ok(metrics) => metrics
Expand Down Expand Up @@ -201,7 +212,14 @@ pub async fn status(req: Path<Params>, ch_client: Data<Arc<Client>>) -> impl Res
}
}

let metrics_bps = fetch_metrics_value(&ch_client, &req.env, &req.cluster, "%bps").await;
let metrics_bps = fetch_metrics_value(
&ch_client,
&req.env,
&req.cluster,
"%bps",
settings.clickhouse.fetch_interval_minute,
)
.await;
let metrics_value_bps = match metrics_bps {
Ok(metrics) => metrics
.into_iter()
Expand Down

0 comments on commit 8b34b46

Please sign in to comment.