diff options
Diffstat (limited to 'day3')
| -rw-r--r-- | day3/task4/task4.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/day3/task4/task4.py b/day3/task4/task4.py new file mode 100644 index 0000000..e04acc7 --- /dev/null +++ b/day3/task4/task4.py @@ -0,0 +1,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") |