Unifieng data
This commit is contained in:
parent
0dc51af51c
commit
4ff103454e
7 changed files with 66 additions and 41 deletions
2
src/data/mod.rs
Normal file
2
src/data/mod.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
pub mod unified;
|
||||
pub use unified::unify_data;
|
||||
37
src/data/unified.rs
Normal file
37
src/data/unified.rs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
use crate::heartbeat::model::{HeartbeatResponse, UptimeData};
|
||||
use crate::status_page::model::{MonitorInfo, StatusPageResponse};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct UnifiedMonitorData {
|
||||
pub monitor_info: MonitorInfo,
|
||||
pub uptime_data: Option<UptimeData>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct UnifiedData {
|
||||
pub status_page_title: String,
|
||||
pub status_page_description: Option<String>,
|
||||
pub monitors: Vec<UnifiedMonitorData>,
|
||||
}
|
||||
pub fn unify_data(status_page: &StatusPageResponse, heartbeat: &HeartbeatResponse) -> UnifiedData {
|
||||
let mut monitors = Vec::new();
|
||||
|
||||
for group in &status_page.public_group_list {
|
||||
for monitor_info in &group.monitor_list {
|
||||
let uptime_data = heartbeat.get_uptime(monitor_info.id, 24).cloned();
|
||||
|
||||
monitors.push(UnifiedMonitorData {
|
||||
monitor_info: monitor_info.clone(),
|
||||
uptime_data: uptime_data,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
monitors.sort_by_key(|m| m.monitor_info.id);
|
||||
|
||||
UnifiedData {
|
||||
status_page_title: status_page.config.title.clone(),
|
||||
status_page_description: status_page.config.description.clone(),
|
||||
monitors,
|
||||
}
|
||||
}
|
||||
|
|
@ -99,11 +99,4 @@ impl HeartbeatResponse {
|
|||
.iter()
|
||||
.find(|u| u.monitor_id == monitor_id && u.period_hours == period_hours)
|
||||
}
|
||||
|
||||
pub fn get_latest_heartbeat(&self, monitor_id: u64) -> Option<&HeartbeatEntry> {
|
||||
self.monitors
|
||||
.iter()
|
||||
.find(|m| m.monitor_id == monitor_id)
|
||||
.and_then(|m| m.heartbeats.last())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
53
src/main.rs
53
src/main.rs
|
|
@ -3,7 +3,10 @@ use clap::Parser;
|
|||
use reqwest::blocking::Client;
|
||||
use std::result::Result::Ok;
|
||||
mod i18n;
|
||||
use i18n::{init_locales, t};
|
||||
use i18n::init_locales;
|
||||
|
||||
use crate::data::unify_data;
|
||||
mod data;
|
||||
mod heartbeat;
|
||||
mod status_page;
|
||||
|
||||
|
|
@ -22,16 +25,8 @@ fn main() -> Result<()> {
|
|||
let args = Args::parse();
|
||||
let base_url = args.base_url.trim_end_matches("/");
|
||||
|
||||
let heartbeat_url = format!(
|
||||
"{}/api/status-page/heartbeat/{}",
|
||||
base_url,
|
||||
args.slug
|
||||
);
|
||||
let status_page_url = format!(
|
||||
"{}/api/status-page/{}",
|
||||
base_url,
|
||||
args.slug
|
||||
);
|
||||
let heartbeat_url = format!("{}/api/status-page/heartbeat/{}", base_url, args.slug);
|
||||
let status_page_url = format!("{}/api/status-page/{}", base_url, args.slug);
|
||||
|
||||
let client = Client::new();
|
||||
|
||||
|
|
@ -39,30 +34,34 @@ fn main() -> Result<()> {
|
|||
|
||||
let heartbeat_response = client.get(heartbeat_url).send()?;
|
||||
|
||||
if heartbeat_response.status().is_success() {
|
||||
let heartbeat_data = if heartbeat_response.status().is_success() {
|
||||
let json_text = heartbeat_response.text()?;
|
||||
match heartbeat::parse_response(&json_text) {
|
||||
Ok(data) => println!("moises: {}", data.uptime_data[0].get_perc_formated()),
|
||||
Err(e) => println!("{}", e),
|
||||
}
|
||||
heartbeat::parse_response(&json_text)?
|
||||
} else {
|
||||
println!("{}", heartbeat_response.status());
|
||||
println!("{}: {}", t("Response"), heartbeat_response.text()?);
|
||||
}
|
||||
return Err(anyhow::anyhow!(heartbeat_response.status()));
|
||||
};
|
||||
|
||||
println!("Status Page URL: {}", status_page_url);
|
||||
|
||||
let status_page_response = client.get(status_page_url).send()?;
|
||||
|
||||
if status_page_response.status().is_success() {
|
||||
let status_page_data = if status_page_response.status().is_success() {
|
||||
let json_text = status_page_response.text()?;
|
||||
match status_page::parse_response(&json_text) {
|
||||
Ok(data) => println!("moises: {}", data.config.title),
|
||||
Err(e) => println!("{}", e),
|
||||
}
|
||||
status_page::parse_response(&json_text)?
|
||||
} else {
|
||||
println!("{}", status_page_response.status());
|
||||
println!("{}: {}", t("Response"), status_page_response.text()?);
|
||||
return Err(anyhow::anyhow!(status_page_response.status()));
|
||||
};
|
||||
|
||||
let unified_data = unify_data(&status_page_data, &heartbeat_data);
|
||||
|
||||
println!("Title: {}", unified_data.status_page_title);
|
||||
|
||||
for monitor in unified_data.monitors {
|
||||
println!("Monitor ID: {}", monitor.monitor_info.id);
|
||||
println!("Nome: {}", monitor.monitor_info.name);
|
||||
println!(
|
||||
"Uptime Service: {}",
|
||||
monitor.uptime_data.expect("Formgen?").get_perc_formated()
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
pub mod model;
|
||||
pub mod parser;
|
||||
pub use model::StatusPageResponse;
|
||||
pub use parser::parse_response;
|
||||
|
|
|
|||
|
|
@ -1,10 +1,5 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::i18n::t;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct StatusPageConfig {
|
||||
pub slug: String,
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ use crate::i18n::t;
|
|||
use anyhow::{Context, Ok, Result};
|
||||
|
||||
pub fn parse_response(json_text: &str) -> Result<StatusPageResponse> {
|
||||
let mut response: StatusPageResponse =
|
||||
let response: StatusPageResponse =
|
||||
serde_json::from_str(json_text).with_context(|| t("invalid-json-status-page"))?;
|
||||
Ok(response)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue