Permalink
Browse files

Limited the stats for past 30days and added an option to change the t…

…ime period.
  • Loading branch information...
Anand Chitipothu
Anand Chitipothu committed Dec 9, 2013
1 parent a44491b commit 2dd96446f6692cc09b523b9074713384a0a4b7a0
Showing with 41 additions and 0 deletions.
  1. +15 −0 openlibrary/core/loanstats.py
  2. +5 −0 openlibrary/templates/stats/lending.html
  3. +21 −0 openlibrary/views/loanstats.py
@@ -23,8 +23,16 @@ def __init__(self, region=None, library=None, collection=None, subject=None):
self.library = library
self.collection = collection
self.subject = subject
+ self.time_period = None
+
self._library_titles = None
self._facet_counts = None
+ self._total_loans = None
+
+ def get_total_loans(self):
+ # populate total loans
+ self._get_all_facet_counts()
+ return self._total_loans
def solr_select(self, params):
fq = params.get("fq", [])
@@ -42,6 +50,12 @@ def solr_select(self, params):
if self.subject:
params['fq'].append(self._get_subject_filter(self.solrescape(self.subject)))
+ if self.time_period:
+ start, end = self.time_period
+ def solrtime(t):
+ return t.isoformat() + "Z"
+ params['fq'].append("start_time_dt:[%s TO %s]" % (solrtime(start), solrtime(end)))
+
logger.info("SOLR query %s", params)
q = urllib.urlencode(params, doseq=True)
@@ -100,6 +114,7 @@ def _get_all_facet_counts(self):
"facet.limit": 20
}
response = self.solr_select(params)
+ self._total_loans = response['response']['numFound']
self._facet_counts = dict((name, web.group(counts, 2)) for name, counts in response['facet_counts']['facet_fields'].items())
return self._facet_counts
@@ -11,6 +11,9 @@ <h1><a href="/stats/lending">Lending Stats</a> / Collections / $stats.collection
<h1><a href="/stats/lending">Lending Stats</a> / Subjects / $stats.subject</h1>
$else:
<h1>Lending Stats</h1>
+ <p class="collapse sansserief"><strong class="black">for last 30 days</strong></p>
+
+ Show stats for: <a href="$:changequery(t='30days')">30 days</a> | <a href="$:changequery(t='60days')">60 days</a> | <a href="$:changequery(t='120days')">120 days</a> | <a href="$:changequery(t='99999days')">Entire duration</a>
</div>
$ facet_urls = {"libraries": "/stats/lending/libraries/%(slug)s", "regions": "/stats/lending/regions/%(slug)s", "collections": "/stats/lending/collections/%(slug)s", "subjects": "/stats/lending/subjects/%(slug)s", "places": "/stats/lending/subjects/place:%(slug)s", "people": "/stats/lending/subjects/person:%(slug)s", "time": "/stats/lending/subjects/time:%(slug)s", "popular books": "%(slug)s", "popular authors": "%(slug)s"}
@@ -36,6 +39,8 @@ <h2>Loans Per Day</h2>
</div>
</div>
+ Total Loans during this period: $commify(stats.get_total_loans())
+
<h2>Loans Per Type</h2>
<div class="left">
<div id="loans-types-pie-chart" class="pie">
@@ -1,5 +1,8 @@
"""Loan Stats"""
+import re
+import datetime
import web
+
from .. import app
from ..core.loanstats import LoanStats
@@ -9,6 +12,9 @@ class stats(app.view):
def GET(self):
raise web.seeother("/stats/lending")
+re_time_period1 = re.compile("(\d+)days")
+#re_time_period2 = re.compile("(\d\d\d)(\d\d)(\d\d)-(\d\d\d\d)(\d\d)(\d\d)")
+
class lending_stats(app.view):
path = "/stats/lending(?:/(libraries|regions|collections|subjects)/(.+))?"
@@ -22,4 +28,19 @@ def GET(self, key, value):
stats.collection = value
elif key == 'subjects':
stats.subject = value
+
+ i = web.input(t="30days")
+ stats.time_period = self.parse_time(i.t)
+
return app.render_template("stats/lending.html", stats)
+
+ def parse_time(self, t):
+ m = re_time_period1.match(t)
+ if m:
+ delta_days = int(m.group(1))
+ else:
+ delta_days = 30
+ now = datetime.datetime.utcnow()
+ begin = now.replace(hour=0, minute=0, second=0, microsecond=0) - datetime.timedelta(days=delta_days)
+ end = now
+ return begin, end

0 comments on commit 2dd9644

Please sign in to comment.