forked from onejgordon/flow-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
153 lines (135 loc) · 5.68 KB
/
tasks.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import logging
from models import User, TrackingDay, HabitDay
import handlers
from google.appengine.ext import ndb
from datetime import datetime, timedelta, time
import tools
class WarmupHandler(handlers.BaseRequestHandler):
def get(self):
logging.info("Warmup Request")
def post(self):
logging.info("Warmup Request")
class SyncReadables(handlers.BaseRequestHandler):
def get(self):
from services import pocket, goodreads
logging.debug("Running SyncReadables cron...")
TS_KEY = 'pocket_last_timestamp'
users = User.SyncActive(['pocket', 'goodreads'])
user_put = []
for user in users:
# Pocket
user_changes = False
access_token = user.get_integration_prop('pocket_access_token')
if access_token:
success, readables, latest_timestamp = pocket.sync(user, access_token)
logging.debug("Got %d readables from pocket" % len(readables))
user.set_integration_prop(TS_KEY, latest_timestamp)
user_changes = True
success, readables = goodreads.get_books_on_shelf(user, shelf='currently-reading')
logging.debug("Got %d readables from good reads" % len(readables))
if user_changes:
user_put.append(user)
ndb.put_multi(user_put)
class SyncGithub(handlers.BaseRequestHandler):
GH_COMMIT_OVERLAP = 3 # Days back to capture late-pushed commits
def get(self):
from services.github import GithubClient
last_date = self.request.get('date')
if last_date:
last_date = tools.fromISODate(last_date)
else:
last_date = (datetime.today() - timedelta(days=1))
users = User.SyncActive('github')
res = {}
td_put = []
for user in users:
gh_client = GithubClient(user)
if gh_client._can_run():
date_range = [(last_date - timedelta(days=x)).date() for x in range(self.GH_COMMIT_OVERLAP)]
logging.debug("Running SyncGithub cron for %s on %s..." % (user, date_range))
commits_dict = gh_client.get_contributions_on_date_range(date_range)
if commits_dict is not None:
for date, n_commits in commits_dict.items():
td = TrackingDay.Create(user, date)
td.set_properties({
'commits': n_commits
})
td_put.append(td)
else:
logging.debug("Github updater can't run")
if td_put:
ndb.put_multi(td_put)
self.json_out(res)
class SyncFromGoogleFit(handlers.BaseRequestHandler):
def get(self):
from services.gfit import FitClient
users = User.SyncActive('gfit')
res = {}
date = (datetime.today() - timedelta(days=1)).date()
for user in users:
fit_enabled = bool(user.get_integration_prop('gfit_activities'))
logging.debug("Running SyncFromGoogleFit cron for %s on %s..." % (user, date))
if fit_enabled:
fit_client = FitClient(user)
if fit_client:
var_durations = fit_client.aggregate_activity_durations(date)
logging.debug(var_durations)
if var_durations:
td = TrackingDay.Create(user, date)
td.set_properties(var_durations)
td.put()
else:
logging.debug("Fit not authorized")
self.json_out(res, debug=True)
class PushToBigQuery(handlers.BaseRequestHandler):
def get(self):
from services.flow_bigquery import BigQueryClient
days_ago = self.request.get_range('days_ago', default=8)
days_ago_end = self.request.get_range('days_ago_end', default=1)
users = User.SyncActive('bigquery')
res = {}
date = (datetime.today() - timedelta(days=1)).date()
for user in users:
enabled = bool(user.get_integration_prop('bigquery_dataset_name')) and \
bool(user.get_integration_prop('bigquery_table_name'))
if enabled:
logging.debug("Running PushToBigQuery cron for %s on %s..." % (user, date))
bq_client = BigQueryClient(user, days_ago=days_ago, days_ago_end=days_ago_end)
if bq_client:
bq_client.run()
else:
logging.debug("BigQuery not enabled")
self.json_out(res, debug=True)
class DeleteOldReports(handlers.BaseRequestHandler):
def get(self):
from models import Report
cutoff = datetime.now() - timedelta(days=30)
old_reports = Report.query().filter(Report.dt_created < cutoff).fetch(limit=None)
n = 0
if old_reports:
for report in old_reports:
try:
report.clean_delete(self_delete=False)
except Exception, e:
logging.info(str(e))
n = len(old_reports)
ndb.delete_multi([dr.key for dr in old_reports])
logging.debug("Deleted %d old reports" % n)
def backgroundReportRun(rkey, start_cursor=None):
rkey = ndb.Key(urlsafe=rkey)
r = rkey.get()
if r:
r.run(start_cursor=start_cursor)
def backgroundHabitDayDeletion(hkey, start_cursor=None):
hkey = ndb.Key(urlsafe=hkey)
done = False
maxit = 50
its = 0
while not done and its < maxit:
hds = HabitDay.All(hkey)
if hds:
ndb.delete_multi(hds)
logging.debug("Deleted %d habit days from hkey=%s" % (len(hds), hkey))
else:
done = True
its += 1