summaryrefslogtreecommitdiff
path: root/day3/task4/task4.py
blob: e04acc72579f257aa79381ee921c905eb49d68e1 (plain)
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
import re
from datetime import datetime

f = open('otrs_error.log')

matcher = re.compile(r'\[(?P<date>.*?)\]\[(?P<type>.*?)\]\[(?P<module>.*?)\]\[(?P<id>.*?)\] (?P<text>.*)')

match_date_low = datetime(2017, 8, 6, 10, 0)
match_date_high = datetime(2017, 8, 6, 19, 0)
count_module = 'Kernel::System::Ticket::TicketPermission'
counter = 0

errors_by_module = {}
errors_by_date = {}

lines = f.readlines()
for line in lines:
    line = line.strip()
    match = re.match(matcher, line)

    if match:
        date = datetime.strptime(match.group('date'), '%c')
        module = match.group('module')

        if match_date_low <= date <= match_date_high and module == count_module:
            counter += 1

        errors_by_module[module] = errors_by_module.get(module, 0) + 1

        err_date = datetime(date.year, date.month, date.day)
        errors_by_date[err_date] = errors_by_date.get(err_date, []) + [{
            'date': date,
            'type': match.group('type'),
            'module': module,
            'id': match.group('id'),
            'text': match.group('text')
        }]

    else:
        print(f'Ошибка при разборе строки:\n{line}')

print(f'За период с {match_date_low} по {match_date_high} в модуле "{count_module}" произошло {counter} ошибок')

for module, count in sorted(errors_by_module.items(), key=lambda p: p[1], reverse=True):
    print(f'В модуле {module} за все время произошло {count} ошибок')

for date, errors in errors_by_date.items():
    # Дополняем дни и месяцы до ширины 2 с помощью нулей слева
    with open(f'{str(date.day).rjust(2, "0")}.{str(date.month).rjust(2, "0")}.{date.year}.txt', 'w') as f:
        for error in errors:
            f.write(f"[{date.ctime()}][{error['type']}][{error['module']}][{error['id']}] {error['text']}\n")