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(data-warehouse): add 3 missing tables to the TemporalIO source (#128498)

Tom Owersb636140f9415
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/COVERAGE_GAPS_APPENDIX.md b/products/warehouse_sources/backend/temporal/data_imports/sources/COVERAGE_GAPS_APPENDIX.md
index baf01f2ff5a6..328094b15fe0 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/COVERAGE_GAPS_APPENDIX.md
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/COVERAGE_GAPS_APPENDIX.md
@@ -12244,9 +12244,9 @@ Today (2): `workflow_histories`, `workflows`
Diffed against: <https://raw.githubusercontent.com/temporalio/api/master/openapi/openapiv3.yaml>
-- [ ] `namespaces/{namespace}/schedules (ListSchedules)` — schedule definitions and their recent action results — lookup resolving which schedule spawned the workflows we already sync (high)
-- [ ] `namespaces (ListNamespaces)` — namespace lookup with retention and archival config; the top-level partition of every other table (medium)
-- [ ] `namespaces/{namespace}/activities (ListActivities)` — activity-level state including paused/retrying activities, without reconstructing it from raw history events (medium)
+- [x] `namespaces/{namespace}/schedules (ListSchedules)` — schedule definitions and their recent action results — lookup resolving which schedule spawned the workflows we already sync (high)
+- [x] `namespaces (ListNamespaces)` — namespace lookup with retention and archival config; the top-level partition of every other table (medium)
+- [x] `namespaces/{namespace}/activities (ListActivities)` — activity-level state including paused/retrying activities, without reconstructing it from raw history events (medium)
- [ ] `namespaces/{namespace}/task-queues/{task_queue.name} (DescribeTaskQueue)` — backlog depth and poller counts per task queue — the key operational health metric (medium)
- [ ] `namespaces/{namespace}/batch-operations (ListBatchOperations)` — bulk terminate/signal/reset jobs and their progress, which explain mass workflow state changes (medium)
- [ ] `namespaces/{namespace}/workers (ListWorkers)` — worker fleet inventory for attributing workflow execution to hosts/builds (medium)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/temporalio/canonical_descriptions.py b/products/warehouse_sources/backend/temporal/data_imports/sources/temporalio/canonical_descriptions.py
index 43ef470dadad..db297cd0fd83 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/temporalio/canonical_descriptions.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/temporalio/canonical_descriptions.py
@@ -46,4 +46,52 @@ CANONICAL_DESCRIPTIONS: CanonicalDescriptions = {
"version": "Version of the event, used for failover and replication.",
},
},
+ "schedules": {
+ "description": "A schedule in your Temporal namespace that starts workflows on a recurring spec, with its recent and upcoming actions.",
+ "docs_url": "https://docs.temporal.io/schedule",
+ "columns": {
+ "id": "The schedule ID, unique within the namespace.",
+ "spec": "When the schedule takes actions: calendar, interval, and cron specifications.",
+ "workflow_type": "Name of the workflow type that the schedule starts.",
+ "notes": "Human-readable notes about the schedule, such as why it was paused.",
+ "paused": "Whether the schedule is paused.",
+ "recent_actions": "The most recent actions the schedule took, with the workflow ID and run ID each one started.",
+ "future_action_times": "The next times at which the schedule will take an action.",
+ "search_attributes": "Indexed key-value attributes used to filter and find schedules.",
+ },
+ },
+ "namespaces": {
+ "description": "A namespace on the Temporal cluster, with its retention and archival configuration.",
+ "docs_url": "https://docs.temporal.io/namespaces",
+ "columns": {
+ "id": "Unique identifier of the namespace.",
+ "name": "Name of the namespace.",
+ "state": "Registration state of the namespace (e.g. NAMESPACE_STATE_REGISTERED, NAMESPACE_STATE_DEPRECATED).",
+ "description": "Description of the namespace.",
+ "owner_email": "Email address of the namespace owner.",
+ "data": "A key-value map for any customized purpose.",
+ "capabilities": "All capabilities the namespace supports.",
+ "config": "Namespace configuration: workflow execution retention, and history and visibility archival state and URIs.",
+ "replication_config": "Replication configuration: the active cluster and the clusters the namespace replicates to.",
+ "failover_version": "Failover version of the namespace, used for multi-cluster replication.",
+ "is_global_namespace": "Whether the namespace is replicated across clusters.",
+ },
+ },
+ "standalone_activities": {
+ "description": "An activity execution started directly by a client rather than by a workflow, with its status and timing.",
+ "docs_url": "https://docs.temporal.io/activities",
+ "columns": {
+ "activity_id": "A unique identifier of this activity within its namespace, together with the run ID.",
+ "run_id": "The run ID of the standalone activity.",
+ "activity_type": "Name of the activity type, which maps to an activity registered on a worker.",
+ "schedule_time": "Time the activity was originally scheduled.",
+ "close_time": "Time the activity reached a terminal status, if it has closed.",
+ "status": "Execution status. Only the running status and terminal statuses appear in this table.",
+ "task_queue": "Task queue the activity was scheduled on.",
+ "execution_time": "Time at which the first activity task is made available for dispatch: the schedule time plus the start delay.",
+ "execution_duration": "Duration from schedule time to close time, only set when the activity has closed.",
+ "state_transition_count": "Number of state transitions of the activity, updated on terminal status.",
+ "search_attributes": "Indexed key-value attributes from the start request.",
+ },
+ },
}
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/temporalio/temporalio.py b/products/warehouse_sources/backend/temporal/data_imports/sources/temporalio/temporalio.py
index ee7f74576ca0..e9c9cbc5a9cd 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/temporalio/temporalio.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/temporalio/temporalio.py
@@ -1,6 +1,7 @@
import base64
import asyncio
import datetime
+import functools
import threading
import dataclasses
from collections.abc import Awaitable, Callable, Iterable
@@ -8,8 +9,20 @@ from enum import StrEnum
from queue import Queue
from typing import Any, Optional, TypeVar
+from google.protobuf.json_format import MessageToDict
+from google.protobuf.message import Message
from structlog.types import FilteringBoundLogger
+from temporalio.api.activity.v1 import ActivityExecutionListInfo
+from temporalio.api.schedule.v1 import ScheduleListEntry
+from temporalio.api.workflowservice.v1 import (
+ DescribeNamespaceRequest,
+ DescribeNamespaceResponse,
+ ListActivityExecutionsRequest,
+ ListNamespacesRequest,
+ ListSchedulesRequest,
+)
from temporalio.client import Client
+from temporalio.converter import decode_search_attributes
from temporalio.service import RPCError, RPCStatusCode
from posthog.dataclasses import frozen
@@ -27,9 +40,18 @@ from products.warehouse_sources.backend.types import IncrementalField, Increment
class TemporalIOResource(StrEnum):
Workflows = "workflows"
WorkflowHistories = "workflow_histories"
+ Schedules = "schedules"
+ Namespaces = "namespaces"
+ StandaloneActivities = "standalone_activities"
-ENDPOINTS = (TemporalIOResource.Workflows, TemporalIOResource.WorkflowHistories)
+ENDPOINTS = (
+ TemporalIOResource.Workflows,
+ TemporalIOResource.WorkflowHistories,
+ TemporalIOResource.Schedules,
+ TemporalIOResource.Namespaces,
+ TemporalIOResource.StandaloneActivities,
+)
INCREMENTAL_ENDPOINTS = (TemporalIOResource.Workflows, TemporalIOResource.WorkflowHistories)
INCREMENTAL_FIELDS: dict[str, list[IncrementalField]] = {
@@ -448,6 +470,162 @@ async def _get_workflow_histories(
break
+_LIST_PAGE_SIZE = 100
+
+# Temporal Cloud scopes a client certificate to one namespace, so it can refuse the cluster-wide
+# ListNamespaces call. The configured namespace can still be described on its own.
+_LIST_NAMESPACES_REFUSED_STATUSES = frozenset({RPCStatusCode.PERMISSION_DENIED, RPCStatusCode.UNIMPLEMENTED})
+
+
+def _message_to_row(message: Message) -> dict[str, Any]:
+ return MessageToDict(message, preserving_proto_field_name=True, always_print_fields_with_no_presence=True)
+
+
+def _proto_datetime(message: Message, field: str) -> datetime.datetime | None:
+ if not message.HasField(field):
+ return None
+ return getattr(message, field).ToDatetime(tzinfo=datetime.UTC)
+
+
+def _namespace_row(response: DescribeNamespaceResponse) -> dict[str, Any]:
+ row = _message_to_row(response)
+ return {**row.pop("namespace_info", {}), **row}
+
+
+def _schedule_row(entry: ScheduleListEntry) -> dict[str, Any]:
+ # The memo is left out because the payload codec can encrypt it.
+ return {
+ "id": entry.schedule_id,
+ **_message_to_row(entry.info),
+ "workflow_type": entry.info.workflow_type.name or None,
+ "search_attributes": decode_search_attributes(entry.search_attributes),
+ }
+
+
+def _activity_row(info: ActivityExecutionListInfo) -> dict[str, Any]:
+ return {
+ **_message_to_row(info),
+ "activity_type": info.activity_type.name or None,
+ "schedule_time": _proto_datetime(info, "schedule_time"),
+ "close_time": _proto_datetime(info, "close_time"),
+ "execution_time": _proto_datetime(info, "execution_time"),
+ "search_attributes": decode_search_attributes(info.search_attributes),
+ }
+
+
+async def _paged_rows(
+ fetch_page: Callable[[bytes], Awaitable[Any]],
+ to_rows: Callable[[Any], Iterable[dict[str, Any]]],
+ resumable_source_manager: ResumableSourceManager[TemporalIOResumeConfig],
+ logger: FilteringBoundLogger,
+):
+ resume_config = resumable_source_manager.load_state() if resumable_source_manager.can_resume() else None
+ page_token = b""
+ if resume_config is not None:
+ page_token = _decode_page_token(resume_config.next_page_token)
+ logger.debug("TemporalIO: resuming from next_page_token")
+
+ while True:
+ response = await _with_transient_rpc_retry(functools.partial(fetch_page, page_token), logger)
+ page_token = response.next_page_token
+ # A full refresh appends without deduplication, so a resume must not read this page again:
+ # stage the token of the next page, then hand the page on as one item so the batcher never
+ # writes part of it.
+ if page_token:
+ yield _ResumePoint(state=TemporalIOResumeConfig(next_page_token=_encode_page_token(page_token)))
+
+ rows = list(to_rows(response))
+ if rows:
+ yield rows
+
+ if not page_token:
+ break
+
+
+async def _get_schedules(
+ config: TemporalIOSourceConfig,
+ resumable_source_manager: ResumableSourceManager[TemporalIOResumeConfig],
+ logger: FilteringBoundLogger,
+ team_id: int,
+):
+ client = await _get_temporal_client(config, team_id)
+
+ def fetch_page(page_token: bytes):
+ return client.workflow_service.list_schedules(
+ ListSchedulesRequest(
+ namespace=config.namespace, maximum_page_size=_LIST_PAGE_SIZE, next_page_token=page_token
+ ),
+ retry=True,
+ )
+
+ async for item in _paged_rows(
+ fetch_page,
+ lambda response: (_schedule_row(entry) for entry in response.schedules),
+ resumable_source_manager,
+ logger,
+ ):
+ yield item
+
+
+async def _get_standalone_activities(
+ config: TemporalIOSourceConfig,
+ resumable_source_manager: ResumableSourceManager[TemporalIOResumeConfig],
+ logger: FilteringBoundLogger,
+ team_id: int,
+):
+ client = await _get_temporal_client(config, team_id)
+
+ def fetch_page(page_token: bytes):
+ return client.workflow_service.list_activity_executions(
+ ListActivityExecutionsRequest(
+ namespace=config.namespace, page_size=_LIST_PAGE_SIZE, next_page_token=page_token
+ ),
+ retry=True,
+ )
+
+ async for item in _paged_rows(
+ fetch_page,
+ lambda response: (_activity_row(info) for info in response.executions),
+ resumable_source_manager,
+ logger,
+ ):
+ yield item
+
+
+async def _get_namespaces(
+ config: TemporalIOSourceConfig,
+ logger: FilteringBoundLogger,
+ team_id: int,
+):
+ client = await _get_temporal_client(config, team_id)
+ service = client.workflow_service
+
+ page_token = b""
+ while True:
+ request = ListNamespacesRequest(page_size=_LIST_PAGE_SIZE, next_page_token=page_token)
+ try:
+ response = await _with_transient_rpc_retry(
+ functools.partial(service.list_namespaces, request, retry=True), logger
+ )
+ except RPCError as e:
+ if page_token or e.status not in _LIST_NAMESPACES_REFUSED_STATUSES:
+ raise
+ logger.debug("TemporalIO: ListNamespaces refused, describing the configured namespace", status=e.status)
+ described = await _with_transient_rpc_retry(
+ lambda: service.describe_namespace(DescribeNamespaceRequest(namespace=config.namespace), retry=True),
+ logger,
+ )
+ yield _namespace_row(described)
+ return
+
+ for namespace in response.namespaces:
+ yield _namespace_row(namespace)
+
+ page_token = response.next_page_token
+ if not page_token:
+ break
+
+
def temporalio_source(
config: TemporalIOSourceConfig,
resource: TemporalIOResource,
@@ -511,5 +689,42 @@ def temporalio_source(
partition_keys=["workflow_close_time"],
sort_mode="desc",
)
+ elif resource == TemporalIOResource.Schedules:
+
+ async def get_schedules_iterator():
+ return _get_schedules(config, resumable_source_manager, logger, team_id)
+
+ schedules = _async_iter_to_sync(
+ asyncio.run(get_schedules_iterator()), save_resume_state=resumable_source_manager.save_state
+ )
+
+ return SourceResponse(name=resource.value, items=lambda: schedules, primary_keys=["id"])
+ elif resource == TemporalIOResource.Namespaces:
+
+ async def get_namespaces_iterator():
+ return _get_namespaces(config, logger, team_id)
+
+ namespaces = _async_iter_to_sync(asyncio.run(get_namespaces_iterator()))
+
+ return SourceResponse(name=resource.value, items=lambda: namespaces, primary_keys=["id"])
+ elif resource == TemporalIOResource.StandaloneActivities:
+
+ async def get_activities_iterator():
+ return _get_standalone_activities(config, resumable_source_manager, logger, team_id)
+
+ activities = _async_iter_to_sync(
+ asyncio.run(get_activities_iterator()), save_resume_state=resumable_source_manager.save_state
+ )
+
+ return SourceResponse(
+ name=resource.value,
+ items=lambda: activities,
+ primary_keys=["activity_id", "run_id"],
+ partition_count=1, # this enables partitioning
+ partition_size=1, # this enables partitioning
+ partition_mode="datetime",
+ partition_format="day",
+ partition_keys=["schedule_time"],
+ )
else:
raise Exception(f"TemporalIOResource '{resource}' not recognised")
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/temporalio/test_temporalio.py b/products/warehouse_sources/backend/temporal/data_imports/sources/temporalio/test_temporalio.py
index 2b2ef750628e..35176fb2e410 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/temporalio/test_temporalio.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/temporalio/test_temporalio.py
@@ -1,14 +1,30 @@
+import datetime
+
import pytest
from unittest.mock import AsyncMock, MagicMock, call, patch
from django.test import override_settings
+from google.protobuf.duration_pb2 import Duration
+from google.protobuf.timestamp_pb2 import Timestamp
+from temporalio.api.activity.v1 import ActivityExecutionListInfo
+from temporalio.api.common.v1 import ActivityType, Payload, SearchAttributes, WorkflowExecution, WorkflowType
+from temporalio.api.enums.v1 import ActivityExecutionStatus, NamespaceState
+from temporalio.api.namespace.v1 import NamespaceConfig, NamespaceInfo
+from temporalio.api.schedule.v1 import ScheduleActionResult, ScheduleListEntry, ScheduleListInfo
+from temporalio.api.workflowservice.v1 import (
+ DescribeNamespaceResponse,
+ ListActivityExecutionsResponse,
+ ListNamespacesResponse,
+ ListSchedulesResponse,
+)
from temporalio.client import Client
from temporalio.service import RPCError, RPCStatusCode
from posthog.temporal.common.codec import EncryptionCodec
from products.warehouse_sources.backend.temporal.data_imports.sources.common.mixins import HostNotAllowedError
+from products.warehouse_sources.backend.temporal.data_imports.sources.common.testing import SourceDriver
from products.warehouse_sources.backend.temporal.data_imports.sources.common.tests.resolver import addrinfo, resolver
from products.warehouse_sources.backend.temporal.data_imports.sources.generated_configs.temporalio import (
TemporalIOSourceConfig,
@@ -19,13 +35,16 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.temporalio
TemporalIOResumeConfig,
_async_iter_to_sync,
_ByteBudget,
+ _encode_page_token,
_estimate_size_bytes,
_get_temporal_client,
+ _paged_rows,
_ResumePoint,
_with_transient_rpc_retry,
)
_MIXINS_MODULE = "products.warehouse_sources.backend.temporal.data_imports.sources.common.mixins"
+_TEMPORALIO_MODULE = "products.warehouse_sources.backend.temporal.data_imports.sources.temporalio.temporalio"
def _rpc_error(message: str, status: RPCStatusCode) -> RPCError:
@@ -257,3 +276,225 @@ class TestAsyncIterToSync:
assert reserved[0] == len(token)
assert [state.next_page_token for state in saved] == [token]
+
+
+def _timestamp(value: datetime.datetime) -> Timestamp:
+ ts = Timestamp()
+ ts.FromDatetime(value)
+ return ts
+
+
+def _keyword_search_attributes(**values: str) -> SearchAttributes:
+ return SearchAttributes(
+ indexed_fields={
+ key: Payload(metadata={"encoding": b"json/plain", "type": b"Keyword"}, data=f'"{value}"'.encode())
+ for key, value in values.items()
+ }
+ )
+
+
+def _schedules_page(schedule_ids: list[str], next_page_token: bytes = b"") -> ListSchedulesResponse:
+ return ListSchedulesResponse(
+ schedules=[
+ ScheduleListEntry(
+ schedule_id=schedule_id,
+ search_attributes=_keyword_search_attributes(Team="billing"),
+ info=ScheduleListInfo(
+ workflow_type=WorkflowType(name="NightlyExport"),
+ paused=True,
+ recent_actions=[
+ ScheduleActionResult(
+ start_workflow_result=WorkflowExecution(workflow_id=f"{schedule_id}-run", run_id="r-1")
+ )
+ ],
+ ),
+ )
+ for schedule_id in schedule_ids
+ ],
+ next_page_token=next_page_token,
+ )
+
+
+def _activities_page(activity_ids: list[str], next_page_token: bytes = b"") -> ListActivityExecutionsResponse:
+ return ListActivityExecutionsResponse(
+ executions=[ActivityExecutionListInfo(activity_id=activity_id, run_id="r-1") for activity_id in activity_ids],
+ next_page_token=next_page_token,
+ )
+
+
+def _namespace(name: str) -> DescribeNamespaceResponse:
+ return DescribeNamespaceResponse(
+ namespace_info=NamespaceInfo(name=name, id=f"{name}-id", state=NamespaceState.NAMESPACE_STATE_REGISTERED),
+ config=NamespaceConfig(workflow_execution_retention_ttl=Duration(seconds=259200)),
+ )
+
+
+def _drive(schema_name: str, workflow_service: MagicMock, **run_kwargs):
+ client = MagicMock()
+ client.workflow_service = workflow_service
+ with patch(f"{_TEMPORALIO_MODULE}._get_temporal_client", new=AsyncMock(return_value=client)):
+ return SourceDriver(TemporalIOSource(), _config()).run(schema_name, [], **run_kwargs)
+
+
+def _page_tokens(rpc: AsyncMock) -> list[bytes]:
+ return [rpc_call.args[0].next_page_token for rpc_call in rpc.await_args_list]
+
+
+class TestPagedListTables:
+ @pytest.mark.parametrize(
+ "schema_name,rpc_name,id_field,make_page",
+ [
+ ("schedules", "list_schedules", "id", _schedules_page),
+ ("standalone_activities", "list_activity_executions", "activity_id", _activities_page),
+ ],
+ )
+ @pytest.mark.parametrize(
+ "resume_token,expected_tokens,expected_ids",
+ [
+ (None, [b"", b"page-2"], ["a", "b", "c"]),
+ (b"page-2", [b"page-2"], ["c"]),
+ ],
+ )
+ def test_walks_every_page_and_resumes_from_the_saved_token(
+ self, schema_name, rpc_name, id_field, make_page, resume_token, expected_tokens, expected_ids
+ ):
+ pages = {b"": make_page(["a", "b"], b"page-2"), b"page-2": make_page(["c"])}
+ rpc = AsyncMock(side_effect=lambda request, **kwargs: pages[request.next_page_token])
+ service = MagicMock(**{rpc_name: rpc})
+ resume_state = (
+ TemporalIOResumeConfig(next_page_token=_encode_page_token(resume_token)) if resume_token else None
+ )
+
+ result = _drive(schema_name, service, resume_state=resume_state)
+
+ assert result.raised is None
+ assert [row[id_field] for row in result.rows] == expected_ids
+ assert _page_tokens(rpc) == expected_tokens
+ assert all(rpc_call.args[0].namespace == "namespace" for rpc_call in rpc.await_args_list)
+ assert result.final_state == TemporalIOResumeConfig(next_page_token=_encode_page_token(b"page-2"))
+
+ async def test_stages_the_next_page_token_before_handing_on_the_whole_page(self):
+ pages = {b"": _activities_page(["a", "b"], b"page-2"), b"page-2": _activities_page(["c"])}
+ manager = MagicMock(**{"can_resume.return_value": False})
+
+ items = [
+ item
+ async for item in _paged_rows(
+ AsyncMock(side_effect=lambda token: pages[token]),
+ lambda response: ({"id": info.activity_id} for info in response.executions),
+ manager,
+ MagicMock(),
+ )
+ ]
+
+ # A resume after the first page is written starts at page 2, so a full refresh appends no row twice.
+ assert items == [
+ _ResumePoint(state=TemporalIOResumeConfig(next_page_token=_encode_page_token(b"page-2"))),
+ [{"id": "a"}, {"id": "b"}],
+ [{"id": "c"}],
+ ]
+
+ def test_schedule_rows_carry_the_workflow_type_recent_runs_and_search_attributes(self):
+ service = MagicMock(list_schedules=AsyncMock(return_value=_schedules_page(["nightly"])))
+
+ [row] = _drive("schedules", service).rows
+
+ assert row["id"] == "nightly"
+ assert row["workflow_type"] == "NightlyExport"
+ assert row["paused"] is True
+ assert row["recent_actions"][0]["start_workflow_result"] == {"workflow_id": "nightly-run", "run_id": "r-1"}
+ assert row["search_attributes"] == {"Team": ["billing"]}
+ assert "memo" not in row
+
+ @pytest.mark.parametrize(
+ "closed,expected_close_time,expected_duration",
+ [
+ (True, datetime.datetime(2026, 1, 2, 3, 5, tzinfo=datetime.UTC), "60s"),
+ (False, None, None),
+ ],
+ )
+ def test_activity_rows_use_datetimes_and_status_names(self, closed, expected_close_time, expected_duration):
+ scheduled = datetime.datetime(2026, 1, 2, 3, 4, tzinfo=datetime.UTC)
+ info = ActivityExecutionListInfo(
+ activity_id="charge-card",
+ run_id="r-1",
+ activity_type=ActivityType(name="ChargeCard"),
+ schedule_time=_timestamp(scheduled),
+ status=ActivityExecutionStatus.ACTIVITY_EXECUTION_STATUS_RUNNING,
+ search_attributes=_keyword_search_attributes(Region="eu"),
+ )
+ if closed:
+ info.close_time.CopyFrom(_timestamp(expected_close_time))
+ info.execution_duration.CopyFrom(Duration(seconds=60))
+ info.status = ActivityExecutionStatus.ACTIVITY_EXECUTION_STATUS_COMPLETED
+ service = MagicMock(
+ list_activity_executions=AsyncMock(return_value=ListActivityExecutionsResponse(executions=[info]))
+ )
+
+ [row] = _drive("standalone_activities", service).rows
+
+ assert row["activity_type"] == "ChargeCard"
+ assert row["schedule_time"] == scheduled
+ assert row["close_time"] == expected_close_time
+ assert row.get("execution_duration") == expected_duration
+ assert row["status"] == (
+ "ACTIVITY_EXECUTION_STATUS_COMPLETED" if closed else "ACTIVITY_EXECUTION_STATUS_RUNNING"
+ )
+ assert row["search_attributes"] == {"Region": ["eu"]}
+
+
+class TestNamespaces:
+ def test_lists_every_namespace_with_its_info_at_the_row_root(self):
+ pages = {
+ b"": ListNamespacesResponse(namespaces=[_namespace("orders")], next_page_token=b"page-2"),
+ b"page-2": ListNamespacesResponse(namespaces=[_namespace("billing")]),
+ }
+ list_namespaces = AsyncMock(side_effect=lambda request, **kwargs: pages[request.next_page_token])
+ describe_namespace = AsyncMock()
+ service = MagicMock(list_namespaces=list_namespaces, describe_namespace=describe_namespace)
+
+ result = _drive("namespaces", service)
+
+ assert result.raised is None
+ assert [(row["id"], row["name"], row["state"]) for row in result.rows] == [
+ ("orders-id", "orders", "NAMESPACE_STATE_REGISTERED"),
+ ("billing-id", "billing", "NAMESPACE_STATE_REGISTERED"),
+ ]
+ assert result.rows[0]["config"]["workflow_execution_retention_ttl"] == "259200s"
+ assert _page_tokens(list_namespaces) == [b"", b"page-2"]
+ describe_namespace.assert_not_awaited()
+
+ @pytest.mark.parametrize("status", [RPCStatusCode.PERMISSION_DENIED, RPCStatusCode.UNIMPLEMENTED])
+ def test_a_refused_listing_falls_back_to_the_configured_namespace(self, status):
+ describe_namespace = AsyncMock(return_value=_namespace("namespace"))
+ service = MagicMock(
+ list_namespaces=AsyncMock(side_effect=_rpc_error("Request unauthorized.", status)),
+ describe_namespace=describe_namespace,
+ )
+
+ result = _drive("namespaces", service)
+
+ assert result.raised is None
+ assert [row["name"] for row in result.rows] == ["namespace"]
+ describe_namespace.assert_awaited_once()
+ assert describe_namespace.await_args_list[0].args[0].namespace == "namespace"
+
+ @pytest.mark.parametrize(
+ "side_effect",
+ [
+ # Falling back after rows were yielded would replace a partial listing with one namespace.
+ [
+ ListNamespacesResponse(namespaces=[_namespace("orders")], next_page_token=b"page-2"),
+ _rpc_error("Request unauthorized.", RPCStatusCode.PERMISSION_DENIED),
+ ],
+ [_rpc_error("bad request", RPCStatusCode.INVALID_ARGUMENT)],
+ ],
+ )
+ def test_other_listing_failures_are_raised(self, side_effect):
+ describe_namespace = AsyncMock()
+ service = MagicMock(list_namespaces=AsyncMock(side_effect=side_effect), describe_namespace=describe_namespace)
+
+ result = _drive("namespaces", service)
+
+ assert isinstance(result.raised, RPCError)
+ describe_namespace.assert_not_awaited()