Skip to content

Commit 72c51c9

Browse files
feat(admin): time social progress rollups
1 parent 7d692d0 commit 72c51c9

3 files changed

Lines changed: 73 additions & 3 deletions

File tree

api/routers/socials/__init__.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -481,12 +481,15 @@ def post_social_landing_progress_rollup(
481481
) -> dict[str, Any]:
482482
from trr_backend.db import pg
483483

484+
started_at = perf_counter()
484485
targets = _normalize_landing_progress_targets(payload.platforms, payload.account_handles)
485486
if not targets:
487+
total_ms = int((perf_counter() - started_at) * 1000)
486488
return {
487489
"rows": [],
488490
"cache_status": "bypass",
489491
"generated_at": datetime.now(tz=UTC).isoformat(),
492+
"timing": {"backend_ms": total_ms, "database_ms": 0, "total_ms": total_ms},
490493
}
491494

492495
cache_key = _landing_progress_cache_key(targets)
@@ -499,9 +502,9 @@ def post_social_landing_progress_rollup(
499502
cached_payload["cache_status"] = "hit"
500503
return cached_payload
501504

502-
started_at = perf_counter()
503505
instagram_reported_comments_sql = _instagram_reported_comments_sql("p")
504506
try:
507+
db_started_at = perf_counter()
505508
rows = pg.fetch_all(
506509
f"""
507510
WITH targets AS (
@@ -779,11 +782,14 @@ def post_social_landing_progress_rollup(
779782
[[platform for platform, _handle in targets], [handle for _platform, handle in targets]],
780783
pool_name="social_profile",
781784
)
785+
db_ms = int((perf_counter() - db_started_at) * 1000)
786+
total_ms = int((perf_counter() - started_at) * 1000)
782787
generated_at = datetime.now(tz=UTC).isoformat()
783788
payload_out = {
784789
"rows": jsonable_encoder(rows),
785790
"cache_status": "miss",
786791
"generated_at": generated_at,
792+
"timing": {"backend_ms": total_ms, "database_ms": db_ms, "total_ms": total_ms},
787793
}
788794
_set_ttl_cached_payload(
789795
_SOCIAL_LANDING_PROGRESS_ROLLUP_CACHE,
@@ -794,10 +800,11 @@ def post_social_landing_progress_rollup(
794800
max_entries=SOCIAL_LANDING_PROGRESS_ROLLUP_CACHE_MAX_ENTRIES,
795801
)
796802
logger.info(
797-
"Built social landing progress rollup targets=%s rows=%s elapsed_ms=%s cache_status=miss",
803+
"Built social landing progress rollup targets=%s rows=%s elapsed_ms=%s db_ms=%s cache_status=miss",
798804
len(targets),
799805
len(rows),
800-
int((perf_counter() - started_at) * 1000),
806+
total_ms,
807+
db_ms,
801808
)
802809
return payload_out
803810
except Exception as exc: # noqa: BLE001
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
BEGIN;
2+
3+
-- Match the normalized-handle predicates used by the admin social landing
4+
-- progress rollup so Postgres can narrow each platform table before aggregate
5+
-- work begins.
6+
CREATE INDEX IF NOT EXISTS idx_social_instagram_posts_landing_account_norm
7+
ON social.instagram_posts ((ltrim(lower(coalesce(source_account, '')), '@')));
8+
9+
CREATE INDEX IF NOT EXISTS idx_social_tiktok_posts_landing_account_norm
10+
ON social.tiktok_posts ((ltrim(lower(coalesce(source_account, '')), '@')));
11+
12+
CREATE INDEX IF NOT EXISTS idx_social_twitter_tweets_landing_account_norm
13+
ON social.twitter_tweets ((ltrim(lower(coalesce(source_account, '')), '@')));
14+
15+
CREATE INDEX IF NOT EXISTS idx_social_youtube_videos_landing_account_norm
16+
ON social.youtube_videos ((ltrim(lower(coalesce(source_account, '')), '@')));
17+
18+
CREATE INDEX IF NOT EXISTS idx_social_facebook_posts_landing_account_norm
19+
ON social.facebook_posts ((ltrim(lower(coalesce(source_account, '')), '@')));
20+
21+
CREATE INDEX IF NOT EXISTS idx_social_threads_posts_landing_account_norm
22+
ON social.meta_threads_posts ((ltrim(lower(coalesce(source_account, '')), '@')));
23+
24+
CREATE INDEX IF NOT EXISTS idx_social_ig_catalog_posts_landing_account_norm
25+
ON social.instagram_account_catalog_posts ((ltrim(lower(coalesce(source_account, '')), '@')));
26+
27+
CREATE INDEX IF NOT EXISTS idx_social_tiktok_catalog_posts_landing_account_norm
28+
ON social.tiktok_account_catalog_posts ((ltrim(lower(coalesce(source_account, '')), '@')));
29+
30+
CREATE INDEX IF NOT EXISTS idx_social_twitter_catalog_posts_landing_account_norm
31+
ON social.twitter_account_catalog_posts ((ltrim(lower(coalesce(source_account, '')), '@')));
32+
33+
CREATE INDEX IF NOT EXISTS idx_social_youtube_catalog_posts_landing_account_norm
34+
ON social.youtube_account_catalog_posts ((ltrim(lower(coalesce(source_account, '')), '@')));
35+
36+
CREATE INDEX IF NOT EXISTS idx_social_facebook_catalog_posts_landing_account_norm
37+
ON social.facebook_account_catalog_posts ((ltrim(lower(coalesce(source_account, '')), '@')));
38+
39+
CREATE INDEX IF NOT EXISTS idx_social_threads_catalog_posts_landing_account_norm
40+
ON social.threads_account_catalog_posts ((ltrim(lower(coalesce(source_account, '')), '@')));
41+
42+
CREATE INDEX IF NOT EXISTS idx_social_instagram_profiles_landing_account_norm
43+
ON social.instagram_profiles (
44+
(ltrim(lower(coalesce(normalized_username, username, source_account, '')), '@')),
45+
last_scraped_at DESC NULLS LAST,
46+
updated_at DESC NULLS LAST,
47+
id
48+
);
49+
50+
CREATE INDEX IF NOT EXISTS idx_pipeline_socialblade_landing_platform_handle_norm
51+
ON pipeline.socialblade_growth_data (
52+
(lower(coalesce(nullif(platform, ''), 'instagram'))),
53+
(ltrim(lower(coalesce(nullif(account_handle, ''), instagram_handle, '')), '@'))
54+
);
55+
56+
COMMIT;

tests/api/test_admin_socials_landing_summary.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ def fail_fetch_all(*_args, **_kwargs):
161161
assert payload["rows"] == []
162162
assert payload["cache_status"] == "bypass"
163163
assert "generated_at" in payload
164+
assert payload["timing"]["database_ms"] == 0
165+
assert payload["timing"]["backend_ms"] >= 0
166+
assert payload["timing"]["total_ms"] >= 0
164167

165168

166169
def test_social_landing_progress_rollup_rejects_mismatched_targets() -> None:
@@ -223,6 +226,10 @@ def fake_fetch_all(query, params, *, pool_name):
223226
assert "comment_counts AS" not in str(captured["query"])
224227
assert first["cache_status"] == "miss"
225228
assert second["cache_status"] == "hit"
229+
assert first["timing"]["database_ms"] >= 0
230+
assert first["timing"]["backend_ms"] >= first["timing"]["database_ms"]
231+
assert first["timing"]["total_ms"] == first["timing"]["backend_ms"]
232+
assert second["timing"] == first["timing"]
226233
assert first["rows"][0] == {
227234
"platform": "instagram",
228235
"account_handle": "bravotv",

0 commit comments

Comments
 (0)