-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathusercounter.py
More file actions
executable file
·168 lines (147 loc) · 5.03 KB
/
usercounter.py
File metadata and controls
executable file
·168 lines (147 loc) · 5.03 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
#! /usr/bin/python3
import sys, getopt, string, signal
import os, re
#
#
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
-d -- specify the directory (it expects an argument)
-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])
usertype = values[1]
action = values[3]
schoolid = values[5]
userid = values[7]
return userid, action
###############################################
#
hit_counts = {'student': 0, 'teacher':0, 'guardian':0}
students = {}
teachers = {}
guardians = {}
teacher_homeworks = {}
teacher_resources = {}
teacher_topiclines = {}
teacher_reviews = {}
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:
logfile = open(filename, 'r')
print ("Reading... ", filename)
for line in logfile:
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
if re.search('USER: student',line) != None:
userid, action = split_line(line)
#print(userid,"did this",action)
hit_counts['student'] += 1
if userid in students:
students[userid] += 1
else:
students[userid] = 1
elif re.search('USER: teacher',line) != None:
userid, action = split_line(line)
#print(userid,"did this",action)
hit_counts['teacher'] += 1
if userid in teachers:
teachers[userid] += 1
else:
teachers[userid] = 1
if action == 'teacher/createHomework':
if userid in teacher_homeworks:
teacher_homeworks[userid] += 1
else:
teacher_homeworks[userid] = 1
elif re.search('homework/review',action) != None:
if userid in teacher_reviews:
teacher_reviews[userid] += 1
else:
teacher_reviews[userid] = 1
elif action == 'resources/new':
if userid in teacher_resources:
teacher_resources[userid] += 1
else:
teacher_resources[userid] = 1
elif action == 'resources/topicLine':
if userid in teacher_topiclines:
teacher_topiclines[userid] += 1
else:
teacher_topiclines[userid] = 1
elif re.search('USER: guardian',line) != None:
userid, action = split_line(line)
#print(userid,"did this",action)
hit_counts['guardian'] += 1
if userid in guardians:
guardians[userid] += 1
else:
guardians[userid] = 1
logfile.close()
except IOError as e:
print("Can't open file:", filename)
#
#
print("--")
print("Students =",len(students))
print("Student hits =",hit_counts['student'])
print("--")
print("Parents =",len(guardians))
print("Parent hits =",hit_counts['guardian'])
print("--")
print("Teachers =",len(teachers))
print("Teacher hits =",hit_counts['teacher'])
print("Teacher homework users =",len(teacher_homeworks))
print("Teacher reviews homework users =",len(teacher_reviews))
print("Teacher resource users =",len(teacher_resources))
print("Teacher topicline users =",len(teacher_topiclines))
super_teachers = 0
for userid in teacher_homeworks.keys():
if userid in teacher_topiclines and teacher_topiclines[userid]>4 and teacher_homeworks[userid]>10:
super_teachers += 1
print(userid)
print("")
print("Super Teachers! Doing both homework and topiclines ", super_teachers)