: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(data-imports): re-list app store connect segments before download (#111702)
Tom Owers9b7dd4a51918
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/app_store_connect/app_store_connect.py b/products/warehouse_sources/backend/temporal/data_imports/sources/app_store_connect/app_store_connect.pyindex 8828a55f772a..7d066a4c316a 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/app_store_connect/app_store_connect.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/app_store_connect/app_store_connect.py@@ -1691,11 +1691,12 @@ def _get_analytics_report(has_snapshot_instances = any(walk_instance.is_snapshot for walk_instances in instances_by_date.values() for walk_instance in walk_instances)- probed_segments: dict[str, list[dict[str, Any]]] = {}if should_use_incremental_field and snapshot_ceiling is not None:# Probe every instance at or below the snapshot for downloadable files before emitting# anything: a not-ready instance below the snapshot would stop the walk mid-emission,- # ratchet the watermark, and strand the history until a manual resync.+ # ratchet the watermark, and strand the history until a manual resync. The segment list+ # itself is discarded: its presigned URLs are only valid for a few minutes, and probing+ # every date up front before any downloads start can take longer than that.for processing_date in sorted(candidate for candidate in instances_by_date if candidate <= snapshot_ceiling):for walk_instance in instances_by_date[processing_date]:segments = _analytics_segments(segments_session, token_provider, logger, walk_instance.instance_id)@@ -1707,7 +1708,6 @@ def _get_analytics_report(f"processing_date={processing_date.isoformat()}")return- probed_segments[walk_instance.instance_id] = segmentsinstances_fetched = 0for processing_date in sorted(instances_by_date):@@ -1731,12 +1731,10 @@ def _get_analytics_report()return- cached_segments = probed_segments.get(walk_instance.instance_id)- segments = (- cached_segments- if cached_segments is not None- else _analytics_segments(segments_session, token_provider, logger, walk_instance.instance_id)- )+ # Always re-list right before downloading, even for an instance the probe above+ # already found non-empty: its segment URLs are presigned and short-lived, and the+ # probe for a large backlog can finish well before this instance's turn to download.+ segments = _analytics_segments(segments_session, token_provider, logger, walk_instance.instance_id)if not segments:# The instance is listed but its files aren't ready. Stop the whole walk at# this date so no newer date is emitted past the gap: the watermark thendiff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/app_store_connect/tests/test_app_store_connect.py b/products/warehouse_sources/backend/temporal/data_imports/sources/app_store_connect/tests/test_app_store_connect.pyindex b44896b42551..8670fd217677 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/app_store_connect/tests/test_app_store_connect.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/app_store_connect/tests/test_app_store_connect.py@@ -1216,13 +1216,18 @@ class TestAnalyticsSnapshotBackfill:# The fulfilled snapshot request is reused, never re-created.assert api.posts == []- def test_readiness_probe_reuses_segments_during_snapshot_emission(self) -> None:+ def test_download_relists_segments_the_readiness_probe_already_saw(self) -> None:+ # Apple's segment URLs are presigned and expire minutes after being listed. A backlog+ # large enough that the readiness probe (which walks every date up front) outlives that+ # window would hand a stale, already-expired URL to the download if the probe's listing+ # were reused instead of re-fetched — so every probed instance must be listed again right+ # before its download.api = self._ready_api()_collect_analytics(api, _FakeManager(), should_use_incremental_field=True)for instance_id in ("I1", "I2", "IS1"):- assert [url for url, _ in api.calls].count(_segments_url(instance_id)) == 1+ assert [url for url, _ in api.calls].count(_segments_url(instance_id)) == 2def test_running_the_backfill_twice_emits_identical_keys(self) -> None:first = _collect_analytics(self._ready_api(), _FakeManager())