diff options
Diffstat (limited to 'overlays/worktime')
-rwxr-xr-x | overlays/worktime/worktime/__main__.py | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/overlays/worktime/worktime/__main__.py b/overlays/worktime/worktime/__main__.py index 2caf7604..35aef8f7 100755 --- a/overlays/worktime/worktime/__main__.py +++ b/overlays/worktime/worktime/__main__.py | |||
@@ -34,12 +34,9 @@ from pathlib import Path | |||
34 | 34 | ||
35 | from collections import defaultdict | 35 | from collections import defaultdict |
36 | 36 | ||
37 | import shelve | ||
38 | import jsonpickle | 37 | import jsonpickle |
39 | from hashlib import blake2s | 38 | from hashlib import blake2s |
40 | 39 | ||
41 | shelve_d = shelve.open(str(Path(BaseDirectory.save_cache_path('worktime')) / 'entry_durations')) | ||
42 | |||
43 | class TogglAPISection(Enum): | 40 | class TogglAPISection(Enum): |
44 | TOGGL = '/api/v8' | 41 | TOGGL = '/api/v8' |
45 | REPORTS = '/reports/api/v2' | 42 | REPORTS = '/reports/api/v2' |
@@ -99,6 +96,7 @@ class TogglAPI(object): | |||
99 | if client_ids is not None and not client_ids: | 96 | if client_ids is not None and not client_ids: |
100 | return | 97 | return |
101 | 98 | ||
99 | cache_dir = Path(BaseDirectory.save_cache_path('worktime')) / 'entry_durations' | ||
102 | step = timedelta(days = 120) | 100 | step = timedelta(days = 120) |
103 | for req_start in (start_date + step * i for i in count()): | 101 | for req_start in (start_date + step * i for i in count()): |
104 | req_start = min(req_start, end_date) | 102 | req_start = min(req_start, end_date) |
@@ -110,7 +108,7 @@ class TogglAPI(object): | |||
110 | # elif req_start > start_date: | 108 | # elif req_start > start_date: |
111 | # req_start = datetime.combine(req_start.astimezone(timezone.utc).date(), time(tzinfo=timezone.utc)) + timedelta(days = 1) | 109 | # req_start = datetime.combine(req_start.astimezone(timezone.utc).date(), time(tzinfo=timezone.utc)) + timedelta(days = 1) |
112 | 110 | ||
113 | cache_key = None | 111 | cache_path = None |
114 | if req_end + timedelta(days=60) < datetime.now().astimezone(timezone.utc): | 112 | if req_end + timedelta(days=60) < datetime.now().astimezone(timezone.utc): |
115 | cache_key = blake2s(jsonpickle.encode({ | 113 | cache_key = blake2s(jsonpickle.encode({ |
116 | 'start': req_start, | 114 | 'start': req_start, |
@@ -120,9 +118,13 @@ class TogglAPI(object): | |||
120 | 'workspace': self._workspace_id, | 118 | 'workspace': self._workspace_id, |
121 | 'workspace_clients': self._client_ids | 119 | 'workspace_clients': self._client_ids |
122 | }).encode('utf-8'), key = self._api_token.encode('utf-8')).hexdigest() | 120 | }).encode('utf-8'), key = self._api_token.encode('utf-8')).hexdigest() |
123 | if cache_key in shelve_d: | 121 | cache_path = cache_dir / cache_key[:2] / cache_key[2:4] / f'{cache_key[4:]}.json' |
124 | yield from shelve_d[cache_key] | 122 | try: |
125 | continue | 123 | with cache_path.open('r', encoding='utf-8') as ch: |
124 | yield from jsonpickle.decode(ch.read()) | ||
125 | continue | ||
126 | except FileNotFoundError: | ||
127 | pass | ||
126 | 128 | ||
127 | entries = list() | 129 | entries = list() |
128 | params = { 'since': (req_start - timedelta(days=1)).astimezone(timezone.utc).isoformat(), | 130 | params = { 'since': (req_start - timedelta(days=1)).astimezone(timezone.utc).isoformat(), |
@@ -152,8 +154,10 @@ class TogglAPI(object): | |||
152 | if not report['data']: | 154 | if not report['data']: |
153 | break | 155 | break |
154 | 156 | ||
155 | if cache_key: | 157 | if cache_path: |
156 | shelve_d[cache_key] = entries | 158 | cache_path.parent.mkdir(parents=True, exist_ok=True) |
159 | with cache_path.open('w', encoding='utf-8') as ch: | ||
160 | ch.write(jsonpickle.encode(entries)) | ||
157 | # res = timedelta(milliseconds=report['total_billable']) if report['total_billable'] else timedelta(milliseconds=0) | 161 | # res = timedelta(milliseconds=report['total_billable']) if report['total_billable'] else timedelta(milliseconds=0) |
158 | # return res | 162 | # return res |
159 | 163 | ||