Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tx Plan execution time metric pushed to Sentry #2245

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/planscape/impacts/tasks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from sentry_sdk import metrics
from typing import Tuple
from urllib.parse import urljoin
from celery import chord, chain
Expand Down Expand Up @@ -132,6 +133,27 @@ def async_set_status(
treatment_plan.save()
log.info(f"Treatment plan {treatment_plan_pk} changed status to {status}.")

if not start and treatment_plan.started_at and treatment_plan.finished_at:
try:
elapsed_time_seconds = (
treatment_plan.finished_at - treatment_plan.started_at
).total_seconds()
log.info(
f"Elapsed time for treatment plan {treatment_plan_pk}: {elapsed_time_seconds} seconds."
)
metrics.gauge(
key="tx_plan_elapsed_time",
value=elapsed_time_seconds,
unit="seconds",
tags={
"stand_size": treatment_plan.scenario.get_stand_size(),
},
)
except Exception as e:
log.exception(
"Failed to send elapsed time metric to Sentry.", extra={"exception": e}
)

return (True, treatment_plan_pk)


Expand Down
34 changes: 31 additions & 3 deletions src/planscape/impacts/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@
from impacts.tasks import (
async_send_email_process_finished,
async_calculate_impacts_for_variable_action_year,
async_set_status,
)
from stands.models import Stand
from impacts.services import (
get_calculation_matrix,
)
from impacts.models import (
AVAILABLE_YEARS,
ProjectAreaTreatmentResult,
TreatmentPrescriptionAction,
TreatmentResult,
ImpactVariable,
TreatmentPlanStatus,
)
from stands.models import StandMetric
from datasets.models import DataLayerType
from datasets.tests.factories import DataLayerFactory
from impacts.tests.factories import (
Expand Down Expand Up @@ -189,3 +188,32 @@ def setUp(self):
self.project_area = ProjectAreaFactory.create(
scenario=self.plan.scenario, geometry=self.project_area_geometry
)


class AsyncTestStatusTest(TransactionTestCase):
def setUp(self):
self.plan = TreatmentPlanFactory.create()

@mock.patch("impacts.tasks.metrics", autospec=True, return_value=True)
def test_status(self, metrics_mock):
async_set_status(
treatment_plan_pk=self.plan.pk,
status=TreatmentPlanStatus.RUNNING,
start=True,
)
self.plan.refresh_from_db()
self.assertEquals(self.plan.status, TreatmentPlanStatus.RUNNING)
self.assertIsNotNone(self.plan.started_at)
self.assertIsNone(self.plan.finished_at)
self.assertFalse(metrics_mock.gauge.called)

async_set_status(
treatment_plan_pk=self.plan.pk,
status=TreatmentPlanStatus.SUCCESS,
start=False,
)
self.plan.refresh_from_db()
self.assertEquals(self.plan.status, TreatmentPlanStatus.SUCCESS)
self.assertIsNotNone(self.plan.started_at)
self.assertIsNotNone(self.plan.finished_at)
self.assertTrue(metrics_mock.gauge.called)
Loading