diff --git a/src/sentry/analytics/events/__init__.py b/src/sentry/analytics/events/__init__.py index d2f92d36245a3b..b059eb52c36966 100644 --- a/src/sentry/analytics/events/__init__.py +++ b/src/sentry/analytics/events/__init__.py @@ -77,3 +77,4 @@ from .user_created import * # noqa: F401,F403 from .user_signup import * # noqa: F401,F403 from .webhook_repository_created import * # noqa: F401,F403 +from .weekly_report import * # noqa: F401,F403 diff --git a/src/sentry/analytics/events/weekly_report.py b/src/sentry/analytics/events/weekly_report.py new file mode 100644 index 00000000000000..6f0d28b53edbc0 --- /dev/null +++ b/src/sentry/analytics/events/weekly_report.py @@ -0,0 +1,15 @@ +from sentry import analytics + + +class WeeklyReportSent(analytics.Event): + type = "weekly_report.sent" + + attributes = ( + analytics.Attribute("organization_id"), + analytics.Attribute("user_id"), + analytics.Attribute("notification_uuid"), + analytics.Attribute("user_project_count", type=int), + ) + + +analytics.register(WeeklyReportSent) diff --git a/src/sentry/notifications/utils/__init__.py b/src/sentry/notifications/utils/__init__.py index dcc6479bb02a31..4de0f69f4e873e 100644 --- a/src/sentry/notifications/utils/__init__.py +++ b/src/sentry/notifications/utils/__init__.py @@ -1,6 +1,8 @@ from __future__ import annotations import logging +import random +import string import time from collections import defaultdict from dataclasses import dataclass @@ -441,6 +443,13 @@ def get_replay_id(event: Event | GroupEvent) -> str | None: return replay_id +def generate_notification_uuid() -> str: + """ + Generates a random string of 16 characters to be used as a notification uuid + """ + return "".join(random.choices(string.ascii_letters + string.digits, k=16)) + + @dataclass class PerformanceProblemContext: problem: PerformanceProblem diff --git a/src/sentry/tasks/weekly_reports.py b/src/sentry/tasks/weekly_reports.py index 60cce8100b4ab9..822f5662557067 100644 --- a/src/sentry/tasks/weekly_reports.py +++ b/src/sentry/tasks/weekly_reports.py @@ -19,7 +19,7 @@ from snuba_sdk.orderby import Direction, OrderBy from snuba_sdk.query import Limit, Query -from sentry import features +from sentry import analytics, features from sentry.api.serializers.snuba import zerofill from sentry.constants import DataCategory from sentry.models import ( @@ -32,6 +32,7 @@ OrganizationMember, OrganizationStatus, ) +from sentry.notifications.utils import generate_notification_uuid from sentry.services.hybrid_cloud.user_option import user_option_service from sentry.silo import SiloMode from sentry.snuba.dataset import Dataset @@ -739,6 +740,8 @@ def render_template_context(ctx, user_id): "organizations:session-replay", ctx.organization ) and features.has("organizations:session-replay-weekly-email", ctx.organization) + notification_uuid = generate_notification_uuid() + # Render the first section of the email where we had the table showing the # number of accepted/dropped errors/transactions for each project. def trends(): @@ -793,7 +796,9 @@ def sum_event_counts(project_ctxs): legend = [ { "slug": project_ctx.project.slug, - "url": project_ctx.project.get_absolute_url(), + "url": project_ctx.project.get_absolute_url( + params={"referrer": "weekly_report", "notification_uuid": notification_uuid} + ), "color": project_breakdown_colors[i], "dropped_error_count": project_ctx.dropped_error_count, "accepted_error_count": project_ctx.accepted_error_count, @@ -1031,6 +1036,8 @@ def issue_summary(): "key_performance_issues": key_performance_issues(), "key_replays": key_replays() if has_replay_section else [], "issue_summary": issue_summary(), + "user_project_count": len(user_projects), + "notification_uuid": notification_uuid, } @@ -1052,6 +1059,14 @@ def send_email(ctx, user_id, dry_run=False, email_override=None): ) if dry_run: return + else: + analytics.record( + "weekly_report.sent", + user_id=user_id, + organization_id=ctx.organization.id, + notification_uuid=template_ctx["notification_uuid"], + user_project_count=template_ctx["user_project_count"], + ) if email_override: message.send(to=(email_override,)) else: diff --git a/src/sentry/templates/sentry/emails/reports/body.html b/src/sentry/templates/sentry/emails/reports/body.html index d77bf66fa2a87e..864e779d927ef4 100644 --- a/src/sentry/templates/sentry/emails/reports/body.html +++ b/src/sentry/templates/sentry/emails/reports/body.html @@ -233,7 +233,8 @@