Skip to content

Commit b172b9d

Browse files
committed
Update /stats page with login counts
1 parent 0a879b1 commit b172b9d

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

openlibrary/plugins/openlibrary/api.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
)
2626
from openlibrary.core import cache, lending, models
2727
from openlibrary.core import helpers as h
28+
from openlibrary.core.admin import get_cached_unique_logins_since
2829
from openlibrary.core.auth import ExpiredTokenError, HMACToken
2930
from openlibrary.core.bestbook import Bestbook
3031
from openlibrary.core.bookshelves_events import BookshelvesEvents
@@ -1061,3 +1062,16 @@ def make_dark(self, edition):
10611062
if not data['source_records']:
10621063
del data['source_records']
10631064
web.ctx.site.save(data, 'Remove OCAID: Item no longer available to borrow.')
1065+
1066+
1067+
class monthly_logins(delegate.page):
1068+
path = "/api/monthly_logins"
1069+
encoding = "json"
1070+
1071+
def GET(self):
1072+
return delegate.RawText(
1073+
json.dumps({
1074+
"loginCount": get_cached_unique_logins_since()
1075+
}),
1076+
content_type="application/json"
1077+
)

openlibrary/plugins/openlibrary/js/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,4 +582,11 @@ jQuery(function () {
582582
import(/* webpackChunkName: "list-books" */ './list_books')
583583
.then(module => module.ListBooks.init());
584584
}
585+
586+
// Stats page login counts
587+
const monthlyLoginStats = document.querySelector('.monthly-login-counts')
588+
if (monthlyLoginStats) {
589+
import(/* webpackChunkName: "stats" */ './stats')
590+
.then(module => module.initUniqueLoginCounts(monthlyLoginStats))
591+
}
585592
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Fetches unique login counts and updates the view with the results.
3+
*
4+
* @param {HTMLElement} containerElem
5+
* @returns {Promise<void>}
6+
* @see /openlibrary/templates/admin/index.html
7+
*/
8+
export async function initUniqueLoginCounts(containerElem) {
9+
const loadingIndicator = containerElem.querySelector('.loadingIndicator')
10+
const i18nStrings = JSON.parse(containerElem.dataset.i18n)
11+
12+
const counts = await fetchCounts()
13+
.then((resp) => {
14+
if (resp.status !== 200) {
15+
throw new Error(`Failed to fetch partials. Status code: ${resp.status}`)
16+
}
17+
return resp.json()
18+
})
19+
20+
const countDiv = document.createElement('DIV')
21+
countDiv.innerHTML = i18nStrings.uniqueLoginsCopy
22+
const countSpan = countDiv.querySelector('.login-counts')
23+
countSpan.textContent = counts.loginCount
24+
loadingIndicator.replaceWith(countDiv)
25+
}
26+
27+
/**
28+
* Fetches login counts from the server.
29+
*
30+
* @returns {Promise<Response>}
31+
* @see `monthly_logins` class in /openlibrary/plugins/openlibrary/api.py
32+
*/
33+
async function fetchCounts() {
34+
return fetch("/api/monthly_logins.json")
35+
}

openlibrary/templates/admin/index.html

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
$ _x = ctx.setdefault('usergroup', 'admin')
55

66
$ stats = get_admin_stats()
7+
8+
$ i18n_strings = {
9+
$ "uniqueLoginsCopy": _('<span class="login-counts">0</span> patrons have logged in at least once over the past 30 days.')
10+
$ }
11+
712
<script type="text/json+graph" id="graph-json-editgraph">$:json_encode(counts.human_edits.get_counts(80, True))</script>
813
<script type="text/json+graph" id="graph-json-membergraph">$:json_encode(counts.members.get_counts(80, True))</script>
914

@@ -248,5 +253,8 @@ <h2>$_("Borrows")</h2>
248253
<img alt="$_('Borrows, last 2 years graph')" src="https://graphite.us.archive.org/render?target=hitcount(stats.ol.loans.bookreader,%221d%22)&from=-24months&tz=UTC&width=900"/>
249254

250255
<div class="contentSpacer"></div>
251-
256+
<h2>$_("Unique Logins")</h2>
257+
<div class="monthly-login-counts" data-i18n="$json_encode(i18n_strings)">
258+
$:macros.LoadingIndicator(_("Gathering login statistics..."), hidden=False)
259+
</div>
252260
</div>

0 commit comments

Comments
 (0)