0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-28 18:26:15 +01:00
posthog/ee/tasks/subscriptions/subscription_utils.py
Paul D'Ambra b5f07ae0ad
feat: add text cards to DB (#12110)
Problem
Stacked on top of #12067 so the commit history is weird :/

Changes
adds DB wiring to add text or insights to dashboard tiles

How did you test this code?
developer tests and running the site locally

 move insight to dashboard
 duplicate insight from insight view
 duplicate insight from card list view
 duplicate insight from list view
 duplicate insight from dashboard
 remove insight from dashboard
 add insight to dashboard
 delete dashboard
 duplicate dashboard
 set card color
🤔 set card layout - updating layout starts refresh loop for dashboards
I think these changes make it more obvious but this is the case in master too. -> It's fixed (or at least worked-around) in #12132
 update insight data updates dashboard view for that data
 rename insight from dashboard
2022-10-10 11:49:33 +01:00

51 lines
1.8 KiB
Python

from datetime import datetime, timedelta
from time import sleep
from typing import List, Tuple, Union
import structlog
from celery import group
from posthog.models.dashboard_tile import get_tiles_ordered_by_position
from posthog.models.exported_asset import ExportedAsset
from posthog.models.insight import Insight
from posthog.models.sharing_configuration import SharingConfiguration
from posthog.models.subscription import Subscription
from posthog.tasks import exporter
logger = structlog.get_logger(__name__)
UTM_TAGS_BASE = "utm_source=posthog&utm_campaign=subscription_report"
DEFAULT_MAX_ASSET_COUNT = 6
ASSET_GENERATION_MAX_TIMEOUT = timedelta(minutes=10)
def generate_assets(
resource: Union[Subscription, SharingConfiguration], max_asset_count: int = DEFAULT_MAX_ASSET_COUNT
) -> Tuple[List[Insight], List[ExportedAsset]]:
if resource.dashboard:
tiles = get_tiles_ordered_by_position(resource.dashboard)
insights = [tile.insight for tile in tiles if tile.insight]
elif resource.insight:
insights = [resource.insight]
else:
raise Exception("There are no insights to be sent for this Subscription")
# Create all the assets we need
assets = [
ExportedAsset(team=resource.team, export_format="image/png", insight=insight, dashboard=resource.dashboard)
for insight in insights[:max_asset_count]
]
ExportedAsset.objects.bulk_create(assets)
# Wait for all assets to be exported
tasks = [exporter.export_asset.s(asset.id) for asset in assets]
parallel_job = group(tasks).apply_async()
start_time = datetime.now()
while not parallel_job.ready():
sleep(1)
if datetime.now() > start_time + ASSET_GENERATION_MAX_TIMEOUT:
raise Exception("Timed out waiting for exports")
return insights, assets