Skip to content

Commit

Permalink
Set up to generate charts from several metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
karencfv committed Dec 5, 2024
1 parent 77fc165 commit bcc51cc
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 28 deletions.
20 changes: 7 additions & 13 deletions clickhouse-admin/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2114,15 +2114,15 @@ snapshot_storage_disk=LocalSnapshotDisk

let expected = vec![
SystemTimeSeries {
time: crate::Timestamp::Unix("1732494720".to_string()),
time: "1732494720".to_string(),
value: 110220450825.75238,
},
SystemTimeSeries {
time: crate::Timestamp::Unix("1732494840".to_string()),
time: "1732494840".to_string(),
value: 110339992917.33331,
},
SystemTimeSeries {
time: crate::Timestamp::Unix("1732494960".to_string()),
time: "1732494960".to_string(),
value: 110421854037.33331,
},
];
Expand All @@ -2142,21 +2142,15 @@ snapshot_storage_disk=LocalSnapshotDisk

let expected = vec![
SystemTimeSeries {
time: crate::Timestamp::Utc(
"2024-11-25T00:34:00Z".parse::<DateTime<Utc>>().unwrap(),
),
time: "2024-11-25T00:34:00Z".to_string(),
value: 110220450825.75238,
},
SystemTimeSeries {
time: crate::Timestamp::Utc(
"2024-11-25T00:35:00Z".parse::<DateTime<Utc>>().unwrap(),
),
time: "2024-11-25T00:35:00Z".to_string(),
value: 110339992917.33331,
},
SystemTimeSeries {
time: crate::Timestamp::Utc(
"2024-11-25T00:36:00Z".parse::<DateTime<Utc>>().unwrap(),
),
time: "2024-11-25T00:36:00Z".to_string(),
value: 110421854037.33331,
},
];
Expand Down Expand Up @@ -2191,7 +2185,7 @@ snapshot_storage_disk=LocalSnapshotDisk

assert_eq!(
format!("{}", root_cause),
"data did not match any variant of untagged enum Timestamp at line 1 column 12",
"invalid type: integer `2024`, expected a string at line 1 column 12",
);
}
}
57 changes: 42 additions & 15 deletions dev-tools/clickana/src/clickana.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,28 @@ use ratatui::{
use slog::{o, Drain, Logger};
use slog_async::Async;
use slog_term::{FullFormat, PlainDecorator};
use std::fmt::Display;
use std::net::SocketAddr;
use std::time::{Duration, Instant};
use tokio::runtime::Runtime;

const GIBIBYTE_F64: f64 = 1073741824.0;
const GIBIBYTE_U64: u64 = 1073741824;

enum MetricName {
// TODO: Add other metrics to gather
DiskUsed,
}

impl Display for MetricName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
MetricName::DiskUsed => "DiskUsed_default",
};
write!(f, "{s}")
}
}

pub struct Clickana {
clickhouse_addr: SocketAddr,
log_path: Utf8PathBuf,
Expand Down Expand Up @@ -58,7 +73,10 @@ impl Clickana {
let tick_rate = Duration::from_secs(self.refresh_interval);
let mut last_tick = Instant::now();
loop {
let top_left_frame = ChartData::new(self.get_api_data()?)?;
let top_left_frame = ChartData::new(
self.get_api_data(MetricName::DiskUsed)?,
"Disk Usage".to_string(),
)?;
let dashboard = Dashboard {
top_left_frame,
_top_right_frame: None,
Expand Down Expand Up @@ -91,7 +109,10 @@ impl Clickana {
dashboard.top_left_frame.render_line_chart(frame, line_chart);
}

fn get_api_data(&self) -> Result<Vec<SystemTimeSeries>> {
fn get_api_data(
&self,
metric: MetricName,
) -> Result<Vec<SystemTimeSeries>> {
let admin_url = format!("http://{}", self.clickhouse_addr);
let log = self.new_logger()?;
let client = ClickhouseServerClient::new(&admin_url, log.clone());
Expand All @@ -101,13 +122,13 @@ impl Clickana {
let timeseries = client
.system_timeseries_avg(
types::SystemTable::AsynchronousMetricLog,
"DiskUsed_default",
&format!("{metric}"),
Some(self.sampling_interval),
Some(self.time_range),
Some(TimestampFormat::UnixEpoch),
)
.await
.map(|t| t.into_inner()) //;
.map(|t| t.into_inner())
.map_err(|e| {
anyhow!(
concat!(
Expand Down Expand Up @@ -154,6 +175,7 @@ struct Dashboard {

#[derive(Debug)]
struct ChartData {
title: String,
data_points: Vec<(f64, f64)>,
avg_time_utc: DateTime<Utc>,
start_time_utc: DateTime<Utc>,
Expand All @@ -168,17 +190,21 @@ struct ChartData {
}

impl ChartData {
fn new(raw_data: Vec<SystemTimeSeries>) -> Result<Self> {
fn new(raw_data: Vec<SystemTimeSeries>, title: String) -> Result<Self> {
// These values will be used to render the graph and ratatui
// requires them to be f64
let data_points: Vec<(f64, f64)> = raw_data
.iter()
.map(|ts| {
(
ts.time.trim_matches('"').parse::<f64>().expect(&format!(
"could not parse timestamp {} into f64",
ts.time
)),
ts.time.trim_matches('"').parse::<f64>().unwrap_or_else(
|_| {
panic!(
"could not parse timestamp {} into f64",
ts.time
)
},
),
ts.value,
)
})
Expand Down Expand Up @@ -227,10 +253,9 @@ impl ChartData {
let timestamps: Vec<i64> = raw_data
.iter()
.map(|ts| {
ts.time.trim_matches('"').parse::<i64>().expect(&format!(
"could not parse timestamp {} into i64",
ts.time
))
ts.time.trim_matches('"').parse::<i64>().unwrap_or_else(|_| {
panic!("could not parse timestamp {} into i64", ts.time)
})
})
.collect();

Expand Down Expand Up @@ -268,6 +293,7 @@ impl ChartData {
let end_time_unix = *end_time as f64;

Ok(Self {
title,
data_points,
avg_time_utc,
start_time_utc,
Expand All @@ -291,8 +317,9 @@ impl ChartData {

let chart = Chart::new(datasets)
.block(
Block::bordered()
.title(Line::from("Disk Usage").cyan().bold().centered()),
Block::bordered().title(
Line::from(self.title.clone()).cyan().bold().centered(),
),
)
.x_axis(
Axis::default()
Expand Down

0 comments on commit bcc51cc

Please sign in to comment.