2 # -*- coding: utf-8 -*-
4 # stats.py - part of the FDroid server tools
5 # Copyright (C) 2010-13, Ciaran Gultnieks, ciaran@ciarang.com
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
26 from optparse import OptionParser
28 import common, metadata
32 def carbon_send(key, value):
34 s.connect((config['carbon_host'], config['carbon_port']))
35 msg = '%s %d %d\n' % (key, value, int(time.time()))
44 global options, config
46 # Parse command line...
47 parser = OptionParser()
48 parser.add_option("-v", "--verbose", action="store_true", default=False,
49 help="Spew out even more information than normal")
50 parser.add_option("-d", "--download", action="store_true", default=False,
51 help="Download logs we don't have")
52 parser.add_option("--nologs", action="store_true", default=False,
53 help="Don't do anything logs-related")
54 (options, args) = parser.parse_args()
56 config = common.read_config(options)
58 if not config['update_stats']:
59 print "Stats are disabled - check your configuration"
62 # Get all metadata-defined apps...
63 metaapps = metadata.read_metadata(options.verbose)
66 logsdir = os.path.join(statsdir, 'logs')
67 datadir = os.path.join(statsdir, 'data')
68 if not os.path.exists(statsdir):
70 if not os.path.exists(logsdir):
72 if not os.path.exists(datadir):
76 # Get any access logs we don't have...
80 print 'Retrieving logs'
81 ssh = paramiko.SSHClient()
82 ssh.load_system_host_keys()
83 ssh.connect('f-droid.org', username='fdroid', timeout=10,
84 key_filename=config['webserver_keyfile'])
86 ftp.get_channel().settimeout(60)
92 if f.startswith('access-') and f.endswith('.log.gz'):
94 destpath = os.path.join(logsdir, f)
95 destsize = ftp.stat(f).st_size
96 if (not os.path.exists(destpath) or
97 os.path.getsize(destpath) != destsize):
98 print "...retrieving " + f
101 traceback.print_exc()
110 knownapks = common.KnownApks()
113 if not options.nologs:
116 print 'Processing logs...'
119 logexpr = '(?P<ip>[.:0-9a-fA-F]+) - - \[(?P<time>.*?)\] "GET (?P<uri>.*?) HTTP/1.\d" (?P<statuscode>\d+) \d+ "(?P<referral>.*?)" "(?P<useragent>.*?)"'
120 logsearch = re.compile(logexpr).search
121 for logfile in glob.glob(os.path.join(logsdir,'access-*.log.gz')):
123 print '...' + logfile
124 p = subprocess.Popen(["zcat", logfile], stdout = subprocess.PIPE)
125 matches = (logsearch(line) for line in p.stdout)
126 for match in matches:
127 if match and match.group('statuscode') == '200':
128 uri = match.group('uri')
129 if uri.endswith('.apk'):
130 _, apkname = os.path.split(uri)
131 app = knownapks.getapp(apkname)
138 # Strip the '.apk' from apkname
139 appVer = apkname[:-4]
140 if appVer in appsVer:
145 if not apkname in unknownapks:
146 unknownapks.append(apkname)
148 # Calculate and write stats for total downloads...
151 for app, count in apps.iteritems():
152 lst.append(app + " " + str(count))
153 if config['stats_to_carbon']:
154 carbon_send('fdroid.download.' + app.replace('.', '_'), count)
155 alldownloads += count
156 lst.append("ALL " + str(alldownloads))
157 f = open('stats/total_downloads_app.txt', 'w')
158 f.write('# Total downloads by application, since October 2011\n')
159 for line in sorted(lst):
163 f = open('stats/total_downloads_app_version.txt', 'w')
164 f.write('# Total downloads by application and version, since October 2011\n')
166 for appver, count in appsVer.iteritems():
167 lst.append(appver + " " + str(count))
168 for line in sorted(lst):
172 # Calculate and write stats for repo types...
174 print "Processing repo types..."
177 if len(app['Repo Type']) == 0:
180 if app['Repo Type'] == 'srclib':
181 rtype = common.getsrclibvcs(app['Repo'])
183 rtype = app['Repo Type']
184 if rtype in repotypes:
185 repotypes[rtype] += 1;
188 f = open('stats/repotypes.txt', 'w')
189 for rtype, count in repotypes.iteritems():
190 f.write(rtype + ' ' + str(count) + '\n')
193 # Calculate and write stats for update check modes...
195 print "Processing update check modes..."
198 checkmode = app['Update Check Mode'].split('/')[0]
199 if checkmode in ucms:
200 ucms[checkmode] += 1;
203 f = open('stats/update_check_modes.txt', 'w')
204 for checkmode, count in ucms.iteritems():
205 f.write(checkmode + ' ' + str(count) + '\n')
209 print "Processing categories..."
212 if app['Categories'] is None:
214 categories = [c.strip() for c in app['Categories'].split(',')]
215 for category in categories:
220 f = open('stats/categories.txt', 'w')
221 for category, count in ctgs.iteritems():
222 f.write(category + ' ' + str(count) + '\n')
226 print "Processing antifeatures..."
229 if app['AntiFeatures'] is None:
231 antifeatures = [a.strip() for a in app['AntiFeatures'].split(',')]
232 for antifeature in antifeatures:
233 if antifeature in afs:
234 afs[antifeature] += 1;
237 f = open('stats/antifeatures.txt', 'w')
238 for antifeature, count in afs.iteritems():
239 f.write(antifeature + ' ' + str(count) + '\n')
242 # Calculate and write stats for licenses...
244 print "Processing licenses..."
247 license = app['License']
248 if license in licenses:
249 licenses[license] += 1;
251 licenses[license] = 1
252 f = open('stats/licenses.txt', 'w')
253 for license, count in licenses.iteritems():
254 f.write(license + ' ' + str(count) + '\n')
257 # Write list of latest apps added to the repo...
259 print "Processing latest apps..."
260 latest = knownapks.getlatest(10)
261 f = open('stats/latestapps.txt', 'w')
267 print '\nUnknown apks:'
268 for apk in unknownapks:
273 if __name__ == "__main__":