Skip to content
Snippets Groups Projects
Verified Commit 0b6b6e63 authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

Generate stats independent of exporters.

parent e850fe71
No related branches found
No related tags found
No related merge requests found
...@@ -57,7 +57,7 @@ def stats(): ...@@ -57,7 +57,7 @@ def stats():
""" Command for all kinds of statistics """ """ Command for all kinds of statistics """
parser = ArgumentParser(parents=[_PARENT_PARSER]) parser = ArgumentParser(parents=[_PARENT_PARSER])
parser.add_argument('--found-by', help='limit caches found by cacher name', default=None) parser.add_argument('--geocacher', help='name of geocacher to produce stats for', default=None)
parser.add_argument('--format', help='output format', default='json') parser.add_argument('--format', help='output format', default='json')
args = parser.parse_args() args = parser.parse_args()
...@@ -66,8 +66,7 @@ def stats(): ...@@ -66,8 +66,7 @@ def stats():
config = get_config(args.config) config = get_config(args.config)
database = get_db(config) database = get_db(config)
exporter = CacheExporter(config, database) data = all_stats(database, geocacher_name=args.geocacher)
data = all_stats(exporter.export(found_by=args.found_by))
if args.format == 'json': if args.format == 'json':
print(stats_to_json(data)) print(stats_to_json(data))
......
from ..model import Geocacher
STATS_PLUGINS = [] STATS_PLUGINS = []
def stats_plugin(func): def stats_plugin(func):
STATS_PLUGINS.append(func) STATS_PLUGINS.append(func)
return func return func
def all_stats(data): def all_stats(db, geocacher_name):
geocacher = db.Session().query(Geocacher).filter_by(name=geocacher_name).one()
res = {} res = {}
for plugin in STATS_PLUGINS: for plugin in STATS_PLUGINS:
res[plugin.__name__] = plugin(data) res[plugin.__name__] = plugin(geocacher)
return res return res
from .basic import * from .basic import *
from . import stats_plugin from . import stats_plugin
def _field_count(data, field): def _field_count(geocacher, field):
res = {} res = {}
for cache in data: for cache in geocacher.found_geocaches:
if not cache[field] in res: value = getattr(cache, field)
res[cache[field]] = 0 if not value in res:
res[cache[field]] += 1 res[value] = 0
res[value] += 1
return res return res
@stats_plugin @stats_plugin
def type_count(data): def type_count(geocacher):
return _field_count(data, 'type') return _field_count(geocacher, 'type')
@stats_plugin @stats_plugin
def size_count(data): def size_count(geocacher):
return _field_count(data, 'size') return _field_count(geocacher, 'size')
@stats_plugin @stats_plugin
def difficulty_count(data): def difficulty_count(geocacher):
return _field_count(data, 'difficulty') return _field_count(geocacher, 'difficulty')
@stats_plugin @stats_plugin
def terrain_count(data): def terrain_count(geocacher):
return _field_count(data, 'terrain') return _field_count(geocacher, 'terrain')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment