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.

1.6 KB
Raw Blame
"""
Celery tasks for metrics.

Async entrypoints that call the facade (facade/api.py).
Keep task functions thin - only call facade methods.
"""

from datetime import datetime

from celery import shared_task

from posthog.clickhouse.query_tagging import Feature, Product, tags_context
from posthog.models.scoping import with_team_scope
from posthog.tasks.utils import CeleryQueue

from products.metrics.backend.facade import api

FINALIZE_SOFT_TIME_LIMIT_SECONDS = 5 * 60
HARD_TIME_LIMIT_GRACE_SECONDS = 60


@shared_task(
    ignore_result=True,
    queue=CeleryQueue.LONG_RUNNING.value,
    soft_time_limit=FINALIZE_SOFT_TIME_LIMIT_SECONDS,
    time_limit=FINALIZE_SOFT_TIME_LIMIT_SECONDS + HARD_TIME_LIMIT_GRACE_SECONDS,
)
@with_team_scope()
def finalize_metrics_dashboard_import(team_id: int, import_id: str) -> None:
    with tags_context(product=Product.METRICS, feature=Feature.QUERY, team_id=team_id):
        api.finalize_dashboard_import(team_id=team_id, import_id=import_id)


@shared_task(
    ignore_result=True,
    queue=CeleryQueue.LONG_RUNNING.value,
    soft_time_limit=FINALIZE_SOFT_TIME_LIMIT_SECONDS,
    time_limit=FINALIZE_SOFT_TIME_LIMIT_SECONDS + HARD_TIME_LIMIT_GRACE_SECONDS,
)
@with_team_scope()
def check_metrics_dashboard_import_layout(team_id: int, import_id: str, saved_at: str | None = None) -> None:
    with tags_context(product=Product.METRICS, feature=Feature.QUERY, team_id=team_id):
        api.check_dashboard_import_layout(
            team_id=team_id,
            import_id=import_id,
            saved_at=datetime.fromisoformat(saved_at) if saved_at else None,
        )