-
Notifications
You must be signed in to change notification settings - Fork 4
/
ReadFeedback.py
executable file
·223 lines (177 loc) · 8.07 KB
/
ReadFeedback.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
#!/usr/bin/env python3
"""
Parses the feedback logfiles created by a BBB Instance and makes them humanly readable.
To enable feedback logs: https://docs.bigbluebutton.org/admin/customize.html#collect-feedback-from-the-users
"""
__author__ = "Lukas Mahler"
__version__ = "0.4.0"
__date__ = "26.09.2022"
__email__ = "[email protected]"
__status__ = "Development"
import csv
import gzip
import shutil
import textwrap
from pathlib import Path
from datetime import datetime
from typing import Tuple, Dict, List
from argparse import ArgumentParser, Namespace
def parsefeedback(args: Namespace) -> Tuple[List[Dict], Dict]:
logspath = args.path
parsezip = args.parsezip
silent = args.silent
data = []
myrating = 0
numratings = 0
if not silent:
print("[x] Started parsing feedback logs\n")
# Find all (rotated) logfiles
logs = list(logspath.glob("html5-client.log*"))
# No html5-client.log found
if len(logs) == 0:
raise FileNotFoundError("[x] No logfiles found")
for index, log in enumerate(logs):
if not silent:
print(f"[{index+1}/{len(logs)}] parsed")
unzipped = False
if log.suffix == ".gz":
if parsezip:
with gzip.open(log, 'rb') as zipin:
with open(log.with_suffix(''), 'wb') as zipout:
shutil.copyfileobj(zipin, zipout)
log = Path(zipout.name)
unzipped = True
else:
continue
with open(log, 'r') as readfile:
for line in readfile:
# Get good UTF-8 encoding
line = bytes(line, encoding='latin1').decode('unicode_escape')
# Skip empty lines
try:
line = line.split(r" [")[2][:-2]
except IndexError:
continue
# String formatting
line = line.replace(r"\n", " ")
line = line.replace("}", "")
line = line.replace("{", "")
line = line.replace(",", ":")
line = line.replace('"', '')
line = line.split(":")
# Read the timestamp
if "time" in line:
start = line.index("time")
time = "".join(line[start + 1:start + 3])
timestamp = datetime.strptime(time, "%Y-%m-%dT%H%M").strftime("%d.%m.%y %H:%M")
# Read the given rating
if "rating" in line:
start = line.index("rating")
rating = "".join(line[start + 1:start + 2])
myrating = myrating + int(rating)
numratings += 1
# Read the commenters name
if "fullname" in line:
start = line.index("fullname")
name = ",".join(line[start + 1:start + 3])
name = bytes(name, encoding='latin1').decode('UTF-8')
if "confname" in name:
# Non registered accounts will have "confname" in their name.
# Replace these with (temp)
name = name.replace(",confname", " (temp)")
# Split comment every args.charlimit (default = 100) characters
if "comment" in line:
start = line.index("comment") + 1
end = line.index("userRole")
raw_comment = "".join(line[start:end])
raw_comment = bytes(raw_comment, encoding='latin1').decode('UTF-8')
comment = f"\n{'':15s}│ {'':8s}│ {'':30s}│ ".join(textwrap.wrap(raw_comment, args.charlimit))
# Add good comments to dict
if "comment" in line:
data.append({
'timestamp': timestamp,
'rating': rating + " Stars",
'name': name,
'comment': comment,
'raw_comment': raw_comment
})
# Delete unzipped files
if unzipped:
log.unlink()
# Sort data by timestamp
sorted_data = sorted(data, key=lambda k: datetime.strptime(k['timestamp'], "%d.%m.%y %H:%M"))
# Calculate rating_data
if numratings == 0:
median = 0
else:
median = round(myrating/numratings, 2)
rating_data = {'num': numratings, 'median': median}
if not silent:
print("\n[x] Finished parsing feedback logs")
return sorted_data, rating_data
def print_parsed(args: Namespace, data: List[Dict], rating: Dict) -> None:
print(f"\n{'Timestamp:':15s}│ {'Rating:':8s}│ {'Author:':30s}│ Comment:")
print(f"{15*'─'}┼{9*'─'}┼{31*'─'}┼{(args.charlimit + 2)*'─'}")
lastline = len(data)-1
for index, entry in enumerate(data):
print(f"{entry['timestamp']:15}│ {entry['rating']:8}│ {entry['name']:30s}│ {entry['comment']}")
if index != lastline:
print(f"{15*'─'}┼{9 *'─'}┼{31*'─'}┼{(args.charlimit + 2)*'─'}")
else:
print(f"{15*'─'}┴{9*'─'}┴{31*'─'}┴{(args.charlimit + 2)*'─'}\n")
print(f"Median rating: {rating['median']} with {rating['num']} Votes\n")
def write_parsed(args: Namespace, data: List[Dict], rating: Dict) -> None:
logspath = args.path
silent = args.silent
writefile = logspath / "html5-client-readable.log"
with open(writefile, 'w', encoding='UTF-8') as f:
f.write(f"{'Timestamp:':15s}│ {'Rating:':8s}│ {'Author':30s}│ Comment:\n")
f.write(f"{15*'─'}┼{9*'─'}┼{31*'─'}┼{(args.charlimit + 2)*'─'}\n")
lastline = len(data) - 1
for index, entry in enumerate(data):
f.write(f"{entry['timestamp']:15}│ {entry['rating']:8}│ {entry['name']:30s}│ {entry['comment']}\n")
if index != lastline:
f.write(f"{15*'─'}┼{9*'─'}┼{31*'─'}┼{(args.charlimit + 2)*'─'}\n")
else:
f.write(f"{15*'─'}┴{9*'─'}┴{31*'─'}┴{(args.charlimit + 2)*'─'}\n")
f.write(f"Median rating: {rating['median']} with {rating['num']} Votes")
if not silent:
print(f"→ Wrote to file: {writefile.name}")
def write_csv(args: Namespace, data: List[Dict]) -> None:
logspath = args.path
silent = args.silent
writefile = logspath / "BBB-Feedback.csv"
with open(writefile, 'w', encoding='UTF-8', newline='') as f:
write = csv.writer(f)
for entry in data:
write.writerow([entry['timestamp'], entry['rating'], entry['name'], entry['raw_comment']])
if not silent:
print(f"→ Wrote to file: {writefile.name}")
# ======================================================================================================================
def main():
# Parse cmd parameter
parser = ArgumentParser()
parser.add_argument('-p', '--path', default='/var/log/nginx/', type=Path,
help='Provide the full path to the directory containing the feedback logs')
parser.add_argument('-cl', '--charlimit', default=100, type=int,
help='The character length on which we wrap the comment')
parser.add_argument('-s', '--silent', action='store_true',
help="If True the script won't have any output")
parser.add_argument('-tf', '--tofile', action='store_true',
help='If True parse the output to `html5-client-readable.log`')
parser.add_argument('-csv', action='store_true',
help='If True parse the output into `feedback.csv`')
parser.add_argument('-pz', '--parsezip', action='store_true',
help='If True unzip .gz logs and parse them aswell')
args = parser.parse_args()
silent = args.silent
# Execute feedback parsing
data, rating = parsefeedback(args)
if not silent:
print_parsed(args, data, rating)
if args.tofile:
write_parsed(args, data, rating)
if args.csv:
write_csv(args, data)
if __name__ == "__main__":
main()