-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from rasulkireev/status-page
Add Project Status Page
- Loading branch information
Showing
5 changed files
with
288 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,55 @@ | ||
from django.utils import timezone | ||
from datetime import timedelta | ||
|
||
from django.utils import timezone | ||
|
||
|
||
class StatusSummaryMixin: | ||
@staticmethod | ||
def get_status_summary(statuses, now, start_time, number_of_sticks): | ||
def get_status_summary(statuses, end_time, start_time, number_of_sticks): | ||
summary = [] | ||
duration = end_time - start_time | ||
interval = duration / number_of_sticks | ||
|
||
for i in range(number_of_sticks): | ||
minutes = 60*24 / number_of_sticks # Each stick represents X minutes | ||
stick_time = start_time + timedelta(minutes=i*minutes) | ||
status = statuses.filter(checked_at__lte=stick_time).last() | ||
if status: | ||
if status.is_up: | ||
summary.append('up') | ||
elif status.is_down: | ||
summary.append('down') | ||
stick_end = start_time + interval * (i + 1) | ||
stick_start = start_time + interval * i | ||
|
||
stick_statuses = [s for s in statuses if stick_start <= s.checked_at < stick_end] | ||
|
||
if stick_statuses: | ||
# Prioritize 'down' status, then 'degraded', then 'up' | ||
if any(s.status == "DOWN" for s in stick_statuses): | ||
summary.append("down") | ||
elif any(s.status == "DEGRADED" for s in stick_statuses): | ||
summary.append("degraded") | ||
elif any(s.status == "UP" for s in stick_statuses): | ||
summary.append("up") | ||
else: | ||
summary.append('unknown') | ||
summary.append("unknown") | ||
else: | ||
summary.append('unknown') | ||
summary.append("unknown") | ||
|
||
return summary | ||
|
||
def add_status_summary_to_services(self, services, number_of_sticks=85): | ||
now = timezone.now() | ||
twenty_four_hours_ago = now - timedelta(hours=24) | ||
def add_status_summary_to_services(self, services, days=1, number_of_sticks=24): | ||
end_time = timezone.now() | ||
start_time = end_time - timedelta(days=days) | ||
|
||
for service in services: | ||
statuses = list(service.statuses.filter(checked_at__gte=start_time).order_by("checked_at")) | ||
service.status_summary = self.get_status_summary(statuses, end_time, start_time, number_of_sticks) | ||
|
||
# Add current status | ||
latest_status = statuses[-1] if statuses else None | ||
service.current_status = latest_status.status.lower() if latest_status else "unknown" | ||
|
||
def get_overall_project_status(self, services, days=90, number_of_sticks=90): | ||
end_time = timezone.now() | ||
start_time = end_time - timedelta(days=days) | ||
|
||
all_statuses = [] | ||
for service in services: | ||
statuses = service.statuses.filter(checked_at__gte=twenty_four_hours_ago).order_by('checked_at') | ||
service.status_summary = self.get_status_summary(statuses, now, twenty_four_hours_ago, number_of_sticks) | ||
service_statuses = list(service.statuses.filter(checked_at__gte=start_time)) | ||
all_statuses.extend(service_statuses) | ||
|
||
return self.get_status_summary(all_statuses, end_time, start_time, number_of_sticks) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.