manager: Simplify very complex mermaid graphs
All checks were successful
/ build (push) Successful in 1m4s

Forgejo has a limit of 5000 characters per mermaid graph.  We started
hitting this for some issues.  Regenerate a somewhat simplified version
of the subtree in this case which hopefully does not hit the same limit
again.
This commit is contained in:
Rahix 2025-05-25 03:42:59 +02:00
parent 42ec50cb64
commit cc05ba7407
3 changed files with 39 additions and 17 deletions

View file

@ -111,9 +111,24 @@ pub async fn update_bot_comment_from_subtree(
subtree: &crate::tree::Subtree<'_>, subtree: &crate::tree::Subtree<'_>,
hash: &str, hash: &str,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let mermaid = subtree.to_mermaid(&ctx.repo_url.to_string()); let mut mermaid = subtree.to_mermaid(&ctx.repo_url.to_string(), false);
let full_text = format!( // When the mermaid graph gets too big, regenerate a simplified version.
if mermaid.len() > 4990 {
log::info!("Mermaid graph is too big, generating simplified version...");
mermaid = subtree.to_mermaid(&ctx.repo_url.to_string(), true);
}
let full_text = if mermaid.len() > 4990 {
format!(
r##"## Partial Techtree
_Sorry, the partial techtree is still too big to be displayed for this element..._
<small>Digest: {hash}; Last Updated: {timestamp}</small>"##,
timestamp = ctx.timestamp,
)
} else {
format!(
r##"## Partial Techtree r##"## Partial Techtree
```mermaid ```mermaid
{mermaid} {mermaid}
@ -121,7 +136,8 @@ pub async fn update_bot_comment_from_subtree(
<small>Digest: {hash}; Last Updated: {timestamp}</small>"##, <small>Digest: {hash}; Last Updated: {timestamp}</small>"##,
timestamp = ctx.timestamp, timestamp = ctx.timestamp,
); )
};
update_bot_comment(&ctx, id, full_text).await?; update_bot_comment(&ctx, id, full_text).await?;

View file

@ -51,6 +51,7 @@ impl Element {
index: ElementIndex, index: ElementIndex,
role: Option<SubtreeElementRole>, role: Option<SubtreeElementRole>,
repo_url: &str, repo_url: &str,
simple: bool,
) -> String { ) -> String {
let Element { let Element {
issue_number, issue_number,
@ -59,9 +60,13 @@ impl Element {
status, status,
} = self; } = self;
let label = format!( let label = if simple {
format!(r##"#{issue_number} | {status}\n<i>{ty}</i>\n<b>{description}</b>"##)
} else {
format!(
r##"<a href='{repo_url}/issues/{issue_number}' target='_blank'>#{issue_number}</a> | {status}\n<i>{ty}</i>\n<b>{description}</b>"## r##"<a href='{repo_url}/issues/{issue_number}' target='_blank'>#{issue_number}</a> | {status}\n<i>{ty}</i>\n<b>{description}</b>"##
); )
};
let class = match (role, status) { let class = match (role, status) {
(Some(SubtreeElementRole::ElementOfInterest), _) => "eoi", (Some(SubtreeElementRole::ElementOfInterest), _) => "eoi",
@ -176,13 +181,13 @@ impl Tree {
) )
} }
pub fn to_mermaid(&self, repo_url: &str) -> String { pub fn to_mermaid(&self, repo_url: &str, simple: bool) -> String {
let mut mermaid = String::new(); let mut mermaid = String::new();
mermaid.push_str("flowchart BT\n"); mermaid.push_str("flowchart BT\n");
mermaid.push_str(&mermaid_classes()); mermaid.push_str(&mermaid_classes());
for index in self.graph.node_indices() { for index in self.graph.node_indices() {
mermaid.push_str(&self.graph[index].to_mermaid_node(index, None, repo_url)); mermaid.push_str(&self.graph[index].to_mermaid_node(index, None, repo_url, simple));
mermaid.push_str("\n"); mermaid.push_str("\n");
} }
@ -300,18 +305,19 @@ impl<'a> Subtree<'a> {
format!("{:?}", dot) format!("{:?}", dot)
} }
pub fn to_mermaid(&self, repo_url: &str) -> String { pub fn to_mermaid(&self, repo_url: &str, simple: bool) -> String {
let mut mermaid = String::new(); let mut mermaid = String::new();
mermaid.push_str("flowchart BT\n"); mermaid.push_str("flowchart BT\n");
mermaid.push_str(&mermaid_classes()); mermaid.push_str(&mermaid_classes());
for index in self.graph.node_indices() { for index in self.graph.node_indices() {
let node = &self.graph[index]; let node = &self.graph[index];
mermaid.push_str( mermaid.push_str(&node.element.to_mermaid_node(
&node index,
.element Some(node.role),
.to_mermaid_node(index, Some(node.role), repo_url), repo_url,
); simple,
));
mermaid.push_str("\n"); mermaid.push_str("\n");
} }

View file

@ -5,7 +5,7 @@ pub async fn update_wiki_from_tree(
ctx: &crate::Context, ctx: &crate::Context,
tree: &crate::tree::Tree, tree: &crate::tree::Tree,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let mermaid = tree.to_mermaid(&ctx.repo_url.to_string()); let mermaid = tree.to_mermaid(&ctx.repo_url.to_string(), false);
let wiki_text = format!( let wiki_text = format!(
r##"This page is automatically updated to show the latest and greatest FAFO techtree: r##"This page is automatically updated to show the latest and greatest FAFO techtree: