Add summary of time spent of tasks in report view.

This commit is contained in:
Faerbit 2020-03-02 19:43:55 +01:00
parent c6d252d57e
commit 6f3b74099e
2 changed files with 25 additions and 14 deletions

29
data.py
View File

@ -159,6 +159,7 @@ class Report:
def __init__(self, data, pdate):
self._data = data
self._date = pdate
self._actual_data_len = 0
def report(self):
tmp = []
@ -173,14 +174,18 @@ class Report:
tmp.append(("End", datetime.now()))
ret = []
dsum = timedelta()
tasks_sums = {}
total_sum = timedelta()
for i, t in enumerate(tmp):
task, start_time = t
if i < len(tmp) - 1:
end_time = tmp[i+1][1]
duration = end_time - start_time
if task != "Pause":
dsum += duration
task_sum = tasks_sums.setdefault(task, timedelta())
task_sum += duration
tasks_sums[task] = task_sum
total_sum += duration
dhours, rem = divmod(duration.seconds, 3600)
dmins, _ = divmod(rem, 60)
ret.append([task, start_time.strftime("%H:%M"), f"{dhours:02d}:{dmins:02d}"])
@ -188,16 +193,22 @@ class Report:
ret.append([task, start_time.strftime("%H:%M"), ""])
ret.append(["", "", ""])
dhours, rem = divmod(dsum.seconds, 3600)
ret.append(["", "Sums", ""])
for k, v in tasks_sums.items():
dhours, rem = divmod(v.seconds, 3600)
dmins, _ = divmod(rem, 60)
ret.append(["Sum", "", f"{dhours:02d}:{dmins:02d}"])
return ret
ret.append([k, "", f"{dhours:02d}:{dmins:02d}"])
dhours, rem = divmod(total_sum.seconds, 3600)
dmins, _ = divmod(rem, 60)
ret.append(["Total sum", "", f"{dhours:02d}:{dmins:02d}"])
edit_len = len(ret) - (4 + len(tasks_sums))
self._actual_data_len = edit_len
if self._date != date.today():
self._actual_data_len += 1
return ret, edit_len
def save(self, report):
if self._date == date.today():
report = report[:-3] # cut off sum display and end time
else:
report = report[:-2] # cut off sum display
report = report[:self._actual_data_len]
if not report:
return
save_list = []

View File

@ -72,7 +72,7 @@ class Report(QtWidgets.QDialog):
self.setWindowTitle(f"Report {self._report.date()}")
def refresh_table(self):
self._report_data = self._report.report()
self._report_data, edit_len = self._report.report()
self.tableWidget.setRowCount(len(self._report_data))
self._changing_items = True
@ -84,10 +84,10 @@ class Report(QtWidgets.QDialog):
item2 = QtWidgets.QTableWidgetItem(self._report_data[row][2])
self.tableWidget.setItem(row, 2, item2)
item2.setFlags(item2.flags() & QtCore.Qt.ItemIsEnabled)
if row > len(self._report_data) - 3:
item1.setFlags(item1.flags() & QtCore.Qt.ItemIsEnabled)
if row > len(self._report_data) - 4:
item0.setFlags(item0.flags() & QtCore.Qt.ItemIsEnabled)
if row >= edit_len:
item0.setFlags(item1.flags() & QtCore.Qt.ItemIsEnabled)
if row > edit_len:
item1.setFlags(item0.flags() & QtCore.Qt.ItemIsEnabled)
self._changing_items = False
self.tableWidget.resizeColumnsToContents()