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

Add basic stats functionality.

parent 18207b6d
No related branches found
No related tags found
No related merge requests found
......@@ -6,6 +6,8 @@ import logging
from .lib.adaptors.gpx import GpxImporter
from .lib.exporters.caches import CacheExporter
from .lib.exporters.formatters import caches_to_csv, caches_to_json
from .lib.stats import all_stats
from .lib.stats.formatters import stats_to_json
from .lib.config import get_default_config_path, get_config
from .lib.model import get_db
......@@ -50,3 +52,24 @@ def export_caches():
print(caches_to_json(data))
elif args.format == 'csv':
print(caches_to_csv(data))
def stats():
""" Command for all kinds of statistics """
parser = ArgumentParser(parents=[_PARENT_PARSER])
parser.add_argument('--found-by', help='limit caches found by cacher name', default=None)
parser.add_argument('--format', help='output format', default='json')
args = parser.parse_args()
logging.basicConfig(level=args.loglevel, format='%(levelname)-8s %(message)s')
config = get_config(args.config)
database = get_db(config)
exporter = CacheExporter(config, database)
data = all_stats(exporter.export(found_by=args.found_by))
if args.format == 'json':
print(stats_to_json(data))
# elif args.format == 'csv':
# print(caches_to_csv(data))
STATS_PLUGINS = []
def stats_plugin(func):
STATS_PLUGINS.append(func)
return func
def all_stats(data):
res = {}
for plugin in STATS_PLUGINS:
res[plugin.__name__] = plugin(data)
return res
from .basic import *
from . import stats_plugin
def _field_count(data, field):
res = {}
for cache in data:
if not cache[field] in res:
res[cache[field]] = 0
res[cache[field]] += 1
return res
@stats_plugin
def type_count(data):
return _field_count(data, 'type')
@stats_plugin
def size_count(data):
return _field_count(data, 'size')
@stats_plugin
def difficulty_count(data):
return _field_count(data, 'difficulty')
@stats_plugin
def terrain_count(data):
return _field_count(data, 'terrain')
import json
from ..util import json_serialise
def stats_to_json(data):
return json.dumps(data, default=json_serialise)
......@@ -22,7 +22,8 @@ setup(
entry_points={
'console_scripts': [
'librestash-import-gpx = librestash.cmd:import_gpx',
'librestash-export-caches = librestash.cmd:export_caches'
'librestash-export-caches = librestash.cmd:export_caches',
'librestash-stats = librestash.cmd:stats'
]
},
classifiers=[
......
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