File tree Expand file tree Collapse file tree 4 files changed +65
-1
lines changed
Expand file tree Collapse file tree 4 files changed +65
-1
lines changed Original file line number Diff line number Diff line change 2525)
2626from openlibrary .core import cache , lending , models
2727from openlibrary .core import helpers as h
28+ from openlibrary .core .admin import get_cached_unique_logins_since
2829from openlibrary .core .auth import ExpiredTokenError , HMACToken
2930from openlibrary .core .bestbook import Bestbook
3031from 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+ )
Original file line number Diff line number Diff 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} ) ;
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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 >
You can’t perform that action at this time.
0 commit comments