author | Tero Marttila <terom@fixme.fi> |
Tue, 10 Feb 2009 03:22:43 +0200 | |
changeset 80 | a0662cff1d9d |
parent 78 | 85345abbd46a |
child 81 | 745032a57803 |
permissions | -rw-r--r-- |
41 | 1 |
""" |
2 |
A source of IRC log files |
|
3 |
""" |
|
4 |
||
77
4287fb77e312
implement max_pages, and paginate channel_date by default now
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
5 |
import datetime, calendar, itertools, functools, math |
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
6 |
import os, errno |
41 | 7 |
import pytz |
8 |
||
9 |
class LogSource (object) : |
|
10 |
""" |
|
11 |
A collection of IRC logs for a specific target in some format. Provides the possibility to read specific events |
|
12 |
""" |
|
13 |
||
14 |
def get_latest (self, count) : |
|
15 |
""" |
|
16 |
Yield the latest events, up to `count` of them. |
|
17 |
""" |
|
18 |
||
19 |
abstract |
|
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
20 |
|
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
21 |
def get_date (self, dt) : |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
22 |
""" |
76
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
23 |
Get logs for the given date (as a datetime). |
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
24 |
""" |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
25 |
|
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
26 |
abstract |
54
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
27 |
|
76
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
28 |
def get_date_paged (self, dt, count, page=None) : |
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
29 |
""" |
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
30 |
Get the logs for a given date (as a datetime), divided into pages of count each. If page is given, the time |
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
31 |
portion of the dt is ignored, and the lines for the given page are returned. Otherwise, if page is None, |
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
32 |
then the lines for the page containing the given timestamp is returned. |
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
33 |
|
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
34 |
The return value is a (page, max, lines) tuple. |
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
35 |
""" |
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
36 |
|
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
37 |
# how to act? |
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
38 |
if page : |
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
39 |
# constant skip |
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
40 |
skip = (page - 1) * count |
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
41 |
|
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
42 |
else : |
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
43 |
skip = None |
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
44 |
|
78
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
45 |
# go through the logs a page at a time |
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
46 |
this_page = 1 |
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
47 |
|
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
48 |
# last line's timestamp |
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
49 |
last_ts = None |
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
50 |
|
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
51 |
# found it yet? |
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
52 |
found = False |
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
53 |
|
77
4287fb77e312
implement max_pages, and paginate channel_date by default now
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
54 |
# count the full number of lines |
4287fb77e312
implement max_pages, and paginate channel_date by default now
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
55 |
line_count = 0 |
4287fb77e312
implement max_pages, and paginate channel_date by default now
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
56 |
|
76
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
57 |
# collect lines |
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
58 |
lines = [] |
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
59 |
|
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
60 |
# iterate using get_date |
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
61 |
for line in self.get_date(dt) : |
77
4287fb77e312
implement max_pages, and paginate channel_date by default now
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
62 |
# count them |
4287fb77e312
implement max_pages, and paginate channel_date by default now
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
63 |
line_count += 1 |
4287fb77e312
implement max_pages, and paginate channel_date by default now
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
64 |
|
76
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
65 |
# skip? |
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
66 |
if skip : |
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
67 |
skip -= 1 |
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
68 |
continue |
78
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
69 |
|
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
70 |
# is this page all that we want/need? |
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
71 |
if page or found : |
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
72 |
# already full? |
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
73 |
if len(lines) >= count : |
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
74 |
continue |
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
75 |
|
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
76 |
# specfic timestamp |
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
77 |
else : |
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
78 |
# didn't find it in this page? |
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
79 |
if len(lines) >= count : |
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
80 |
# reset to next page |
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
81 |
lines = [] |
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
82 |
this_page += 1 |
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
83 |
|
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
84 |
# is dt between these two timestamps? |
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
85 |
if (not last_ts or last_ts <= dt) and (dt <= line.timestamp) : |
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
86 |
# found! |
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
87 |
found = True |
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
88 |
page = this_page |
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
89 |
|
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
90 |
else : |
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
91 |
# keep looking |
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
92 |
last_ts = line.timestamp |
85345abbd46a
implement pagination for channel_link
Tero Marttila <terom@fixme.fi>
parents:
77
diff
changeset
|
93 |
|
76
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
94 |
# store line |
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
95 |
lines.append(line) |
77
4287fb77e312
implement max_pages, and paginate channel_date by default now
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
96 |
|
4287fb77e312
implement max_pages, and paginate channel_date by default now
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
97 |
# calculate max_pages |
4287fb77e312
implement max_pages, and paginate channel_date by default now
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
98 |
max_pages = math.ceil(float(line_count) / count) |
4287fb77e312
implement max_pages, and paginate channel_date by default now
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
99 |
|
4287fb77e312
implement max_pages, and paginate channel_date by default now
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
100 |
# return |
4287fb77e312
implement max_pages, and paginate channel_date by default now
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
101 |
return (page, max_pages, lines) |
76
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
102 |
|
54
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
103 |
def get_month_days (self, dt) : |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
104 |
""" |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
105 |
Get a set of dates, telling which days in the given month (as a datetime) have logs available |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
106 |
""" |
41 | 107 |
|
54
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
108 |
abstract |
76
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
109 |
|
54
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
110 |
class LogFile (object) : |
41 | 111 |
""" |
112 |
A file containing LogEvents |
|
54
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
113 |
|
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
114 |
XXX: modify to implement LogSource? |
41 | 115 |
""" |
116 |
||
73
5a7188bf2894
split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
117 |
def __init__ (self, path, parser, charset, start_date=None, sep='\n') : |
41 | 118 |
""" |
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
119 |
Open the file at the given path, which contains data with the given charset, as lines separated by the |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
120 |
given separator. Lines are parsed using the given parser, using the given date as an initial date, see |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
121 |
LogParser for more info. XXX: currently we assume start_date also for the end of the file |
41 | 122 |
""" |
123 |
||
124 |
# store |
|
125 |
self.path = path |
|
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
126 |
self.parser = parser |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
127 |
self.start_date = start_date |
41 | 128 |
self.charset = charset |
129 |
self.sep = sep |
|
130 |
||
131 |
# open |
|
48 | 132 |
self.file = open(path, 'rb') |
41 | 133 |
|
134 |
def __iter__ (self) : |
|
135 |
""" |
|
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
136 |
Yields a series of unicode lines, as read from the top of the file |
41 | 137 |
""" |
138 |
||
139 |
# seek to beginning |
|
140 |
self.file.seek(0) |
|
141 |
||
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
142 |
# iterate over lines, decoding them as well |
65 | 143 |
return (line.decode(self.charset).rstrip(self.sep) for line in self.file) |
41 | 144 |
|
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
145 |
def read_full (self) : |
41 | 146 |
""" |
76
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
147 |
Reads all LogLines. The LogLines will have a valid offset. |
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
148 |
""" |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
149 |
|
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
150 |
# just use our __iter__ |
64
cdb6403c2498
beginnings of a LogSearchIndex class
Tero Marttila <terom@fixme.fi>
parents:
63
diff
changeset
|
151 |
return self.parser.parse_lines(self, self.start_date, starting_offset=1) |
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
152 |
|
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
153 |
def read_from (self, dt) : |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
154 |
""" |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
155 |
Reads all LogLines from the given naive timestamp onwards |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
156 |
""" |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
157 |
|
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
158 |
# start reading at beginning |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
159 |
events = self.read_full() |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
160 |
|
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
161 |
# skip unwanted events |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
162 |
for event in events : |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
163 |
if event.timestamp < dt : |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
164 |
continue |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
165 |
|
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
166 |
else : |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
167 |
# include this line as well |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
168 |
yield event |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
169 |
break |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
170 |
|
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
171 |
# yield the rest as-is |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
172 |
for event in events : |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
173 |
yield event |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
174 |
|
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
175 |
def read_until (self, dt) : |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
176 |
""" |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
177 |
Reads all LogLines up until the given naive timestamp |
41 | 178 |
""" |
179 |
||
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
180 |
# start reading events at the beginning |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
181 |
events = self.read_full() |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
182 |
|
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
183 |
# yield events until we hit the given timestamp |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
184 |
for event in events : |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
185 |
if event.timestamp <= dt : |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
186 |
yield event |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
187 |
|
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
188 |
else : |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
189 |
break |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
190 |
|
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
191 |
# ignore the rest |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
192 |
return |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
193 |
|
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
194 |
def _read_blocks_reverse (self, blocksize=1024) : |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
195 |
""" |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
196 |
Yields blocks of file data in reverse order, starting at the end of the file |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
197 |
""" |
41 | 198 |
|
199 |
# seek to end of file |
|
200 |
self.file.seek(0, os.SEEK_END) |
|
201 |
||
202 |
# read offset |
|
48 | 203 |
# XXX: hack -1 to get rid of trailing newline |
204 |
size = offset = self.file.tell() - 1 |
|
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
205 |
|
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
206 |
# do not try to read past the beginning of the file |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
207 |
while offset > 0: |
48 | 208 |
# calc new offset + size |
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
209 |
if offset > blocksize : |
48 | 210 |
# full block |
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
211 |
offset -= blocksize |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
212 |
read_size = blocksize |
41 | 213 |
|
48 | 214 |
else : |
215 |
# partial block |
|
216 |
read_size = offset |
|
41 | 217 |
offset = 0 |
218 |
||
43
fc11c4e86a82
implement channel_view count, the query stuff, css, layout all need some cleanup :(
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
219 |
# seek to offset |
41 | 220 |
self.file.seek(offset) |
221 |
||
48 | 222 |
# read the data we want |
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
223 |
block = self.file.read(read_size) |
41 | 224 |
|
48 | 225 |
# sanity check |
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
226 |
assert len(block) == read_size |
41 | 227 |
|
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
228 |
# yield |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
229 |
yield block |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
230 |
|
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
231 |
def _read_lines_reverse (self) : |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
232 |
""" |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
233 |
Yields decoded lines from the end of the file, in reverse order. |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
234 |
""" |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
235 |
|
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
236 |
# partial lines |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
237 |
buf = '' |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
238 |
|
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
239 |
# read from end of file, a block at a time |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
240 |
for block in self._read_blocks_reverse() : |
41 | 241 |
# add in our previous buf |
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
242 |
buf = block + buf |
41 | 243 |
|
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
244 |
# split up lines |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
245 |
lines = buf.split(self.sep) |
41 | 246 |
|
247 |
# keep the first one as our buffer, as it's incomplete |
|
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
248 |
buf = lines[0] |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
249 |
|
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
250 |
# yield the rest a line at a time in reverse order... this looks weird, but that's how slicing works :) |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
251 |
# XXX: use something like islice, this has to build a slice object |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
252 |
for line in lines[:0:-1] : |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
253 |
yield line.decode(self.charset) |
41 | 254 |
|
54
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
255 |
def read_latest (self, count) : |
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
256 |
""" |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
257 |
Returns up to count events, from the end of the file, or less, if the file doesn't contain that many lines. |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
258 |
""" |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
259 |
|
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
260 |
# the list of lines |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
261 |
lines = [] |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
262 |
|
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
263 |
# start reading lines into lines |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
264 |
for line in self._read_lines_reverse() : |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
265 |
# append |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
266 |
lines.append(line) |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
267 |
|
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
268 |
# done? |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
269 |
if len(lines) >= count : |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
270 |
break |
48 | 271 |
|
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
272 |
# decode in reverse order, using our starting date.... |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
273 |
# XXX: use lines[::-1] or reversed? |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
274 |
# XXX: it may make more sense to parse in reverse order, using 'self.end_date' or something like that |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
275 |
return self.parser.parse_lines(reversed(lines), self.start_date) |
41 | 276 |
|
277 |
class LogDirectory (LogSource) : |
|
278 |
""" |
|
279 |
A directory containing a series of timestamped LogFiles |
|
280 |
""" |
|
281 |
||
73
5a7188bf2894
split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
282 |
def __init__ (self, path, tz, parser, charset, filename_fmt) : |
41 | 283 |
""" |
284 |
Load the logfiles at the given path. |
|
285 |
||
286 |
The files contain data in the given charset, and are named according the the date in the given timezone and |
|
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
287 |
date format, and will be parsed using the given parser. |
41 | 288 |
""" |
289 |
||
290 |
# store |
|
291 |
self.path = path |
|
292 |
self.tz = tz |
|
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
293 |
self.parser = parser |
41 | 294 |
self.charset = charset |
295 |
self.filename_fmt = filename_fmt |
|
296 |
||
297 |
def _get_logfile_datetime (self, dt) : |
|
298 |
""" |
|
299 |
Get the logfile corresponding to the given datetime |
|
300 |
""" |
|
301 |
||
302 |
# convert to target timezone |
|
303 |
dtz = dt.astimezone(self.tz) |
|
304 |
||
305 |
# convert to date and use that |
|
306 |
return self._get_logfile_date(dtz.date()) |
|
307 |
||
54
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
308 |
def _get_logfile_date (self, d, load=True) : |
41 | 309 |
""" |
54
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
310 |
Get the logfile corresponding to the given naive date in our timezone. If load is False, only test for the |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
311 |
presence of the logfile, do not actually open it. |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
312 |
|
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
313 |
Returns None if the logfile does not exist. |
41 | 314 |
""" |
315 |
||
316 |
# format filename |
|
317 |
filename = d.strftime(self.filename_fmt) |
|
318 |
||
319 |
# build path |
|
320 |
path = os.path.join(self.path, filename) |
|
54
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
321 |
|
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
322 |
try : |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
323 |
if load : |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
324 |
# open+return the LogFile |
73
5a7188bf2894
split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
325 |
return LogFile(path, self.parser, self.charset, d) |
54
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
326 |
|
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
327 |
else : |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
328 |
# test |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
329 |
return os.path.exists(path) |
41 | 330 |
|
54
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
331 |
# XXX: move to LogFile |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
332 |
except IOError, e : |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
333 |
# return None for missing files |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
334 |
if e.errno == errno.ENOENT : |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
335 |
return None |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
336 |
|
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
337 |
else : |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
338 |
raise |
41 | 339 |
|
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
340 |
def _iter_date_reverse (self, dt=None) : |
41 | 341 |
""" |
342 |
Yields an infinite series of naive date objects in our timezone, iterating backwards in time starting at the |
|
343 |
given *datetime*, or the the current date, if none given |
|
344 |
""" |
|
345 |
||
346 |
# default to now |
|
347 |
if not dt : |
|
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
348 |
dt = datetime.datetime.now(pytz.utc) |
41 | 349 |
|
350 |
# convert to target timezone |
|
351 |
dtz = dt.astimezone(self.tz) |
|
352 |
||
353 |
# our timedelta |
|
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
354 |
ONE_DAY = datetime.timedelta(1) |
41 | 355 |
|
356 |
# iterate unto infinity |
|
357 |
while True : |
|
358 |
# yield |
|
359 |
yield dtz.date() |
|
360 |
||
361 |
# one day sdrawkcab |
|
362 |
dtz -= ONE_DAY |
|
363 |
||
63 | 364 |
def _iter_logfile_reverse (self, dt=None, max_files=100) : |
41 | 365 |
""" |
63 | 366 |
Yields a series of LogFile objects, iterating backwards in time starting at the given datetime, or the |
367 |
current date, if none given. |
|
368 |
||
369 |
Reads/probes at most max_files files. |
|
41 | 370 |
""" |
371 |
||
63 | 372 |
# start counting at zero... |
373 |
file_count = 0 |
|
48 | 374 |
|
63 | 375 |
# iterate backwards over days |
376 |
for day in self._iter_date_reverse(dt) : |
|
377 |
# stop if we've handled enough files by now |
|
378 |
if file_count > max_files : |
|
379 |
break |
|
380 |
||
381 |
# try and open the next logfile |
|
41 | 382 |
logfile = None |
383 |
||
63 | 384 |
file_count += 1 |
385 |
logfile = self._get_logfile_date(day) |
|
54
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
386 |
|
63 | 387 |
# no logfile there? |
54
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
388 |
if not logfile : |
63 | 389 |
# if we didn't find any logfiles at all, terminate rudely |
390 |
if file_count > max_files : |
|
41 | 391 |
raise Exception("No recent logfiles found") |
392 |
||
393 |
else : |
|
394 |
# skip to next day |
|
395 |
continue |
|
48 | 396 |
|
63 | 397 |
# yield it |
398 |
yield logfile |
|
399 |
||
400 |
def get_latest (self, count) : |
|
401 |
""" |
|
402 |
Uses _iter_backwards + _get_logfile_date to read the yield the given lines from as many logfiles as needed |
|
403 |
""" |
|
404 |
||
405 |
# iterate over logfiles |
|
406 |
iter = self._iter_logfile_reverse() |
|
407 |
||
408 |
# read the events into here |
|
409 |
lines = [] |
|
410 |
||
411 |
# loop until done |
|
412 |
while len(lines) < count : |
|
413 |
# next logfile |
|
414 |
logfile = iter.next() |
|
415 |
||
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
416 |
# read the events |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
417 |
# XXX: use a queue |
54
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
418 |
lines = list(logfile.read_latest(count)) + lines |
48 | 419 |
|
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
420 |
# return the events |
48 | 421 |
return lines |
41 | 422 |
|
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
423 |
def get_date (self, dt) : |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
424 |
""" |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
425 |
A 'day' is considered to be a 24-hour period from 00:00:00 23:59:59. If the timezone of the given datetime |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
426 |
differs from our native datetime, this may involve lines from more than one logfile. |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
427 |
""" |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
428 |
|
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
429 |
# begin/end of 24h period, in target timezone |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
430 |
dtz_begin = dt.replace(hour=0, minute=0, second=0).astimezone(self.tz) |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
431 |
dtz_end = dt.replace(hour=23, minute=59, second=59, microsecond=999999).astimezone(self.tz) |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
432 |
|
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
433 |
# as dates |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
434 |
d_begin = dtz_begin.date() |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
435 |
d_end = dtz_end.date() |
72
5ade0288f2ec
implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents:
65
diff
changeset
|
436 |
|
5ade0288f2ec
implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents:
65
diff
changeset
|
437 |
|
5ade0288f2ec
implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents:
65
diff
changeset
|
438 |
# print "LogDirectory.get_date - %s" % dt |
5ade0288f2ec
implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents:
65
diff
changeset
|
439 |
# print "\t %s %s" % (d_begin, dtz_begin) |
5ade0288f2ec
implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents:
65
diff
changeset
|
440 |
# print "\t-> %s %s" % (d_end, dtz_end) |
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
441 |
|
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
442 |
# if they're the same, just pull the full log for that date |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
443 |
if d_begin == d_end : |
64
cdb6403c2498
beginnings of a LogSearchIndex class
Tero Marttila <terom@fixme.fi>
parents:
63
diff
changeset
|
444 |
# open that log |
cdb6403c2498
beginnings of a LogSearchIndex class
Tero Marttila <terom@fixme.fi>
parents:
63
diff
changeset
|
445 |
logfile = self._get_logfile_date(d_begin) |
cdb6403c2498
beginnings of a LogSearchIndex class
Tero Marttila <terom@fixme.fi>
parents:
63
diff
changeset
|
446 |
|
cdb6403c2498
beginnings of a LogSearchIndex class
Tero Marttila <terom@fixme.fi>
parents:
63
diff
changeset
|
447 |
if not logfile : |
cdb6403c2498
beginnings of a LogSearchIndex class
Tero Marttila <terom@fixme.fi>
parents:
63
diff
changeset
|
448 |
raise Exception("No logfile for date=%r" % (dt, )) |
cdb6403c2498
beginnings of a LogSearchIndex class
Tero Marttila <terom@fixme.fi>
parents:
63
diff
changeset
|
449 |
|
cdb6403c2498
beginnings of a LogSearchIndex class
Tero Marttila <terom@fixme.fi>
parents:
63
diff
changeset
|
450 |
# return the full data |
cdb6403c2498
beginnings of a LogSearchIndex class
Tero Marttila <terom@fixme.fi>
parents:
63
diff
changeset
|
451 |
return logfile.read_full() |
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
452 |
|
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
453 |
# otherwise, we need to pull two partial logs |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
454 |
else : |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
455 |
# open both of them |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
456 |
f_begin = self._get_logfile_date(d_begin) |
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
457 |
f_end = self._get_logfile_date(d_end) |
76
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
458 |
|
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
459 |
# chain together the two sources |
55 | 460 |
return itertools.chain( |
461 |
f_begin.read_from(dtz_begin), |
|
462 |
f_end.read_until(dtz_end) if f_end else [] |
|
463 |
) |
|
50
f13cf27a360b
implement more LogSource features (logs for date, cleanup last_logs), implement irssi parser, formatter, other misc. stuff
Tero Marttila <terom@fixme.fi>
parents:
48
diff
changeset
|
464 |
|
54
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
465 |
def get_month_days (self, month) : |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
466 |
""" |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
467 |
Returns a set of dates for which logfiles are available in the given datetime's month |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
468 |
""" |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
469 |
|
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
470 |
# the set of days |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
471 |
days = set() |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
472 |
|
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
473 |
# iterate over month's days using Calendar |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
474 |
for date in calendar.Calendar().itermonthdates(month.year, month.month) : |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
475 |
# convert date to target datetime |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
476 |
dtz = month.tzinfo.localize(datetime.datetime.combine(date, datetime.time(0))).astimezone(self.tz) |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
477 |
|
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
478 |
# date in our target timezone |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
479 |
log_date = dtz.date() |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
480 |
|
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
481 |
# test for it |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
482 |
if self._get_logfile_date(log_date, load=False) : |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
483 |
# add to set |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
484 |
days.add(date) |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
485 |
|
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
486 |
# return set |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
487 |
return days |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
488 |