Compare commits
No commits in common. "72fc8fb5889c7e37dad307a454330f43599af935" and "49e8db169c74ae7238b9eeba2a51b277dad9a1bf" have entirely different histories.
72fc8fb588
...
49e8db169c
47
src/main.rs
47
src/main.rs
@ -1,5 +1,4 @@
|
||||
use axum::{
|
||||
extract::State,
|
||||
routing::get,
|
||||
Router,
|
||||
};
|
||||
@ -11,8 +10,6 @@ use serde::{
|
||||
Serialize,
|
||||
};
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct TrafficDataThresholds {
|
||||
level: i64,
|
||||
@ -32,7 +29,12 @@ struct TrafficDataItem {
|
||||
thresholds: Vec<TrafficDataThresholds>,
|
||||
}
|
||||
|
||||
async fn get_baeder_names() -> Result<HashMap<String, String>, String> {
|
||||
struct BaederNames {
|
||||
id: String,
|
||||
name: String,
|
||||
}
|
||||
|
||||
async fn get_baeder_names() -> Result<Vec<BaederNames>, String> {
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let baedernames_req = client.get(String::from("https://www.berlinerbaeder.de/baeder/sommerbaeder-auslastung/"))
|
||||
@ -49,23 +51,19 @@ async fn get_baeder_names() -> Result<HashMap<String, String>, String> {
|
||||
let r = Regex::new(r#"<tr data-bath="(.+)" data-name="(.+)" data-address="#)
|
||||
.map_err(|_err| String::from("Cannot parse regex"))?;
|
||||
|
||||
let mut baedernames= HashMap::new();
|
||||
let mut baedernames: Vec<BaederNames> = vec!{};
|
||||
|
||||
for c in r.captures_iter(baedernames_body.as_str()) {
|
||||
baedernames.insert(
|
||||
c.get(1).unwrap().as_str().to_string(), // id
|
||||
c.get(2).unwrap().as_str().to_string(), // name
|
||||
);
|
||||
baedernames.push(BaederNames {
|
||||
id: c.get(1).unwrap().as_str().to_string(),
|
||||
name: c.get(2).unwrap().as_str().to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
return Ok(baedernames);
|
||||
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct AppState {
|
||||
baedernames: HashMap<String, String>,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
@ -93,24 +91,11 @@ async fn main() {
|
||||
}
|
||||
}
|
||||
|
||||
// If baedernames cannot be fetched, ignore them
|
||||
let baedernames = get_baeder_names().await.unwrap_or(HashMap::new());
|
||||
|
||||
println!("Baedernames:");
|
||||
for (id, name) in &baedernames {
|
||||
println!("{id}: {name}");
|
||||
}
|
||||
println!("");
|
||||
|
||||
let state = AppState {
|
||||
baedernames,
|
||||
};
|
||||
|
||||
let baedernames = get_baeder_names().await;
|
||||
|
||||
let app = Router::new()
|
||||
.route("/", get(route_index))
|
||||
.route("/metrics", get(route_metrics))
|
||||
.with_state(state);
|
||||
.route("/metrics", get(route_metrics));
|
||||
|
||||
let listener = tokio::net::TcpListener::bind(listen).await.unwrap();
|
||||
println!("Server listening on: http://{}", listener.local_addr().unwrap());
|
||||
@ -122,9 +107,7 @@ async fn route_index() -> String {
|
||||
return String::from("Prometheus exporter for Berlinerbaeder occupancy");
|
||||
}
|
||||
|
||||
async fn route_metrics(
|
||||
State(state): State<AppState>,
|
||||
) -> Result<String, String> {
|
||||
async fn route_metrics() -> Result<String, String> {
|
||||
let client = reqwest::Client::new();
|
||||
let trafficdata_req = client.get(String::from("https://www.berlinerbaeder.de/traffic/trafficdata.json"))
|
||||
.send().await
|
||||
@ -143,7 +126,7 @@ async fn route_metrics(
|
||||
let mut out = String::new();
|
||||
|
||||
for bad in trafficdata {
|
||||
out.push_str(&format!("berlinerbaeder_occupation{{bad=\"{}\" badname=\"{}\"}} {}\n", bad.id, state.baedernames.get(&bad.id).unwrap_or(&String::new()), bad.counter.unwrap_or(-1)));
|
||||
out.push_str(&format!("berlinerbaeder_occupation{{bad=\"{}\"}} {}\n", bad.id, bad.counter.unwrap_or(-1)));
|
||||
}
|
||||
|
||||
return Ok(out);
|
||||
|
Loading…
Reference in New Issue
Block a user