Use Counter objects when processing logs

This should somewhat speed things up, and makes the code simpler
This commit is contained in:
Daniel Martí 2014-02-19 09:54:24 +01:00
parent 27c0caba60
commit f3db0003e7

View file

@ -29,6 +29,7 @@ import socket
import logging import logging
import common, metadata import common, metadata
import subprocess import subprocess
from collections import Counter
def carbon_send(key, value): def carbon_send(key, value):
s = socket.socket() s = socket.socket()
@ -114,8 +115,8 @@ def main():
if not options.nologs: if not options.nologs:
# Process logs # Process logs
logging.info('Processing logs...') logging.info('Processing logs...')
apps = {} appscount = Counter()
appsVer = {} appsvercount = Counter()
logexpr = '(?P<ip>[.:0-9a-fA-F]+) - - \[(?P<time>.*?)\] "GET (?P<uri>.*?) HTTP/1.\d" (?P<statuscode>\d+) \d+ "(?P<referral>.*?)" "(?P<useragent>.*?)"' logexpr = '(?P<ip>[.:0-9a-fA-F]+) - - \[(?P<time>.*?)\] "GET (?P<uri>.*?) HTTP/1.\d" (?P<statuscode>\d+) \d+ "(?P<referral>.*?)" "(?P<useragent>.*?)"'
logsearch = re.compile(logexpr).search logsearch = re.compile(logexpr).search
for logfile in glob.glob(os.path.join(logsdir,'access-*.log.gz')): for logfile in glob.glob(os.path.join(logsdir,'access-*.log.gz')):
@ -132,16 +133,10 @@ def main():
app = knownapks.getapp(apkname) app = knownapks.getapp(apkname)
if app: if app:
appid, _ = app appid, _ = app
if appid in apps: appscount[appid] += 1
apps[appid] += 1
else:
apps[appid] = 1
# Strip the '.apk' from apkname # Strip the '.apk' from apkname
appVer = apkname[:-4] appver = apkname[:-4]
if appVer in appsVer: appsvercount[appver] += 1
appsVer[appVer] += 1
else:
appsVer[appVer] = 1
else: else:
if not apkname in unknownapks: if not apkname in unknownapks:
unknownapks.append(apkname) unknownapks.append(apkname)
@ -149,7 +144,8 @@ def main():
# Calculate and write stats for total downloads... # Calculate and write stats for total downloads...
lst = [] lst = []
alldownloads = 0 alldownloads = 0
for app, count in apps.iteritems(): for appid in appscount:
count = appscount[appid]
lst.append(app + " " + str(count)) lst.append(app + " " + str(count))
if config['stats_to_carbon']: if config['stats_to_carbon']:
carbon_send('fdroid.download.' + app.replace('.', '_'), count) carbon_send('fdroid.download.' + app.replace('.', '_'), count)
@ -164,7 +160,8 @@ def main():
f = open('stats/total_downloads_app_version.txt', 'w') f = open('stats/total_downloads_app_version.txt', 'w')
f.write('# Total downloads by application and version, since October 2011\n') f.write('# Total downloads by application and version, since October 2011\n')
lst = [] lst = []
for appver, count in appsVer.iteritems(): for appver in appsvercount:
count = appsvercount[appver]
lst.append(appver + " " + str(count)) lst.append(appver + " " + str(count))
for line in sorted(lst): for line in sorted(lst):
f.write(line + "\n") f.write(line + "\n")