forked from jradavenport/batlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_log.py
268 lines (197 loc) · 7.56 KB
/
parse_log.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# coding: utf-8
import sys
sys.path.extend(['/Users/jstemm/anaconda/lib/python2.7/site-packages'])
import pandas as pd
import itertools
import datetime
import calendar
import numpy
# import sys
import os
start_day = datetime.datetime(2014, 10, 8, 0, 0)
def wk2num(dt):
"""take in a datetime object and return a number, 0-6.99 corresponding to day of week and time"""
# import numpy
wkd = dt.dayofweek
tme = (wkd * 288) + (dt.hour * 12) + (dt.minute / 5)
return tme
def sid_check(sid):
if len(sid) == 0:
return 'No WiFi'
home = ['DangerZone', 'Danger Zone', 'Office Zone']
work = ['University of Washington']
coffee = ['IssaquahCoffee', 'Allegro', 'Google Starbucks']
hotspot = ['JStemm']
if sid in home:
return 'Home'
elif sid in work:
return 'Work'
elif sid in coffee:
return 'Coffee Shop'
elif sid in hotspot:
return 'Mobile Hotspot'
else:
return 'Other'
topdir = os.path.join(os.getenv('HOME'), '.logstats')
dbdir = os.path.join(os.getenv('HOME'), 'Dropbox', 'workshare', 'batlog')
log = os.path.join(topdir, 'batlog.dat')
#log = os.path.join(topdir, 'example.data')
#D = pd.read_table(wake, names = ['dow', 'month', 'day', 'time', 'tz', 'year', 'user', 'SSID', 'runmode'],
# parse_dates = {'dtime': ['dow', 'month', 'day', 'time', 'year']},
# sep = ' ',
# index_col='dtime',
# header=None)
#D = D.fillna("WiFi Disconnected")
#H = D.groupby('SSID')
DATA = pd.DataFrame(columns=['Capacity', 'CycleCount', 'BatLife',
'CurrentPowerState', 'SSID', 'PCT',
'WiFi', 'Eth'])
with open(log) as f:
for a, b, c, d, e in itertools.izip_longest(*[f]*5):
# parse the date and time
A = a.split()
dt = A[1]+A[2].zfill(2)+A[3]+A[5]
dt = datetime.datetime.strptime(dt, '%b%d%H:%M:%S%Y')
# parse the nework info
sid = sid_check(b.split(' SSID ')[-1].split("\n")[0])
wifistat = b.split()[3]
C = c.split()
if len(C) < 2:
ethstat = "inactive"
elif len(C) >= 3:
ethstat = "active"
# parse the battery info
D = eval(d.split(" = ")[1].replace("=", ":"))
# parse the power state
if len(e) < 7:
E = {'CurrentPowerState': 0}
else:
E = eval(e.split(" = ")[1].replace("=", ":"))
# Create the single-row DataFrame
G = pd.DataFrame({'Capacity': D['Capacity'],
'Current': D['Current'],
'PCT' : (1.*D['Current']/D['Capacity'])*100.,
'BatLife' : D['Capacity']/8440.*100.,
'CycleCount': D['Cycle Count'],
'CurrentPowerState': E['CurrentPowerState'],
'SSID': sid,
'WiFi': wifistat,
'Eth': ethstat}, index=[dt,])
DATA = DATA.append(G)
nDays = (DATA.index[-1].date() - DATA.index[0].date()).days + 1
nTimeOfDay = (60./5)*24 # number of 5 minute samples per day
startDate = DATA.index[0].date()
SSID_namelist = DATA.SSID.unique()
SSID_namelist.sort()
nSSID = len(SSID_namelist)
netnames = SSID_namelist.tolist()
ssids = [netnames.index(i) for i in DATA.SSID]
DATA['ssids'] = ssids
AWAKE = DATA[DATA.CurrentPowerState != 0]
batgrid = numpy.zeros((nTimeOfDay, nDays))*numpy.nan
ssidgrid = numpy.zeros_like(batgrid)*numpy.nan
#awakegrid = numpy.zeros_like(batgrid)
for i in AWAKE.iterrows():
dS = (i[0].date() - startDate).days
tOD = numpy.int(numpy.floor(
(i[0].to_pydatetime() -
datetime.datetime(i[0].year, i[0].month, i[0].day)).seconds/300.))
batgrid[tOD, dS] = i[1].PCT
ssidgrid[tOD, dS] = i[1].ssids
#if i[1].CurrentPowerState !=0:
# awakegrid[tOD, dS] = 1
import matplotlib.pyplot as plt
from matplotlib import cm
# batfig
#################################################
batfig = plt.figure(figsize=(9,6), dpi=300)
ax = batfig.add_subplot(211)
ax.plot_date(DATA.index, DATA['CycleCount'], '-')
ax.set_ylabel('Cycle Count')
ax.set_xticklabels([])
ax.grid('on')
ax.set_title('Battery Statistics')
ax = batfig.add_subplot(212)
ax.plot_date(DATA.index, DATA['BatLife'], 'k.')
ax.set_ylabel('Battery Capacity (%)')
#ax.set_xticklabels([])
ax.grid('on')
#ax = batfig.add_subplot(313)
#ax.plot_date(DATA.index, DATA['Capacity'], 'k.')
#ax.set_ylabel('Battery Current')
#ax.grid('on')
plt.savefig(os.path.join(dbdir, 'batstats.png'), dpi=300)
# batyear
#################################################
batyear = plt.figure(figsize=(10, 5), dpi=300)
ax = batyear.add_axes([0.12, 0.1, 0.9, 0.8])
#c = ax.scatter(DOY, TOD, c=DATA['PCT'], edgecolor='none', marker='s', s=1,
# cmap=cm.rainbow_r, vmax=100., vmin=0.)
c = ax.imshow(batgrid, vmin=0., vmax=100, aspect='auto', interpolation='none',
cmap=cm.jet_r)
ax.set_xlabel('Days')
ax.set_ylabel('Time of Day')
#ax.set_xlim((0, 366))
#ax.set_ylim((-288, 0))
ax.set_yticks(numpy.arange(0, 25, 3)*12)
ax.set_yticklabels(['Midnight', '3am', '6am', '9am', 'Noon', '3pm', '6pm', '9pm', 'Midnight'])
ax.grid('on')
cb = plt.colorbar(c, pad=0.02)
cb.set_label('Battery Percentage')
cb.ax.tick_params(labelsize=8)
plt.savefig(os.path.join(dbdir, 'batyear.png'), dpi=300)
# yearview
#################################################
from matplotlib.colors import ListedColormap as LC
fig = plt.figure(figsize=(10,5), dpi=300)
ax = fig.add_subplot(111)
#c=ax.scatter(DOY, TOD, c=numpy.array(ssids), edgecolor='none', marker='s',
# s=1, cmap=cm.rainbow)
mp = cm.jet_r(numpy.linspace(0, 1, nSSID))
ssidcols = LC(mp, 'ssidcols')
cm.register_cmap(cmap = ssidcols)
ssidtick = AWAKE.ssids.unique()
ssidtick.sort()
ssidtick = ssidtick * (ssidtick.max() / float(nSSID))
ssidtick = ssidtick + ssidtick[1]/2.
c = ax.imshow(ssidgrid, interpolation='none', aspect='auto', cmap=ssidcols)
ax.set_xlabel('Day of Year')
ax.set_ylabel('Time of Day')
#ax.set_xlim((0, 366))
#ax.set_ylim((-288, 0))
ax.set_yticks(numpy.arange(0, 25, 3)*12)
ax.set_yticklabels(['Midnight', '3am', '6am', '9am', 'Noon', '3pm', '6pm', '9pm', 'Midnight'])
ax.grid('on')
cb = plt.colorbar(c)
cb.set_ticks(ssidtick)
cb.set_ticklabels(netnames)
cb.ax.tick_params(labelsize=8)
plt.savefig(os.path.join(dbdir, 'yearview.png'), dpi=300)
# weekstats
#################################################
fig.clf()
#ax = fig.add_axes([0.05, 0.1, 0.7, 0.8])
ax = fig.add_subplot(111)
histbins = numpy.arange(0, 2016, 3)
counts = [wk2num(g) for g in DATA.index]
ax.hist(counts, bins=histbins, histtype='step', label='ALL')
#H = AWAKE.groupby('SSID')
##H = DATA.groupby('SSID')
#for g in H.groups.keys():
# counts = [wk2num(v) for v in H.get_group(g).index]
# ax.hist(counts, bins=histbins, histtype='step', label=g)
ax.set_xticks(numpy.arange(7)*288)
ax.set_xticks((numpy.arange(7)+.5)*288, minor=True)
ax.set_xticklabels([])
ax.set_xticklabels(calendar.day_name[0:7], minor=True)
ax.set_xlim((0, 2016))
ax.set_yticks([])
ax.set_ylim(0, ax.get_ylim()[1]*1.1)
#ax.legend(loc='upper left', bbox_to_anchor=(1, 1), fontsize=10,
# title="SSID", frameon=False)
ax.grid('on', which='major')
ax.set_xlabel('Day of Week')
ax.set_ylabel('Computer Useage')
#plt.suptitle('Computer Useage by Day of Week and SSID')
plt.savefig(os.path.join(dbdir, 'weekstats.png'), dpi=300)
plt.close('all')