Skip to content

Commit 88a4bb7

Browse files
PR comments fixes:
- Remove Cache - Rename "stuff" variables - Change the date range
1 parent e3bfe86 commit 88a4bb7

File tree

1 file changed

+36
-28
lines changed

1 file changed

+36
-28
lines changed

dojo/metrics/views.py

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,6 @@ def engineer_metrics(request):
668668
"""
669669

670670

671-
@cache_page(60 * 5) # cache for 5 minutes
672671
@vary_on_cookie
673672
def view_engineer(request, eid):
674673
user = get_object_or_404(Dojo_User, pk=eid)
@@ -696,24 +695,27 @@ def view_engineer(request, eid):
696695
# --------------------
697696
# Month & week buckets
698697
month_start = datetime(now.year, now.month, 1, tzinfo=tz)
699-
month_end = month_start.replace(day=monthrange(now.year, now.month)[1])
698+
month_end = month_start + relativedelta(months=1) # first day of next month (exclusive)
700699

701-
open_month = reporter_findings.filter(date__range=[month_start, month_end])
702-
closed_month = closed_findings.filter(mitigated__range=[month_start, month_end])
700+
open_month = reporter_findings.filter(date__gte=month_start, date__lt=month_end)
701+
closed_month = closed_findings.filter(mitigated__gte=month_start, mitigated__lt=month_end)
703702
accepted_month = (
704703
Finding.objects.filter(
705704
risk_acceptance__owner=user,
706-
risk_acceptance__created__range=[month_start, month_end],
705+
risk_acceptance__created__gte=month_start,
706+
risk_acceptance__created__lt=month_end,
707707
).distinct()
708708
)
709709

710710
week_start = (now - timedelta(days=now.weekday())).replace(hour=0, minute=0, second=0, microsecond=0)
711-
open_week = reporter_findings.filter(date__range=[week_start, now])
712-
closed_week = closed_findings.filter(mitigated__range=[week_start, now])
711+
week_end = week_start + timedelta(days=7) # next Monday 00:00 (exclusive)
712+
open_week = reporter_findings.filter(date__gte=week_start, date__lt=week_end)
713+
closed_week = closed_findings.filter(mitigated__gte=week_start, mitigated__lt=week_end)
713714
accepted_week = (
714715
Finding.objects.filter(
715716
risk_acceptance__owner=user,
716-
risk_acceptance__created__range=[week_start, now],
717+
risk_acceptance__created__gte=week_start,
718+
risk_acceptance__created__lt=week_end,
717719
).distinct()
718720
)
719721

@@ -726,20 +728,20 @@ def view_engineer(request, eid):
726728

727729
# --------------------------
728730
# Historic series for charts
729-
stuff, o_stuff, a_stuff = [], [], []
730-
findings_this_period(reporter_findings, 1, stuff, o_stuff, a_stuff)
731+
monthly_total_series, monthly_open_series, monthly_accepted_series = [], [], []
732+
findings_this_period(reporter_findings, 1, monthly_total_series, monthly_open_series, monthly_accepted_series)
731733

732-
week_stuff, week_o_stuff, week_a_stuff = [], [], []
733-
findings_this_period(reporter_findings, 0, week_stuff, week_o_stuff, week_a_stuff)
734+
weekly_total_series, weekly_open_series, weekly_accepted_series = [], [], []
735+
findings_this_period(reporter_findings, 0, weekly_total_series, weekly_open_series, weekly_accepted_series)
734736

735737
ras_owner_qs = Risk_Acceptance.objects.filter(owner=user)
736-
_augment_series_with_accepted(a_stuff, ras_owner_qs, period="month", tz=tz)
737-
_augment_series_with_accepted(week_a_stuff, ras_owner_qs, period="week", tz=tz)
738+
_augment_series_with_accepted(monthly_accepted_series, ras_owner_qs, period="month", tz=tz)
739+
_augment_series_with_accepted(weekly_accepted_series, ras_owner_qs, period="week", tz=tz)
738740

739-
chart_data = [["Date", "S0", "S1", "S2", "S3", "Total"], *o_stuff]
740-
a_chart_data = [["Date", "S0", "S1", "S2", "S3", "Total"], *a_stuff]
741-
week_chart_data = [["Date", "S0", "S1", "S2", "S3", "Total"], *week_o_stuff]
742-
week_a_chart_data = [["Date", "S0", "S1", "S2", "S3", "Total"], *week_a_stuff]
741+
chart_data = [["Date", "S0", "S1", "S2", "S3", "Total"], *monthly_open_series]
742+
a_chart_data = [["Date", "S0", "S1", "S2", "S3", "Total"], *monthly_accepted_series]
743+
week_chart_data = [["Date", "S0", "S1", "S2", "S3", "Total"], *weekly_open_series]
744+
week_a_chart_data = [["Date", "S0", "S1", "S2", "S3", "Total"], *weekly_accepted_series]
743745

744746
# --------------
745747
# Product tables
@@ -797,12 +799,12 @@ def view_engineer(request, eid):
797799
"high_c_month": closed_count["high"],
798800
"critical_c_month": closed_count["crit"],
799801
# week
800-
"week_stuff": week_stuff,
801-
"week_a_stuff": week_a_stuff,
802+
"week_stuff": weekly_total_series,
803+
"week_a_stuff": weekly_accepted_series,
802804
# series
803-
"a_total": a_stuff,
804-
"total": stuff,
805-
"sub": len(stuff),
805+
"a_total": monthly_accepted_series,
806+
"total": monthly_total_series,
807+
"sub": len(monthly_total_series),
806808
# product tables
807809
"update": update,
808810
"total_update": total_update,
@@ -863,15 +865,19 @@ def _augment_series_with_accepted(series: list[list], ras_qs, *, period: str, tz
863865
for bucket in series:
864866
if period == "month":
865867
start = datetime.strptime(bucket[0].strip(), "%b %Y").replace(tzinfo=tz)
866-
end = start.replace(day=monthrange(start.year, start.month)[1])
868+
end = start + relativedelta(months=1) # first day of next month (exclusive)
867869
else: # "week"
868-
wk_a, wk_b = (d.strip() for d in bucket[0].split("-"))
870+
wk_a, _ = (d.strip() for d in bucket[0].split("-"))
869871
year = timezone.now().year
870872
start = datetime.strptime(f"{wk_a} {year}", "%b %d %Y").replace(tzinfo=tz)
871-
end = datetime.strptime(f"{wk_b} {year}", "%b %d %Y").replace(tzinfo=tz)
873+
end = start + timedelta(days=7) # next Monday 00:00 (exclusive)
872874

873875
accepted = (
874-
Finding.objects.filter(risk_acceptance__owner=owner, risk_acceptance__created__range=[start, end])
876+
Finding.objects.filter(
877+
risk_acceptance__owner=owner,
878+
risk_acceptance__created__gte=start,
879+
risk_acceptance__created__lt=end,
880+
)
875881
.values("severity")
876882
.annotate(cnt=Count("id"))
877883
)
@@ -902,8 +908,10 @@ def _product_stats(products) -> tuple[list, list]:
902908
by_id = {c["pid"]: c for c in counts}
903909
top10 = sorted(by_id.items(), key=lambda kv: kv[1]["total"], reverse=True)[:10]
904910

911+
product_lookup = {p.id: p for p in products}
912+
905913
def row(prod_id):
906-
prod = next(p for p in products if p.id == prod_id)
914+
prod = product_lookup[prod_id]
907915
link = f"<a href='{reverse('product_open_findings', args=(prod.id,))}'>{escape(prod.name)}</a>"
908916
data = by_id[prod_id]
909917
return [link, data["critical"], data["high"], data["medium"], data["low"], data["total"]]

0 commit comments

Comments
 (0)