1- from collections import Counter , defaultdict
2- from dataclasses import dataclass
1+ from dataclasses import dataclass , field
32from typing import Dict
43
54from git_analytics .entities import AnalyticsCommit , AnalyticsResult
87
98@dataclass
109class Result (AnalyticsResult ):
11- hour_of_day : Dict [int , int ]
12- day_of_week : Dict [str , int ]
13- day_of_month : Dict [int , int ]
10+ hour_of_day : Dict [int , Dict [str , int ]] = field (default_factory = lambda : {h : {} for h in range (24 )})
11+ day_of_week : Dict [str , Dict [str , int ]] = field (
12+ default_factory = lambda : {d : {} for d in [_get_day_name (i ) for i in range (7 )]}
13+ )
14+ day_of_month : Dict [int , Dict [str , int ]] = field (default_factory = lambda : {d : {} for d in range (1 , 32 )})
1415
1516
16- def get_day_name (day_index : int ) -> str :
17+ def _get_day_name (day_index : int ) -> str :
1718 days = [
1819 "Monday" ,
1920 "Tuesday" ,
@@ -30,31 +31,18 @@ class HistoricalStatisticsAnalyzer(CommitAnalyzer):
3031 name = "historical_statistics"
3132
3233 def __init__ (self ) -> None :
33- self ._dict_hour_of_day = defaultdict (Counter )
34- self ._dict_day_of_week = defaultdict (Counter )
35- self ._dict_day_of_month = defaultdict (Counter )
34+ self ._result = Result ()
3635
3736 def process (self , commit : AnalyticsCommit ) -> None :
38- c = commit
39- self ._dict_day_of_week [c .committed_datetime .weekday ()][c .commit_author ] += 1
40- self ._dict_hour_of_day [c .committed_datetime .hour ][c .commit_author ] += 1
41- self ._dict_day_of_month [c .committed_datetime .day ][c .commit_author ] += 1
37+ author = commit .commit_author
38+ hour = commit .committed_datetime .hour
39+ day = commit .committed_datetime .weekday ()
40+ month_day = commit .committed_datetime .day
41+
42+ self ._result .hour_of_day [hour ][author ] = self ._result .hour_of_day [hour ].get (author , 0 ) + 1
43+ day_name = _get_day_name (day )
44+ self ._result .day_of_week [day_name ][author ] = self ._result .day_of_week [day_name ].get (author , 0 ) + 1
45+ self ._result .day_of_month [month_day ][author ] = self ._result .day_of_month [month_day ].get (author , 0 ) + 1
4246
4347 def result (self ) -> Result :
44- hour_of_day = {hour : 0 for hour in range (24 )}
45- for hour , authors in self ._dict_hour_of_day .items ():
46- hour_of_day [hour ] = dict (authors )
47-
48- day_of_week = {get_day_name (day ): 0 for day in range (7 )}
49- for day , authors in self ._dict_day_of_week .items ():
50- day_of_week [get_day_name (day )] = dict (authors )
51-
52- day_of_month = {day : 0 for day in range (1 , 32 )}
53- for day , authors in self ._dict_day_of_month .items ():
54- day_of_month [day ] = dict (authors )
55-
56- return Result (
57- hour_of_day = hour_of_day ,
58- day_of_week = day_of_week ,
59- day_of_month = day_of_month ,
60- )
48+ return self ._result
0 commit comments