-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added Yearly Reading Goals to /stats page #9301
added Yearly Reading Goals to /stats page #9301
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@classmethod | ||
def summary(cls): | ||
return { | ||
'total_yearly_reading_goals': { | ||
'total': BookshelvesEvents.total_yearly_reading_goals(), | ||
'month': BookshelvesEvents.total_yearly_reading_goals( | ||
since=DATE_ONE_MONTH_AGO | ||
), | ||
'week': BookshelvesEvents.total_yearly_reading_goals( | ||
since=DATE_ONE_WEEK_AGO | ||
), | ||
}, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you move this and the total_yearly_reading_goals
function to https://github.com/internetarchive/openlibrary/blob/master/openlibrary/core/yearly_reading_goals.py?
""" | ||
oldb = db.get_db() | ||
|
||
query = "SELECT count(*) from yearly_reading_goals" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After moving this function to core/yearly_reading_goals.py
, please replace the hard-coded table name with TABLENAME
:
TABLENAME = 'yearly_reading_goals' |
results = tuple(oldb.query(query, vars={'since': since})) | ||
print('total_yearly_reading_goals', results) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
results = tuple(oldb.query(query, vars={'since': since})) | |
print('total_yearly_reading_goals', results) | |
results = list(oldb.query(query, vars={'since': since})) |
While casting to a tuple
works here, please cast to a list
instead. This is how it's done throughout the codebase, and I'm not sure if using tuple
here may have unintended side-effects.
Also, remove the print
statement.
We simply don't have good test coverage. |
d5d25cf
to
5af03de
Compare
5af03de
to
5cf7f6f
Compare
I see - do you think you think we should make an issue for that? I wouldn't mind adding a few more unit tests (for the related functions on the stats page - and also updating the comments 😁). Also, in terms of the 1st point I raised in my PR, is there anything that should be noted there? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks again. @kevinc16! Just make sure that totaly_yearly_reading_goals
returns an int
, and remove the unit tests. Due to the way that these DB unit tests are currently set up, some functions cannot be tested.
results = list(oldb.query(query, vars={'since': since})) | ||
return results[0] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
results = list(oldb.query(query, vars={'since': since})) | |
return results[0] | |
results = oldb.query(query, vars={'since': since}) | |
return results[0]['count'] if results else 0 |
Just return an int
here. This follows the same pattern used in Booknotes.total_booknotes
.
def total_yearly_reading_goals(cls, since: date | None = None) -> int: | ||
"""Returns a Storage object of <Storage {'count': int}> where 'count' specifies the | ||
number reading goals updated. `since` may be used |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def total_yearly_reading_goals(cls, since: date | None = None) -> int: | |
"""Returns a Storage object of <Storage {'count': int}> where 'count' specifies the | |
number reading goals updated. `since` may be used | |
def total_yearly_reading_goals(cls, since: date | None = None) -> int: | |
"""Returns the number reading goals that were set. `since` may be used |
Type hints and docstring are conflicting. Return an int
here.
openlibrary/tests/core/test_db.py
Outdated
def test_total_yearly_reading_goals(self): | ||
assert YearlyReadingGoals.total_yearly_reading_goals()['count(*)'] == 3 | ||
# Combination of issues | ||
assert ( | ||
YearlyReadingGoals.total_yearly_reading_goals(since="2022-12-25")[ | ||
'count(*)' | ||
] | ||
== 1 | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def test_total_yearly_reading_goals(self): | |
assert YearlyReadingGoals.total_yearly_reading_goals()['count(*)'] == 3 | |
# Combination of issues | |
assert ( | |
YearlyReadingGoals.total_yearly_reading_goals(since="2022-12-25")[ | |
'count(*)' | |
] | |
== 1 | |
) |
Just remove this test. The DB used for this test is not mocked --- we spin up an instance of sqlite for these tests, but we use Postgres in production. It looks like the key for the counts is different depending on which DB is used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see, that explains why they are different in the 2 places - but if that is the case, why don't we just mock the Postgres DB? Is there a specific reason?
If you want to add more unit tests, create a feature request proposal issue and outline what test you will be adding. |
d9ce955
to
4439cc6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great, thanks @kevinc16!
Closes #7352
Added
summary
andtotal_yearly_reading_goals
functions toyearly_reading_goals.py
. Added unit test fortotal_yearly_reading_goals
function. Added Yearly Reading Goals row inopenlibrary/templates/admin/index.html
.Let me know if there are things we want to change!
Technical
There are a few things to note:
'count'
while in the unit test it is'count(*)'
)Application log:
But when I use the key
'count'
in the unit tests:total_books_logged
inopenlibrary/core/bookshelves.py
) don't have unit tests - is there a specific reason for that?Testing
UI changes and
docker compose run --rm home make test
.Screenshot
Stakeholders
@jimchamp