Skip to content

perf(citations): serve the Gaps tab from daily rollups #1002

Closed

Description

@gkhngyk

Problem

The Citations page's Domains and URLs tabs read daily rollups since 00118, but the Gaps tab still runs two raw scans per load:

  • citation_gap_domains
  • citation_competitor_sources

Both scan every answer in the window, every citation of those answers, and unpack competitor_mentions. On the largest brand they take about 2s. Before 00118 the domain table showed the same cost growing with citations: it reached 11.8s.

The existing rollups can't answer these reads, because the measures are per answer:

  • Present answers: answers where the brand is present, meaning it is mentioned, or one of its domains (p_brand_domains) is cited in the same answer.
  • Competitor-present answers: answers where a competitor is mentioned.
  • Domain weight: each answer weights its cited domains 1 / number of distinct domains it cites.
  • Competitor names per domain: the competitors mentioned in the answers behind that domain.

Proposal

Add gap rollups at the same grain as citation_domain_daily (day, model_used, platform, region, topic_id), written by refresh_citations_daily:

  1. Per domain:
    • gap answers: competitor present, brand not present;
    • gap strength: sum of the weight over those answers;
    • present answers, for appears_in_ours.
  2. Per (domain, competitor):
    • answers feeding the domain, and their strength, for citation_competitor_sources;
    • the same counts restricted to gap answers, for competitor_names.
  3. Per day: present answers next to answers in citation_result_daily, for our_answer_count.

Add *_daily read variants that take the same filters as citations_domains_daily. Keep these steps at read time:

  • domain tagging against p_brand_domains and p_competitor_domains, so excluded domains stay a filter;
  • competitor names, taken from the live competitors table with the current name fallback.

Use the security definer + explicit org check pattern from 00119. citation_urls has no select policy for authenticated.

Brand domains are baked in

Whether the brand counts as present depends on the brand's own domains when the rollup is written. A change to brand_domains must therefore queue a rebuild through citation_rollup_rebuilds, the same way a topic move does. #1001 makes that queue run shortly after a change.

Prompt-filtered reads stay on the raw functions, as they do for the other Citations tabs.

Acceptance criteria

  • The Gaps tab reads the daily functions for every preset except a prompt filter.
  • Results match the raw functions for whole-day windows on at least three brands, including the largest.
  • Adding or removing a brand domain queues a rebuild.
  • Verified under the authenticated role as an org member (rows) and as a member of another org (no rows).
  • Backfill script, as for 00118.

Activity

  1. added
    enhancement
    citations
    needs-migration
    on Oct 11, 2026
  2. knockknock10 commented on Oct 18, 2026

    @knockknock10
    Contributor

    hey @gkhngyk I would love to work on again !!
    If available could you please assign these to me?

  3. gkhngyk commented on Oct 18, 2026

    @gkhngyk
    ContributorAuthor

    Thanks @knockknock10. This one stays with us for now: it rewrites refresh_citations_daily, which #1131 is changing at the same time. How is #918 going? A first PR for one tool group would be welcome.

  4. knockknock10 commented on Oct 18, 2026

    @knockknock10
    Contributor

    3. Thanks @knockknock10. This one stays with us for now: it rewrites refresh_citations_daily, which #1131 is changing at the same time. How is #918 going? A first PR for one tool group would be welcome.

    Hey @gkhngyk Sorry, I completely forgot to follow up on #918 because of my exams. I've now completed the work and will get the first PR for one tool group submitted. Thanks for the clarification and patience!

  5. added a commit that references this issue on Oct 20, 2026
    34f7efa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    citationsenhancementneeds-migration

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions