mirror of
https://github.com/GothenburgBitFactory/timewarrior.git
synced 2025-06-26 10:54:28 +02:00
Make report 'totals.py' truncate intervals to search range
Closes #505 Signed-off-by: Thomas Lauf <thomas.lauf@tngtech.com>
This commit is contained in:
parent
6d826cb1d2
commit
49c33591b8
2 changed files with 25 additions and 23 deletions
|
@ -28,8 +28,8 @@
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
import json
|
import json
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from dateutil import tz
|
from dateutil import tz
|
||||||
|
|
||||||
DATEFORMAT = "%Y%m%dT%H%M%SZ"
|
DATEFORMAT = "%Y%m%dT%H%M%SZ"
|
||||||
|
@ -55,6 +55,7 @@ def calculate_totals(input_stream):
|
||||||
header = 1
|
header = 1
|
||||||
configuration = dict()
|
configuration = dict()
|
||||||
body = ""
|
body = ""
|
||||||
|
|
||||||
for line in input_stream:
|
for line in input_stream:
|
||||||
if header:
|
if header:
|
||||||
if line == "\n":
|
if line == "\n":
|
||||||
|
@ -68,17 +69,33 @@ def calculate_totals(input_stream):
|
||||||
else:
|
else:
|
||||||
body += line
|
body += line
|
||||||
|
|
||||||
|
if "temp.report.start" not in configuration:
|
||||||
|
return ["There is no data in the database"]
|
||||||
|
|
||||||
|
report_start_utc = datetime.datetime.strptime(configuration["temp.report.start"], DATEFORMAT)
|
||||||
|
report_start_utc = report_start_utc.replace(tzinfo=from_zone)
|
||||||
|
report_start = report_start_utc.astimezone(tz=to_zone)
|
||||||
|
|
||||||
|
if "temp.report.end" in configuration:
|
||||||
|
report_end_utc = datetime.datetime.strptime(configuration["temp.report.end"], DATEFORMAT)
|
||||||
|
report_end_utc = report_end_utc.replace(tzinfo=from_zone)
|
||||||
|
report_end = report_end_utc.astimezone(to_zone)
|
||||||
|
else:
|
||||||
|
report_end_utc = datetime.datetime.now(tz=from_zone)
|
||||||
|
report_end = report_end_utc.astimezone(tz=to_zone)
|
||||||
|
|
||||||
# Sum the seconds tracked by tag.
|
# Sum the seconds tracked by tag.
|
||||||
totals = dict()
|
totals = dict()
|
||||||
untagged = None
|
untagged = None
|
||||||
j = json.loads(body)
|
j = json.loads(body)
|
||||||
|
|
||||||
for object in j:
|
for object in j:
|
||||||
start = datetime.datetime.strptime(object["start"], DATEFORMAT)
|
start = max(report_start_utc, datetime.datetime.strptime(object["start"], DATEFORMAT).replace(tzinfo=from_zone))
|
||||||
|
|
||||||
if "end" in object:
|
if "end" in object:
|
||||||
end = datetime.datetime.strptime(object["end"], DATEFORMAT)
|
end = min(report_end_utc, datetime.datetime.strptime(object["end"], DATEFORMAT).replace(tzinfo=from_zone))
|
||||||
else:
|
else:
|
||||||
end = datetime.datetime.utcnow()
|
end = min(report_end_utc, datetime.datetime.now(tz=from_zone))
|
||||||
|
|
||||||
tracked = end - start
|
tracked = end - start
|
||||||
|
|
||||||
|
@ -100,27 +117,13 @@ def calculate_totals(input_stream):
|
||||||
if len(tag) > max_width:
|
if len(tag) > max_width:
|
||||||
max_width = len(tag)
|
max_width = len(tag)
|
||||||
|
|
||||||
if "temp.report.start" not in configuration:
|
|
||||||
return ["There is no data in the database"]
|
|
||||||
|
|
||||||
start_utc = datetime.datetime.strptime(configuration["temp.report.start"], DATEFORMAT)
|
|
||||||
start_utc = start_utc.replace(tzinfo=from_zone)
|
|
||||||
start = start_utc.astimezone(to_zone)
|
|
||||||
|
|
||||||
if "temp.report.end" in configuration:
|
|
||||||
end_utc = datetime.datetime.strptime(configuration["temp.report.end"], DATEFORMAT)
|
|
||||||
end_utc = end_utc.replace(tzinfo=from_zone)
|
|
||||||
end = end_utc.astimezone(to_zone)
|
|
||||||
else:
|
|
||||||
end = datetime.datetime.now()
|
|
||||||
|
|
||||||
if len(totals) == 0 and untagged is None:
|
if len(totals) == 0 and untagged is None:
|
||||||
return ["No data in the range {:%Y-%m-%d %H:%M:%S} - {:%Y-%m-%d %H:%M:%S}".format(start, end)]
|
return ["No data in the range {:%Y-%m-%d %H:%M:%S} - {:%Y-%m-%d %H:%M:%S}".format(report_start, report_end)]
|
||||||
|
|
||||||
# Compose report header.
|
# Compose report header.
|
||||||
output = [
|
output = [
|
||||||
"",
|
"",
|
||||||
"Total by Tag, for {:%Y-%m-%d %H:%M:%S} - {:%Y-%m-%d %H:%M:%S}".format(start, end),
|
"Total by Tag, for {:%Y-%m-%d %H:%M:%S} - {:%Y-%m-%d %H:%M:%S}".format(report_start, report_end),
|
||||||
""
|
""
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -28,9 +28,8 @@
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
import os
|
import os
|
||||||
import unittest
|
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
import unittest
|
||||||
|
|
||||||
# Ensure python finds the local simpletap module
|
# Ensure python finds the local simpletap module
|
||||||
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
||||||
|
@ -356,7 +355,7 @@ class TestTotals(TestCase):
|
||||||
'temp.report.start: {:%Y%m%dT%H%M%S}Z\n'.format(one_hour_before_utc),
|
'temp.report.start: {:%Y%m%dT%H%M%S}Z\n'.format(one_hour_before_utc),
|
||||||
'temp.report.end: {:%Y%m%dT%H%M%S}Z\n'.format(now_utc),
|
'temp.report.end: {:%Y%m%dT%H%M%S}Z\n'.format(now_utc),
|
||||||
'\n',
|
'\n',
|
||||||
'[{"start":"20160101T070000Z","end":"20160101T080000Z","tags":[]}]',
|
'[{{"start":"{:%Y%m%dT%H%M%S}Z","end":"{:%Y%m%dT%H%M%S}Z","tags":[]}}]'.format(one_hour_before_utc, now_utc)
|
||||||
]
|
]
|
||||||
|
|
||||||
out = calculate_totals(input_stream)
|
out = calculate_totals(input_stream)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue