def calculate_report(lines, date_from, date_to): """Creates and returns report dictionaries Report dicts have form like this: {<Project>: {<log_message>: <accumulative time>}, {<log_message1>: <accumulative time1>}} """ work_dict = defaultdict(lambda: defaultdict(dict)) slack_dict = defaultdict(lambda: defaultdict(dict)) line_begins = date_begins(lines, date_from) line_ends = date_ends(lines, date_to) date_not_found = (line_begins is None or line_ends < line_begins) if date_not_found: return work_dict, slack_dict data = parse_lines() for i, line in enumerate(data[line_begins:line_ends+1]): # if we got to the last line - stop if line_begins+i+1 > line_ends: break next_line = data[line_begins+i+1] line_date = line.date next_line_date = next_line.date # if it's day switch, skip this cycle if line_date != next_line_date: continue time_diff = calc_time_diff(line, next_line) project = strip_log(next_line.project) log = strip_log(next_line.log) if next_line.is_slack: # if log message is identical add time_diff # to total time of the log if slack_dict[project][log]: total_time = slack_dict[project][log] total_time += time_diff slack_dict[project][log] = total_time else: slack_dict[project][log] = time_diff else: if work_dict[project][log]: total_time = work_dict[project][log] total_time += time_diff work_dict[project][log] = total_time else: work_dict[project][log] = time_diff return work_dict, slack_dict
def calculate_stats(lines, date_from, date_to, today=False): work_time = [] slack_time = [] today_work_time = None line_begins = date_begins(lines, date_from) line_ends = date_ends(lines, date_to) date_not_found = (line_begins is None or line_ends < line_begins) if date_not_found: return work_time, slack_time, today_work_time data = parse_lines() for i, line in enumerate(data[line_begins:line_ends+1]): # if we got to the last line - stop if line_begins+i+1 > line_ends: break next_line = data[line_begins+i+1] line_date = line.date next_line_date = next_line.date # if it's day switch, skip this cycle if line_date != next_line_date: continue if next_line.is_slack: slack_time.append(calc_time_diff(line, next_line)) else: work_time.append(calc_time_diff(line, next_line)) if today: today_start_time = dt.strptime( "{} {}".format(data[line_begins].date, data[line_begins].time), DATETIME_FORMAT ) today_work_time = (dt.now() - today_start_time).seconds return work_time, slack_time, today_work_time