Skip to content

Commit

Permalink
updated last check-in to last successful check-in
Browse files Browse the repository at this point in the history
  • Loading branch information
rjo100 committed Jun 29, 2023
1 parent 29c191f commit 20b1a13
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
17 changes: 16 additions & 1 deletion src/sentry/monitors/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,13 @@ class Meta:
def get_audit_log_data(self):
return {"name": self.environment.name, "status": self.status, "monitor": self.monitor.name}

def get_last_successful_checkin(self):
return (
MonitorCheckIn.objects.filter(monitor_environment=self, status=CheckInStatus.OK)
.order_by("-date_added")
.first()
)

def mark_failed(
self, last_checkin=None, reason=MonitorFailure.UNKNOWN, occurrence_context=None
):
Expand Down Expand Up @@ -534,6 +541,12 @@ def mark_failed(

occurrence_data = get_occurrence_data(reason, **occurrence_context)

# Get last successful check-in to show in evidence display
last_successful_checkin_timestamp = "None"
last_successful_checkin = self.get_last_successful_checkin()
if last_successful_checkin:
last_successful_checkin_timestamp = last_successful_checkin.date_added.isoformat()

occurrence = IssueOccurrence(
id=uuid.uuid4().hex,
resource_id=None,
Expand All @@ -551,7 +564,9 @@ def mark_failed(
),
IssueEvidence(name="Environment", value=self.environment.name, important=False),
IssueEvidence(
name="Last check-in", value=last_checkin.isoformat(), important=False
name="Last successful check-in",
value=last_successful_checkin_timestamp,
important=False,
),
],
evidence_data={},
Expand Down
27 changes: 21 additions & 6 deletions tests/sentry/monitors/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
MonitorCheckInTimeout,
)
from sentry.monitors.models import (
CheckInStatus,
Monitor,
MonitorCheckIn,
MonitorEnvironment,
MonitorEnvironmentLimitsExceeded,
MonitorFailure,
Expand Down Expand Up @@ -314,6 +316,13 @@ def test_mark_failed_default_params_issue_platform(self, mock_produce_occurrence
status=monitor.status,
)

successful_check_in = MonitorCheckIn.objects.create(
monitor=monitor,
monitor_environment=monitor_environment,
project_id=self.project.id,
status=CheckInStatus.OK,
)

last_checkin = timezone.now()
assert monitor_environment.mark_failed(last_checkin=last_checkin)

Expand All @@ -339,8 +348,8 @@ def test_mark_failed_default_params_issue_platform(self, mock_produce_occurrence
"important": False,
},
{
"name": "Last check-in",
"value": last_checkin.isoformat(),
"name": "Last successful check-in",
"value": successful_check_in.date_added.isoformat(),
"important": False,
},
],
Expand Down Expand Up @@ -391,6 +400,12 @@ def test_mark_failed_with_reason_issue_platform(self, mock_produce_occurrence_to
environment=self.environment,
status=monitor.status,
)
successful_check_in = MonitorCheckIn.objects.create(
monitor=monitor,
monitor_environment=monitor_environment,
project_id=self.project.id,
status=CheckInStatus.OK,
)
last_checkin = timezone.now()
assert monitor_environment.mark_failed(
last_checkin=last_checkin,
Expand Down Expand Up @@ -420,8 +435,8 @@ def test_mark_failed_with_reason_issue_platform(self, mock_produce_occurrence_to
"important": False,
},
{
"name": "Last check-in",
"value": last_checkin.isoformat(),
"name": "Last successful check-in",
"value": successful_check_in.date_added.isoformat(),
"important": False,
},
],
Expand Down Expand Up @@ -506,8 +521,8 @@ def test_mark_failed_with_missed_reason_issue_platform(self, mock_produce_occurr
"important": False,
},
{
"name": "Last check-in",
"value": last_checkin.isoformat(),
"name": "Last successful check-in",
"value": "None",
"important": False,
},
],
Expand Down

0 comments on commit 20b1a13

Please sign in to comment.