-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdogtrack.py
executable file
·72 lines (57 loc) · 2.32 KB
/
dogtrack.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
#!/usr/bin/env python
import requests
import sqlite3
import time
from calendar import timegm
from datadog import initialize, api
import json
settings = json.load(open('/home/john/dogtrack/conf/settings.json'))
url = 'https://www.strava.com/api/v3/clubs/%s/activities' % settings['strava']['club_id']
headers = {'Authorization': 'Bearer %s' % settings['strava']['token']}
options = {
'api_key': settings['datadog']['api_key'],
'app_key': settings['datadog']['app_key']
}
initialize(**options)
r = requests.get(url, headers=headers)
conn = sqlite3.connect('/home/john/dogtrack/runlog.db')
c = conn.cursor()
for run in r.json():
activity_id = str(run['id'])
runner = run['athlete']['firstname'].lower()
distance = float(run['distance']) / 1609.34
time_start = time.strptime(run['start_date_local'], '%Y-%m-%dT%H:%M:%SZ')
timestamp = timegm(time_start)
c.execute('SELECT * FROM runs WHERE run_id=?', (activity_id,))
if c.fetchone() is None:
c.execute('INSERT INTO runs VALUES (?,?,?,?)',
(activity_id, runner, distance, timestamp,))
conn.commit()
else:
pass
seven_days = time.time() - 604800
c.execute('SELECT runner, sum(miles) FROM runs WHERE time > ? GROUP BY runner;',
(seven_days,))
seven_day_mileage = c.fetchall()
c.execute('SELECT runner, sum(miles) FROM runs WHERE time > 1451606400 GROUP BY runner;')
all_time_mileage = c.fetchall()
c.execute('SELECT runner, miles, time FROM runs WHERE time > 1451606400 ORDER BY miles DESC LIMIT 10;')
high_scores = c.fetchall()
conn.close()
for dog in seven_day_mileage:
host = str(dog[0])
point = round(dog[1], 3)
api.Metric.send(metric='dogtrack.running.seven_day_mileage', points=point,
host="runclub", tags=["runner:%s" % host])
for dog in all_time_mileage:
host = str(dog[0])
point = round(dog[1], 3)
api.Metric.send(metric='dogtrack.running.alltime_mileage', points=point,
host="runclub", tags=["runner:%s" % host])
for dog in high_scores:
host = str(dog[0])
point = round(dog[1], 3)
run_date_struct = time.gmtime(dog[2])
run_date_readable = "%s-%s-%s" % (run_date_struct[0], run_date_struct[1], run_date_struct[2])
api.Metric.send(metric='dogtrack.running.high_scores', points=point,
host="runclub", tags=["high_score_mark:%s-%s" % (host, run_date_readable)])