-
Notifications
You must be signed in to change notification settings - Fork 6
/
test.py
233 lines (206 loc) · 8.53 KB
/
test.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
"""Main module to combine functions into complete services"""
from db import *
from time_api import *
from note import *
from plot_func import *
import argparse
def update_db():
"""Insert last two days' entries in aTimeLogger into database."""
mysql_switch(1)
auth_header = get_auth_header()
new_entries = get_new_intervals(auth_header)
insert_intervals(new_entries)
mysql_switch(0)
print "Update complete!"
def rebuild_table(table):
"""
Empty all entries in passed table and reinsert current data into table.
This function is used when history or types are edited.
:param table: A string of table name in database, 'types' or 'intervals'
"""
empty_db(table)
mysql_switch(1)
auth_header = get_auth_header()
echo = 'Please correct your table name!'
if table == 'types':
types = get_types(auth_header)
insert_types(types)
echo = "Rebuild types complete!"
elif table == 'intervals':
intervals = get_all_intervals(auth_header)
insert_types(intervals)
echo = "Rebuild intervals complete!"
mysql_switch(0)
print echo
def rebuild_db(op='truncate'):
"""
Rebuild the whole database.
:param op: the option when empty the database, 'truncate' (default) or 'drop'
"""
mysql_switch(1)
if op == 'truncate':
empty_db()
else:
empty_db(op=op)
create_all_tables()
auth_header = get_auth_header()
types = get_types(auth_header)
intervals = get_all_intervals(auth_header)
insert_all(types, intervals)
mysql_switch(0)
print "Rebuild database complete!"
def daily_report(date=None):
"""
Generate daily report in Evernote.
Report content: 1.Date info, 2.Sleep table, 3.Group pie chart, 4.Task table.
:param date: A string of date in the format 'YYYYMMDD', default None for yesterday.
"""
mysql_switch(1)
if date:
start, end = str2level_range(date, 0)
date_info = day_info(date)
else:
# TODO Replace human_qr with str2level_range
start, end = human_qr('last 1 days')
date_info = day_info()
title = ts2str_level(start, 0)
tags = date_tag(start, 0)
sleep_table_plot(sleep_compare(date))
last_cut = get_cut_level_dataframe(start, end, 0)
group_pie_plot(last_cut)
task_data = get_task_table(last_cut)
dev_token, note_store = connect_note()
# in case of early date with zero task
if task_data.shape[0] != 0:
task_table_plot(task_data)
resources = create_resources(['img/sleep_table.png', 'img/group_pie.png', 'img/task_table.png'])
headings = ['1. Good Morning!', '2. What\'s up?', '3. How are things going?']
widths = [500, None, None]
else:
resources = create_resources(['img/sleep_table.png', 'img/group_pie.png'])
headings = ['1. Good Morning!', '2. What\'s up?']
widths = [500, None]
# create_daily_note(dev_token, note_store, title, date_info, resources, headings, widths)
create_note(dev_token, note_store, 0, title, resources, headings, widths, tags, date_info)
mysql_switch(0)
print "Generate daily report for {0}!".format(title)
def weekly_report(week=None):
"""
Generate weekly report in Evernote.
Report content: 1.Group pie chart, 2.Type table, 3.Group stacked bar chart, 4. Type grid chart, 5. Sleep plot.
:param week: A string of week number in the format 'YYYYWww', e.g '2015W07'. Default None for last week.
"""
mysql_switch(1)
if week:
start, end = str2level_range(week, 1)
else:
now = arrow.get(datetime.now(), 'US/Eastern')
start, end = str2level_range(now.strftime('%YW%V'), 1)
start_date = ts2datetime(start).strftime('%b %d')
end_date = ts2datetime(end-1).strftime('%b %d')
title = ts2datetime(start).strftime("%Y Week%V ({0} - {1})".format(start_date, end_date))
tags = date_tag(start, 1)
cut_data = get_cut_dataframe(start, end)
group_pie_plot(cut_data)
type_data = get_type_detail(cut_data)
type_table_plot(type_data)
agg_data_group = agg_level(start, end, 'group', 0)
group_barh_plot(agg_data_group, 0)
agg_data_type = agg_level(start, end, 'type', 0)
type_bar_grid_plot(agg_data_type, 0)
sleep_data = get_sleep_dataframe(start, end)
sleep_plot(sleep_data)
dev_token, note_store = connect_note()
resources = create_resources(['img/group_pie.png', 'img/type_table.png', 'img/group_bar.png',
'img/type_bar_grid.png', 'img/sleep_plot.png'])
headings = ['1. Group Overview', '2. Type Detail', '3. Group Trends', '4. Type Trends', '5. Sleep Trends']
widths = [None] * 5
# create_weekly_note(dev_token, note_store, title, resources, headings, widths)
create_note(dev_token, note_store, 1, title, resources, headings, widths, tags)
mysql_switch(0)
print "Generate weekly report for {0}!".format(title)
def monthly_report(month=None):
"""
Generate monthly report in Evernote.
Report content: 1.Group pie chart, 2.Type table, 3.Group stacked bar chart, 4. Type grid chart, 5. Sleep plot.
:param month: A string of month number in the format 'YYYYMmm', e.g '2015M02'. Default None for last month.
"""
mysql_switch(1)
if month:
start, end = str2level_range(month, 2)
else:
now = arrow.get(datetime.now(), 'US/Eastern')
start, end = str2level_range(now.strftime('%YM%m'), 2)
start_week = ts2datetime(start).strftime('%V')
end_week = ts2datetime(end-1).strftime('%V')
title = ts2datetime(start).strftime("%Y Month%m (W{0} - W{1})".format(start_week, end_week))
tags = date_tag(start, 2)
cut_data = get_cut_dataframe(start, end)
group_pie_plot(cut_data)
type_data = get_type_detail(cut_data)
type_table_plot(type_data)
agg_data_group = agg_level(start, end, 'group', 1)
group_barh_plot(agg_data_group, 1)
agg_data_type = agg_level(start, end, 'type', 1)
type_bar_grid_plot(agg_data_type, 1)
sleep_data = get_sleep_dataframe(start, end)
sleep_plot(sleep_data)
dev_token, note_store = connect_note()
resources = create_resources(['img/group_pie.png', 'img/type_table.png', 'img/group_bar.png',
'img/type_bar_grid.png', 'img/sleep_plot.png'])
headings = ['1. Group Overview', '3. Type Detail', '2. Group Trends', '4. Type Trends', '5. Sleep Trends']
widths = [None] * 5
create_note(dev_token, note_store, 2, title, resources, headings, widths, tags)
mysql_switch(0)
print "Generate monthly report for {0}!".format(title)
def gen_report(level=0, date=None):
"""
Report wrapper function.
:param level: An integer for different time levels. 0: daily (default), 1: weekly, 2: monthly.
:param date: A string to specify date/week/month.
"""
if level == 0:
daily_report(date)
elif level == 1:
weekly_report(date)
elif level == 2:
monthly_report(date)
def test_func():
"""Test function"""
mysql_switch(1)
# start, end = human_qr('last 1 week')
# cut_data = get_cut_dataframe(start, end)
# group_pie_plot(cut_data)
# day_cut_data = get_cut_day_dataframe(start, end)
# agg_data = agg_level(start, end, 'type', 0)
# types = get_type_order('Health').type
# agg_line_plot(agg_data, 'type', 0, lst=types, smooth=False)
# sleep_data = get_sleep_dataframe(start, end)
# sleep_plot(sleep_data)
# agg_data = agg_level(start, end, 'group', 0)
# group_barh_plot(agg_data, 0)
# agg_data = agg_level(start, end, 'type', 0)
# type_bar_grid_plot(agg_data, 0)
weekly_report("2015W48")
mysql_switch(0)
def main():
# Add argument to program for more flexible console control
parser = argparse.ArgumentParser(description='Report system actions.')
parser.add_argument("-o", "--operation", choices=['re', 'db'], default='re', help="choose operation (default: re)")
parser.add_argument("-u", "--update", type=int, choices=[0, 1], help="update or rebuild db")
parser.add_argument("-l", "--level", type=int, default=0, choices=[0, 1, 2],
help="choose the report level (default: 0)")
parser.add_argument("-d", "--date", help="Specify date/week/month for report. "
"Day: '19970215'; Week: '1999W05'; Year: '2010M02'")
args = parser.parse_args()
if args.operation == 're':
update_db()
gen_report(args.level, args.date)
else:
if args.update == 0:
update_db()
else:
rebuild_db()
if __name__ == '__main__':
main()
# test_func()