refactor: simplify report generation code

This commit is contained in:
Lars Wirzenius
2025-10-07 10:23:23 +03:00
parent c459b6763a
commit f9aad668be
2 changed files with 92 additions and 72 deletions
BIN
View File
Binary file not shown.
+92 -72
View File
@@ -97,37 +97,21 @@ impl PageData {
}
fn status_page_as_html(&self) -> Result<HtmlPage, PageError> {
let mut doc = HtmlPage::default();
let title = format!("CI for Radicle node {}", self.node_alias);
Self::head(&mut doc, &title);
doc.push_to_body(Element::new(Tag::H1).with_text(&title));
let mut doc = ReportPage::new(&self.node_alias);
if let Some(snippet) = &self.desc_snippet {
let mut desc = Element::new(Tag::Div);
desc.push_html(snippet);
doc.push_to_body(desc);
doc.desc(snippet);
}
doc.rss_feeds();
self.broker_status(&mut doc);
self.repo_table(&mut doc);
self.event_queue(&mut doc);
self.recent_status(&mut doc)?;
Ok(doc.page())
}
doc.push_to_body(Element::new(Tag::H2).with_text("RSS feeds"));
doc.push_to_body(
Element::new(Tag::P)
.with_child(
Element::new(Tag::A)
.with_text("all")
.with_attribute("href", BROKER_RSS),
)
.with_text(" ")
.with_child(
Element::new(Tag::A)
.with_text("failed")
.with_attribute("href", FAILURE_RSS),
),
);
Self::h1(&mut doc, "Broker status");
doc.push_to_body(
fn broker_status(&self, doc: &mut ReportPage) {
doc.h2("Broker status");
doc.push(
Element::new(Tag::P)
.with_text("Last updated: ")
.with_text(&self.timestamp)
@@ -138,9 +122,11 @@ impl PageData {
.with_child(Element::new(Tag::Code).with_text(self.ci_broker_git_commit))
.with_text(")"),
);
}
Self::h1(&mut doc, "Repositories");
Self::p_text(&mut doc, "Latest CI run for each repository.");
fn repo_table(&self, doc: &mut ReportPage) {
doc.h2("Repositories");
doc.push(Element::new(Tag::P).with_text("Latest CI run for each repository."));
let total = self.runs.len();
let failed = self
@@ -148,10 +134,9 @@ impl PageData {
.values()
.filter(|run| run.result() == Some(&RunResult::Failure))
.count();
Self::p_text(
&mut doc,
&format!("Total {total} CI runs recorded, of which {failed} failed."),
);
doc.push(Element::new(Tag::P).with_text(&format!(
"Total {total} CI runs recorded, of which {failed} failed."
)));
let mut table = Element::new(Tag::Table).with_class("repolist").with_child(
Element::new(Tag::Tr)
@@ -196,9 +181,11 @@ impl PageData {
.with_child(Element::new(Tag::Td).with_child(info_url)),
);
}
doc.push_to_body(table);
doc.push(table);
}
Self::h1(&mut doc, "Event queue");
fn event_queue(&self, doc: &mut ReportPage) {
doc.h2("Event queue");
let mut table = Element::new(Tag::Table)
.with_class("event-queue")
.with_child(
@@ -302,11 +289,13 @@ impl PageData {
.with_child(Element::new(Tag::Td).with_child(event_element)),
);
}
doc.push_to_body(table);
doc.push(table);
}
Self::h1(&mut doc, "Recent status");
fn recent_status(&self, doc: &mut ReportPage) -> Result<(), PageError> {
doc.h2("Recent status");
let status = StatusData::from(self).as_json()?;
doc.push_to_body(
doc.push(
Element::new(Tag::P)
.with_text("See also as a separate file ")
.with_child(
@@ -316,11 +305,10 @@ impl PageData {
)
.with_text(": "),
);
doc.push_to_body(
doc.push(
Element::new(Tag::Blockquote).with_child(Element::new(Tag::Pre).with_text(&status)),
);
Ok(doc)
Ok(())
}
fn run_state(run: &Run) -> Element {
@@ -347,12 +335,8 @@ impl PageData {
}
fn per_repo_page_as_html(&self, rid: RepoId, alias: &str, timestamp: &str) -> HtmlPage {
let mut doc = HtmlPage::default();
let title = format!("CI runs for repository {alias}");
Self::head(&mut doc, &title);
doc.push_to_body(
let mut doc = ReportPage::new(alias);
doc.push(
Element::new(Tag::P).with_child(
Element::new(Tag::A)
.with_attribute("href", "./index.html")
@@ -360,16 +344,14 @@ impl PageData {
),
);
Self::h1(&mut doc, &title);
doc.push_to_body(
doc.push(
Element::new(Tag::P).with_text("Repository ID ").with_child(
Element::new(Tag::Code)
.with_class("repoid")
.with_text(&rid.to_string()),
),
);
Self::p_text(&mut doc, &format!("Last updated: {timestamp}"));
doc.push(Element::new(Tag::P).with_text(&format!("Last updated: {timestamp}")));
let mut table = Element::new(Tag::Table).with_class("run-list").with_child(
Element::new(Tag::Tr)
@@ -417,27 +399,9 @@ impl PageData {
);
}
doc.push_to_body(table);
doc.push(table);
doc
}
fn head(page: &mut HtmlPage, title: &str) {
page.push_to_head(Element::new(Tag::Title).with_text(title));
page.push_to_head(Element::new(Tag::Style).with_text(CSS));
page.push_to_head(
Element::new(Tag::Meta)
.with_attribute("http-equiv", "refresh")
.with_attribute("content", REFERESH_INTERVAL),
);
}
fn h1(page: &mut HtmlPage, text: &str) {
page.push_to_body(Element::new(Tag::H2).with_text(text));
}
fn p_text(page: &mut HtmlPage, text: &str) {
page.push_to_body(Element::new(Tag::P).with_text(text));
doc.page()
}
fn repository(repo_id: RepoId, alias: &str, runs: Vec<&Run>) -> Element {
@@ -772,6 +736,62 @@ impl PageData {
}
}
struct ReportPage {
page: HtmlPage,
}
impl ReportPage {
fn new(node_alias: &str) -> Self {
let mut page = HtmlPage::default();
let title = format!("CI for Radicle node {node_alias}");
page.push_to_head(Element::new(Tag::Title).with_text(&title));
page.push_to_head(Element::new(Tag::Style).with_text(CSS));
page.push_to_head(
Element::new(Tag::Meta)
.with_attribute("http-equiv", "refresh")
.with_attribute("content", REFERESH_INTERVAL),
);
page.push_to_body(Element::new(Tag::H1).with_text(&title));
Self { page }
}
fn desc(&mut self, snippet: &str) {
let mut desc = Element::new(Tag::Div);
desc.push_html(snippet);
self.page.push_to_body(desc);
}
fn h2(&mut self, heading: &str) {
self.push(Element::new(Tag::H2).with_text(heading));
}
fn push(&mut self, e: Element) {
self.page.push_to_body(e);
}
fn rss_feeds(&mut self) {
self.h2("RSS feeds");
self.push(
Element::new(Tag::P)
.with_child(
Element::new(Tag::A)
.with_text("all")
.with_attribute("href", BROKER_RSS),
)
.with_text(" ")
.with_child(
Element::new(Tag::A)
.with_text("failed")
.with_attribute("href", FAILURE_RSS),
),
);
}
fn page(self) -> HtmlPage {
self.page
}
}
struct RssEntry {
repoid: RepoId,
commit: Oid,