Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 84 additions & 52 deletions src/NetCorePal.Extensions.CodeAnalysis/VisualizationHtmlBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ public static string GenerateVisualizationHtml(
}

// 构建 dataSources 数组 - 每个snapshot生成完整的数据源
var dataSourcesJson = BuildDataSourcesJson(snapshotList);
// 当没有快照时,使用 analysisResult 直接构建一个 Runtime 数据源,确保 dataSources 不为空
var dataSourcesJson = snapshotList.Count > 0
? BuildDataSourcesJson(snapshotList)
: BuildDataSourcesJsonFromAnalysisResult(analysisResult);
var diagramConfigsJson = BuildDiagramConfigsJson();

// 替换模板中的占位符
Expand All @@ -80,68 +83,97 @@ private static string BuildDataSourcesJson(System.Collections.Generic.List<Snaps
{
var sb = new StringBuilder();
sb.Append("[");

for (int i = 0; i < snapshots.Count; i++)
{
var snapshot = snapshots[i];
var analysisResult = snapshot.GetAnalysisResult();

// 生成该快照的Mermaid图表
var architectureOverviewMermaid =
MermaidVisualizers.ArchitectureOverviewMermaidVisualizer.GenerateMermaid(analysisResult);
var allProcessingFlowMermaid =
MermaidVisualizers.ProcessingFlowMermaidVisualizer.GenerateMermaid(analysisResult);
var allAggregateMermaid =
MermaidVisualizers.AggregateRelationMermaidVisualizer.GenerateAllAggregateMermaid(analysisResult);

sb.Append("{");

// 元数据
sb.Append("\"metadata\":{");
sb.Append($"\"version\":\"{EscapeJavaScript(snapshot.Metadata.Version)}\",");
sb.Append($"\"timestamp\":\"{snapshot.Metadata.Timestamp:yyyy-MM-dd HH:mm:ss}\",");
sb.Append($"\"description\":\"{EscapeJavaScript(snapshot.Metadata.Description)}\",");
sb.Append($"\"hash\":\"{EscapeJavaScript(snapshot.Metadata.Hash)}\",");
sb.Append($"\"nodeCount\":{snapshot.Metadata.NodeCount},");
sb.Append($"\"relationshipCount\":{snapshot.Metadata.RelationshipCount}");
sb.Append("},");

// 分析结果
sb.Append("\"analysisResult\":");
sb.Append(BuildAnalysisResultJson(analysisResult));
sb.Append(",");

// 统计信息
sb.Append("\"statistics\":");
sb.Append(BuildStatisticsJson(analysisResult));
sb.Append(",");

// 图表数据
sb.Append("\"diagrams\":");
sb.Append(BuildArchitectureOverviewMermaidJson(architectureOverviewMermaid));
sb.Append(",");

// 处理流程图
sb.Append("\"allChainFlowCharts\":");
sb.Append(BuildProcessingFlowMermaidJson(allProcessingFlowMermaid));
sb.Append(",");

// 聚合关系图
sb.Append("\"allAggregateRelationDiagrams\":");
sb.Append(BuildAllAggregateRelationDiagramsJson(allAggregateMermaid));

sb.Append("}");

sb.Append(BuildSingleDataSourceEntryJson(
analysisResult,
snapshot.Metadata.Version,
snapshot.Metadata.Timestamp.ToString("yyyy-MM-dd HH:mm:ss"),
snapshot.Metadata.Description,
snapshot.Metadata.Hash,
snapshot.Metadata.NodeCount,
snapshot.Metadata.RelationshipCount));
if (i < snapshots.Count - 1)
{
sb.Append(",");
}
}

sb.Append("]");
return sb.ToString();
}

// 当没有快照时,直接从 analysisResult 构建包含单个 Runtime 数据源的 dataSources JSON 字符串
private static string BuildDataSourcesJsonFromAnalysisResult(CodeFlowAnalysisResult analysisResult)
{
var now = DateTime.Now;
return "[" + BuildSingleDataSourceEntryJson(
analysisResult,
now.ToString("yyyyMMddHHmmss"),
now.ToString("yyyy-MM-dd HH:mm:ss"),
"Runtime",
string.Empty,
analysisResult.Nodes.Count,
analysisResult.Relationships.Count) + "]";
}

// 构建单个数据源条目的 JSON 字符串(snapshots 路径和 runtime 回退路径共享此逻辑)
private static string BuildSingleDataSourceEntryJson(
CodeFlowAnalysisResult analysisResult,
string version,
string timestamp,
string description,
string hash,
int nodeCount,
int relationshipCount)
{
var architectureOverviewMermaid =
MermaidVisualizers.ArchitectureOverviewMermaidVisualizer.GenerateMermaid(analysisResult);
var allProcessingFlowMermaid =
MermaidVisualizers.ProcessingFlowMermaidVisualizer.GenerateMermaid(analysisResult);
var allAggregateMermaid =
MermaidVisualizers.AggregateRelationMermaidVisualizer.GenerateAllAggregateMermaid(analysisResult);
Comment on lines +105 to +134
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BuildDataSourcesJsonFromAnalysisResult duplicates most of the JSON assembly logic from BuildDataSourcesJson (mermaid generation + dataSource object shape). This increases drift risk if the dataSource schema changes in the future (one path may be updated and the other forgotten). Consider extracting a shared helper that builds a single dataSource entry from (analysisResult, metadata) and have both code paths call it (looping for snapshots vs. one-off for runtime).

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 7514183. Extracted a shared BuildSingleDataSourceEntryJson(analysisResult, version, timestamp, description, hash, nodeCount, relationshipCount) helper. Both BuildDataSourcesJson (snapshot loop) and BuildDataSourcesJsonFromAnalysisResult (runtime fallback) now delegate to it — all JSON assembly and EscapeJavaScript calls live in one place.


var sb = new StringBuilder();
sb.Append("{");

// 元数据
sb.Append("\"metadata\":{");
sb.Append($"\"version\":\"{EscapeJavaScript(version)}\",");
sb.Append($"\"timestamp\":\"{EscapeJavaScript(timestamp)}\",");
sb.Append($"\"description\":\"{EscapeJavaScript(description)}\",");
sb.Append($"\"hash\":\"{EscapeJavaScript(hash)}\",");
sb.Append($"\"nodeCount\":{nodeCount},");
sb.Append($"\"relationshipCount\":{relationshipCount}");
sb.Append("},");

// 分析结果
sb.Append("\"analysisResult\":");
sb.Append(BuildAnalysisResultJson(analysisResult));
sb.Append(",");

// 统计信息
sb.Append("\"statistics\":");
sb.Append(BuildStatisticsJson(analysisResult));
sb.Append(",");

// 图表数据
sb.Append("\"diagrams\":");
sb.Append(BuildArchitectureOverviewMermaidJson(architectureOverviewMermaid));
sb.Append(",");

// 处理流程图
sb.Append("\"allChainFlowCharts\":");
sb.Append(BuildProcessingFlowMermaidJson(allProcessingFlowMermaid));
sb.Append(",");

// 聚合关系图
sb.Append("\"allAggregateRelationDiagrams\":");
sb.Append(BuildAllAggregateRelationDiagramsJson(allAggregateMermaid));

sb.Append("}");
return sb.ToString();
}

// 构建 analysisResult 的 JSON 字符串
private static string BuildAnalysisResultJson(CodeFlowAnalysisResult analysisResult)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@ namespace NetCorePal.Extensions.CodeAnalysis.UnitTests;

public class VisualizationHtmlBuilderTests
{
[Fact]
public void GenerateVisualizationHtml_WithNoSnapshots_ShouldContainNonEmptyDataSources()
{
var result = CodeFlowAnalysisHelper.GetResultFromAssemblies(typeof(VisualizationHtmlBuilderTests).Assembly);

// Do not pass snapshots - this is the bug scenario
var html = VisualizationHtmlBuilder.GenerateVisualizationHtml(result);

// dataSources must not be empty so the page script can access dataSources[0].statistics
Assert.Contains("const dataSources =", html);
Assert.DoesNotContain("const dataSources = []", html);
Assert.Contains("\"statistics\":", html);
Assert.Contains("\"metadata\":", html);
Assert.Contains("\"analysisResult\":", html);
// Runtime fallback entry should have "Runtime" as description
Assert.Contains("\"description\":\"Runtime\"", html);
}

[Fact]
public void GenerateVisualizationHtml_ShouldContainBasicHtmlAndDiagrams()
{
Expand Down
Loading