Skip to content

Admin social progress timing and handle indexes#148

Merged
therealityreport merged 3 commits into
mainfrom
codex/admin-load-time-backend-20260618
Jun 18, 2026
Merged

Admin social progress timing and handle indexes#148
therealityreport merged 3 commits into
mainfrom
codex/admin-load-time-backend-20260618

Conversation

@therealityreport

@therealityreport therealityreport commented Jun 18, 2026

Copy link
Copy Markdown
Owner

Summary

  • Add backend/database timing to admin social landing progress rollup responses.
  • Preserve timing fields on cached rollup responses.
  • Add normalized-handle indexes for social landing progress table lookups.
  • Extend landing progress rollup tests for timing payloads.

Validation

  • /Users/thomashulihan/Projects/TRR/TRR-Backend/.venv/bin/pytest -q tests/api/test_admin_socials_landing_summary.py
  • git diff --check

Summary by CodeRabbit

  • Performance

    • Added additional database indexes to speed up admin social landing progress rollup lookups using normalized handle expressions and improved Instagram handle indexing.
    • Enhanced the admin rollup response with execution timing details (backend, database, total) across empty-result, cache-hit, and cache-miss paths.
  • Tests

    • Updated social landing progress rollup tests to validate timing fields and expected relationships for cache and no-DB scenarios.

Copilot AI review requested due to automatic review settings June 18, 2026 22:13
@coderabbitai

coderabbitai Bot commented Jun 18, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 07153b49-dc70-4a86-a5a5-87b867dc4cac

📥 Commits

Reviewing files that changed from the base of the PR and between a1d29cf and cf96286.

📒 Files selected for processing (3)
  • api/routers/socials/__init__.py
  • supabase/migrations/20260618170000_social_landing_progress_normalized_handle_indexes.sql
  • tests/api/test_admin_socials_landing_summary.py
🚧 Files skipped from review as they are similar to previous changes (2)
  • supabase/migrations/20260618170000_social_landing_progress_normalized_handle_indexes.sql
  • api/routers/socials/init.py

📝 Walkthrough

Walkthrough

Adds perf_counter()-based timing to the /admin/socials/landing-progress-rollup endpoint, exposing backend_ms, database_ms, and total_ms in all response paths and updating the log message. Also introduces a SQL migration creating expression indexes for normalized handle lookups across multiple social tables. Includes minor code cleanup in the router.

Changes

Landing Rollup Timing Instrumentation

Layer / File(s) Summary
Timing capture and response payload
api/routers/socials/__init__.py, tests/api/test_admin_socials_landing_summary.py
Captures started_at at entry; on no-targets early return sets database_ms=0 with computed backend/total ms; on cache-hit computes total_ms and attaches a timing object to the cached payload; on cache-miss captures db_started_at before the DB call, computes db_ms after, and embeds a timing object in the payload. Updates log statement to report total_ms and db_ms. Simplifies _sql_json_text_non_negative_int return expression and reformats two Instagram HTTP 400 error payloads. Tests assert database_ms==0 on empty-target path, timing field relationships on cache-miss/cache-hit scenarios, and that cached responses reuse the original timing object.

Normalized Handle Expression Indexes

Layer / File(s) Summary
SQL migration: normalized handle indexes
supabase/migrations/20260618170000_social_landing_progress_normalized_handle_indexes.sql
Creates multiple IF NOT EXISTS btree expression indexes in a single transaction: ltrim(lower(coalesce(...)), '@') expressions on source_account across social.facebook_posts, social.instagram_posts, social.tiktok_posts, and related platform tables; a compound descending index on social.instagram_profiles combining normalized handle with last_scraped_at/updated_at NULLS LAST and id; and a normalized platform/handle index on pipeline.socialblade_growth_data.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐇 Tick-tock goes the clock in the rollup's reply,
A timing object now tells how the milliseconds fly.
database_ms zeros when no targets appear,
And indexes bloom for the handles so dear.
This rabbit hops fast — but now we know by how much! 🕐

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Admin social progress timing and handle indexes' accurately summarizes the main changes: adding timing metrics to admin social progress responses and creating normalized-handle database indexes for performance optimization.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/admin-load-time-backend-20260618

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions

Copy link
Copy Markdown
Contributor

Codex Exhaustive Code Review

Findings

  1. High - transactional index build can block production writes
    supabase/migrations/20260618170000_social_landing_progress_normalized_handle_indexes.sql wraps 14 CREATE INDEX statements in one transaction and none use CONCURRENTLY. On large, actively written social tables, regular CREATE INDEX blocks inserts/updates/deletes, and the locks are held until the final COMMIT. This can stall ingestion and admin workflows during deploy.
    Smallest fix: apply these as CREATE INDEX CONCURRENTLY IF NOT EXISTS outside an explicit transaction with lock/statement timeouts, or pre-apply them concurrently in production and make this migration a canonical no-op/check-style replay with a rollout note like the FK hardening migrations.

  2. Medium - cache hits report stale miss timing as if it happened on the hit request
    api/routers/socials/init.py returns the cached payload after only changing cache_status, but the cached payload now includes the original miss timing saved at api/routers/socials/init.py. A cache hit can therefore report nonzero database_ms and an old backend_ms, even though no DB call happened. The added test currently locks in that incorrect behavior at tests/api/test_admin_socials_landing_summary.py.
    Smallest fix: on cache hit, compute current request timing and override timing with database_ms: 0, current backend_ms, and current total_ms, or move cached generation timing under a separate field.

