Skip to content

Commit fc656a2

Browse files
PR comments fixes:
- Remove Cache - Rename "stuff" variables - Change the date range
1 parent 94ca50e commit fc656a2

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
@@ -677,7 +677,6 @@ def engineer_metrics(request):
677677
"""
678678

679679

680-
@cache_page(60 * 5) # cache for 5 minutes
681680
@vary_on_cookie
682681
def view_engineer(request, eid):
683682
user = get_object_or_404(Dojo_User, pk=eid)
@@ -705,24 +704,27 @@ def view_engineer(request, eid):
705704
# --------------------
706705
# Month & week buckets
707706
month_start = datetime(now.year, now.month, 1, tzinfo=tz)
708-
month_end = month_start.replace(day=monthrange(now.year, now.month)[1])
707+
month_end = month_start + relativedelta(months=1) # first day of next month (exclusive)
709708

710-
open_month = reporter_findings.filter(date__range=[month_start, month_end])
711-
closed_month = closed_findings.filter(mitigated__range=[month_start, month_end])
709+
open_month = reporter_findings.filter(date__gte=month_start, date__lt=month_end)
710+
closed_month = closed_findings.filter(mitigated__gte=month_start, mitigated__lt=month_end)
712711
accepted_month = (
713712
Finding.objects.filter(
714713
risk_acceptance__owner=user,
715-
risk_acceptance__created__range=[month_start, month_end],
714+
risk_acceptance__created__gte=month_start,
715+
risk_acceptance__created__lt=month_end,
716716
).distinct()
717717
)
718718

719719
week_start = (now - timedelta(days=now.weekday())).replace(hour=0, minute=0, second=0, microsecond=0)
720-
open_week = reporter_findings.filter(date__range=[week_start, now])
721-
closed_week = closed_findings.filter(mitigated__range=[week_start, now])
720+
week_end = week_start + timedelta(days=7) # next Monday 00:00 (exclusive)
721+
open_week = reporter_findings.filter(date__gte=week_start, date__lt=week_end)
722+
closed_week = closed_findings.filter(mitigated__gte=week_start, mitigated__lt=week_end)
722723
accepted_week = (
723724
Finding.objects.filter(
724725
risk_acceptance__owner=user,
725-
risk_acceptance__created__range=[week_start, now],
726+
risk_acceptance__created__gte=week_start,
727+
risk_acceptance__created__lt=week_end,
726728
).distinct()
727729
)
728730

@@ -735,20 +737,20 @@ def view_engineer(request, eid):
735737

736738
# --------------------------
737739
# Historic series for charts
738-
stuff, o_stuff, a_stuff = [], [], []
739-
findings_this_period(reporter_findings, 1, stuff, o_stuff, a_stuff)
740+
monthly_total_series, monthly_open_series, monthly_accepted_series = [], [], []
741+
findings_this_period(reporter_findings, 1, monthly_total_series, monthly_open_series, monthly_accepted_series)
740742

741-
week_stuff, week_o_stuff, week_a_stuff = [], [], []
742-
findings_this_period(reporter_findings, 0, week_stuff, week_o_stuff, week_a_stuff)
743+
weekly_total_series, weekly_open_series, weekly_accepted_series = [], [], []
744+
findings_this_period(reporter_findings, 0, weekly_total_series, weekly_open_series, weekly_accepted_series)
743745

744746
ras_owner_qs = Risk_Acceptance.objects.filter(owner=user)
745-
_augment_series_with_accepted(a_stuff, ras_owner_qs, period="month", tz=tz)
746-
_augment_series_with_accepted(week_a_stuff, ras_owner_qs, period="week", tz=tz)
747+
_augment_series_with_accepted(monthly_accepted_series, ras_owner_qs, period="month", tz=tz)
748+
_augment_series_with_accepted(weekly_accepted_series, ras_owner_qs, period="week", tz=tz)
747749

748-
chart_data = [["Date", "S0", "S1", "S2", "S3", "Total"], *o_stuff]
749-
a_chart_data = [["Date", "S0", "S1", "S2", "S3", "Total"], *a_stuff]
750-
week_chart_data = [["Date", "S0", "S1", "S2", "S3", "Total"], *week_o_stuff]
751-
week_a_chart_data = [["Date", "S0", "S1", "S2", "S3", "Total"], *week_a_stuff]
750+
chart_data = [["Date", "S0", "S1", "S2", "S3", "Total"], *monthly_open_series]
751+
a_chart_data = [["Date", "S0", "S1", "S2", "S3", "Total"], *monthly_accepted_series]
752+
week_chart_data = [["Date", "S0", "S1", "S2", "S3", "Total"], *weekly_open_series]
753+
week_a_chart_data = [["Date", "S0", "S1", "S2", "S3", "Total"], *weekly_accepted_series]
752754

753755
# --------------
754756
# Product tables
@@ -806,12 +808,12 @@ def view_engineer(request, eid):
806808
"high_c_month": closed_count["high"],
807809
"critical_c_month": closed_count["crit"],
808810
# week
809-
"week_stuff": week_stuff,
810-
"week_a_stuff": week_a_stuff,
811+
"week_stuff": weekly_total_series,
812+
"week_a_stuff": weekly_accepted_series,
811813
# series
812-
"a_total": a_stuff,
813-
"total": stuff,
814-
"sub": len(stuff),
814+
"a_total": monthly_accepted_series,
815+
"total": monthly_total_series,
816+
"sub": len(monthly_total_series),
815817
# product tables
816818
"update": update,
817819
"total_update": total_update,
@@ -872,15 +874,19 @@ def _augment_series_with_accepted(series: list[list], ras_qs, *, period: str, tz
872874
for bucket in series:
873875
if period == "month":
874876
start = datetime.strptime(bucket[0].strip(), "%b %Y").replace(tzinfo=tz)
875-
end = start.replace(day=monthrange(start.year, start.month)[1])
877+
end = start + relativedelta(months=1) # first day of next month (exclusive)
876878
else: # "week"
877-
wk_a, wk_b = (d.strip() for d in bucket[0].split("-"))
879+
wk_a, _ = (d.strip() for d in bucket[0].split("-"))
878880
year = timezone.now().year
879881
start = datetime.strptime(f"{wk_a} {year}", "%b %d %Y").replace(tzinfo=tz)
880-
end = datetime.strptime(f"{wk_b} {year}", "%b %d %Y").replace(tzinfo=tz)
882+
end = start + timedelta(days=7) # next Monday 00:00 (exclusive)
881883

882884
accepted = (
883-
Finding.objects.filter(risk_acceptance__owner=owner, risk_acceptance__created__range=[start, end])
885+
Finding.objects.filter(
886+
risk_acceptance__owner=owner,
887+
risk_acceptance__created__gte=start,
888+
risk_acceptance__created__lt=end,
889+
)
884890
.values("severity")
885891
.annotate(cnt=Count("id"))
886892
)
@@ -911,8 +917,10 @@ def _product_stats(products) -> tuple[list, list]:
911917
by_id = {c["pid"]: c for c in counts}
912918
top10 = sorted(by_id.items(), key=lambda kv: kv[1]["total"], reverse=True)[:10]
913919

920+
product_lookup = {p.id: p for p in products}
921+
914922
def row(prod_id):
915-
prod = next(p for p in products if p.id == prod_id)
923+
prod = product_lookup[prod_id]
916924
link = f"<a href='{reverse('product_open_findings', args=(prod.id,))}'>{escape(prod.name)}</a>"
917925
data = by_id[prod_id]
918926
return [link, data["critical"], data["high"], data["medium"], data["low"], data["total"]]

0 commit comments

Comments
 (0)