Skip to content

fix: GenerateVisualizationHtml produces non-empty dataSources when called without snapshots#351

Merged
witskeeper merged 5 commits intomainfrom
copilot/fix-generate-visualization-html-error
Mar 25, 2026
Merged

fix: GenerateVisualizationHtml produces non-empty dataSources when called without snapshots#351
witskeeper merged 5 commits intomainfrom
copilot/fix-generate-visualization-html-error

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 25, 2026

VisualizationHtmlBuilder.GenerateVisualizationHtml emitted dataSources = [] whenever no snapshots argument was passed, causing the page script to immediately throw TypeError: Cannot read properties of undefined (reading 'statistics') at dataSources[0].

Changes

  • VisualizationHtmlBuilder.cs

    • When snapshotList is empty, route through a new BuildDataSourcesJsonFromAnalysisResult fallback instead of BuildDataSourcesJson, producing a single-element dataSources array with description "Runtime" and metadata derived from the passed-in analysisResult.
    • The fallback emits the same JSON shape as snapshot-based entries (metadata, analysisResult, statistics, diagrams, allChainFlowCharts, allAggregateRelationDiagrams), so the page script works identically regardless of which path produced the data.
  • VisualizationHtmlBuilderTests.cs

    • Added GenerateVisualizationHtml_WithNoSnapshots_ShouldContainNonEmptyDataSources covering the previously broken call signature.

Minimal working call (now works without workaround)

var analysisResult = CodeFlowAnalysisHelper.GetResultFromAssemblies(assemblies);
var html = VisualizationHtmlBuilder.GenerateVisualizationHtml(analysisResult);
// dataSources now contains one "Runtime" entry — page no longer crashes
return Results.Content(html, "text/html; charset=utf-8");
Original prompt

This section details on the original issue you should resolve

<issue_title>[Bug] GenerateVisualizationHtml 在未传入 snapshots 时生成空 dataSources,导致可视化页面脚本报错</issue_title>
<issue_description>Image

环境

  • 包:NetCorePal.Extensions.CodeAnalysis 3.2.1-preview.1.2603240625
  • 目标框架:net10.0

目前解决方案

  // Code analysis endpoint
app.MapGet("/code-analysis", () =>
{
   Assembly[] assemblies =
   [
       typeof(Program).Assembly,
       typeof(ApplicationDbContext).Assembly,
       typeof(Ncp.Admin.Domain.AggregatesModel.UserAggregate.User).Assembly
   ];
   var analysisResult = CodeFlowAnalysisHelper.GetResultFromAssemblies(assemblies);
   // NetCorePal 3.2.1-preview:GenerateVisualizationHtml 仅从 snapshots 构建 dataSources;
   // 未传入快照时 dataSources 为 [],页面脚本访问 dataSources[0].statistics 会抛错。
   var snapshots = CodeFlowAnalysisSnapshotHelper.ScanSnapshotsFromAssemblies(assemblies);
   snapshots.Add(CodeFlowAnalysisSnapshotHelper.CreateSnapshot(
       CodeFlowAnalysisHelper.GetAllMetadataAttributes(assemblies).ToArray(),
       "Runtime"));
   var html = VisualizationHtmlBuilder.GenerateVisualizationHtml(analysisResult, snapshots: snapshots);
   return Results.Content(html, "text/html; charset=utf-8");
});

</issue_description>

<agent_instructions>修复该bug</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.

Copilot AI changed the title [WIP] Fix GenerateVisualizationHtml to handle empty snapshots fix: GenerateVisualizationHtml produces non-empty dataSources when called without snapshots Mar 25, 2026
Copilot AI requested a review from witskeeper March 25, 2026 06:27
@witskeeper witskeeper requested review from Copilot and zhouda1fu March 25, 2026 06:44
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a runtime crash in the embedded visualization page by ensuring VisualizationHtmlBuilder.GenerateVisualizationHtml always emits a non-empty dataSources array even when no snapshots are provided.

Changes:

  • Add a runtime fallback path that builds a single dataSources entry directly from analysisResult when the snapshot list is empty.
  • Add a unit test that covers calling GenerateVisualizationHtml(result) without passing snapshots.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/NetCorePal.Extensions.CodeAnalysis/VisualizationHtmlBuilder.cs Adds a runtime fallback dataSources builder so the page script can safely read dataSources[0].
test/NetCorePal.Extensions.CodeAnalysis.UnitTests/VisualizationHtmlBuilderTests.cs Adds a regression test ensuring dataSources is non-empty and contains expected fields without snapshots.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +148 to +156
// 当没有快照时,直接从 analysisResult 构建包含单个 Runtime 数据源的 dataSources JSON 字符串
private static string BuildDataSourcesJsonFromAnalysisResult(CodeFlowAnalysisResult analysisResult)
{
var architectureOverviewMermaid =
MermaidVisualizers.ArchitectureOverviewMermaidVisualizer.GenerateMermaid(analysisResult);
var allProcessingFlowMermaid =
MermaidVisualizers.ProcessingFlowMermaidVisualizer.GenerateMermaid(analysisResult);
var allAggregateMermaid =
MermaidVisualizers.AggregateRelationMermaidVisualizer.GenerateAllAggregateMermaid(analysisResult);
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.

Comment thread src/NetCorePal.Extensions.CodeAnalysis/VisualizationHtmlBuilder.cs Outdated
witskeeper and others added 3 commits March 25, 2026 15:35
…r.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…tion between snapshot and runtime paths

Co-authored-by: witskeeper <5594094+witskeeper@users.noreply.github.com>
Agent-Logs-Url: https://github.com/netcorepal/netcorepal-cloud-framework/sessions/8ed1e14d-3148-4d96-8663-ce2c347cca1f
…on-html-error' into copilot/fix-generate-visualization-html-error

# Conflicts:
#	src/NetCorePal.Extensions.CodeAnalysis/VisualizationHtmlBuilder.cs
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@witskeeper witskeeper marked this pull request as ready for review March 25, 2026 08:09
@witskeeper witskeeper merged commit 79e7768 into main Mar 25, 2026
41 of 64 checks passed
@witskeeper witskeeper deleted the copilot/fix-generate-visualization-html-error branch March 25, 2026 08:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] GenerateVisualizationHtml 在未传入 snapshots 时生成空 dataSources,导致可视化页面脚本报错

3 participants