@@ -668,7 +668,6 @@ def engineer_metrics(request):
668
668
"""
669
669
670
670
671
- @cache_page (60 * 5 ) # cache for 5 minutes
672
671
@vary_on_cookie
673
672
def view_engineer (request , eid ):
674
673
user = get_object_or_404 (Dojo_User , pk = eid )
@@ -696,24 +695,27 @@ def view_engineer(request, eid):
696
695
# --------------------
697
696
# Month & week buckets
698
697
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 )
700
699
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 )
703
702
accepted_month = (
704
703
Finding .objects .filter (
705
704
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 ,
707
707
).distinct ()
708
708
)
709
709
710
710
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 )
713
714
accepted_week = (
714
715
Finding .objects .filter (
715
716
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 ,
717
719
).distinct ()
718
720
)
719
721
@@ -726,20 +728,20 @@ def view_engineer(request, eid):
726
728
727
729
# --------------------------
728
730
# 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 )
731
733
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 )
734
736
735
737
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 )
738
740
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 ]
743
745
744
746
# --------------
745
747
# Product tables
@@ -797,12 +799,12 @@ def view_engineer(request, eid):
797
799
"high_c_month" : closed_count ["high" ],
798
800
"critical_c_month" : closed_count ["crit" ],
799
801
# 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 ,
802
804
# 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 ),
806
808
# product tables
807
809
"update" : update ,
808
810
"total_update" : total_update ,
@@ -863,15 +865,19 @@ def _augment_series_with_accepted(series: list[list], ras_qs, *, period: str, tz
863
865
for bucket in series :
864
866
if period == "month" :
865
867
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 )
867
869
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 ("-" ))
869
871
year = timezone .now ().year
870
872
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 )
872
874
873
875
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
+ )
875
881
.values ("severity" )
876
882
.annotate (cnt = Count ("id" ))
877
883
)
@@ -902,8 +908,10 @@ def _product_stats(products) -> tuple[list, list]:
902
908
by_id = {c ["pid" ]: c for c in counts }
903
909
top10 = sorted (by_id .items (), key = lambda kv : kv [1 ]["total" ], reverse = True )[:10 ]
904
910
911
+ product_lookup = {p .id : p for p in products }
912
+
905
913
def row (prod_id ):
906
- prod = next ( p for p in products if p . id == prod_id )
914
+ prod = product_lookup [ prod_id ]
907
915
link = f"<a href='{ reverse ('product_open_findings' , args = (prod .id ,))} '>{ escape (prod .name )} </a>"
908
916
data = by_id [prod_id ]
909
917
return [link , data ["critical" ], data ["high" ], data ["medium" ], data ["low" ], data ["total" ]]
0 commit comments