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

Add basic implementation of HTML statistics.

parent 91f324d9
No related branches found
No related tags found
No related merge requests found
include LICENSE include LICENSE
recursive-include librestash/templates *
...@@ -7,7 +7,7 @@ from .lib.adaptors.gpx import GpxImporter ...@@ -7,7 +7,7 @@ from .lib.adaptors.gpx import GpxImporter
from .lib.exporters.caches import CacheExporter from .lib.exporters.caches import CacheExporter
from .lib.exporters.formatters import caches_to_csv, caches_to_json from .lib.exporters.formatters import caches_to_csv, caches_to_json
from .lib.stats import all_stats from .lib.stats import all_stats
from .lib.stats.formatters import stats_to_json from .lib.stats.formatters import stats_to_html, stats_to_json
from .lib.config import get_default_config_path, get_config from .lib.config import get_default_config_path, get_config
from .lib.model import get_db from .lib.model import get_db
...@@ -70,5 +70,5 @@ def stats(): ...@@ -70,5 +70,5 @@ def stats():
if args.format == 'json': if args.format == 'json':
print(stats_to_json(data)) print(stats_to_json(data))
# elif args.format == 'csv': elif args.format == 'html':
# print(caches_to_csv(data)) print(stats_to_html(data))
import json import json
try:
import jinja2
_HAVE_JINJA = True
except ImportError:
_HAVE_JINJA = False
from ..util import json_serialise from ..util import json_serialise
def stats_to_json(data): def stats_to_json(data):
return json.dumps(data, default=json_serialise) return json.dumps(data, default=json_serialise)
def stats_to_html(data, main_template='simple'):
if not _HAVE_JINJA:
return '<html><body>Jinja2 could not be imported.</body></html>'
env = jinja2.Environment(
loader=jinja2.PackageLoader('librestash', 'templates'),
autoescape=jinja2.select_autoescape(['html'])
)
plugin_templates = [env.get_template(template_name) for template_name in env.list_templates(
filter_func=lambda path: 'stats/plugins/' in path
)]
template = env.get_template('stats/main_%s.html' % main_template)
return template.render(data=data, plugin_templates=plugin_templates)
from collections import OrderedDict
from . import stats_plugin from . import stats_plugin
from ..model import Log from ..model import Log
...@@ -13,7 +15,7 @@ def _finds_by_date(db, geocacher): ...@@ -13,7 +15,7 @@ def _finds_by_date(db, geocacher):
res[date] = 0 res[date] = 0
res[date] += 1 res[date] += 1
return res return OrderedDict(sorted(res.items(), key=lambda t: t[0]))
@stats_plugin @stats_plugin
def finds_by_day(db, geocacher): def finds_by_day(db, geocacher):
...@@ -24,7 +26,7 @@ def finds_by_day(db, geocacher): ...@@ -24,7 +26,7 @@ def finds_by_day(db, geocacher):
for date, count in data.items(): for date, count in data.items():
res[date.strftime('%Y-%m-%d')] = count res[date.strftime('%Y-%m-%d')] = count
return res return OrderedDict(sorted(res.items(), key=lambda t: t[0]))
@stats_plugin @stats_plugin
def finds_by_month(db, geocacher): def finds_by_month(db, geocacher):
...@@ -37,7 +39,7 @@ def finds_by_month(db, geocacher): ...@@ -37,7 +39,7 @@ def finds_by_month(db, geocacher):
res[date.strftime('%Y-%m')] = 0 res[date.strftime('%Y-%m')] = 0
res[date.strftime('%Y-%m')] += count res[date.strftime('%Y-%m')] += count
return res return OrderedDict(sorted(res.items(), key=lambda t: t[0]))
@stats_plugin @stats_plugin
def finds_by_year(db, geocacher): def finds_by_year(db, geocacher):
...@@ -50,4 +52,4 @@ def finds_by_year(db, geocacher): ...@@ -50,4 +52,4 @@ def finds_by_year(db, geocacher):
res[date.strftime('%Y')] = 0 res[date.strftime('%Y')] = 0
res[date.strftime('%Y')] += count res[date.strftime('%Y')] += count
return res return OrderedDict(sorted(res.items(), key=lambda t: t[0]))
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Librestash" />
<title>Geocache Statistics</title>
</head>
<body>
<h1>Geocache Statistics</h1>
{% for template in plugin_templates %}
{% include template %}
{% endfor %}
</body>
</html>
<table>
<thead>
<tr>
<th>Year</th>
<th>Finds</th>
<th>Find Rate</th>
</tr>
</thead>
<tbody>
{% for year, count in data['finds_by_year'].items() %}
<tr>
<td>{{ year }}</td>
<td>{{ count }}</td>
<td>{{ count / 365 }}</td>
</tr>
{% endfor %}
</tbody>
</table>
...@@ -19,6 +19,9 @@ setup( ...@@ -19,6 +19,9 @@ setup(
'python-dateutil', 'python-dateutil',
'SQLAlchemy', 'SQLAlchemy',
], ],
extras_require={
'html': ['Jinja2'],
},
entry_points={ entry_points={
'console_scripts': [ 'console_scripts': [
'librestash-import-gpx = librestash.cmd:import_gpx', 'librestash-import-gpx = librestash.cmd:import_gpx',
......
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