author | Tero Marttila <terom@fixme.fi> |
Tue, 10 Feb 2009 01:05:35 +0200 | |
changeset 77 | 4287fb77e312 |
parent 76 | cc3ab2c39ded |
child 78 | 85345abbd46a |
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 |
|
77
4287fb77e312
implement max_pages, and paginate channel_date by default now
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
45 |
# 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
|
46 |
line_count = 0 |
4287fb77e312
implement max_pages, and paginate channel_date by default now
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
47 |
|
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
|
48 |
# 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
|
49 |
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
|
50 |
|
cc3ab2c39ded
fix off-by-one with search paginate, and implement basic pagination for channel_date
Tero Marttila <terom@fixme.fi>
parents:
73
diff
changeset
|
51 |
# 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
|
52 |
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
|
53 |
# count them |
4287fb77e312
implement max_pages, and paginate channel_date by default now
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
54 |
line_count += 1 |
4287fb77e312
implement max_pages, and paginate channel_date by default now
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
55 |
|
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
|
56 |
# 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
|
57 |
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
|
58 |
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
|
59 |
continue |
77
4287fb77e312
implement max_pages, and paginate channel_date by default now
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
60 |
|
4287fb77e312
implement max_pages, and paginate channel_date by default now
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
61 |
# already full? |
4287fb77e312
implement max_pages, and paginate channel_date by default now
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
62 |
if len(lines) > count : |
4287fb77e312
implement max_pages, and paginate channel_date by default now
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
63 |
continue |
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 |
# 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
|
66 |
lines.append(line) |
77
4287fb77e312
implement max_pages, and paginate channel_date by default now
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
67 |
|
4287fb77e312
implement max_pages, and paginate channel_date by default now
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
68 |
# calculate max_pages |
4287fb77e312
implement max_pages, and paginate channel_date by default now
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
69 |
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
|
70 |
|
4287fb77e312
implement max_pages, and paginate channel_date by default now
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
71 |
# return |
4287fb77e312
implement max_pages, and paginate channel_date by default now
Tero Marttila <terom@fixme.fi>
parents:
76
diff
changeset
|
72 |
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
|
73 |
|
54
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
74 |
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
|
75 |
""" |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
76 |
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
|
77 |
""" |
41 | 78 |
|
54
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
79 |
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
|
80 |
|
54
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
81 |
class LogFile (object) : |
41 | 82 |
""" |
83 |
A file containing LogEvents |
|
54
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
84 |
|
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
85 |
XXX: modify to implement LogSource? |
41 | 86 |
""" |
87 |
||
73
5a7188bf2894
split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
88 |
def __init__ (self, path, parser, charset, start_date=None, sep='\n') : |
41 | 89 |
""" |
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
|
90 |
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
|
91 |
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
|
92 |
LogParser for more info. XXX: currently we assume start_date also for the end of the file |
41 | 93 |
""" |
94 |
||
95 |
# store |
|
96 |
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
|
97 |
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
|
98 |
self.start_date = start_date |
41 | 99 |
self.charset = charset |
100 |
self.sep = sep |
|
101 |
||
102 |
# open |
|
48 | 103 |
self.file = open(path, 'rb') |
41 | 104 |
|
105 |
def __iter__ (self) : |
|
106 |
""" |
|
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
|
107 |
Yields a series of unicode lines, as read from the top of the file |
41 | 108 |
""" |
109 |
||
110 |
# seek to beginning |
|
111 |
self.file.seek(0) |
|
112 |
||
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
|
113 |
# iterate over lines, decoding them as well |
65 | 114 |
return (line.decode(self.charset).rstrip(self.sep) for line in self.file) |
41 | 115 |
|
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
|
116 |
def read_full (self) : |
41 | 117 |
""" |
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
|
118 |
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
|
119 |
""" |
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 |
|
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 |
# just use our __iter__ |
64
cdb6403c2498
beginnings of a LogSearchIndex class
Tero Marttila <terom@fixme.fi>
parents:
63
diff
changeset
|
122 |
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
|
123 |
|
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
|
124 |
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
|
125 |
""" |
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 |
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
|
127 |
""" |
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
|
128 |
|
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
|
129 |
# 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
|
130 |
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
|
131 |
|
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
|
132 |
# 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
|
133 |
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
|
134 |
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
|
135 |
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
|
136 |
|
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
|
137 |
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
|
138 |
# 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
|
139 |
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
|
140 |
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
|
141 |
|
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 |
# 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
|
143 |
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
|
144 |
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
|
145 |
|
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
|
146 |
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
|
147 |
""" |
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 |
Reads all LogLines up until the given naive timestamp |
41 | 149 |
""" |
150 |
||
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
|
151 |
# 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
|
152 |
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
|
153 |
|
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 |
# 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
|
155 |
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
|
156 |
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
|
157 |
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
|
158 |
|
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 |
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
|
160 |
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
|
161 |
|
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 |
# 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
|
163 |
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
|
164 |
|
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 |
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
|
166 |
""" |
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 |
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
|
168 |
""" |
41 | 169 |
|
170 |
# seek to end of file |
|
171 |
self.file.seek(0, os.SEEK_END) |
|
172 |
||
173 |
# read offset |
|
48 | 174 |
# XXX: hack -1 to get rid of trailing newline |
175 |
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
|
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 |
# 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
|
178 |
while offset > 0: |
48 | 179 |
# 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
|
180 |
if offset > blocksize : |
48 | 181 |
# 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
|
182 |
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
|
183 |
read_size = blocksize |
41 | 184 |
|
48 | 185 |
else : |
186 |
# partial block |
|
187 |
read_size = offset |
|
41 | 188 |
offset = 0 |
189 |
||
43
fc11c4e86a82
implement channel_view count, the query stuff, css, layout all need some cleanup :(
Tero Marttila <terom@fixme.fi>
parents:
41
diff
changeset
|
190 |
# seek to offset |
41 | 191 |
self.file.seek(offset) |
192 |
||
48 | 193 |
# 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
|
194 |
block = self.file.read(read_size) |
41 | 195 |
|
48 | 196 |
# 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
|
197 |
assert len(block) == read_size |
41 | 198 |
|
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
|
199 |
# 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
|
200 |
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
|
201 |
|
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
|
202 |
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
|
203 |
""" |
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
|
204 |
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
|
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 |
|
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 |
# 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
|
208 |
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
|
209 |
|
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
|
210 |
# 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
|
211 |
for block in self._read_blocks_reverse() : |
41 | 212 |
# 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
|
213 |
buf = block + buf |
41 | 214 |
|
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
|
215 |
# 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
|
216 |
lines = buf.split(self.sep) |
41 | 217 |
|
218 |
# 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
|
219 |
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
|
220 |
|
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
|
221 |
# 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
|
222 |
# 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
|
223 |
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
|
224 |
yield line.decode(self.charset) |
41 | 225 |
|
54
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
226 |
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
|
227 |
""" |
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 |
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
|
229 |
""" |
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 |
# 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
|
232 |
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
|
233 |
|
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 |
# 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
|
235 |
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
|
236 |
# 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
|
237 |
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
|
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 |
# 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
|
240 |
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
|
241 |
break |
48 | 242 |
|
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
|
243 |
# 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
|
244 |
# 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
|
245 |
# 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
|
246 |
return self.parser.parse_lines(reversed(lines), self.start_date) |
41 | 247 |
|
248 |
class LogDirectory (LogSource) : |
|
249 |
""" |
|
250 |
A directory containing a series of timestamped LogFiles |
|
251 |
""" |
|
252 |
||
73
5a7188bf2894
split defined configuration constants into config, and implement search result pagination
Tero Marttila <terom@fixme.fi>
parents:
72
diff
changeset
|
253 |
def __init__ (self, path, tz, parser, charset, filename_fmt) : |
41 | 254 |
""" |
255 |
Load the logfiles at the given path. |
|
256 |
||
257 |
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
|
258 |
date format, and will be parsed using the given parser. |
41 | 259 |
""" |
260 |
||
261 |
# store |
|
262 |
self.path = path |
|
263 |
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
|
264 |
self.parser = parser |
41 | 265 |
self.charset = charset |
266 |
self.filename_fmt = filename_fmt |
|
267 |
||
268 |
def _get_logfile_datetime (self, dt) : |
|
269 |
""" |
|
270 |
Get the logfile corresponding to the given datetime |
|
271 |
""" |
|
272 |
||
273 |
# convert to target timezone |
|
274 |
dtz = dt.astimezone(self.tz) |
|
275 |
||
276 |
# convert to date and use that |
|
277 |
return self._get_logfile_date(dtz.date()) |
|
278 |
||
54
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
279 |
def _get_logfile_date (self, d, load=True) : |
41 | 280 |
""" |
54
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
281 |
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
|
282 |
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
|
283 |
|
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
284 |
Returns None if the logfile does not exist. |
41 | 285 |
""" |
286 |
||
287 |
# format filename |
|
288 |
filename = d.strftime(self.filename_fmt) |
|
289 |
||
290 |
# build path |
|
291 |
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
|
292 |
|
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
293 |
try : |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
294 |
if load : |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
295 |
# 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
|
296 |
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
|
297 |
|
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
298 |
else : |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
299 |
# test |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
300 |
return os.path.exists(path) |
41 | 301 |
|
54
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
302 |
# XXX: move to LogFile |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
303 |
except IOError, e : |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
304 |
# return None for missing files |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
305 |
if e.errno == errno.ENOENT : |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
306 |
return None |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
307 |
|
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
308 |
else : |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
309 |
raise |
41 | 310 |
|
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
|
311 |
def _iter_date_reverse (self, dt=None) : |
41 | 312 |
""" |
313 |
Yields an infinite series of naive date objects in our timezone, iterating backwards in time starting at the |
|
314 |
given *datetime*, or the the current date, if none given |
|
315 |
""" |
|
316 |
||
317 |
# default to now |
|
318 |
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
|
319 |
dt = datetime.datetime.now(pytz.utc) |
41 | 320 |
|
321 |
# convert to target timezone |
|
322 |
dtz = dt.astimezone(self.tz) |
|
323 |
||
324 |
# 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
|
325 |
ONE_DAY = datetime.timedelta(1) |
41 | 326 |
|
327 |
# iterate unto infinity |
|
328 |
while True : |
|
329 |
# yield |
|
330 |
yield dtz.date() |
|
331 |
||
332 |
# one day sdrawkcab |
|
333 |
dtz -= ONE_DAY |
|
334 |
||
63 | 335 |
def _iter_logfile_reverse (self, dt=None, max_files=100) : |
41 | 336 |
""" |
63 | 337 |
Yields a series of LogFile objects, iterating backwards in time starting at the given datetime, or the |
338 |
current date, if none given. |
|
339 |
||
340 |
Reads/probes at most max_files files. |
|
41 | 341 |
""" |
342 |
||
63 | 343 |
# start counting at zero... |
344 |
file_count = 0 |
|
48 | 345 |
|
63 | 346 |
# iterate backwards over days |
347 |
for day in self._iter_date_reverse(dt) : |
|
348 |
# stop if we've handled enough files by now |
|
349 |
if file_count > max_files : |
|
350 |
break |
|
351 |
||
352 |
# try and open the next logfile |
|
41 | 353 |
logfile = None |
354 |
||
63 | 355 |
file_count += 1 |
356 |
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
|
357 |
|
63 | 358 |
# no logfile there? |
54
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
359 |
if not logfile : |
63 | 360 |
# if we didn't find any logfiles at all, terminate rudely |
361 |
if file_count > max_files : |
|
41 | 362 |
raise Exception("No recent logfiles found") |
363 |
||
364 |
else : |
|
365 |
# skip to next day |
|
366 |
continue |
|
48 | 367 |
|
63 | 368 |
# yield it |
369 |
yield logfile |
|
370 |
||
371 |
def get_latest (self, count) : |
|
372 |
""" |
|
373 |
Uses _iter_backwards + _get_logfile_date to read the yield the given lines from as many logfiles as needed |
|
374 |
""" |
|
375 |
||
376 |
# iterate over logfiles |
|
377 |
iter = self._iter_logfile_reverse() |
|
378 |
||
379 |
# read the events into here |
|
380 |
lines = [] |
|
381 |
||
382 |
# loop until done |
|
383 |
while len(lines) < count : |
|
384 |
# next logfile |
|
385 |
logfile = iter.next() |
|
386 |
||
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
|
387 |
# 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
|
388 |
# XXX: use a queue |
54
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
389 |
lines = list(logfile.read_latest(count)) + lines |
48 | 390 |
|
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
|
391 |
# return the events |
48 | 392 |
return lines |
41 | 393 |
|
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
|
394 |
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
|
395 |
""" |
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
|
396 |
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
|
397 |
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
|
398 |
""" |
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
|
399 |
|
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
|
400 |
# 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
|
401 |
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
|
402 |
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
|
403 |
|
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
|
404 |
# 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
|
405 |
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
|
406 |
d_end = dtz_end.date() |
72
5ade0288f2ec
implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents:
65
diff
changeset
|
407 |
|
5ade0288f2ec
implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents:
65
diff
changeset
|
408 |
|
5ade0288f2ec
implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents:
65
diff
changeset
|
409 |
# print "LogDirectory.get_date - %s" % dt |
5ade0288f2ec
implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents:
65
diff
changeset
|
410 |
# print "\t %s %s" % (d_begin, dtz_begin) |
5ade0288f2ec
implement line-links as UTC timestamps
Tero Marttila <terom@fixme.fi>
parents:
65
diff
changeset
|
411 |
# 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
|
412 |
|
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
|
413 |
# 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
|
414 |
if d_begin == d_end : |
64
cdb6403c2498
beginnings of a LogSearchIndex class
Tero Marttila <terom@fixme.fi>
parents:
63
diff
changeset
|
415 |
# open that log |
cdb6403c2498
beginnings of a LogSearchIndex class
Tero Marttila <terom@fixme.fi>
parents:
63
diff
changeset
|
416 |
logfile = self._get_logfile_date(d_begin) |
cdb6403c2498
beginnings of a LogSearchIndex class
Tero Marttila <terom@fixme.fi>
parents:
63
diff
changeset
|
417 |
|
cdb6403c2498
beginnings of a LogSearchIndex class
Tero Marttila <terom@fixme.fi>
parents:
63
diff
changeset
|
418 |
if not logfile : |
cdb6403c2498
beginnings of a LogSearchIndex class
Tero Marttila <terom@fixme.fi>
parents:
63
diff
changeset
|
419 |
raise Exception("No logfile for date=%r" % (dt, )) |
cdb6403c2498
beginnings of a LogSearchIndex class
Tero Marttila <terom@fixme.fi>
parents:
63
diff
changeset
|
420 |
|
cdb6403c2498
beginnings of a LogSearchIndex class
Tero Marttila <terom@fixme.fi>
parents:
63
diff
changeset
|
421 |
# return the full data |
cdb6403c2498
beginnings of a LogSearchIndex class
Tero Marttila <terom@fixme.fi>
parents:
63
diff
changeset
|
422 |
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
|
423 |
|
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 |
# 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
|
425 |
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
|
426 |
# 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
|
427 |
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
|
428 |
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
|
429 |
|
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
|
430 |
# chain together the two sources |
55 | 431 |
return itertools.chain( |
432 |
f_begin.read_from(dtz_begin), |
|
433 |
f_end.read_until(dtz_end) if f_end else [] |
|
434 |
) |
|
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
|
435 |
|
54
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
436 |
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
|
437 |
""" |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
438 |
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
|
439 |
""" |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
440 |
|
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
441 |
# the set of days |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
442 |
days = set() |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
443 |
|
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
444 |
# 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
|
445 |
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
|
446 |
# convert date to target datetime |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
447 |
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
|
448 |
|
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
449 |
# date in our target timezone |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
450 |
log_date = dtz.date() |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
451 |
|
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
452 |
# test for it |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
453 |
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
|
454 |
# add to set |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
455 |
days.add(date) |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
456 |
|
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
457 |
# return set |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
458 |
return days |
b65a95eb9f6b
implement browse-by-date to show a nice calendar
Tero Marttila <terom@fixme.fi>
parents:
50
diff
changeset
|
459 |