Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

chore(replay-vision): drop the small thumbnail backfill (#123499)

Tue Haulundbc6079ba924d
diff --git a/products/replay_vision/backend/management/commands/backfill_replay_vision_small_thumbnails.py b/products/replay_vision/backend/management/commands/backfill_replay_vision_small_thumbnails.py
deleted file mode 100644
index 2e56407b019f..000000000000
--- a/products/replay_vision/backend/management/commands/backfill_replay_vision_small_thumbnails.py
+++ /dev/null
@@ -1,31 +0,0 @@
-from typing import Any
-
-from django.core.management.base import BaseCommand, CommandParser
-
-from products.replay_vision.backend.media_small_copies import (
- backfill_small_copies,
- media_missing_small_copies,
- teams_missing_small_copies,
-)
-
-
-class Command(BaseCommand):
- help = "Add the small copy list views load to Replay Vision frames rendered before small copies existed"
-
- def add_arguments(self, parser: CommandParser) -> None:
- parser.add_argument("--team-id", type=int, action="append", help="Only this team; repeat for more")
- parser.add_argument("--limit", type=int, help="At most this many frames per team")
- parser.add_argument(
- "--dry-run", action="store_true", help="Count the frames without a small copy, write nothing"
- )
-
- def handle(self, *args: Any, **options: Any) -> None:
- for team_id in options["team_id"] or teams_missing_small_copies():
- if options["dry_run"]:
- count = media_missing_small_copies(team_id, options["limit"]).count()
- self.stdout.write(f"Team {team_id}: {count} frames need a small copy.")
- continue
- result = backfill_small_copies(team_id, options["limit"])
- self.stdout.write(
- f"Team {team_id}: added {result.created} of {result.candidates} small copies, {result.failed} failed."
- )
diff --git a/products/replay_vision/backend/media_small_copies.py b/products/replay_vision/backend/media_small_copies.py
deleted file mode 100644
index 213fc4fe40b7..000000000000
--- a/products/replay_vision/backend/media_small_copies.py
+++ /dev/null
@@ -1,135 +0,0 @@
-"""Backfill of the small copy list views load, for frames rendered as a PNG before the rasterizer made one.
-
-It encodes the copy from the stored PNG, because the analysis video the rasterizer cuts frames from expires
-long before the frame does.
-"""
-
-import io
-
-from django.db import transaction
-from django.db.models import Q, QuerySet
-from django.utils.timezone import now
-
-import structlog
-from PIL import Image
-
-from posthog.dataclasses import frozen
-from posthog.storage import object_storage
-
-from products.exports.backend.models.exported_asset import ExportedAsset
-from products.replay_vision.backend.models.replay_observation_media import ReplayObservationMedia
-
-logger = structlog.get_logger(__name__)
-
-# Twice the widest list cell, so the small copy stays sharp on a high-density screen.
-SMALL_COPY_WIDTH_PX = 480
-# Lossy WebP, because a phone-width frame is already under the width and a PNG of it saves nothing.
-SMALL_COPY_FORMAT = ExportedAsset.ExportFormat.WEBP
-_WEBP_QUALITY = 80
-
-
-def shrink_frame(content: bytes) -> bytes:
- """Encode a PNG frame as the small copy: at most the small width, an even height, never upscaled."""
- with Image.open(io.BytesIO(content)) as source:
- image: Image.Image = source.convert("RGB")
- if image.width > SMALL_COPY_WIDTH_PX:
- height = max(2, round(image.height * SMALL_COPY_WIDTH_PX / image.width / 2) * 2)
- image = image.resize((SMALL_COPY_WIDTH_PX, height), Image.Resampling.LANCZOS)
- out = io.BytesIO()
- image.save(out, format="WEBP", quality=_WEBP_QUALITY)
- return out.getvalue()
-
-
-def small_copy_location(large_location: str) -> str | None:
- # A fixed name beside the large object, so a retry overwrites rather than leaks.
- return f"{large_location.removesuffix('.png')}-small.webp" if large_location.endswith(".png") else None
-
-
-def write_small_copy(large_location: str) -> str | None:
- """Store the small copy next to the large frame and return its location, or None when the frame is unreadable."""
- small_location = small_copy_location(large_location)
- if small_location is None:
- return None
- content = object_storage.read_bytes(large_location, missing_ok=True)
- if not content:
- return None
- object_storage.write(small_location, shrink_frame(content))
- return small_location
-
-
-@frozen
-class SmallCopyBackfillResult:
- candidates: int
- created: int
- failed: int
-
-
-def teams_missing_small_copies() -> list[int]:
- return sorted(
- ReplayObservationMedia.objects.unscoped()
- .filter(small_asset__isnull=True, asset__export_format=ExportedAsset.ExportFormat.PNG)
- .values_list("team_id", flat=True)
- .distinct()
- )
-
-
-def media_missing_small_copies(team_id: int, limit: int | None = None) -> QuerySet[ReplayObservationMedia]:
- # A frame that is not rendered or has expired would never be served, so it gets no copy either. Only PNG
- # frames get one, which leaves out media of other formats, such as video clips.
- queryset = (
- ReplayObservationMedia.objects.for_team(team_id, canonical=True)
- .filter(
- small_asset__isnull=True,
- asset__content_location__isnull=False,
- asset__export_format=ExportedAsset.ExportFormat.PNG,
- )
- .filter(Q(asset__expires_after__isnull=True) | Q(asset__expires_after__gt=now()))
- .select_related("asset")
- .order_by("id")
- )
- return queryset[:limit] if limit is not None else queryset
-
-
-def backfill_small_copies(team_id: int, limit: int | None = None) -> SmallCopyBackfillResult:
- created = failed = total = 0
- for media in media_missing_small_copies(team_id, limit).iterator(chunk_size=200):
- total += 1
- try:
- created += _add_small_copy(team_id, media)
- except Exception as error:
- # One unreadable frame must not stop the rest of the team.
- failed += 1
- logger.warning("replay_vision.small_copy_backfill_failed", media_id=str(media.id), error=str(error))
- return SmallCopyBackfillResult(candidates=total, created=created, failed=failed)
-
-
-def _add_small_copy(team_id: int, media: ReplayObservationMedia) -> bool:
- large = media.asset
- small_location = write_small_copy(large.content_location or "")
- if small_location is None:
- return False
- with transaction.atomic():
- # Locked first, so a losing run creates no row whose expiry would delete the shared object.
- locked = (
- ReplayObservationMedia.objects.for_team(team_id, canonical=True)
- .select_for_update(of=("self",))
- .filter(pk=media.pk)
- .select_related("small_asset")
- .first()
- )
- linked = locked.small_asset if locked is not None else None
- if locked is None or linked is not None:
- # Gone, or linked by another writer: the object is ours alone unless that writer stored the same key.
- if linked is None or linked.content_location != small_location:
- object_storage.delete(small_location)
- return False
- small = ExportedAsset.objects.create(
- team_id=large.team_id,
- export_format=SMALL_COPY_FORMAT,
- export_context={**(large.export_context or {}), "media_size": ReplayObservationMedia.Size.SMALL.value},
- content_location=small_location,
- expires_after=large.expires_after,
- is_system=True,
- )
- ReplayObservationMedia.objects.for_team(team_id, canonical=True).filter(pk=media.pk).update(small_asset=small)
- return True
diff --git a/products/replay_vision/backend/temporal/activities/observation_media.py b/products/replay_vision/backend/temporal/activities/observation_media.py
index 299d8dff8997..d067928380e8 100644
--- a/products/replay_vision/backend/temporal/activities/observation_media.py
+++ b/products/replay_vision/backend/temporal/activities/observation_media.py
@@ -287,8 +287,7 @@ def _link_media(team_id: int, observation_id: UUID, links: list[_MediaLink]) ->
"asset_id": link.asset_id,
"video_start_ms": link.video_start_ms,
"rec_start_ms": link.rec_start_ms,
- # Left alone without a copy, so a render from before small copies keeps one the backfill linked.
- **({"small_asset_id": link.small_asset_id} if link.small_asset_id is not None else {}),
+ "small_asset_id": link.small_asset_id,
},
)
for asset_id, location in link.asset_locations():
diff --git a/products/replay_vision/backend/tests/test_media_small_copies.py b/products/replay_vision/backend/tests/test_media_small_copies.py
deleted file mode 100644
index c242c28d2cb6..000000000000
--- a/products/replay_vision/backend/tests/test_media_small_copies.py
+++ /dev/null
@@ -1,139 +0,0 @@
-import io
-from datetime import timedelta
-
-from posthog.test.base import BaseTest
-from unittest.mock import patch
-
-from django.test import SimpleTestCase
-from django.utils import timezone
-
-from parameterized import parameterized
-from PIL import Image
-
-from posthog.models.utils import uuid7
-
-from products.exports.backend.models.exported_asset import ExportedAsset
-from products.replay_vision.backend.media_small_copies import (
- backfill_small_copies,
- media_missing_small_copies,
- shrink_frame,
-)
-from products.replay_vision.backend.models.replay_observation import (
- ObservationStatus,
- ObservationTrigger,
- ReplayObservation,
-)
-from products.replay_vision.backend.models.replay_observation_media import ReplayObservationMedia
-from products.replay_vision.backend.models.replay_scanner import ReplayScanner, ScannerModel, ScannerType
-from products.replay_vision.backend.tests.helpers import snapshot_for
-
-
-def _png(width: int, height: int) -> bytes:
- out = io.BytesIO()
- Image.new("RGB", (width, height), (40, 120, 200)).save(out, format="PNG")
- return out.getvalue()
-
-
-class TestShrinkPng(SimpleTestCase):
- @parameterized.expand(
- [
- ("desktop_frame_scales_to_an_even_height", 1280, 689, (480, 258)),
- ("phone_frame_is_never_upscaled", 336, 688, (336, 688)),
- ]
- )
- def test_shrink(self, _name: str, width: int, height: int, expected: tuple[int, int]) -> None:
- with Image.open(io.BytesIO(shrink_frame(_png(width, height)))) as image:
- assert (image.format, image.size) == ("WEBP", expected)
-
-
-class TestBackfillSmallCopies(BaseTest):
- def setUp(self) -> None:
- super().setUp()
- scanner = ReplayScanner.objects.create(
- team=self.team,
- name="Checkout monitor",
- scanner_type=ScannerType.MONITOR,
- scanner_config={"prompt": "did the user check out?"},
- model=ScannerModel.GEMINI_3_8_FLASH,
- )
- self.observation = ReplayObservation.objects.create(
- scanner=scanner,
- team=self.team,
- session_id=f"s-{uuid7()}",
- status=ObservationStatus.SUCCEEDED,
- completed_at=timezone.now(),
- scanner_snapshot=snapshot_for(scanner),
- triggered_by=ObservationTrigger.SCHEDULE,
- )
-
- def _media(self, position: int, *, rendered: bool = True, expired: bool = False) -> ReplayObservationMedia:
- asset = ExportedAsset.objects.create(
- team=self.team,
- export_format=ExportedAsset.ExportFormat.PNG,
- export_context={"observation_id": str(self.observation.id), "media_kind": "chapter"},
- content_location=f"replay-vision/media/team-{self.team.id}/{self.observation.id}/{position}.png"
- if rendered
- else None,
- expires_after=timezone.now() + (timedelta(seconds=-1) if expired else timedelta(days=60)),
- is_system=True,
- )
- return ReplayObservationMedia.objects.for_team(self.team.id).create(
- team_id=self.team.id,
- observation=self.observation,
- asset=asset,
- kind=ReplayObservationMedia.Kind.CHAPTER,
- position=position,
- video_start_ms=1000,
- )
-
- def test_it_links_a_small_copy_to_each_servable_frame_once(self) -> None:
- servable = self._media(0)
- self._media(1, rendered=False)
- self._media(2, expired=True)
- ExportedAsset.objects.filter(pk=self._media(3).asset_id).update(export_format=ExportedAsset.ExportFormat.MP4)
-
- with (
- patch("products.replay_vision.backend.media_small_copies.object_storage.read_bytes") as read,
- patch("products.replay_vision.backend.media_small_copies.object_storage.write") as write,
- ):
- read.return_value = _png(1280, 688)
-
- assert media_missing_small_copies(self.team.id).count() == 1
-
- result = backfill_small_copies(self.team.id)
- again = backfill_small_copies(self.team.id)
-
- assert (result.candidates, result.created, result.failed) == (1, 1, 0)
- assert again.candidates == 0
- assert write.call_count == 1
- small_location, small_bytes = write.call_args.args
- assert small_location == (servable.asset.content_location or "").removesuffix(".png") + "-small.webp"
- with Image.open(io.BytesIO(small_bytes)) as image:
- assert image.width == 480
-
- servable.refresh_from_db()
- assert servable.small_asset is not None
- assert servable.small_asset.content_location == small_location
- assert servable.small_asset.expires_after == servable.asset.expires_after
- assert servable.small_asset.export_format == ExportedAsset.ExportFormat.WEBP
- assert (servable.small_asset.export_context or {})["media_size"] == "small"
-
- def test_a_copy_written_while_its_observation_is_deleted_is_removed(self) -> None:
- media = self._media(0)
- small_location = (media.asset.content_location or "").removesuffix(".png") + "-small.webp"
-
- with (
- patch(
- "products.replay_vision.backend.media_small_copies.object_storage.read_bytes",
- return_value=_png(1280, 688),
- ),
- patch(
- "products.replay_vision.backend.media_small_copies.object_storage.write",
- side_effect=lambda *_: self.observation.delete(),
- ),
- patch("products.replay_vision.backend.media_small_copies.object_storage.delete") as delete,
- ):
- result = backfill_small_copies(self.team.id)
-
- assert result.created == 0
- delete.assert_called_once_with(small_location)
diff --git a/products/replay_vision/backend/tests/test_observation_media.py b/products/replay_vision/backend/tests/test_observation_media.py
index 32d253cf7f41..05fd54ad2d0f 100644
--- a/products/replay_vision/backend/tests/test_observation_media.py
+++ b/products/replay_vision/backend/tests/test_observation_media.py
@@ -92,7 +92,6 @@ class TestObservationMedia(BaseTest):
rendered: list[PreparedFrame],
observation_id: UUID,
with_small: list[PreparedFrame] | None = None,
- ext: str = "webp",
) -> None:
small_ids = {f.object_id for f in (rendered if with_small is None else with_small)}
prefix = f"s3://{settings.OBJECT_STORAGE_BUCKET}/replay-vision/media/team-{self.team.id}/{observation_id}"
@@ -105,9 +104,9 @@ class TestObservationMedia(BaseTest):
frames=[
ExtractedFrame(
id=f.object_id,
- s3_uri=f"{prefix}/{f.object_id}.{ext}",
+ s3_uri=f"{prefix}/{f.object_id}.webp",
file_size_bytes=4096,
- small_s3_uri=f"{prefix}/{f.object_id}-small.{ext}" if f.object_id in small_ids else None,
+ small_s3_uri=f"{prefix}/{f.object_id}-small.webp" if f.object_id in small_ids else None,
)
for f in rendered
]
@@ -235,22 +234,6 @@ class TestObservationMedia(BaseTest):
small = ExportedAsset.objects_including_ttl_deleted.get(pk=thumbnail.small_media_asset_id)
assert small.expires_after == expired_at
- def test_a_render_from_before_small_copies_keeps_a_backfilled_copy(self) -> None:
- prepared = self._prepare()
- self._finalize(prepared, rendered=prepared.frames, observation_id=self.observation.id)
- linked = ReplayObservationMedia.objects.for_team(self.team.id).get(observation_id=self.observation.id)
- assert linked.small_asset_id is not None
-
- # An in-flight workflow prepared on the old code carries no small asset id, and its rasterizer writes a PNG.
- old = prepared.model_copy(
- update={"frames": [f.model_copy(update={"small_media_asset_id": None}) for f in prepared.frames]}
- )
- self._finalize(old, rendered=old.frames, observation_id=self.observation.id, ext="png")
-
- linked.refresh_from_db()
- assert linked.small_asset_id == prepared.frames[0].small_media_asset_id
- assert linked.asset.export_format == ExportedAsset.ExportFormat.PNG
-
def test_an_observation_without_chapters_renders_only_its_thumbnail(self) -> None:
self.observation.scanner_result = {"model_output": {"scanner_type": "monitor", "verdict": "yes"}}
self.observation.save()