summaryrefslogtreecommitdiff
path: root/day4/task3/db.py
blob: f4cbd0b9febe92bba5674a5439391d2e71b1bbc2 (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
import re
import bisect
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.hour, date.minute, date.second, module, line))

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


db.sort()


def search_by_date(hour, minute=None, second=None, data=None):
    low = [hour]
    high = [hour]
    if minute is not None:
        low.append(minute)
        high.append(minute)

        if second is not None:
            low.append(second)
            high.append(second + 1)
        else:
            high.append(60)
    else:
        high.append(60)

    left = bisect.bisect_left(db, tuple(low))
    right = bisect.bisect_left(db, tuple(high))
    return [t[4] for t in db[left:right] if data in t[3]]