Skip to content

Commit

Permalink
Merge pull request #10179 from schu96/10118/fix/reduce-loading-time-d…
Browse files Browse the repository at this point in the history
…ev-env

Use mock data for homepage charts to reduce startup loading time
  • Loading branch information
jimchamp authored Jan 2, 2025
2 parents ff01de0 + 48e55bc commit 5594cda
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
45 changes: 44 additions & 1 deletion openlibrary/core/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,10 @@ def _get_count_docs(ndays):
return [d for d in docs if d]


def get_stats(ndays=30):
def get_stats(ndays=30, use_mock_data=False):
"""Returns the stats for the past `ndays`"""
if use_mock_data:
return mock_get_stats()
docs = _get_count_docs(ndays)
return {
'human_edits': Stats(docs, "human_edits", "human_edits"),
Expand All @@ -171,3 +173,44 @@ def get_stats(ndays=30):
'authors': Stats(docs, "authors", "total_authors"),
'subjects': Stats(docs, "subjects", "total_subjects"),
}


def mock_get_stats():
keyNames = [
"human_edits",
"bot_edits",
"lists",
"visitors",
"loans",
"members",
"works",
"editions",
"ebooks",
"covers",
"authors",
"subjects",
]
mockKeyValues = [[(1 + x) * y for x in range(len(keyNames))] for y in range(28)][
::-1
]

docs = [dict(zip(keyNames, mockKeyValues[x])) for x in range(len(mockKeyValues))]
today = datetime.date.today()
for x in range(28):
docs[x]["_key"] = (today - datetime.timedelta(days=x + 1)).strftime(
'counts-%Y-%m-%d'
)
return {
'human_edits': Stats(docs, "human_edits", "human_edits"),
'bot_edits': Stats(docs, "bot_edits", "bot_edits"),
'lists': Stats(docs, "lists", "total_lists"),
'visitors': Stats(docs, "visitors", "visitors"),
'loans': Stats(docs, "loans", "loans"),
'members': Stats(docs, "members", "total_members"),
'works': Stats(docs, "works", "total_works"),
'editions': Stats(docs, "editions", "total_editions"),
'ebooks': Stats(docs, "ebooks", "total_ebooks"),
'covers': Stats(docs, "covers", "total_covers"),
'authors': Stats(docs, "authors", "total_authors"),
'subjects': Stats(docs, "subjects", "total_subjects"),
}
2 changes: 1 addition & 1 deletion openlibrary/plugins/openlibrary/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

def get_homepage():
try:
stats = admin.get_stats()
stats = admin.get_stats(use_mock_data=("dev" in web.ctx.features))
except Exception:
logger.error("Error in getting stats", exc_info=True)
stats = None
Expand Down

0 comments on commit 5594cda

Please sign in to comment.