-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogEntryParser.py
81 lines (59 loc) · 1.96 KB
/
LogEntryParser.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import re
import datetime
import time
from config import filters
class EntryParser:
"""
Class used to parse and validate entries in log file.
"""
def parse_date(self, line):
"""
Skip first character '[',
and return the first elem till ':' which is the date
"""
return line[1:].partition(':')[0]
def get_since(self, line):
"""
Return 'since' timestamp value.
"""
since = re.search(r'since=\d+', line).group()
return re.search(r'\d+', since).group()
def get_until(self, line, date):
"""
Return 'until' timestamp value.
If 'until' value is missing, set it to date of request
"""
until = re.search(r'until=\d+', line)
if not until:
date = datetime.datetime.strptime(date, '%d/%b/%Y')
return str(time.mktime(date.timetuple()))
return re.search(r'\d+', until.group()).group()
def parse_interval(self, line):
"""
Return a (since, until) tuple from line
"""
date = self.parse_date(line)
return (self.get_since(line), self.get_until(line, date))
def is_interval_valid(self, interval):
"""
Check whether 'since' values are valid.
Faulty entries with 'since=0' have been found in log.
"""
if len(interval) != 2:
return False
since = interval[0]
if since is None or int(since) == 0:
return False
return True
def is_entry_valid(self, line):
"""
Check if log entry contains data request for a certain interval
by checking if all filter entries apply.
Filter entries are set in config.py
"""
if 'since' not in line:
return False
for filter_entry in filters:
if filter_entry not in line:
return False
return True