diff --git a/bin/seer/trigger-night-shift b/bin/seer/trigger-night-shift index 606d9c7f0deff7..04b80ce8ec1dfa 100755 --- a/bin/seer/trigger-night-shift +++ b/bin/seer/trigger-night-shift @@ -7,6 +7,8 @@ configure() import argparse import sys +from sentry import options +from sentry.models.organization import Organization from sentry.tasks.seer.night_shift.cron import run_night_shift_for_org @@ -17,11 +19,31 @@ def _positive_int(value: str) -> int: return parsed +def _get_explorer_url(org_id: int, run_id: int) -> str | None: + url_prefix = options.get("system.url-prefix") + if not url_prefix: + return None + + try: + slug = Organization.objects.values_list("slug", flat=True).get(id=org_id) + except Organization.DoesNotExist: + return None + + return f"{url_prefix}/organizations/{slug}/issues/?explorerRunId={run_id}" + + def main(org_id: int, max_candidates: int | None) -> None: sys.stdout.write(f"> Running night shift for organization {org_id}...\n") - run_night_shift_for_org(org_id, max_candidates=max_candidates) + agent_run_id = run_night_shift_for_org(org_id, max_candidates=max_candidates) sys.stdout.write("> Done.\n") + if agent_run_id is not None: + explorer_url = _get_explorer_url(org_id, agent_run_id) + if explorer_url: + sys.stdout.write(f"> Explorer run: {explorer_url}\n") + else: + sys.stdout.write(f"> Explorer run ID: {agent_run_id}\n") + if __name__ == "__main__": parser = argparse.ArgumentParser(description="Trigger night shift for an organization.") diff --git a/src/sentry/tasks/seer/night_shift/cron.py b/src/sentry/tasks/seer/night_shift/cron.py index 9b2215fbe00c04..19295af944764e 100644 --- a/src/sentry/tasks/seer/night_shift/cron.py +++ b/src/sentry/tasks/seer/night_shift/cron.py @@ -91,13 +91,13 @@ def run_night_shift_for_org( organization_id: int, dry_run: bool = False, max_candidates: int | None = None, -) -> None: +) -> int | None: try: organization = Organization.objects.get( id=organization_id, status=OrganizationStatus.ACTIVE ) except Organization.DoesNotExist: - return + return None sentry_sdk.set_tags( { @@ -118,7 +118,7 @@ def run_night_shift_for_org( "organization_slug": organization.slug, }, ) - return + return None except Exception: logger.exception( "night_shift.failed_to_get_eligible_projects", @@ -126,7 +126,7 @@ def run_night_shift_for_org( "organization_id": organization_id, }, ) - return + return None sentry_sdk.metrics.distribution("night_shift.eligible_projects", len(eligible_projects)) @@ -168,7 +168,7 @@ def run_night_shift_for_org( }, ) run.update(error_message="Night shift run failed") - return + return None sentry_sdk.metrics.distribution("night_shift.candidates_selected", len(candidates)) for c in candidates: @@ -205,6 +205,8 @@ def run_night_shift_for_org( autofix_triggered += 1 sentry_sdk.metrics.count("night_shift.autofix_triggered", autofix_triggered) + return agent_run_id + def _get_eligible_orgs_from_batch( orgs: Sequence[Organization],