Skip to content

Commit 719805a

Browse files
davidv1992rnijveld
authored andcommitted
[Management] Implement filtering of monitoring logs.
1 parent c1f41cf commit 719805a

File tree

4 files changed

+62
-10
lines changed

4 files changed

+62
-10
lines changed

nts-pool-management/.sqlx/query-ed2041e8d00e1549407b85e23ed6754885eecc9e86f1c7fbc442fab80db3d4b2.json renamed to nts-pool-management/.sqlx/query-171a166b3904d3adc72871e46be498526c402b598ad4ac3043581eb4e0b0ced6.json

Lines changed: 14 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nts-pool-management/src/models/time_source.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ impl std::fmt::Display for LogRow {
273273
pub async fn logs(
274274
conn: impl DbConnLike<'_>,
275275
time_source_id: TimeSourceId,
276+
monitor_id: MonitorId,
277+
protocol: IpVersion,
276278
offset: i64,
277279
limit: i64,
278280
) -> Result<Vec<LogRow>, sqlx::Error> {
@@ -281,12 +283,14 @@ pub async fn logs(
281283
r#"
282284
SELECT score, protocol as "protocol: IpVersion", monitor_id as monitor, raw_sample AS sample, received_at
283285
FROM monitor_samples
284-
WHERE time_source_id = $1
286+
WHERE time_source_id = $1 AND protocol = $2 AND monitor_id = $3
285287
ORDER BY monitor_samples.received_at DESC
286-
OFFSET $2
287-
LIMIT $3
288+
OFFSET $4
289+
LIMIT $5
288290
"#,
289291
time_source_id as _,
292+
protocol as _,
293+
monitor_id as _,
290294
offset,
291295
limit,
292296
)

nts-pool-management/src/routes/management.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,23 @@ use std::collections::HashMap;
33
use askama::Template;
44
use axum::{
55
Form,
6-
extract::{Path, State},
6+
extract::{Path, Query, State},
77
response::{IntoResponse, Redirect},
88
};
99
use nts_pool_shared::IpVersion;
10+
use serde::Deserialize;
1011

1112
use crate::{
1213
AppState,
1314
auth::{self, AuthorizedUser},
1415
context::AppContext,
1516
cookies::CookieService,
1617
error::AppError,
17-
models::time_source::{
18-
self, LogRow, NewTimeSourceForm, TimeSource, TimeSourceId, UpdateTimeSourceForm,
18+
models::{
19+
monitor::MonitorId,
20+
time_source::{
21+
self, LogRow, NewTimeSourceForm, TimeSource, TimeSourceId, UpdateTimeSourceForm,
22+
},
1923
},
2024
templates::{HtmlTemplate, filters},
2125
};
@@ -52,23 +56,40 @@ struct TimeSourceInfoTemplate {
5256
ts: TimeSource,
5357
log: Vec<LogRow>,
5458
scores: Vec<ScoreTableData>,
59+
monitors: Vec<String>,
60+
cur_monitor: Option<MonitorId>,
61+
cur_protocol: IpVersion,
62+
}
63+
64+
#[derive(Deserialize)]
65+
pub struct LogSelection {
66+
monitor: Option<MonitorId>,
67+
protocol: Option<IpVersion>,
5568
}
5669

5770
pub async fn time_source_info(
5871
Path(time_source_id): Path<TimeSourceId>,
5972
app: AppContext,
6073
State(state): State<AppState>,
74+
Query(log_choice): Query<LogSelection>,
6175
) -> Result<impl IntoResponse, AppError> {
6276
let ts = time_source::details(&state.db, time_source_id).await?;
63-
let log = time_source::logs(&state.db, time_source_id, 0, 200).await?;
6477
let scores = time_source::scores(&state.db, time_source_id).await?;
78+
let cur_monitor = log_choice.monitor.or_else(|| scores.first().map(|v| v.id));
79+
let cur_protocol = log_choice.protocol.unwrap_or(IpVersion::Ipv4);
6580
let mut scoremap: HashMap<String, (f64, f64)> = HashMap::new();
6681
for score in scores {
6782
match score.protocol {
6883
IpVersion::Ipv4 => scoremap.entry(score.id.to_string()).or_default().0 = score.score,
6984
IpVersion::Ipv6 => scoremap.entry(score.id.to_string()).or_default().1 = score.score,
7085
}
7186
}
87+
let monitors: Vec<String> = scoremap.iter().map(|v| v.0.clone()).collect();
88+
let log = if let Some(cur_monitor) = cur_monitor {
89+
time_source::logs(&state.db, time_source_id, cur_monitor, cur_protocol, 0, 200).await?
90+
} else {
91+
vec![]
92+
};
7293

7394
Ok(HtmlTemplate(TimeSourceInfoTemplate {
7495
app,
@@ -82,6 +103,9 @@ pub async fn time_source_info(
82103
})
83104
.collect(),
84105
log,
106+
monitors,
107+
cur_monitor,
108+
cur_protocol,
85109
}))
86110
}
87111

nts-pool-management/templates/management/time_source_details.html.j2

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,19 @@
2727

2828
<section>
2929
<h2>Logs</h2>
30-
{% if !log.is_empty() %}
30+
{% if let Some(cur_monitor) = cur_monitor %}
31+
<form method="get">
32+
Monitor: <select name="monitor">
33+
{% for mon in monitors %}
34+
<option value="{{mon}}" {% if &cur_monitor.to_string() == mon %} selected {% endif %}>{{ mon }}</option>
35+
{% endfor %}
36+
</select>
37+
<select name="protocol">
38+
<option value="Ipv4" {% if cur_protocol == IpVersion::Ipv4 %} selected {% endif %}>IPv4</option>
39+
<option value="Ipv6" {% if cur_protocol == IpVersion::Ipv6 %} selected {% endif %}>IPv6</option>
40+
</select>
41+
<button type="submit">apply</button>
42+
</form>
3143
<div class="grid-table">
3244
<div class="grid-table-header">Received at</div>
3345
<div class="grid-table-header">Monitor</div>

0 commit comments

Comments
 (0)