-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtimeline.py
More file actions
executable file
·169 lines (140 loc) · 5.26 KB
/
Copy pathtimeline.py
File metadata and controls
executable file
·169 lines (140 loc) · 5.26 KB
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
#! /usr/bin/python3
import sys, getopt, string, signal
import os, re
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from datetime import datetime
#########################################
# Handle command line arguments
#
def signal_handler(signal, frame):
print('You pressed Ctrl+C!')
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
#
def help_message():
print ('''Usage: usercount [OPTION] filename
Options:
-h -- displays this help message
-s -- specify a single school using its id
-v -- displays Python version''')
sys.exit(0)
#
def split_line(text):
timestamp, params = text.strip().split( ' - ')
#indexes = (1,3,5,7,)
values = params.split(' ')
#for i in indexes:
# print(values[i])
action_vars={'usertype': values[1],
'action': values[3],
'schoolid': values[5],
'userid': values[7],
'logdate': timestamp[1:11]
}
return action_vars
###############################################
#
#
limit_schoolid = ""
schoolid = -1
#userid = -1
#action = ""
#usertype = ""
try:
options, args = getopt.getopt(sys.argv[1:],'hs:v', ['version'])
except getopt.error:
print('''Nope: didn't recognise that option or you missed an argument.
Try `-h\' for more info''')
sys.exit(0)
for o in options[:]:
if o[0] == '-h':
help_message()
elif o[0] == '-s' and o[1] != '':
limit_schoolid = o[1]
print('Limit results to school id ' + limit_schoolid)
options.remove(o)
break
elif o[0] == '-s' and o[1] == '':
print('-s expects a school id as an argument')
sys.exit(0)
elif o[0] == '-v' or o[0] == '--version':
print('help version 0.0.001')
print('Python '+sys.version)
sys.exit(0)
#
#
for filename in args[:]:
try:
file = open(filename, 'r')
print ("Reading... ", filename)
log_lines = file.read().splitlines()
file.close()
except IOError as e:
print("Can't open file:", filename)
sys.exit(0)
########################################################
#
#
#
dates = pd.date_range('2015-09-01', '2016-02-01', freq='D')
weeks = pd.date_range('2015-09-01', '2016-02-07', freq='W')
student_hit_series = pd.Series(0, dates)
teacher_hit_series = pd.Series(0, dates)
guardian_hit_series = pd.Series(0, dates)
student_active_series = pd.Series(0, dates)
teacher_active_series = pd.Series(0, dates)
guardian_active_series = pd.Series(0, dates)
#
student_daily_active = {datetime.strftime(i,"%Y-%m-%d"):[] for i in dates}
teacher_daily_active = {datetime.strftime(i,"%Y-%m-%d"):[] for i in dates}
guardian_daily_active = {datetime.strftime(i,"%Y-%m-%d"):[] for i in dates}
for line in log_lines:
if re.search('SCHOOL: 101 ',line) != None or re.search('SCHOOL: 1 ',line) != None:
continue
if limit_schoolid!='' and re.search('SCHOOL: ' + limit_schoolid+' ', line) == None:
continue
action_vars = split_line(line)
logdate=action_vars['logdate']
if action_vars['usertype'] == 'student':
student_hit_series[logdate] += 1
if not(action_vars['userid'] in student_daily_active[logdate]):
student_daily_active[logdate].append(action_vars['userid'])
student_active_series[logdate] += 1
elif action_vars['usertype'] == 'teacher':
teacher_hit_series[logdate] += 1
if not(action_vars['userid'] in teacher_daily_active[logdate]):
teacher_daily_active[logdate].append(action_vars['userid'])
teacher_active_series[logdate] += 1
elif action_vars['usertype'] == 'guardian':
guardian_hit_series[logdate] += 1
if not(action_vars['userid'] in guardian_daily_active[logdate]):
guardian_daily_active[logdate].append(action_vars['userid'])
guardian_active_series[logdate] += 1
weekly_hits_frame = pd.DataFrame({'teacher':teacher_hit_series.resample('W',how=sum),
'student':student_hit_series.resample('W',how=sum),
'guardian':guardian_hit_series.resample('W',how=sum)})
weekly_active_frame = pd.DataFrame({'teacher':teacher_active_series.resample('W',how='mean'),
'student':student_active_series.resample('W',how='mean'),
'guardian':guardian_active_series.resample('W',how='mean')})
f, ax_array = plt.subplots(3, 2, figsize=(8, 6), sharex=True)
#sns.despine(bottom=True)
#sns.set_style("whitegrid")
sns.barplot(weekly_active_frame.index,weekly_active_frame['teacher'],ax=ax_array[0,0])
ax_array[0,0].set_ylabel("Teachers")
ax_array[0,1].plot(weekly_hits_frame['teacher']/weekly_active_frame['teacher'])
ax_array[0,1].set_ylabel("Hits")
sns.barplot(weekly_active_frame.index,weekly_active_frame['student'],ax=ax_array[1,0])
ax_array[1,0].set_ylabel("Students")
ax_array[1,1].plot(weekly_hits_frame['student']/weekly_active_frame['student'])
ax_array[1,1].set_ylabel("Hits")
sns.barplot(weekly_active_frame.index,weekly_active_frame['guardian'],ax=ax_array[2,0])
ax_array[2,0].set_ylabel("Parents")
ax_array[2,1].plot(weekly_hits_frame['guardian']/weekly_active_frame['guardian'])
ax_array[2,1].set_ylabel("Hits")
plt.setp(f.axes, xticks=[])
plt.tight_layout(h_pad=3)
plt.show()
#print(weekly_hits_frame.resample('M',how=sum))