@@ -677,7 +677,6 @@ def engineer_metrics(request):
677
677
"""
678
678
679
679
680
- @cache_page (60 * 5 ) # cache for 5 minutes
681
680
@vary_on_cookie
682
681
def view_engineer (request , eid ):
683
682
user = get_object_or_404 (Dojo_User , pk = eid )
@@ -705,24 +704,27 @@ def view_engineer(request, eid):
705
704
# --------------------
706
705
# Month & week buckets
707
706
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 )
709
708
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 )
712
711
accepted_month = (
713
712
Finding .objects .filter (
714
713
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 ,
716
716
).distinct ()
717
717
)
718
718
719
719
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 )
722
723
accepted_week = (
723
724
Finding .objects .filter (
724
725
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 ,
726
728
).distinct ()
727
729
)
728
730
@@ -735,20 +737,20 @@ def view_engineer(request, eid):
735
737
736
738
# --------------------------
737
739
# 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 )
740
742
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 )
743
745
744
746
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 )
747
749
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 ]
752
754
753
755
# --------------
754
756
# Product tables
@@ -806,12 +808,12 @@ def view_engineer(request, eid):
806
808
"high_c_month" : closed_count ["high" ],
807
809
"critical_c_month" : closed_count ["crit" ],
808
810
# 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 ,
811
813
# 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 ),
815
817
# product tables
816
818
"update" : update ,
817
819
"total_update" : total_update ,
@@ -872,15 +874,19 @@ def _augment_series_with_accepted(series: list[list], ras_qs, *, period: str, tz
872
874
for bucket in series :
873
875
if period == "month" :
874
876
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 )
876
878
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 ("-" ))
878
880
year = timezone .now ().year
879
881
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 )
881
883
882
884
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
+ )
884
890
.values ("severity" )
885
891
.annotate (cnt = Count ("id" ))
886
892
)
@@ -911,8 +917,10 @@ def _product_stats(products) -> tuple[list, list]:
911
917
by_id = {c ["pid" ]: c for c in counts }
912
918
top10 = sorted (by_id .items (), key = lambda kv : kv [1 ]["total" ], reverse = True )[:10 ]
913
919
920
+ product_lookup = {p .id : p for p in products }
921
+
914
922
def row (prod_id ):
915
- prod = next ( p for p in products if p . id == prod_id )
923
+ prod = product_lookup [ prod_id ]
916
924
link = f"<a href='{ reverse ('product_open_findings' , args = (prod .id ,))} '>{ escape (prod .name )} </a>"
917
925
data = by_id [prod_id ]
918
926
return [link , data ["critical" ], data ["high" ], data ["medium" ], data ["low" ], data ["total" ]]
0 commit comments