-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_counting.py
More file actions
68 lines (48 loc) · 1.89 KB
/
log_counting.py
File metadata and controls
68 lines (48 loc) · 1.89 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
def main(argv: list[str]) -> int:
if len(argv) != 1:
print("Exactly one argument expected: filename")
return 1
try:
with open(argv[0], "r") as file:
parsedLogLines: list[tuple[str, ...]] = []
counter: int = 0
for line in file:
parsedLine: tuple[str, ...] | None = parse_log_line(line)
if parsedLine:
parsedLogLines.append(parsedLine)
else:
counter += 1
print(str(counter) + " Lines could not be parsed")
top_users: list[tuple[str, int]] = get_top_users(count_requests_per_user(parsedLogLines), 10)
print("These are the top users:")
for user in top_users:
print(user[0] + " " + str(user[1]))
for i, (user, count) in enumerate(top_users, start=1):
print(f"{i}. {user} - {count}")
print()
except OSError:
print("Could not open file.")
return 1
return 0
def parse_log_line(line: str) -> tuple[str, ...] | None:
#2025-11-28T10:03:05Z frank GET /api/items 200
parts: list[str] = line.strip().split()
if len(parts) != 5:
return None
if not parts[2].isalpha():
return None
if parts[2] not in {"GET", "POST", "DELETE", "UPDATE"}:
return None
return tuple(parts)
def count_requests_per_user(lines: list[tuple[str, ...]]) -> dict[str, int]:
names: dict[str, int] = {}
for line in lines:
name = line[1]
names[name] = names.get(name, 0) + 1
return names
def get_top_users(counts: dict[str, int], n: int = 5) -> list[tuple[str, int]]:
sortedNames: list[tuple[str,int]]= sorted(counts.items(), key=lambda item: item[1], reverse=True)
return sortedNames[:n]
if __name__ == "__main__":
import sys
raise SystemExit(main(sys.argv[1:]))