Validation

I reviewed only the requested diff range. git diff --check passed, and api/routers/socials/__init__.py parses successfully with Python AST. I could not run the touched pytest file because pytest is not installed in this environment.

Review Metadata

backend_surface: /admin/socials/landing-progress-rollup and social landing DB hot-path indexes.
contracts_changed: additive timing object in the rollup response.
migrations_added: yes, one Supabase migration.
downstream_updates: none observed in this PR.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Adds timing telemetry to the admin social landing progress rollup response (including cached responses) and introduces new normalized-handle expression indexes intended to speed up the rollup’s platform/table lookups, with tests extended to validate the timing payload.

Changes:

  • Add timing fields (database_ms, backend_ms, total_ms) to /socials/landing-progress-rollup responses and ensure cached payloads preserve timing.
  • Add expression indexes to match the rollup query’s normalized-handle predicates across multiple social.* tables and pipeline.socialblade_growth_data.
  • Extend landing progress rollup tests to assert timing behavior on bypass/miss/hit flows.

Reviewed changes

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

File Description
api/routers/socials/__init__.py Adds timing measurements and includes timing payload in rollup responses + logging.
supabase/migrations/20260618170000_social_landing_progress_normalized_handle_indexes.sql Adds normalized-handle expression indexes to support rollup query filters/joins.
tests/api/test_admin_socials_landing_summary.py Expands tests to validate timing fields and cache-hit timing preservation.

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

Comment on lines +785 to 789
db_ms = int((perf_counter() - db_started_at) * 1000)
total_ms = int((perf_counter() - started_at) * 1000)
generated_at = datetime.now(tz=UTC).isoformat()
payload_out = {
"rows": jsonable_encoder(rows),
Comment on lines +1 to +2
BEGIN;

Comment on lines +6 to +10
CREATE INDEX IF NOT EXISTS idx_social_instagram_posts_landing_account_norm
ON social.instagram_posts ((ltrim(lower(coalesce(source_account, '')), '@')));

CREATE INDEX IF NOT EXISTS idx_social_tiktok_posts_landing_account_norm
ON social.tiktok_posts ((ltrim(lower(coalesce(source_account, '')), '@')));
Comment on lines +12 to +16
CREATE INDEX IF NOT EXISTS idx_social_twitter_tweets_landing_account_norm
ON social.twitter_tweets ((ltrim(lower(coalesce(source_account, '')), '@')));

CREATE INDEX IF NOT EXISTS idx_social_youtube_videos_landing_account_norm
ON social.youtube_videos ((ltrim(lower(coalesce(source_account, '')), '@')));
Comment on lines +18 to +22
CREATE INDEX IF NOT EXISTS idx_social_facebook_posts_landing_account_norm
ON social.facebook_posts ((ltrim(lower(coalesce(source_account, '')), '@')));

CREATE INDEX IF NOT EXISTS idx_social_threads_posts_landing_account_norm
ON social.meta_threads_posts ((ltrim(lower(coalesce(source_account, '')), '@')));
Comment on lines +24 to +28
CREATE INDEX IF NOT EXISTS idx_social_ig_catalog_posts_landing_account_norm
ON social.instagram_account_catalog_posts ((ltrim(lower(coalesce(source_account, '')), '@')));

CREATE INDEX IF NOT EXISTS idx_social_tiktok_catalog_posts_landing_account_norm
ON social.tiktok_account_catalog_posts ((ltrim(lower(coalesce(source_account, '')), '@')));
Comment on lines +30 to +34
CREATE INDEX IF NOT EXISTS idx_social_twitter_catalog_posts_landing_account_norm
ON social.twitter_account_catalog_posts ((ltrim(lower(coalesce(source_account, '')), '@')));

CREATE INDEX IF NOT EXISTS idx_social_youtube_catalog_posts_landing_account_norm
ON social.youtube_account_catalog_posts ((ltrim(lower(coalesce(source_account, '')), '@')));
Comment on lines +36 to +40
CREATE INDEX IF NOT EXISTS idx_social_facebook_catalog_posts_landing_account_norm
ON social.facebook_account_catalog_posts ((ltrim(lower(coalesce(source_account, '')), '@')));

CREATE INDEX IF NOT EXISTS idx_social_threads_catalog_posts_landing_account_norm
ON social.threads_account_catalog_posts ((ltrim(lower(coalesce(source_account, '')), '@')));
@therealityreport therealityreport merged commit 0d1a1b0 into main Jun 18, 2026
6 of 9 checks passed
@therealityreport therealityreport deleted the codex/admin-load-time-backend-20260618 branch June 18, 2026 23:45
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.

2 participants