remove backported Counter class
[mygpo.git] / mygpo / db / couchdb / management / commands / count-view-usage.py
blob99f5fd4f89eacf4e3338721fa08086f640a21859
1 import gzip
2 from collections import Counter
4 from django.core.management.base import BaseCommand
5 from django.core.urlresolvers import resolve, Resolver404
8 class Command(BaseCommand):
9 """ Calculates the view-usage from CouchDB log files
11 log files can be gzipped. Output is printed to stdout """
13 def handle(self, *args, **options):
15 handlers = Counter()
17 show_max = None
19 for f in self.open_files(args):
20 for line in f:
22 # assume standard Apache log format
23 part = line.split(' ')[6]
25 try:
26 match = resolve(part)
27 except Resolver404:
28 pass
30 mod = match.func.__module__
31 key = (mod, match.func.__name__)
32 handlers[key] += 1
35 for (mod, funcname), n in handlers.most_common(show_max):
36 fqname = '{}.{}'.format(mod, funcname)
37 print '{:60} {:>7}'.format(fqname, n)
40 def open_files(self, files):
41 for filename in files:
42 if '.gz' in filename:
43 yield gzip.open(filename)
44 else:
45 yield open(filename)