-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreports.py
111 lines (87 loc) · 4.94 KB
/
reports.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from typing import Tuple
from datetime import datetime, date
from utility_functions import create_task_line
DATETIME_STRING_FORMAT = "%Y-%m-%d"
def create_title_str(file_name: str) -> str:
"""Creates the title string for the report."""
remaining_length = 70 - len(file_name)
if remaining_length % 2 == 1:
return f"{'='* round(remaining_length/2)}{file_name}{'='* (round(remaining_length/2) + 1)}"
else:
return f"{'='* round(remaining_length/2)}{file_name}{'='* round(remaining_length/2)}"
#===================TASK REPORT LOGIC===================
def generate_task_report(tasks: dict) -> None:
"""Creates a report on all tasks and writes to reports/task_overview.txt."""
completed_tasks, incomplete_tasks, overdue_tasks = calculate_task_stats(tasks)
# Create strings to write to file
str_line = f"{'*'*70}"
name_line = create_title_str("task_overview")
number_of_tasks = f"TASKS TOTAL - {len(tasks)}"
completed_tasks_str = create_task_str("completed", completed_tasks, tasks)
incomplete_tasks_str = create_task_str("incomplete", incomplete_tasks, tasks)
overdue_tasks_str = create_task_str("overdue", overdue_tasks, tasks)
text = f"{str_line}\n{name_line}\n{str_line}\n\n{number_of_tasks}\n\n{completed_tasks_str}\n\n{incomplete_tasks_str}\n\n{overdue_tasks_str}"
with open("reports/task_overview.txt", "w", encoding="UTF-8") as f:
f.write(text)
def calculate_task_stats(tasks: dict) -> Tuple[int, int]:
"""Loop through tasks and record the total number of completed tasks."""
current_date = date.today().strftime(DATETIME_STRING_FORMAT)
# Variables to count total of complete, incomplete and overdue tasks.
completed_tasks_total = 0
incomplete_tasks_total = 0
overdue_tasks_total = 0
# Check if tasks are complete, incomplete or overdue. Add to count.
for task_id in tasks:
if getattr(tasks[task_id], "completed"):
completed_tasks_total += 1
else:
if getattr(tasks[task_id], "due_date") < current_date:
overdue_tasks_total += 1
incomplete_tasks_total += 1
return completed_tasks_total, incomplete_tasks_total, overdue_tasks_total
def create_task_str(status: str, status_total: int, tasks: dict) -> str:
"""Build string for each type of task (complete, incomplete and overdue)."""
return f"{status} tasks - {status_total}\nPercentage {status} - {(status_total/len(tasks)*100)}%"
#===================USER REPORT LOGIC===================
def generate_user_report(tasks: dict, users: dict) -> None:
"""Creates a report on all users and writes to reports/user_overview.txt."""
str_line = f"{'*'*70}"
name_line = create_title_str("user_overview")
# Build a string for each user that include total tasks, complete, incomplete and overdue
users_str = ""
for user in users:
users_str += create_user_str(tasks, users, user)
total_users = f"Total users - {len(users)}"
text = f"{str_line}\n{name_line}\n{str_line}\n\n{total_users}\n\n{users_str}"
with open("reports/user_overview.txt", "w", encoding="UTF-8") as f:
f.write(text)
def create_user_str(tasks: dict, users: dict, user: str) -> str:
"""Creates the data for each user. Includes their total tasks, complete, incomplete and incomplete tasks."""
task_list = getattr(users[user], "tasks")
user_str = f"{create_title_str(user)}\nNumber of tasks assigned: {len(task_list)}\n"
user_str += f"As a percentage of total tasks: {round((len(task_list)/ len(tasks))*100)}%\n\n"
user_str += f"{create_user_task_str("complete", tasks, task_list)}\n"
user_str += f"{create_user_task_str("incomplete", tasks, task_list)}\n"
user_str += create_user_task_str("overdue", tasks, task_list)
return f"{user_str}\n"
def create_user_task_str(status: str, tasks: dict, task_list: list) -> str:
"""Builds the string for each type of task status. Includes count, percentage of total and title of all relevant tasks."""
current_date = date.today().strftime(DATETIME_STRING_FORMAT)
status_check = False
tasks_str = ""
if status == "complete":
status_check = True
task_count = 0
for task in tasks:
if status == "overdue":
if (task in task_list) and (getattr(tasks[task], "completed") == status_check) and (getattr(tasks[task], "due_date") < current_date):
task_count += 1
tasks_str += f"* {create_task_line(getattr(tasks[task], "title"), task, 68)}"
elif (task in task_list) and (getattr(tasks[task], "completed") == status_check):
task_count += 1
tasks_str += f"* {create_task_line(getattr(tasks[task],"title"), task, 68)}"
if task_count == 0:
status_str = create_task_line(f"{status} tasks: {task_count}", "0%")
else:
status_str = f"{status} tasks: {task_count}\nas a percentage all of {status} tasks: {round((task_count/len(tasks))* 100)}%\n"
return status_str + tasks_str