diff --git a/openlibrary/core/loanstats.py b/openlibrary/core/loanstats.py index 47bbf4a24a2..65c2699be31 100644 --- a/openlibrary/core/loanstats.py +++ b/openlibrary/core/loanstats.py @@ -4,6 +4,7 @@ """ import re import time +import datetime import urllib import logging import simplejson @@ -78,7 +79,7 @@ def _run_solr_facet_query(self, facet_fields, facet_limit=None): params["facet.limit"] = facet_limit response = self.solr_select(params) - return dict((name, web.group(counts, 2)) for name, counts in response['facet_counts']['facet_fields'].items()) + return dict((name, list(web.group(counts, 2))) for name, counts in response['facet_counts']['facet_fields'].items()) def _get_all_facet_counts(self): if not self._facet_counts: @@ -128,6 +129,13 @@ def get_facet_counts(self, name, limit=20): facets = list(self.solr_select_facet(name))[:limit] return [self.make_facet(name, key, count) for key, count in facets] + def get_loans_by_published_year(self): + d = self._run_solr_facet_query("publish_year")['publish_year'] + # strip bad years + current_year = datetime.date.today().year + min_year = 1800 + return [[int(y), count] for y, count in d if min_year < int(y) <= current_year] + def make_facet(self, name, key, count): if name == "library_s": title = self._get_library_title(key) diff --git a/openlibrary/templates/stats/lending.html b/openlibrary/templates/stats/lending.html index 4e2a2f93ab0..6615423edc5 100644 --- a/openlibrary/templates/stats/lending.html +++ b/openlibrary/templates/stats/lending.html @@ -42,8 +42,15 @@

Loans Per Type

-
+ +

Loans by Published Year

+
+
+ +
+
+

@@ -143,6 +150,42 @@

Loans Per Type

show: false } }); + + \$.plot("#loans-by-published-year", [{data: $:json_encode(stats.get_loans_by_published_year())}], { + series: { + bars: { + show: true, + fill: 0.6, + color: "#ffa337", + align: "center" + }, + points: { + show: true + }, + color: "#ffa337" + }, + grid: { + /* + hoverable: true, + clickable: true, + autoHighlight: true, + */ + tickColor: "#d9d9d9", + borderWidth: 1, + borderColor: "#d9d9d9", + backgroundColor: "#fff" + }, + xaxis: { tickDecimals: 0 }, + yaxis: { tickDecimals: 0 } + /* + selection: { mode: "xy", color: "#00636a" }, + crosshair: { + mode: "xy", + color: "rgba(000, 099, 106, 0.4)", + lineWidth: 1 + } + */ + }); });