Skip to content

fix(insights): divide Positive Sentiment by brand-mentioning results,… - #512

Merged
gkhngyk merged 1 commit into
ansvisor:mainfrom
d180:fix/insights-positive-sentiment-denominator
Jul 24, 2026
Merged

gkhngyk merged 1 commit into
ansvisor:mainfrom
d180:fix/insights-positive-sentiment-denominator

Conversation

@d180

@d180 d180 commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Summary

The "Positive Sentiment" KPI on the Insights dashboard divided positive_count
by total_results — but sentiment analysis is skipped entirely for answers
that never mention the brand, so those rows sit in the denominator and can
never be positive. On brands with low mention volume this collapses the
score toward zero (e.g. a real 43% positive rate among brand-mentioning
answers displayed as ~1% once 13,002 brand-absent rows were mixed in).

This adds mentioning_results (mention_count > 0 OR citation_count > 0) to
the insights_aggregates() RPC and divides by that instead, with an explicit
zero-guard for brands that are tracked but never mentioned in the selected
window. Same fix covers the Insights dashboard, the Topics detail page, and
Reports (PDF + web), since they all read from getInsightsSummary().

Related issue

Closes #508

Type of change

  • fix — Bug fix

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/)
  • 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

@gkhngyk

gkhngyk commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Solid fix — the RPC body is faithful to the current definition on main (only the new counter + JSON key added), the cur/prev sentiment windows use the same denominator, and going through insights_aggregates instead of visible_prompt_stats was the right call: it carries the p_prompt_id / p_topic_id filters the issue's suggested source lacks.

One mechanical blocker before this can land: #511 merged this morning and took migration numbers 00031/00032, so this branch now conflicts on supabase/schema.sql and the migration number is taken. To resolve:

  1. Rename the migration to 00033_insights_sentiment_denominator.sql.
  2. Rebase onto current main and regenerate the consolidated file with bash supabase/build-schema.sh — no need to hand-resolve the schema.sql conflict, the script rebuilds it from the migration files.
  3. Push; the schema CI check will confirm.

Will approve as soon as the rebase is up. (Note for after merge: the migration gets applied to the hosted project by a maintainer — nothing you need to do there.)

@d180
d180 force-pushed the fix/insights-positive-sentiment-denominator branch from 644746d to 71c354f Compare July 23, 2026 22:08
@d180

d180 commented Jul 23, 2026

Copy link
Copy Markdown
Contributor Author

@gkhngyk done the rebase and the needed changes.

@gkhngyk
gkhngyk merged commit 5d9dfa9 into ansvisor:main Jul 24, 2026
3 checks passed
@gkhngyk gkhngyk mentioned this pull request Jul 24, 2026
7 tasks done
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: Positive Sentiment KPI is diluted by answers that never mention the brand

2 participants