Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Commit

Permalink
fix(notifications): omit projects with no reports
Browse files Browse the repository at this point in the history
  • Loading branch information
trowik committed Feb 13, 2023
1 parent 1ab0b82 commit 91a6dd5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
17 changes: 9 additions & 8 deletions timed/notifications/management/commands/budget_check.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import timedelta

import redminelib
from django.conf import settings
from django.core.management.base import BaseCommand
Expand Down Expand Up @@ -27,6 +29,7 @@ def handle(self, *args, **options):
cost_center__name__contains=settings.BUILD_PROJECTS,
redmine_project__isnull=False,
estimated_time__isnull=False,
estimated_time__gt=timedelta(hours=0),
)
.exclude(notifications__notification_type=Notification.BUDGET_CHECK_70)
.order_by("name")
Expand All @@ -37,17 +40,15 @@ def handle(self, *args, **options):
Report.objects.filter(task__project=project, not_billable=False)
.aggregate(billable_hours=Sum("duration"))
.get("billable_hours")
).total_seconds() / 3600
estimated_hours = project.estimated_time.total_seconds() / 3600
)

try:
budget_percentage = billable_hours / estimated_hours
except ZeroDivisionError:
self.stdout.write(
self.style.WARNING(f"Project {project.name} has no estimated time!")
)
if not billable_hours:
continue

billable_hours = billable_hours.total_seconds() / 3600
estimated_hours = project.estimated_time.total_seconds() / 3600
budget_percentage = billable_hours / estimated_hours

if budget_percentage <= 0.3:
continue
try:
Expand Down
8 changes: 5 additions & 3 deletions timed/notifications/tests/test_budget_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

@pytest.mark.parametrize(
"duration, percentage_exceeded, notification_count",
[(1, 0, 0), (3, 0, 0), (4, 30, 1), (8, 70, 2)],
[(1, 0, 0), (3, 0, 0), (4, 30, 1), (8, 70, 2), (0, 0, 0)],
)
def test_budget_check_1(
db, mocker, report_factory, duration, percentage_exceeded, notification_count
Expand All @@ -31,6 +31,9 @@ def test_budget_check_1(
project.cost_center.name = "DEV_BUILD"
project.cost_center.save()

if duration == 0:
report.delete()

if percentage_exceeded == 70:
NotificationFactory(
project=project, notification_type=Notification.BUDGET_CHECK_30
Expand Down Expand Up @@ -98,8 +101,7 @@ def test_budget_check_no_estimated_timed(db, mocker, capsys, report_factory):

call_command("budget_check")

out, _ = capsys.readouterr()
assert f"Project {project.name} has no estimated time!" in out
assert Notification.objects.count() == 0


def test_budget_check_invalid_issue(db, mocker, capsys, report_factory):
Expand Down

0 comments on commit 91a6dd5

Please sign in to comment.