Last active
March 27, 2018 05:02
-
-
Save dpmittal/848309fcadf18307eb2b325f08809859 to your computer and use it in GitHub Desktop.
Function to fetch average ratings and rating count of a particular entity
This file contains 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
from collections import defaultdict | |
from mbdata import models | |
from sqlalchemy.orm import joinedload | |
from brainzutils import cache | |
from critiquebrainz.frontend.external.musicbrainz_db import mb_session, DEFAULT_CACHE_EXPIRATION | |
# Function to return aggregate rating and rating count from the database | |
def get_rating_by_id(id): | |
key = cache.gen_key(id) | |
recording = cache.get(key) | |
if not recording: | |
with mb_db.mb_engine.connect() as connection: | |
result = connection.execute(sqlalchemy.text(""" | |
SELECT rec.id, | |
rm.rating, | |
rm.rating_count | |
FROM recording AS rec | |
INNER JOIN recording_meta rm | |
ON rec.id = rm.id | |
WHERE rec.gid = :recording_id | |
GROUP BY rec.id, rm.id | |
"""),{ | |
"recording_id" = id | |
}) | |
row = result.fetchone() | |
rating_mb = dict(row) | |
# The data fetched needs to be properly arranged | |
# in dictionaries. | |
if not row: | |
raise NoDataFoundError("No data Found") | |
## Cache results as it is done presently | |
cache.set(key=key, val=rating_mb, time=DEFAULT_CACHE_EXPIRATION) | |
## Return rounded off rating for 5 star rating system as well as the rating count | |
rating_mb["rating"] = round(rating_mb["rating"] / 20, 1) | |
return rating_mb["rating"], rating_mb["rating_count"] | |
# Function to return the list of dict of users who rated for that particular entity along with | |
# their individual ratings and names | |
def get_rating_users_by_id(id): | |
key = cache.gen_key(id) | |
recording_users = cache.get(key) | |
if not recording_users: | |
with mb_db.mb_engine.connect() as connection: | |
result = connection.execute(sqlalchemy.text(""" | |
SELECT rec.id, | |
rr.editor, | |
rr.rating, | |
editor.name | |
FROM recording as rec | |
INNER JOIN recording_rating_raw rr | |
ON rec.id = rr.recording | |
INNER JOIN editor | |
ON rr.editor = editor.id | |
WHERE rec.gid = :recording_id | |
GROUP BY rec.id, rr.editor, editor.id | |
"""),{ | |
"recording_id" = id | |
}) | |
if not result: | |
raise NoDataFoundError("No data Found") | |
# The data fetched needs to be properly arranged | |
# in dictionaries in a list of rows as there are many editors with their respective ratings. | |
rating_mb_users = [] | |
for row in result: | |
rating_mb_users.append(dict(row)) | |
## Cache results as it is done presently | |
cache.set(key=key, val=rating_mb_users, time=DEFAULT_CACHE_EXPIRATION) | |
# Return the list of dict of the users with their rating and names | |
return rating_mb_users |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment