Skip to content

fix: handle zero-base insight deltas - #521

Merged
gkhngyk merged 2 commits into
ansvisor:mainfrom
abdullah91111:fix/zero-base-change-deltas
Jul 23, 2026
Merged

gkhngyk merged 2 commits into
ansvisor:mainfrom
abdullah91111:fix/zero-base-change-deltas

Conversation

@abdullah91111

Copy link
Copy Markdown
Contributor

Summary

Fixes ambiguous mentions and citations change deltas when the previous-period count is zero.

  • Keeps mentionsChange and citationsChange strictly percentage-based
  • Returns null when no meaningful percentage can be calculated from a zero base
  • Adds prevMentions and prevCitations to distinguish zero-base growth from unavailable comparison data
  • Displays zero-base growth as +N new instead of an incorrect +N%
  • Updates the Insights dashboard, report viewer, and report PDF together
  • Preserves the existing rendering of reports generated before this change
  • Clarifies delta semantics for the AI report-summary prompt
  • Adds regression tests for zero-base growth and drops to zero

Related issue

Closes #520

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

Testing

  • Web: 45 tests passed across 10 test files
  • Server: 123 tests passed across 10 test files
  • All changed files pass Prettier
  • git diff --check passes

The repository-wide format check reports existing CRLF formatting differences in untouched files in the Windows checkout. All files modified by this PR pass Prettier individually.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This matches #520's fix direction almost exactly — the single-meaning percentageChange helper, the raw prev counts, all four consumers updated together, and the Object.hasOwn fallback that keeps pre-existing snapshots rendering unchanged is exactly right. The prompt paragraph is accurate too (and effectively covers #514's ask — worth cross-linking there so effort isn't duplicated).

One real blocker, invisible to CI:

export function percentageChange is a sync export from a 'use server' module. tracking.ts opens with 'use server', and Next.js only allows async function exports from such modules — next build fails with "Only async functions are allowed to be exported in a 'use server' file." Nothing catches it today: CI runs prettier/eslint/tsc/vitest but no next build, the Vercel build is canceled on PR branches by the ignored-build-step, and vitest doesn't enforce the directive. Merged as-is, this breaks the production build on main.

Fix: move percentageChange into a plain module (e.g. web/src/lib/metrics.ts), import it from tracking.ts, and point the test at the new module.

Tiny nit while you're in there: CountDeltaBadge uses text-green-600 where the report viewer uses emerald-600 — matching them keeps the delta color consistent across surfaces.

Everything else is good to go — happy to approve as soon as the helper moves.

@abdullah91111

Copy link
Copy Markdown
Contributor Author

alright will fix it

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verified the fix end-to-end: percentageChange now lives in web/src/lib/metrics.ts (plain module), tracking.ts is back to async-only exports, the test moved with it, and the emerald nit is fixed too. I also ran a full next build against this branch locally — it compiles and completes cleanly, so the production-build blocker is definitively gone. Thanks for the quick, thorough turnaround!

@gkhngyk
gkhngyk merged commit e88ca92 into ansvisor:main Jul 23, 2026
3 checks passed
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.

Insights & Reports: *Change deltas silently switch to absolute counts on a zero base but always render with a % suffix

2 participants