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.

feat(replay-vision): load small thumbnail copies in list views (#123247)

Tue Haulund9e1d8ef23b76
diff --git a/products/exports/backend/migrations/0017_alter_exportedasset_export_format.py b/products/exports/backend/migrations/0017_alter_exportedasset_export_format.py
new file mode 100644
index 000000000000..646626d329d7
--- /dev/null
+++ b/products/exports/backend/migrations/0017_alter_exportedasset_export_format.py
@@ -0,0 +1,34 @@
+# Generated by Django 5.2.18 on 2026-10-26 09:05
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("exports", "0016_move_subscription_models"),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name="exportedasset",
+ name="export_format",
+ field=models.CharField(
+ choices=[
+ ("image/png", "image/png"),
+ ("application/pdf", "application/pdf"),
+ ("text/csv", "text/csv"),
+ (
+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
+ ),
+ ("video/webm", "video/webm"),
+ ("video/mp4", "video/mp4"),
+ ("image/gif", "image/gif"),
+ ("application/json", "application/json"),
+ ("application/x-ndjson", "application/x-ndjson"),
+ ("image/webp", "image/webp"),
+ ],
+ max_length=100,
+ ),
+ ),
+ ]
diff --git a/products/exports/backend/migrations/max_migration.txt b/products/exports/backend/migrations/max_migration.txt
index bef6425d6e18..d0c820254ca2 100644
--- a/products/exports/backend/migrations/max_migration.txt
+++ b/products/exports/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0016_move_subscription_models
+0017_alter_exportedasset_export_format
diff --git a/products/exports/backend/models/exported_asset.py b/products/exports/backend/models/exported_asset.py
index d316b9919f78..dac072a85549 100644
--- a/products/exports/backend/models/exported_asset.py
+++ b/products/exports/backend/models/exported_asset.py
@@ -80,6 +80,8 @@ class ExportedAsset(models.Model):
GIF = "image/gif", "image/gif"
JSON = "application/json", "application/json"
JSONL = "application/x-ndjson", "application/x-ndjson"
+ # Not in SUPPORTED_FORMATS: only system writers store it, for posters too small to justify a PNG.
+ WEBP = "image/webp", "image/webp"
SUPPORTED_FORMATS = [
ExportFormat.PNG,
diff --git a/products/replay_vision/backend/admin.py b/products/replay_vision/backend/admin.py
index 64ef7a1c7d28..263a762aa231 100644
--- a/products/replay_vision/backend/admin.py
+++ b/products/replay_vision/backend/admin.py
@@ -54,7 +54,7 @@ class ReplayObservationMediaAdmin(admin.ModelAdmin):
list_select_related = ("observation", "team")
list_filter = ("kind",)
# raw_id_fields, not the default select: each of these targets a table too large to enumerate per row.
- raw_id_fields = ("observation", "asset", "team")
+ raw_id_fields = ("observation", "asset", "small_asset", "team")
readonly_fields = ("id", "created_at")
def has_add_permission(self, request: HttpRequest) -> bool:
diff --git a/products/replay_vision/backend/api/observations.py b/products/replay_vision/backend/api/observations.py
index 60ab5fe4b495..6c1d8084ffdb 100644
--- a/products/replay_vision/backend/api/observations.py
+++ b/products/replay_vision/backend/api/observations.py
@@ -44,6 +44,7 @@ from posthog.renderers import ServerSentEventRenderer
from posthog.session_recordings.models.session_recording import SessionRecording
from products.exports.backend.facade.api import get_export_asset_content_response
+from products.exports.backend.models.exported_asset import ExportedAsset
from products.replay_vision.backend.api.errors import ReplayVisionErrorSerializer
from products.replay_vision.backend.api.filters import MultiChoiceFilter, OrderByFilter, ordering_enum, split_csv
from products.replay_vision.backend.api.observation_progress import stream_observation_progress
@@ -238,6 +239,19 @@ class ReplayObservationMediaSerializer(serializers.Serializer):
)
+_THUMBNAIL_REDIRECT_MAX_AGE_S = 600
+
+
+def _is_servable(asset: ExportedAsset | None) -> bool:
+ # The prefetch joins the asset row directly, so the manager's TTL filter does not apply
+ # and an expired frame would serve until the sweep deletes it.
+ return (
+ asset is not None
+ and bool(asset.content_location)
+ and not (asset.expires_after is not None and asset.expires_after <= now())
+ )
+
+
class ObservationThumbnailQuerySerializer(serializers.Serializer):
chapter = serializers.IntegerField(
required=False,
@@ -247,6 +261,15 @@ class ObservationThumbnailQuerySerializer(serializers.Serializer):
"observation's thumbnail."
),
)
+ size = serializers.ChoiceField(
+ choices=ReplayObservationMedia.Size.choices,
+ required=False,
+ default=ReplayObservationMedia.Size.LARGE,
+ help_text=(
+ "`large` is up to 1280px wide, for detail views. `small` is up to 480px wide, for lists and grids. "
+ "A frame rendered before small copies existed serves the large image for both."
+ ),
+ )
class ReplayObservationSerializer(serializers.ModelSerializer):
@@ -1289,12 +1312,7 @@ class ReplayObservationViewSet(
(
entry
for entry in observation.media.all()
- if entry.kind == kind
- and entry.position == position
- and entry.asset.content_location
- # The prefetch joins the asset row directly, so the manager's TTL filter does not apply
- # and an expired frame would serve until the sweep deletes it.
- and not (entry.asset.expires_after is not None and entry.asset.expires_after <= now())
+ if entry.kind == kind and entry.position == position and _is_servable(entry.asset)
),
None,
)
@@ -1312,9 +1330,13 @@ class ReplayObservationViewSet(
raise NotFound()
# Served from here, not through the export content endpoint: that one authorizes a recording
# export by the recording alone, which would let a reader denied this scanner fetch its frames.
- response = get_export_asset_content_response(asset=media.asset, download=False)
- # The response redirects to a signed, expiring URL, so a cached redirect outlives its target.
- response["Cache-Control"] = "no-store"
+ asset = media.asset
+ if query.validated_data["size"] == ReplayObservationMedia.Size.SMALL and _is_servable(media.small_asset):
+ asset = media.small_asset
+ response = get_export_asset_content_response(asset=asset, download=False)
+ # The redirect target is signed for an hour, so a browser keeps the redirect well inside that and a page
+ # of posters does not come back through here on every visit.
+ response["Cache-Control"] = f"private, max-age={_THUMBNAIL_REDIRECT_MAX_AGE_S}"
return response
@extend_schema(
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
new file mode 100644
index 000000000000..2e56407b019f
--- /dev/null
+++ b/products/replay_vision/backend/management/commands/backfill_replay_vision_small_thumbnails.py
@@ -0,0 +1,31 @@
+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_expiry.py b/products/replay_vision/backend/media_expiry.py
index 0bcd07a047db..3b7ec9a947ad 100644
--- a/products/replay_vision/backend/media_expiry.py
+++ b/products/replay_vision/backend/media_expiry.py
@@ -22,6 +22,7 @@ def expire_media_asset(
sender: type[ReplayObservationMedia], instance: ReplayObservationMedia, **kwargs: object
) -> None:
# Filtered rather than saved, so the sweep's own asset delete cascades here and matches nothing.
- ExportedAsset.objects.filter(pk=instance.asset_id, team_id=instance.team_id, expires_after__gt=now()).update(
- expires_after=now()
- )
+ # `__in` drops a None, so a row without a small copy expires only its large one.
+ ExportedAsset.objects.filter(
+ pk__in=[instance.asset_id, instance.small_asset_id], team_id=instance.team_id, expires_after__gt=now()
+ ).update(expires_after=now())
diff --git a/products/replay_vision/backend/media_small_copies.py b/products/replay_vision/backend/media_small_copies.py
new file mode 100644
index 000000000000..213fc4fe40b7
--- /dev/null
+++ b/products/replay_vision/backend/media_small_copies.py
@@ -0,0 +1,135 @@
+"""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/migrations/0129_replayobservationmedia_small_asset.py b/products/replay_vision/backend/migrations/0129_replayobservationmedia_small_asset.py
new file mode 100644
index 000000000000..caa00b403865
--- /dev/null
+++ b/products/replay_vision/backend/migrations/0129_replayobservationmedia_small_asset.py
@@ -0,0 +1,24 @@
+import django.db.models.deletion
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("exports", "0015_exportedasset_session_index"),
+ ("replay_vision", "0128_replayobservation_rlo_scanner_ranked_idx"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="replayobservationmedia",
+ name="small_asset",
+ field=models.ForeignKey(
+ blank=True,
+ db_index=False,
+ null=True,
+ on_delete=django.db.models.deletion.SET_NULL,
+ related_name="+",
+ to="exports.exportedasset",
+ ),
+ ),
+ ]
diff --git a/products/replay_vision/backend/migrations/0130_rom_small_asset_idx.py b/products/replay_vision/backend/migrations/0130_rom_small_asset_idx.py
new file mode 100644
index 000000000000..8c8e79329b16
--- /dev/null
+++ b/products/replay_vision/backend/migrations/0130_rom_small_asset_idx.py
@@ -0,0 +1,18 @@
+from django.db import migrations, models
+
+from posthog.migration_helpers import SafeAddIndexConcurrently
+
+
+class Migration(migrations.Migration):
+ atomic = False
+
+ dependencies = [
+ ("replay_vision", "0129_replayobservationmedia_small_asset"),
+ ]
+
+ operations = [
+ SafeAddIndexConcurrently(
+ model_name="replayobservationmedia",
+ index=models.Index(fields=["small_asset"], name="rom_small_asset_idx"),
+ ),
+ ]
diff --git a/products/replay_vision/backend/migrations/max_migration.txt b/products/replay_vision/backend/migrations/max_migration.txt
index 901ba83cf990..bad141a4bacb 100644
--- a/products/replay_vision/backend/migrations/max_migration.txt
+++ b/products/replay_vision/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0128_replayobservation_rlo_scanner_ranked_idx
+0130_rom_small_asset_idx
diff --git a/products/replay_vision/backend/models/replay_observation.py b/products/replay_vision/backend/models/replay_observation.py
index 274e44e9c120..e248f5f65a27 100644
--- a/products/replay_vision/backend/models/replay_observation.py
+++ b/products/replay_vision/backend/models/replay_observation.py
@@ -237,7 +237,9 @@ def hydrate_for_serialization(
.prefetch_related(
models.Prefetch(
"media",
- queryset=ReplayObservationMedia.objects.unscoped().select_related("asset").order_by("kind", "position"),
+ queryset=ReplayObservationMedia.objects.unscoped()
+ .select_related("asset", "small_asset")
+ .order_by("kind", "position"),
)
)
.annotate(
diff --git a/products/replay_vision/backend/models/replay_observation_media.py b/products/replay_vision/backend/models/replay_observation_media.py
index c7568b7e5872..e041d87c744d 100644
--- a/products/replay_vision/backend/models/replay_observation_media.py
+++ b/products/replay_vision/backend/models/replay_observation_media.py
@@ -15,9 +15,22 @@ class ReplayObservationMedia(TeamScopedRootMixin, UUIDModel):
# One frame per summary chapter, at the chapter's index in `position`.
CHAPTER = "chapter", "Chapter"
+ class Size(models.TextChoices):
+ LARGE = "large", "Large"
+ SMALL = "small", "Small"
+
observation = models.ForeignKey("replay_vision.ReplayObservation", on_delete=models.CASCADE, related_name="media")
# CASCADE so the expiry sweep's asset delete takes this row with it, leaving no link to a gone object.
asset = models.ForeignKey("exports.ExportedAsset", on_delete=models.CASCADE, related_name="+")
+ small_asset = models.ForeignKey(
+ "exports.ExportedAsset",
+ on_delete=models.SET_NULL,
+ null=True,
+ blank=True,
+ related_name="+",
+ # Indexed concurrently in its own migration, which a plain AddField index would not be.
+ db_index=False,
+ )
# db_constraint=False: the migration policy blocks real FK constraints to the hot posthog_team table.
team = models.ForeignKey("posthog.Team", on_delete=models.CASCADE, related_name="+", db_constraint=False)
@@ -45,11 +58,13 @@ class ReplayObservationMedia(TeamScopedRootMixin, UUIDModel):
f"ReplayObservationMedia.team_id ({self.team_id}) must match observation.team_id "
f"({observation_team_id})"
)
- if self.asset.team_id != observation_team_id:
- raise ValueError(
- f"ReplayObservationMedia.asset.team_id ({self.asset.team_id}) must match "
- f"observation.team_id ({observation_team_id})"
- )
+ for field in ("asset", "small_asset"):
+ asset = getattr(self, field)
+ if asset is not None and asset.team_id != observation_team_id:
+ raise ValueError(
+ f"ReplayObservationMedia.{field}.team_id ({asset.team_id}) must match "
+ f"observation.team_id ({observation_team_id})"
+ )
super().save(*args, **kwargs)
class Meta:
@@ -59,3 +74,4 @@ class ReplayObservationMedia(TeamScopedRootMixin, UUIDModel):
fields=["observation", "kind", "position"], name="unique_replay_observation_media_slot"
),
]
+ indexes = [models.Index(fields=["small_asset"], name="rom_small_asset_idx")]
diff --git a/products/replay_vision/backend/temporal/activities/observation_media.py b/products/replay_vision/backend/temporal/activities/observation_media.py
index abd82e2efd95..299d8dff8997 100644
--- a/products/replay_vision/backend/temporal/activities/observation_media.py
+++ b/products/replay_vision/backend/temporal/activities/observation_media.py
@@ -1,3 +1,4 @@
+from collections.abc import Iterator
from datetime import timedelta
from typing import Any
from uuid import UUID, uuid4
@@ -37,6 +38,8 @@ _MEDIA_EXPIRY = timedelta(days=90)
# The first and last seconds of an analysis video show the page before its CSS applies or while it unloads.
_EDGE_MARGIN_S = 3.0
_END_MARGIN_S = 1.0
+# Twice the widest list cell, so the small copy stays sharp on a high-density screen.
+_SMALL_WIDTH_PX = 480
def _footer_crop_px(context: dict[str, Any]) -> int:
@@ -148,9 +151,12 @@ async def _load_render_source(inputs: ObservationMediaInputs) -> _RenderSource:
async def _media_asset(
- inputs: ObservationMediaInputs, kind: ReplayObservationMedia.Kind, position: int
+ inputs: ObservationMediaInputs,
+ kind: ReplayObservationMedia.Kind,
+ position: int,
+ size: ReplayObservationMedia.Size = ReplayObservationMedia.Size.LARGE,
) -> ExportedAsset:
- """Get or create the `is_system` PNG asset for one media slot, so a retried activity leaves no second asset."""
+ """Get or create the `is_system` asset for one media slot, so a retried activity leaves no second asset."""
export_context: dict[str, Any] = {
# The recording id serves the recording-delete cascade, the observation id every other expiry.
"session_recording_id": inputs.session_id,
@@ -166,17 +172,25 @@ async def _media_asset(
if kind != ReplayObservationMedia.Kind.THUMBNAIL:
export_context["media_position"] = position
lookup["export_context__media_position"] = position
- media_asset = (
- await ExportedAsset.objects.filter(
- team_id=inputs.team_id, export_format=ExportedAsset.ExportFormat.PNG, is_system=True, **lookup
- )
- .order_by("id")
- .afirst()
+ candidates = ExportedAsset.objects.filter(
+ team_id=inputs.team_id,
+ # A large asset from before WebP is a PNG, and a re-render reuses it rather than leave it unlinked.
+ export_format__in=[ExportedAsset.ExportFormat.PNG, ExportedAsset.ExportFormat.WEBP],
+ is_system=True,
+ **lookup,
)
+ # Large assets predate the size key, so only the small copy carries one.
+ if size == ReplayObservationMedia.Size.SMALL:
+ export_context["media_size"] = size.value
+ candidates = candidates.filter(export_context__media_size=size.value)
+ else:
+ candidates = candidates.exclude(export_context__has_key="media_size")
+ media_asset = await candidates.order_by("id").afirst()
if media_asset is None:
media_asset = await ExportedAsset.objects.acreate(
team_id=inputs.team_id,
- export_format=ExportedAsset.ExportFormat.PNG,
+ # Set again from the rendered object's extension on link, since a worker before WebP writes a PNG.
+ export_format=ExportedAsset.ExportFormat.WEBP,
export_context=export_context,
# Explicit because the PNG default is six months.
expires_after=now() + _MEDIA_EXPIRY,
@@ -205,6 +219,7 @@ async def prepare_observation_media_activity(inputs: ObservationMediaInputs) ->
extract_frames: list[ExtractThumbnailsFrame] = []
for kind, position, video_time_s in slots:
media_asset = await _media_asset(inputs, kind, position)
+ small_media_asset = await _media_asset(inputs, kind, position, ReplayObservationMedia.Size.SMALL)
object_id = str(uuid4())
extract_frames.append(
ExtractThumbnailsFrame(
@@ -216,6 +231,7 @@ async def prepare_observation_media_activity(inputs: ObservationMediaInputs) ->
kind=kind.value,
position=position,
media_asset_id=media_asset.id,
+ small_media_asset_id=small_media_asset.id,
object_id=object_id,
video_start_ms=int(video_time_s * 1000),
rec_start_ms=source.clock.video_s_to_session_ms(video_time_s) if source.clock else None,
@@ -229,6 +245,8 @@ async def prepare_observation_media_activity(inputs: ObservationMediaInputs) ->
frames=extract_frames,
footer_crop_px=_footer_crop_px(source.context),
width=THUMBNAIL_WIDTH_PX,
+ small_width=_SMALL_WIDTH_PX,
+ image_format="webp",
s3_bucket=settings.OBJECT_STORAGE_BUCKET,
s3_key_prefix=_media_key_prefix(inputs.team_id, inputs.observation_id),
),
@@ -243,6 +261,13 @@ class _MediaLink:
video_start_ms: int
rec_start_ms: int | None
content_location: str
+ small_asset_id: int | None
+ small_content_location: str | None
+
+ def asset_locations(self) -> Iterator[tuple[int, str]]:
+ yield self.asset_id, self.content_location
+ if self.small_asset_id is not None and self.small_content_location is not None:
+ yield self.small_asset_id, self.small_content_location
def _link_media(team_id: int, observation_id: UUID, links: list[_MediaLink]) -> None:
@@ -262,11 +287,22 @@ 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 {}),
},
)
- ExportedAsset.objects.filter(pk=media.asset_id, team_id=team_id).update(
- content_location=link.content_location
- )
+ for asset_id, location in link.asset_locations():
+ ExportedAsset.objects.filter(pk=asset_id, team_id=team_id).update(
+ content_location=location, export_format=_format_of(location)
+ )
+ if link.small_asset_id is not None:
+ # A re-render reuses the large asset with its expiry, so the copy takes that one rather than outlive it.
+ ExportedAsset.objects.filter(pk=link.small_asset_id, team_id=team_id).update(
+ # Unfiltered, so a large frame past its expiry still hands over its date rather than a NULL.
+ expires_after=ExportedAsset.objects_including_ttl_deleted.filter(pk=link.asset_id).values(
+ "expires_after"
+ )[:1]
+ )
async def _link_or_expire(team_id: int, observation_id: UUID, links: list[_MediaLink]) -> bool:
@@ -278,13 +314,18 @@ async def _link_or_expire(team_id: int, observation_id: UUID, links: list[_Media
# The observation went away between the render and this write, so nothing will point at the objects.
# The sweep deletes the stored object only for a row that carries a location.
for link in links:
- await ExportedAsset.objects.filter(pk=link.asset_id, team_id=team_id).aupdate(
- content_location=link.content_location, expires_after=now()
- )
+ for asset_id, location in link.asset_locations():
+ await ExportedAsset.objects.filter(pk=asset_id, team_id=team_id).aupdate(
+ content_location=location, expires_after=now()
+ )
return False
return True
+def _format_of(location: str) -> ExportedAsset.ExportFormat:
+ return ExportedAsset.ExportFormat.PNG if location.endswith(".png") else ExportedAsset.ExportFormat.WEBP
+
+
def _content_location(s3_uri: str) -> str:
try:
return content_location_from_s3_uri(s3_uri)
@@ -297,26 +338,43 @@ def _content_location(s3_uri: str) -> str:
async def finalize_observation_media_activity(inputs: FinalizeObservationMediaInputs) -> None:
"""Link every frame the video had to its slot, and expire the assets of the frames it did not have."""
extracted = {frame.id: frame for frame in inputs.result.frames}
- links = [
- _MediaLink(
- kind=ReplayObservationMedia.Kind(frame.kind),
- position=frame.position,
- asset_id=frame.media_asset_id,
- video_start_ms=frame.video_start_ms,
- rec_start_ms=frame.rec_start_ms,
- content_location=_content_location(extracted[frame.object_id].s3_uri),
+ links: list[_MediaLink] = []
+ # Assets with no object to point at: the frames the video did not have, and the small copies not made.
+ missing: list[int] = []
+ for frame in inputs.frames:
+ result = extracted.get(frame.object_id)
+ if result is None:
+ missing.extend(
+ asset_id for asset_id in (frame.media_asset_id, frame.small_media_asset_id) if asset_id is not None
+ )
+ continue
+ small = frame.small_media_asset_id is not None and result.small_s3_uri is not None
+ if frame.small_media_asset_id is not None and not small:
+ missing.append(frame.small_media_asset_id)
+ links.append(
+ _MediaLink(
+ kind=ReplayObservationMedia.Kind(frame.kind),
+ position=frame.position,
+ asset_id=frame.media_asset_id,
+ video_start_ms=frame.video_start_ms,
+ rec_start_ms=frame.rec_start_ms,
+ content_location=_content_location(result.s3_uri),
+ small_asset_id=frame.small_media_asset_id if small else None,
+ small_content_location=_content_location(result.small_s3_uri)
+ if small and result.small_s3_uri
+ else None,
+ )
)
- for frame in inputs.frames
- if frame.object_id in extracted
- ]
- missing = [frame.media_asset_id for frame in inputs.frames if frame.object_id not in extracted]
+ linked = bool(links) and await _link_or_expire(inputs.team_id, inputs.observation_id, links)
+ # Expired only after the link, so a retry after a failed link can still write these assets.
if missing:
await ExportedAsset.objects.filter(pk__in=missing, team_id=inputs.team_id).aupdate(expires_after=now())
- if links and await _link_or_expire(inputs.team_id, inputs.observation_id, links):
+ if linked:
logger.info(
"replay_vision.media_ready",
observation_id=str(inputs.observation_id),
frames=len(links),
+ small_frames=sum(link.small_asset_id is not None for link in links),
missing=len(missing),
file_size_bytes=sum(frame.file_size_bytes for frame in inputs.result.frames),
)
diff --git a/products/replay_vision/backend/temporal/media_types.py b/products/replay_vision/backend/temporal/media_types.py
index 0dac986df355..a0ec027b847c 100644
--- a/products/replay_vision/backend/temporal/media_types.py
+++ b/products/replay_vision/backend/temporal/media_types.py
@@ -1,6 +1,7 @@
"""Types for the observation media workflow: a thumbnail per succeeded observation, plus a frame per summary chapter."""
import datetime as dt
+from typing import Literal
from uuid import UUID
from pydantic import BaseModel, Field
@@ -53,6 +54,9 @@ class ExtractThumbnailsActivityInput(BaseModel, frozen=True):
frames: list[ExtractThumbnailsFrame]
footer_crop_px: int
width: int
+ small_width: int | None = None
+ # A history from before WebP decodes to the PNG it asked for.
+ image_format: Literal["png", "webp"] = "png"
s3_bucket: str
s3_key_prefix: str
@@ -61,6 +65,8 @@ class ExtractedFrame(BaseModel, frozen=True):
id: str
s3_uri: str
file_size_bytes: int
+ # Missing when the small copy failed, or when a worker that predates it ran the render.
+ small_s3_uri: str | None = None
class ExtractThumbnailsActivityOutput(BaseModel, frozen=True):
@@ -73,6 +79,8 @@ class PreparedFrame(BaseModel, frozen=True):
kind: str
position: int
media_asset_id: int
+ # Defaults for a history recorded before small copies existed.
+ small_media_asset_id: int | None = None
object_id: str
video_start_ms: int
rec_start_ms: int | None
diff --git a/products/replay_vision/backend/tests/test_media_small_copies.py b/products/replay_vision/backend/tests/test_media_small_copies.py
new file mode 100644
index 000000000000..c242c28d2cb6
--- /dev/null
+++ b/products/replay_vision/backend/tests/test_media_small_copies.py
@@ -0,0 +1,139 @@
+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 676401fd0198..32d253cf7f41 100644
--- a/products/replay_vision/backend/tests/test_observation_media.py
+++ b/products/replay_vision/backend/tests/test_observation_media.py
@@ -87,8 +87,14 @@ class TestObservationMedia(BaseTest):
return async_to_sync(prepare_observation_media_activity)(self._inputs(**overrides))
def _finalize(
- self, prepared: PrepareObservationMediaOutput, rendered: list[PreparedFrame], observation_id: UUID
+ self,
+ prepared: PrepareObservationMediaOutput,
+ 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}"
async_to_sync(finalize_observation_media_activity)(
FinalizeObservationMediaInputs(
@@ -97,7 +103,12 @@ class TestObservationMedia(BaseTest):
frames=prepared.frames,
result=ExtractThumbnailsActivityOutput(
frames=[
- ExtractedFrame(id=f.object_id, s3_uri=f"{prefix}/{f.object_id}.png", file_size_bytes=4096)
+ ExtractedFrame(
+ id=f.object_id,
+ s3_uri=f"{prefix}/{f.object_id}.{ext}",
+ file_size_bytes=4096,
+ small_s3_uri=f"{prefix}/{f.object_id}-small.{ext}" if f.object_id in small_ids else None,
+ )
for f in rendered
]
),
@@ -173,20 +184,72 @@ class TestObservationMedia(BaseTest):
# The thumbnail keeps the video-wide edge margin; a chapter frame stays inside its own chapter instead.
assert [f.video_time_s for f in prepared.activity_input.frames] == [25.0, 1.0, 50.0, 99.0]
assert [f.required for f in prepared.activity_input.frames] == [True, False, False, False]
- assert len({f.media_asset_id for f in prepared.frames}) == 4
- # A retried prepare reuses each slot's asset rather than leaving a second one behind.
+ assert (prepared.activity_input.image_format, prepared.activity_input.small_width) == ("webp", 480)
+ assert len({f.media_asset_id for f in prepared.frames} | {f.small_media_asset_id for f in prepared.frames}) == 8
+ # A re-render reuses the large assets with the expiry they already have.
+ ExportedAsset.objects.filter(pk__in=[f.media_asset_id for f in prepared.frames]).update(
+ expires_after=timezone.now() + timedelta(days=30)
+ )
+ # A retried prepare reuses each slot's assets rather than leaving a second one behind.
again = self._prepare()
- assert [f.media_asset_id for f in again.frames] == [f.media_asset_id for f in prepared.frames]
+ assert [(f.media_asset_id, f.small_media_asset_id) for f in again.frames] == [
+ (f.media_asset_id, f.small_media_asset_id) for f in prepared.frames
+ ]
- self._finalize(prepared, rendered=prepared.frames[:3], observation_id=self.observation.id)
+ # The rasterizer made no small copy of the third frame, and the video has no fourth frame at all.
+ self._finalize(
+ prepared,
+ rendered=prepared.frames[:3],
+ with_small=prepared.frames[:2],
+ observation_id=self.observation.id,
+ )
linked = ReplayObservationMedia.objects.for_team(self.team.id).filter(observation_id=self.observation.id)
- assert sorted((m.kind, m.position, m.asset_id) for m in linked) == sorted(
- (f.kind, f.position, f.media_asset_id) for f in prepared.frames[:3]
+ assert sorted((m.kind, m.position, m.asset_id, m.small_asset_id) for m in linked) == sorted(
+ [(f.kind, f.position, f.media_asset_id, f.small_media_asset_id) for f in prepared.frames[:2]]
+ + [(f.kind, f.position, f.media_asset_id, None) for f in prepared.frames[2:3]]
)
assert all((m.asset.content_location or "").startswith("replay-vision/media/") for m in linked)
- missing_asset = ExportedAsset.objects_including_ttl_deleted.get(pk=prepared.frames[3].media_asset_id)
- assert missing_asset.expires_after is not None and missing_asset.expires_after <= timezone.now()
+ assert {m.asset.export_format for m in linked} == {ExportedAsset.ExportFormat.WEBP}
+ for m in linked:
+ if m.small_asset is not None:
+ assert (m.small_asset.content_location or "").endswith("-small.webp")
+ assert m.small_asset.expires_after == m.asset.expires_after
+ unlinked = [
+ prepared.frames[2].small_media_asset_id,
+ prepared.frames[3].media_asset_id,
+ prepared.frames[3].small_media_asset_id,
+ ]
+ for asset in ExportedAsset.objects_including_ttl_deleted.filter(pk__in=unlinked):
+ assert asset.expires_after is not None and asset.expires_after <= timezone.now()
+
+ def test_a_copy_of_a_frame_just_past_its_expiry_still_expires(self) -> None:
+ prepared = self._prepare()
+ thumbnail = prepared.frames[0]
+ assert thumbnail.small_media_asset_id is not None
+ expired_at = timezone.now() - timedelta(seconds=1)
+ ExportedAsset.objects.filter(pk=thumbnail.media_asset_id).update(expires_after=expired_at)
+
+ self._finalize(prepared, rendered=[thumbnail], observation_id=self.observation.id)
+
+ 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"}}
@@ -235,17 +298,26 @@ class TestObservationMediaExpiry(BaseTest):
expires_after=timezone.now() + timedelta(days=90),
is_system=True,
)
+ self.small_asset = ExportedAsset.objects.create(
+ team=self.team,
+ export_format=ExportedAsset.ExportFormat.WEBP,
+ export_context={"observation_id": str(self.observation.id)},
+ content_location=f"replay-vision/media/team-{self.team.id}/{self.observation.id}/x-small.webp",
+ expires_after=timezone.now() + timedelta(days=90),
+ is_system=True,
+ )
ReplayObservationMedia.objects.for_team(self.team.id).create(
team_id=self.team.id,
observation=self.observation,
asset=self.asset,
+ small_asset=self.small_asset,
kind=ReplayObservationMedia.Kind.THUMBNAIL,
position=0,
video_start_ms=1000,
)
- def _expires_after(self) -> datetime:
- expires_after = ExportedAsset.objects_including_ttl_deleted.get(pk=self.asset.pk).expires_after
+ def _expires_after(self, asset: ExportedAsset) -> datetime:
+ expires_after = ExportedAsset.objects_including_ttl_deleted.get(pk=asset.pk).expires_after
assert expires_after is not None
return expires_after
@@ -259,8 +331,9 @@ class TestObservationMediaExpiry(BaseTest):
def test_media_is_expired_when_its_observation_goes_away(self, _name: str, delete) -> None:
delete(self)
- assert self._expires_after() <= timezone.now()
- assert ExportedAsset.objects_including_ttl_deleted.filter(pk=self.asset.pk).exists()
+ for asset in (self.asset, self.small_asset):
+ assert self._expires_after(asset) <= timezone.now()
+ assert ExportedAsset.objects_including_ttl_deleted.filter(pk=asset.pk).exists()
def test_the_sweeps_own_delete_is_not_undone(self) -> None:
self.asset.delete()
diff --git a/products/replay_vision/backend/tests/test_observation_thumbnail_api.py b/products/replay_vision/backend/tests/test_observation_thumbnail_api.py
index b48ffca13cdb..e7bd783d9c32 100644
--- a/products/replay_vision/backend/tests/test_observation_thumbnail_api.py
+++ b/products/replay_vision/backend/tests/test_observation_thumbnail_api.py
@@ -1,7 +1,12 @@
from datetime import timedelta
+from unittest.mock import patch
+
+from django.http import HttpResponse
from django.utils import timezone
+from parameterized import parameterized
+
from products.exports.backend.models.exported_asset import ExportedAsset
from products.replay_vision.backend.models.replay_observation import (
ObservationStatus,
@@ -28,26 +33,31 @@ class TestObservationThumbnail(_VisionAPITestCase):
def _url(self) -> str:
return f"{self.observations_url(self.scanner.id)}{self.observation.id}/thumbnail/"
- def _add_thumbnail(
- self,
- *,
- rendered: bool,
- kind: ReplayObservationMedia.Kind = ReplayObservationMedia.Kind.THUMBNAIL,
- position: int = 0,
- ) -> ExportedAsset:
- asset = ExportedAsset.objects.create(
+ def _png(self, *, rendered: bool, name: str = "x") -> ExportedAsset:
+ return ExportedAsset.objects.create(
team=self.team,
export_format=ExportedAsset.ExportFormat.PNG,
export_context={"observation_id": str(self.observation.id)},
- content_location=f"replay-vision/media/team-{self.team.id}/{self.observation.id}/x.png"
+ content_location=f"replay-vision/media/team-{self.team.id}/{self.observation.id}/{name}.png"
if rendered
else None,
is_system=True,
)
+
+ def _add_thumbnail(
+ self,
+ *,
+ rendered: bool,
+ kind: ReplayObservationMedia.Kind = ReplayObservationMedia.Kind.THUMBNAIL,
+ position: int = 0,
+ small_asset: ExportedAsset | None = None,
+ ) -> ExportedAsset:
+ asset = self._png(rendered=rendered)
ReplayObservationMedia.objects.for_team(self.team.id).create(
team_id=self.team.id,
observation=self.observation,
asset=asset,
+ small_asset=small_asset,
kind=kind,
position=position,
video_start_ms=1000,
@@ -60,8 +70,40 @@ class TestObservationThumbnail(_VisionAPITestCase):
response = self.client.get(self._url())
assert response.status_code in (200, 302)
- # The response redirects to a signed, expiring URL, so a cached one would outlive its target.
- assert response.headers["Cache-Control"] == "no-store"
+ # The redirect target is signed for an hour, so a cache longer than that would outlive it.
+ assert response.headers["Cache-Control"] == "private, max-age=600"
+
+ @parameterized.expand(
+ [
+ ("small_requested", "small", "rendered", "small"),
+ ("large_requested", "large", "rendered", "large"),
+ ("no_size_means_large", None, "rendered", "large"),
+ ("small_missing_falls_back", "small", None, "large"),
+ ("small_unfinished_falls_back", "small", "unrendered", "large"),
+ ("small_expired_falls_back", "small", "expired", "large"),
+ ]
+ )
+ def test_the_size_picks_which_copy_is_served(
+ self, _name: str, size: str | None, small_state: str | None, expected: str
+ ) -> None:
+ small = None if small_state is None else self._png(rendered=small_state != "unrendered", name="small")
+ if small_state == "expired":
+ assert small is not None
+ ExportedAsset.objects_including_ttl_deleted.filter(pk=small.pk).update(
+ expires_after=timezone.now() - timedelta(seconds=1)
+ )
+ large = self._add_thumbnail(rendered=True, small_asset=small)
+
+ with patch(
+ "products.replay_vision.backend.api.observations.get_export_asset_content_response",
+ return_value=HttpResponse(),
+ ) as serve:
+ response = self.client.get(self._url(), {} if size is None else {"size": size})
+
+ assert response.status_code == 200
+ expected_asset = small if expected == "small" else large
+ assert expected_asset is not None
+ assert serve.call_args.kwargs["asset"].pk == expected_asset.pk
def test_a_chapter_frame_is_served_only_for_its_chapter(self) -> None:
self._add_thumbnail(rendered=True, kind=ReplayObservationMedia.Kind.CHAPTER, position=1)
diff --git a/products/replay_vision/frontend/components/ObservationThumbnail.tsx b/products/replay_vision/frontend/components/ObservationThumbnail.tsx
index b352f80fe5a0..5f01fbe92b4e 100644
--- a/products/replay_vision/frontend/components/ObservationThumbnail.tsx
+++ b/products/replay_vision/frontend/components/ObservationThumbnail.tsx
@@ -6,7 +6,7 @@ import { IconVideoCamera } from '@posthog/icons'
import { teamLogic } from 'scenes/teamLogic'
import { getVisionObservationsThumbnailRetrieveUrl } from '../generated/api'
-import { ReplayObservationApi } from '../generated/api.schemas'
+import { ReplayObservationApi, VisionObservationsThumbnailRetrieveSize } from '../generated/api.schemas'
interface ObservationThumbnailProps {
observation: ReplayObservationApi
@@ -14,6 +14,8 @@ interface ObservationThumbnailProps {
chapter?: number
/** Sizing and shape come from the caller, since a table cell and a detail page want very different frames. */
className?: string
+ /** `small` (up to 480px wide) for posters at most 240px wide, so it stays sharp at 2x. Defaults to `large`. */
+ size?: VisionObservationsThumbnailRetrieveSize
children?: React.ReactNode
}
@@ -24,6 +26,7 @@ export function ObservationThumbnail({
observation,
chapter,
className,
+ size = VisionObservationsThumbnailRetrieveSize.Large,
children,
}: ObservationThumbnailProps): JSX.Element {
const { currentTeamId } = useValues(teamLogic)
@@ -56,16 +59,9 @@ export function ObservationThumbnail({
)
const baseSrc =
hasThumbnail && currentTeamId !== null && !failed
- ? getVisionObservationsThumbnailRetrieveUrl(
- String(currentTeamId),
- observation.id,
- chapter === undefined ? undefined : { chapter }
- )
+ ? getVisionObservationsThumbnailRetrieveUrl(String(currentTeamId), observation.id, { chapter, size })
: undefined
- const src =
- baseSrc !== undefined && attempt > 0
- ? `${baseSrc}${baseSrc.includes('?') ? '&' : '?'}retry=${attempt}`
- : baseSrc
+ const src = baseSrc !== undefined && attempt > 0 ? `${baseSrc}&retry=${attempt}` : baseSrc
const onError = (): void => {
// One retry: the redirect target can blink, and the poster would stay blank for the whole mount.
diff --git a/products/replay_vision/frontend/components/RecordingTimeline.tsx b/products/replay_vision/frontend/components/RecordingTimeline.tsx
index 886de81db2f0..3e11f02f1a48 100644
--- a/products/replay_vision/frontend/components/RecordingTimeline.tsx
+++ b/products/replay_vision/frontend/components/RecordingTimeline.tsx
@@ -156,6 +156,7 @@ export function RecordingTimeline(props: RecordingTimelineProps): JSX.Element {
observation={timeline.summary as ReplayObservationApi}
chapter={row.chapter.position}
className="w-20 shrink-0"
+ size="small"
/>
</span>
</Tooltip>
diff --git a/products/replay_vision/frontend/generated/api.schemas.ts b/products/replay_vision/frontend/generated/api.schemas.ts
index 8070ddd4e20b..2ae5737fcd09 100644
--- a/products/replay_vision/frontend/generated/api.schemas.ts
+++ b/products/replay_vision/frontend/generated/api.schemas.ts
@@ -3348,8 +3348,24 @@ export type VisionObservationsThumbnailRetrieveParams = {
* @minimum 0
*/
chapter?: number
+ /**
+ * `large` is up to 1280px wide, for detail views. `small` is up to 480px wide, for lists and grids. A frame rendered before small copies existed serves the large image for both.
+ *
+ * * `large` - Large
+ * * `small` - Small
+ * @minLength 1
+ */
+ size?: VisionObservationsThumbnailRetrieveSize
}
+export type VisionObservationsThumbnailRetrieveSize =
+ (typeof VisionObservationsThumbnailRetrieveSize)[keyof typeof VisionObservationsThumbnailRetrieveSize]
+
+export const VisionObservationsThumbnailRetrieveSize = {
+ Large: 'large',
+ Small: 'small',
+} as const
+
export type VisionObservationsSearchRetrieveParams = {
/**
* Only observations analyzed at or after this time. Accepts ISO 8601, a relative date like `-7d`, or `now`; values without an explicit offset are interpreted in the project's timezone.
@@ -3693,8 +3709,24 @@ export type VisionScannersObservationsThumbnailRetrieveParams = {
* @minimum 0
*/
chapter?: number
+ /**
+ * `large` is up to 1280px wide, for detail views. `small` is up to 480px wide, for lists and grids. A frame rendered before small copies existed serves the large image for both.
+ *
+ * * `large` - Large
+ * * `small` - Small
+ * @minLength 1
+ */
+ size?: VisionScannersObservationsThumbnailRetrieveSize
}
+export type VisionScannersObservationsThumbnailRetrieveSize =
+ (typeof VisionScannersObservationsThumbnailRetrieveSize)[keyof typeof VisionScannersObservationsThumbnailRetrieveSize]
+
+export const VisionScannersObservationsThumbnailRetrieveSize = {
+ Large: 'large',
+ Small: 'small',
+} as const
+
export type VisionScannersObservationsStatsRetrieveParams = {
/**
* Only observations dispatched by this backfill.
diff --git a/products/replay_vision/frontend/replay_scanners/components/ScannerObservationsTable.tsx b/products/replay_vision/frontend/replay_scanners/components/ScannerObservationsTable.tsx
index 3f3254804ca1..b3c42e19e618 100644
--- a/products/replay_vision/frontend/replay_scanners/components/ScannerObservationsTable.tsx
+++ b/products/replay_vision/frontend/replay_scanners/components/ScannerObservationsTable.tsx
@@ -176,7 +176,11 @@ export function ScannerObservationsTable({ scannerId }: { scannerId: string }):
containerClassName="max-w-none p-1"
>
<div className="relative">
- <ObservationThumbnail observation={obs} className="w-40 @7xl/observations:w-52" />
+ <ObservationThumbnail
+ observation={obs}
+ className="w-40 @7xl/observations:w-52"
+ size="small"
+ />
{!obs.viewed && <UnviewedObservationTag className="absolute top-1 left-1" />}
</div>
</Tooltip>
diff --git a/products/replay_vision/frontend/replay_scanners/components/WatchPickRow.tsx b/products/replay_vision/frontend/replay_scanners/components/WatchPickRow.tsx
index 3a0dd4ed8deb..23486fa7bbcb 100644
--- a/products/replay_vision/frontend/replay_scanners/components/WatchPickRow.tsx
+++ b/products/replay_vision/frontend/replay_scanners/components/WatchPickRow.tsx
@@ -43,7 +43,7 @@ export function WatchPickRow({ item, position, isActive, rank }: WatchPickRowPro
>
<span className={cn('flex w-full min-w-0 items-center gap-3 py-1', observation.viewed && 'opacity-60')}>
<span className="relative w-28 shrink-0">
- <ObservationThumbnail observation={observation} className="w-full" />
+ <ObservationThumbnail observation={observation} className="w-full" size="small" />
{rank !== undefined && <WatchPickRankBadge rank={rank} size="small" />}
{!observation.viewed && (
<span className="absolute inset-y-0 left-0 w-1 rounded-l bg-accent" aria-hidden />
diff --git a/products/replay_vision/frontend/replay_scanners/components/variants/VariantObservationRow.tsx b/products/replay_vision/frontend/replay_scanners/components/variants/VariantObservationRow.tsx
index 4146eb298332..833886a94da2 100644
--- a/products/replay_vision/frontend/replay_scanners/components/variants/VariantObservationRow.tsx
+++ b/products/replay_vision/frontend/replay_scanners/components/variants/VariantObservationRow.tsx
@@ -21,7 +21,7 @@ export function VariantObservationRow({ observation }: VariantObservationRowProp
data-attr="vision-variant-latest-observation"
>
<div className="hidden @md:block w-24 shrink-0">
- <ObservationThumbnail observation={observation} className="rounded" />
+ <ObservationThumbnail observation={observation} className="rounded" size="small" />
</div>
<div className="min-w-0 flex-1 space-y-0.5">
<div className="text-sm line-clamp-3">
diff --git a/products/replay_vision/frontend/search/SearchResults.tsx b/products/replay_vision/frontend/search/SearchResults.tsx
index 31f4aac3a3e9..da8d769fb53c 100644
--- a/products/replay_vision/frontend/search/SearchResults.tsx
+++ b/products/replay_vision/frontend/search/SearchResults.tsx
@@ -73,7 +73,11 @@ function WatchLink({
const citedMs = firstCitedTimestampMs(observation)
/* The card clips its own corners and draws its own edge, so the poster goes edge to edge there. */
const thumbnail = (
- <ObservationThumbnail observation={observation} className={clsx(!compact && 'rounded-none border-0')}>
+ <ObservationThumbnail
+ observation={observation}
+ className={clsx(!compact && 'rounded-none border-0')}
+ size={compact ? 'small' : 'large'}
+ >
{expired ? undefined : (
/* A fixed dark scrim, not a theme surface: the chip sits on a frame of any colour, white included. */
<span
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index 81db34514b1c..c7cdee4ae49c 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -139384,8 +139384,24 @@ export namespace Schemas {
* @minimum 0
*/
chapter?: number;
+ /**
+ * `large` is up to 1280px wide, for detail views. `small` is up to 480px wide, for lists and grids. A frame rendered before small copies existed serves the large image for both.
+ *
+ * * `large` - Large
+ * * `small` - Small
+ * @minLength 1
+ */
+ size?: VisionObservationsThumbnailRetrieveSize;
};
+ export type VisionObservationsThumbnailRetrieveSize = typeof VisionObservationsThumbnailRetrieveSize[keyof typeof VisionObservationsThumbnailRetrieveSize];
+
+
+ export const VisionObservationsThumbnailRetrieveSize = {
+ Large: 'large',
+ Small: 'small',
+ } as const;
+
export type VisionObservationsSearchRetrieveParams = {
/**
* Only observations analyzed at or after this time. Accepts ISO 8601, a relative date like `-7d`, or `now`; values without an explicit offset are interpreted in the project's timezone.
@@ -139728,8 +139744,24 @@ export namespace Schemas {
* @minimum 0
*/
chapter?: number;
+ /**
+ * `large` is up to 1280px wide, for detail views. `small` is up to 480px wide, for lists and grids. A frame rendered before small copies existed serves the large image for both.
+ *
+ * * `large` - Large
+ * * `small` - Small
+ * @minLength 1
+ */
+ size?: VisionScannersObservationsThumbnailRetrieveSize;
};
+ export type VisionScannersObservationsThumbnailRetrieveSize = typeof VisionScannersObservationsThumbnailRetrieveSize[keyof typeof VisionScannersObservationsThumbnailRetrieveSize];
+
+
+ export const VisionScannersObservationsThumbnailRetrieveSize = {
+ Large: 'large',
+ Small: 'small',
+ } as const;
+
export type VisionScannersObservationsStatsRetrieveParams = {
/**
* Only observations dispatched by this backfill.