This repository was archived by the owner on Nov 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Add ratings for study documents. #397
Open
NotSpecial
wants to merge
7
commits into
master
Choose a base branch
from
ratings
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8a4505e
WIP: ratings for studydocs
NotSpecial a698146
WIP rating tests
NotSpecial fc446ed
Finish implementation, docs missing
NotSpecial 8755537
Improve security and add description.
NotSpecial 35a460f
Polishing.
NotSpecial f23a688
Remove test stubs (they are part of the rating tests now).
NotSpecial 5d70b54
Merge branch 'master' into ratings
temparus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # | ||
| # license: AGPLv3, see LICENSE for details. In addition we strongly encourage | ||
| # you to buy us beer if we meet and you like the software. | ||
| """Studydoc rating computation. | ||
| For the rating, we use the lower bound of a confidence interval around | ||
| the 'naive' average vote, which accounts for the number of votes. | ||
| The rating for a study document is updated each time a study doc rating is | ||
| for the respective document is POSTed or PATCHed. The value is then written | ||
| to the database to allow sorting of study documents by rating. | ||
| """ | ||
|
|
||
| from flask import current_app | ||
| from math import sqrt | ||
|
|
||
| from amivapi.utils import get_id | ||
|
|
||
|
|
||
| def compute_rating(upvotes, downvotes, z=1.28): | ||
| """Compute the rating. | ||
| Concretely, the lower bound of the wilson confidence interval is returned, | ||
| which takes the number of votes into account. [1] | ||
| We use z = 1.28 by default, which corresponds to a 80% confidence interval | ||
| (As there are only a few votes, we do not want to be overly cautious). | ||
| [1]: http://www.evanmiller.org/how-not-to-sort-by-average-rating.html | ||
| """ | ||
| total = upvotes + downvotes | ||
| if not total: | ||
| return | ||
|
|
||
| p = upvotes / total | ||
| bound = ((p + (z**2)/(2*total) - | ||
NotSpecial marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| z * sqrt((p * (1-p) + (z**2)/(4*total)) / total)) / | ||
| (1 + (z**2)/total)) | ||
|
|
||
| # Ensure that the bound is not below 0 | ||
| return max(bound, 0) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can |
||
|
|
||
|
|
||
| def _update_rating(studydoc_id): | ||
| """Computes the rating for a study document.""" | ||
| docs = current_app.data.driver.db['studydocuments'] | ||
| ratings = current_app.data.driver.db['studydocumentratings'] | ||
| lookup = {'studydocument': studydoc_id} | ||
|
|
||
| # Check votes | ||
| upvotes = ratings.count_documents({'rating': 'up', **lookup}) | ||
| downvotes = ratings.count_documents({'rating': 'down', **lookup}) | ||
|
|
||
| # Compute rating and write to database | ||
| rating = compute_rating(upvotes, downvotes) | ||
| docs.update_one({'_id': studydoc_id}, {'$set': {'rating': rating}}) | ||
|
|
||
|
|
||
| def init_rating(items): | ||
| """On creating of a study-document, set the rating to None.""" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On creation |
||
| for item in items: | ||
| item['rating'] = None | ||
cburchert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def update_rating_post(items): | ||
| """Rating update hook for POST requests.""" | ||
| for item in items: | ||
| _update_rating(get_id(item['studydocument'])) | ||
|
|
||
|
|
||
| def update_rating_patch(updates, original): | ||
| """Rating update hook for PATCH requests.""" | ||
| _update_rating(get_id(updates['studydocument']) | ||
| if 'studydocument' in updates else | ||
| get_id(original['studydocument'])) | ||
|
|
||
|
|
||
| def update_rating_delete(item): | ||
| """Rating update hook for DELETE requests.""" | ||
| _update_rating(get_id(item['studydocument'])) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nit: this empty line is inconsequenctial with the rest of the schema.