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-catalog): summarise the proposals a team still has to review (#103983)

Thiago Salvatored4169a80eab2
diff --git a/products/data_catalog/backend/logic/pending_review.py b/products/data_catalog/backend/logic/pending_review.py
new file mode 100644
index 000000000000..1f6ec60ece86
--- /dev/null
+++ b/products/data_catalog/backend/logic/pending_review.py
@@ -0,0 +1,115 @@
+from enum import StrEnum
+
+import structlog
+
+from posthog.dataclasses import frozen
+from posthog.exceptions_capture import capture_exception
+from posthog.models import Team
+
+from ..facade.enums import CertificationStatus, MetricStatus, RelationshipStatus
+from .analytics import certification_target_name
+from .certifications import certifications_for_team
+from .metrics import metrics_for_team
+from .relationships import relationships_for_team
+
+logger = structlog.get_logger(__name__)
+
+DEFAULT_SAMPLE_SIZE = 5
+
+
+class PendingKind(StrEnum):
+ """A review queue in the data catalog. Each value is also the scene tab that opens the queue."""
+
+ METRICS = "metrics"
+ RELATIONSHIPS = "relationships"
+ CERTIFICATIONS = "certifications"
+
+
+@frozen
+class PendingGroup:
+ kind: PendingKind
+ noun: str
+ count: int
+ sample_names: list[str]
+
+
+@frozen
+class TeamPendingReview:
+ team_id: int
+ team_name: str
+ groups: list[PendingGroup]
+ total: int
+
+
+def _metrics_group(team: Team, sample_size: int) -> PendingGroup:
+ proposed = metrics_for_team(team).filter(status=MetricStatus.PROPOSED)
+ oldest_first = proposed.order_by("created_at")[:sample_size]
+ return PendingGroup(
+ kind=PendingKind.METRICS,
+ noun="metric",
+ count=proposed.count(),
+ sample_names=[metric.display_name or metric.name for metric in oldest_first],
+ )
+
+
+def _relationships_group(team: Team, sample_size: int) -> PendingGroup:
+ proposed = relationships_for_team(team).filter(status=RelationshipStatus.PROPOSED)
+ oldest_first = proposed.order_by("created_at")[:sample_size]
+ return PendingGroup(
+ kind=PendingKind.RELATIONSHIPS,
+ noun="relationship",
+ count=proposed.count(),
+ sample_names=[f"{proposal.source_table_name} to {proposal.joining_table_name}" for proposal in oldest_first],
+ )
+
+
+def _certifications_group(team: Team, sample_size: int) -> PendingGroup:
+ proposed = certifications_for_team(team).filter(status=CertificationStatus.PROPOSED)
+ oldest_first = proposed.order_by("created_at")[:sample_size]
+ return PendingGroup(
+ kind=PendingKind.CERTIFICATIONS,
+ noun="certification",
+ count=proposed.count(),
+ sample_names=[certification_target_name(cert) for cert in oldest_first],
+ )
+
+
+def build_team_pending_review(team: Team, sample_size: int = DEFAULT_SAMPLE_SIZE) -> TeamPendingReview | None:
+ """What a team still has to review, or None when every queue is empty.
+
+ Samples are the oldest items first, so the ones that have waited longest are the ones named.
+ """
+ groups = [
+ group
+ for group in (
+ _metrics_group(team, sample_size),
+ _relationships_group(team, sample_size),
+ _certifications_group(team, sample_size),
+ )
+ if group.count
+ ]
+ if not groups:
+ return None
+ return TeamPendingReview(
+ team_id=team.id,
+ team_name=team.name,
+ groups=groups,
+ total=sum(group.count for group in groups),
+ )
+
+
+def build_org_pending_reviews(
+ teams: list[Team], sample_size: int = DEFAULT_SAMPLE_SIZE
+) -> dict[int, TeamPendingReview]:
+ """Teams with something to review, keyed by team id. A team that raises is logged and left out."""
+ reviews: dict[int, TeamPendingReview] = {}
+ for team in teams:
+ try:
+ review = build_team_pending_review(team, sample_size)
+ except Exception as e:
+ logger.warning("failed to build data catalog pending review", team_id=team.id, error=str(e))
+ capture_exception(e, {"team_id": team.id})
+ continue
+ if review is not None:
+ reviews[team.id] = review
+ return reviews
diff --git a/products/data_catalog/backend/tests/test_pending_review.py b/products/data_catalog/backend/tests/test_pending_review.py
new file mode 100644
index 000000000000..073f2e8e5209
--- /dev/null
+++ b/products/data_catalog/backend/tests/test_pending_review.py
@@ -0,0 +1,113 @@
+from datetime import timedelta
+
+from posthog.test.base import BaseTest
+
+from django.utils import timezone
+
+from posthog.models.team import Team
+
+from products.data_catalog.backend.facade.enums import CertificationStatus, MetricStatus, RelationshipStatus
+from products.data_catalog.backend.logic.pending_review import (
+ PendingKind,
+ build_org_pending_reviews,
+ build_team_pending_review,
+)
+from products.data_catalog.backend.models import Metric, RelationshipProposal, TableCertification
+from products.data_modeling.backend.facade.models import DataWarehouseSavedQuery
+
+
+def _metric(team: Team, name: str, **kwargs) -> Metric:
+ return Metric.objects.unscoped().create(team=team, name=name, description="d", **kwargs)
+
+
+def _relationship(team: Team, fingerprint: str, **kwargs) -> RelationshipProposal:
+ return RelationshipProposal.objects.unscoped().create(
+ team=team,
+ source_table_name="events",
+ source_table_key="distinct_id",
+ joining_table_name="persons",
+ joining_table_key="id",
+ field_name=f"linked_{fingerprint}",
+ undirected_fingerprint=fingerprint,
+ **kwargs,
+ )
+
+
+def _certification(team: Team, view_name: str, **kwargs) -> TableCertification:
+ view = DataWarehouseSavedQuery.objects.create(team=team, name=view_name, query={"kind": "HogQLQuery"})
+ return TableCertification.objects.unscoped().create(team=team, saved_query=view, **kwargs)
+
+
+class TestBuildTeamPendingReview(BaseTest):
+ def test_returns_none_when_nothing_is_pending(self) -> None:
+ _metric(self.team, "approved_one", status=MetricStatus.APPROVED)
+ _relationship(self.team, "fp_accepted", status=RelationshipStatus.ACCEPTED)
+ _certification(self.team, "certified_view", status=CertificationStatus.CERTIFIED)
+
+ assert build_team_pending_review(self.team) is None
+
+ def test_counts_only_proposed_live_items_of_this_team(self) -> None:
+ other_team = Team.objects.create(organization=self.organization, name="Other")
+ _metric(self.team, "proposed_one")
+ _metric(self.team, "approved_one", status=MetricStatus.APPROVED)
+ _metric(self.team, "deleted_one", deleted=True)
+ _metric(other_team, "other_teams_metric")
+ _relationship(self.team, "fp_proposed")
+ _relationship(self.team, "fp_rejected", status=RelationshipStatus.REJECTED)
+ _certification(self.team, "proposed_view")
+ _certification(self.team, "deprecated_view", status=CertificationStatus.DEPRECATED)
+
+ review = build_team_pending_review(self.team)
+
+ assert review is not None
+ assert review.team_id == self.team.id
+ assert review.total == 3
+ assert {group.kind: group.count for group in review.groups} == {
+ PendingKind.METRICS: 1,
+ PendingKind.RELATIONSHIPS: 1,
+ PendingKind.CERTIFICATIONS: 1,
+ }
+
+ def test_groups_with_nothing_pending_are_left_out(self) -> None:
+ _metric(self.team, "proposed_one")
+
+ review = build_team_pending_review(self.team)
+
+ assert review is not None
+ assert [group.kind for group in review.groups] == [PendingKind.METRICS]
+
+ def test_sample_names_are_the_oldest_items_first(self) -> None:
+ for index in range(7):
+ metric = _metric(self.team, f"metric_{index}")
+ Metric.objects.unscoped().filter(pk=metric.pk).update(created_at=timezone.now() - timedelta(days=7 - index))
+
+ review = build_team_pending_review(self.team, sample_size=3)
+
+ assert review is not None
+ group = review.groups[0]
+ assert group.count == 7
+ assert group.sample_names == ["metric_0", "metric_1", "metric_2"]
+
+ def test_sample_names_describe_each_kind(self) -> None:
+ _metric(self.team, "revenue", display_name="Monthly revenue")
+ _relationship(self.team, "fp_proposed")
+ _certification(self.team, "customer_facts")
+
+ review = build_team_pending_review(self.team)
+
+ assert review is not None
+ samples = {group.kind: group.sample_names for group in review.groups}
+ assert samples[PendingKind.METRICS] == ["Monthly revenue"]
+ assert samples[PendingKind.RELATIONSHIPS] == ["events to persons"]
+ assert samples[PendingKind.CERTIFICATIONS] == ["customer_facts"]
+
+
+class TestBuildOrgPendingReviews(BaseTest):
+ def test_keys_by_team_and_omits_teams_with_nothing_pending(self) -> None:
+ quiet_team = Team.objects.create(organization=self.organization, name="Quiet")
+ _metric(self.team, "proposed_one")
+
+ reviews = build_org_pending_reviews([self.team, quiet_team])
+
+ assert list(reviews) == [self.team.id]
+ assert reviews[self.team.id].total == 1