Skip to content

Commit c26bbdf

Browse files
committed
Write log_extract.py to extract timestamps as datetime objects
1 parent 3ffd6cb commit c26bbdf

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

days/01-03-datetimes/code/log_example

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
ERROR 2014-07-03T23:24:31 supybot Invalid user dictionary file, resetting to empty.
2+
ERROR 2014-07-03T23:24:31 supybot Exact error: IOError: [Errno 2] No such file or directory: 'conf/users.conf'
3+
ERROR 2014-07-03T23:24:31 supybot Invalid channel database, resetting to empty.
4+
ERROR 2014-07-03T23:24:31 supybot Exact error: IOError: [Errno 2] No such file or directory: 'conf/channels.conf'
5+
WARNING 2014-07-03T23:24:31 supybot Couldn't open ignore database: [Errno 2] No such file or directory: 'conf/ignores.conf'
6+
INFO 2014-07-03T23:27:51 supybot Shutdown initiated.
7+
INFO 2014-07-03T23:27:51 supybot Killing Driver objects.
8+
INFO 2014-07-03T23:27:51 supybot Killing Irc objects.
9+
INFO 2014-07-03T23:27:51 supybot Shutdown complete.
10+
INFO 2014-07-03T23:30:37 supybot Creating new Irc for freenode.
11+
INFO 2014-07-03T23:30:37 supybot Connecting to irc.freenode.net:8001.
12+
INFO 2014-07-03T23:30:38 supybot Loading plugins (connecting to freenode).
13+
INFO 2014-07-03T23:30:46 supybot Server orwell.freenode.net has version ircd-seven-1.1.3
14+
INFO 2014-07-03T23:30:48 supybot Got end of MOTD from orwell.freenode.net
15+
INFO 2014-07-03T23:30:54 supybot Join to #timvideos on freenode synced in 2.41 seconds.
16+
INFO 2014-07-03T23:31:22 supybot Exiting due to Ctrl-C. If the bot doesn't exit within a few seconds, feel free to press Ctrl-C again to make it exit without flushing its message queues.
17+
INFO 2014-07-03T23:31:22 supybot Flushers flushed and garbage collected.
18+
INFO 2014-07-03T23:31:22 supybot Driver for Irc object for freenode dying.
19+
INFO 2014-07-03T23:31:22 supybot Irc object for freenode dying.
20+
INFO 2014-07-03T23:31:22 supybot Driver for Irc object for freenode dying.
21+
WARNING 2014-07-03T23:31:22 supybot Disconnect from irc.freenode.net:8001: error: [Errno 9] Bad file descriptor.
22+
INFO 2014-07-03T23:31:22 supybot Reconnecting to freenode at 2014-07-03T23:31:32.
23+
INFO 2014-07-03T23:31:22 supybot Removing driver SocketDriver(Irc object for freenode).
24+
INFO 2014-07-03T23:31:22 supybot Total uptime: 45 seconds.
25+
INFO 2014-07-03T23:31:22 supybot Total CPU time taken: 1.12 seconds.
26+
INFO 2014-07-03T23:31:22 supybot No more Irc objects, exiting.
27+
INFO 2014-07-03T23:31:22 supybot Shutdown initiated.
28+
INFO 2014-07-03T23:31:22 supybot Killing Driver objects.
29+
INFO 2014-07-03T23:31:22 supybot Killing Irc objects.
30+
INFO 2014-07-03T23:31:22 supybot Writing registry file to planet-news.conf
31+
INFO 2014-07-03T23:31:22 supybot Finished writing registry file.
32+
INFO 2014-07-03T23:31:22 supybot Shutdown complete.
33+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'''Extract datetimes from log entries and calculate the time
2+
between the first and last shutdown events'''
3+
4+
from datetime import datetime
5+
import os
6+
import urllib.request
7+
import re
8+
9+
SHUTDOWN_EVENT = 'Shutdown initiated'
10+
11+
# read in the log file
12+
logfile = os.path.join('/tmp', 'log')
13+
urllib.request.urlretrieve('http://bit.ly/2AKSIbf', logfile)
14+
with open(logfile) as f:
15+
loglines = f.readlines()
16+
17+
18+
def convert_to_datetime(line):
19+
'''TODO 1:
20+
Given a log line extract its timestamp and convert it to a datetime
21+
object.
22+
For example calling the function with:
23+
INFO 2014-07-03T23:27:51 supybot Shutdown complete.
24+
returns:
25+
datetime(2014, 7, 3, 23, 27, 51)'''
26+
timestamp = re.findall(r'\d{4}-\d{2}-\w+:\d{2}:\d{2}', line)
27+
timestamp_fmt = re.sub('T', ' ', timestamp[0])
28+
# print('Found timestamp: ', timestamp_fmt)
29+
# print(datetime.strptime(timestamp_fmt, "%Y-%m-%d %H:%M:%S"))
30+
d = datetime.strptime(timestamp_fmt, "%Y-%m-%d %H:%M:%S")
31+
return d
32+
33+
34+
def time_between_shutdowns(loglines):
35+
'''TODO 2:
36+
Extract shutdown events ("Shutdown initiated") from loglines and
37+
calculate the
38+
timedelta between the first and last one.
39+
Return this datetime.timedelta object.'''
40+
lines = []
41+
for line in loglines:
42+
if 'Shutdown initiated' in line:
43+
lines.append(line)
44+
45+
delta = convert_to_datetime(lines[len(lines)-1]) - convert_to_datetime(lines[0])
46+
# print(delta)
47+
return delta
48+
49+
50+
for line in loglines:
51+
convert_to_datetime(line)
52+
53+
time_between_shutdowns(loglines)

0 commit comments

Comments
 (0)