]> git.donarmstrong.com Git - neurodebian.git/blob - tools/nd_apachelogs2subscriptionstats
Use builting set() instead of deprecated sets module
[neurodebian.git] / tools / nd_apachelogs2subscriptionstats
1 #!/usr/bin/python
2 # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
3 # vi: set ft=python sts=4 ts=4 sw=4 et:
4 #
5 import fileinput
6 import sys
7 import time
8 from datetime import datetime
9 import re
10 import json
11 import operator
12
13
14 releases = {
15     'etch': 'Debian GNU/Linux 4.0 (etch)',
16     'lenny': 'Debian GNU/Linux 5.0 (lenny)',
17     'squeeze': 'Debian GNU/Linux 6.0 (squeeze)',
18     'wheezy': 'Debian GNU/Linux 7.0 (wheezy)',
19     'jessie': 'Debian testing (jessie)',
20     'sid': 'Debian unstable (sid)',
21     'hardy': 'Ubuntu 08.04 LTS "Hardy Heron" (hardy)',
22     'jaunty': 'Ubuntu 09.04 "Jaunty Jackalope" (jaunty)',
23     'karmic': 'Ubuntu 09.10 "Karmic Koala" (karmic)',
24     'lucid': 'Ubuntu 10.04 LTS "Lucid Lynx" (lucid)',
25     'maverick': 'Ubuntu 10.10 "Maverick Meerkat" (maverick)',
26     'natty': 'Ubuntu 11.04 "Natty Narwhal" (natty)',
27     'oneiric': 'Ubuntu 11.10 "Oneiric Ocelot" (oneiric)',
28     'precise': 'Ubuntu 12.04 LTS "Precise Pangolin" (precise)',
29     'quantal': 'Ubuntu 12.10 "Quantal Quetzal" (quantal)',
30     'raring': 'Ubuntu 13.04 "Raring Ringtail" (raring)',
31     'saucy': 'Ubuntu 13.10 "Saucy Salamander" (saucy)',
32 }
33
34
35 if __name__ == '__main__':
36     data = {}
37     # get the IP, date and target release
38     # the date is truncated to a month/year combo
39     listget = re.compile(r'^([0-9.:]*) .*\[([^:]*).*GET /lists/([a-z]*)')
40     for line in fileinput.FileInput(openhook=fileinput.hook_compressed):
41         match = listget.match(line)
42         if not match:
43             continue
44         addr, date, release = match.groups()
45         if not release in releases:
46             # ignore fantasy names
47             continue
48         date = datetime.strptime(date, '%d/%b/%Y')
49         # truncate to a week
50         try:
51             date = datetime(date.year, date.month, date.day / 7 * 7 + 1)
52         except ValueError:
53             # only on Feb28...
54             date = datetime(date.year, date.month, date.day / 7 * 7)
55         # microseconds since epoch
56         date = int(time.mktime(date.timetuple()) * 1000)
57         rstats = data.setdefault(releases[release], {})
58         rtime = rstats.setdefault(date, 0)
59         rtime += 1
60         rstats[date] = rtime
61         data[releases[release]] = rstats
62     # determine the union of all timestamps
63     timestamps = set()
64     for codename, stats in data.iteritems():
65         timestamps.union_update(stats.keys())
66     export = [{'key': release,
67                'values': [[ts, float(data[release].setdefault(ts, 0)) / 7]
68                         for ts in sorted(timestamps)]}
69                                     for release in sorted(data)]
70     print json.dumps(export)