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
|
#!/usr/bin/python
# - coding: utf-8 -
# Copyright © 2012 Edward Smith
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys, os
import argparse
import matplotlib.pyplot as plt
from datetime import date, timedelta, datetime
modules_dir = "/home/tedks/Projects/metrics/workdir/metrics"
import metrics
from metrics.common import parse_date, get_day
defaults = {
'hamster_productivity_index':
{'call' : lambda m: m.metric('today', 'school,work,projects', 'waste'),
'module' : 'hamster_day_by_categories',
'args' : ['school,work,projects', 'waste']},
'hamster_social_index':
{'call' : lambda m: m.metric('today', 'social', 'neutral'),
'module' : 'hamster_day_by_categories',
'args': ['social']},
'hamster_sleep_index':
{'call' : lambda m: max([m.metric('today', 'sleep', 'neutral'),
m.metric('yesterday', 'sleep', 'neutral')]),
'module' : 'hamster_day_by_categories',
'args': ['sleep']},
'habittrack_global_hitrate':
{'call' : lambda m: m.metric('today'),
'module' : 'habittrack_hitrate'},
'habits_complete':
{'call' : lambda m: m.metric('today'),
'module' : 'habittrack_open'}
}
def get_modules(path):
if not path in sys.path:
sys.path.append(path)
modlist = [__import__(e.rstrip("py")) for e in
os.listdir(args.modules) if e.endswith(".py")]
modules = {}
for m in modlist:
modules[m.__name__] = m
return modules
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--modules', '-m', type=str, default=modules_dir,
help='Path to the metrics modules')
parser.add_argument('--metrics', '-M', type=str, default=None,
help='Comma-seperated list of metrics to display results for')
parser.add_argument('--graph', '-g', action='store_true', default=False,
help='Display a graph of the habits from the given date to today')
parser.add_argument('--date', '-d', default='today',
help='The day to start a graph, display metrics from.')
args = parser.parse_args()
startdate = parse_date(args.date)
modules = get_modules(args.modules)
if args.metrics:
metrics = args.metrics.split(',')
else:
metrics = None
# initialize some things we know are constant now
namecol_length = max(len, defaults.keys())
today = get_day()
if args.graph:
if startdate == today:
startdate -= timedelta(7) # show at least a week by default
x = []
for i in range(0, (today - startdate).days):
x.append(startdate + timedelta(i))
for (name, conf) in defaults.items():
if metrics and not name in metrics:
continue
name = name.replace("_", " ").title()
if args.graph:
y = []
modargs = [None] # create a 1-element list
if 'args' in conf:
modargs.extend(conf['args'])
for d in x:
modargs[0] = d
y.append(modules[conf['module']].metric(*modargs))
plt.plot_date(x, y, '-', xdate=True, label=name)
else:
val = conf['call'](modules[conf['module']])
print "%s: %.4f" % (name, val)
if args.graph:
flatline = map(lambda _:0, x)
plt.plot(x, flatline, 'k-')
plt.legend()
plt.show()
|