diff options
| author | Andrew <saintruler@gmail.com> | 2019-04-17 20:12:14 +0400 |
|---|---|---|
| committer | Andrew <saintruler@gmail.com> | 2019-04-17 20:12:14 +0400 |
| commit | 588cdffdc1f527fac5a7bdd934008882e3efc70d (patch) | |
| tree | 81e3d82e17a8aa5b3e0097a09b179256c1f9d1d3 | |
| parent | ce17eacde37c124abe07288ae1453b5b658f8ff2 (diff) | |
Ускорен поиск по логу
| -rw-r--r-- | day4/task3/client.py | 14 | ||||
| -rw-r--r-- | day4/task3/db.py | 54 | ||||
| -rw-r--r-- | day4/task3/server.py | 9 |
3 files changed, 36 insertions, 41 deletions
diff --git a/day4/task3/client.py b/day4/task3/client.py index 7f5c298..49663c8 100644 --- a/day4/task3/client.py +++ b/day4/task3/client.py @@ -28,14 +28,10 @@ def get_data(connection): def parse_data(data): - data = json.loads(data.decode()) - arr = [f'Найдено {len(data)} совпадений:'] - for error in data: - arr.append(f"[{error['date']}][{error['type']}][{error['module']}][{error['id']}] {error['text']}") - return '\n'.join(arr) + return json.loads(data.decode()) -HOST, PORT = ADDR = 'localhost', 6001 +HOST, PORT = ADDR = 'localhost', 6000 cnt = 0 sock = socket.socket() @@ -67,6 +63,10 @@ while True: else: sock.sendall(pack_data(data)) response = get_data(sock) - print(parse_data(response)) + arr = parse_data(response) + + print(f'Найдено {len(arr)} совпадений:') + for line in arr: + print(line) sock.close() diff --git a/day4/task3/db.py b/day4/task3/db.py index 15eba02..f4cbd0b 100644 --- a/day4/task3/db.py +++ b/day4/task3/db.py @@ -1,4 +1,5 @@ import re +import bisect from datetime import datetime f = open('otrs_error.log') @@ -17,41 +18,30 @@ for line in lines: 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') - }) + 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): - 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 + 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]] diff --git a/day4/task3/server.py b/day4/task3/server.py index b3cb619..f4e5ebe 100644 --- a/day4/task3/server.py +++ b/day4/task3/server.py @@ -3,6 +3,7 @@ import struct from db import search_by_date import re import json +import time def pack_data(data): @@ -14,7 +15,11 @@ def handle_request(data): match = re.match(r'(\d+):?(\d+)?:?(\d+)?-(.*)', data) if match: *date, msg = match.groups() - return pack_data(json.dumps(search_by_date(*date, msg))) + + t = time.time() + found = search_by_date(*map(lambda i: int(i) if i is not None else i, date), msg) + print(f'Search took {time.time() - t} seconds') + return pack_data(json.dumps(found)) else: print('String not matched.') @@ -63,7 +68,7 @@ def handle_connection(connection, address): connection.sendall(response) -HOST, PORT = ADDR = '0.0.0.0', 6001 +HOST, PORT = ADDR = '0.0.0.0', 6000 sock = socket.socket() sock.bind(ADDR) sock.listen(5) |