diff options
| author | Andrew <saintruler@gmail.com> | 2019-04-17 15:01:38 +0400 |
|---|---|---|
| committer | Andrew <saintruler@gmail.com> | 2019-04-17 15:01:38 +0400 |
| commit | ce17eacde37c124abe07288ae1453b5b658f8ff2 (patch) | |
| tree | 04e0f8590ba1ca112a580914ed510f5bbfa75e45 /day4/task3/db.py | |
| parent | 402d0d2b9ebd76b8e99eddceeb85c4a66030b51b (diff) | |
Четвертый день, задача номер 3
Diffstat (limited to 'day4/task3/db.py')
| -rw-r--r-- | day4/task3/db.py | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/day4/task3/db.py b/day4/task3/db.py new file mode 100644 index 0000000..15eba02 --- /dev/null +++ b/day4/task3/db.py @@ -0,0 +1,57 @@ +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>.*)') + +db = [] + +lines = f.readlines() +f.close() +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') + + db.append({ + 'date': date, + 'type': match.group('type'), + 'module': module, + 'id': match.group('id'), + 'text': match.group('text') + }) + + else: + print(f'Ошибка при разборе строки:\n{line}') + + +def search_by_date(hour, minute=None, second=None, data=None): + arr = [] + print(f'filtering by h={hour}, m={minute}, s={second}, data="{data}"') + for err in db: + if err['date'].hour == int(hour): + if minute is not None: + if second is not None: + if err['date'].minute == int(minute) and err['date'].second == int(second): + if data in err['module']: + _ = err.copy() + _['date'] = _['date'].ctime() + arr.append(_) + else: + if err['date'].minute == int(minute): + if data in err['module']: + _ = err.copy() + _['date'] = _['date'].ctime() + arr.append(_) + + else: + if data in err['module']: + _ = err.copy() + _['date'] = _['date'].ctime() + arr.append(_) + + return arr |