Skip to content
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

Make spark reader save stats as all time for now #665

Merged
merged 5 commits into from Nov 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 9 additions & 13 deletions listenbrainz/db/stats.py
Expand Up @@ -25,9 +25,8 @@
import ujson

from listenbrainz import db
from listenbrainz import config

def insert_user_stats(user_id, artists, recordings, releases, artist_count, yearmonth):
def insert_user_stats(user_id, artists, recordings, releases, artist_count):
"""Inserts user stats calculated from Spark into the database.

If stats are already present for some user, they are updated to the new
Expand All @@ -38,30 +37,25 @@ def insert_user_stats(user_id, artists, recordings, releases, artist_count, year
recordings (dict): the top recordings listened to by the user
releases (dict): the top releases listened to by the user
artist_count (int): the total number of artists listened to by the user
yearmonth (str): a string representing the month in which the stats were calculated,
for example '2019-01'
"""

artist_stats = {
'count': artist_count,
'top_month': {
'all_time': {
'artists': artists,
'month': yearmonth,
}
}

recording_stats = {
'top_month': {
'all_time': {
'recordings': recordings,
'month': yearmonth,
},
}


release_stats = {
'top_month': {
'all_time': {
'releases': releases,
'month': yearmonth,
}
}

Expand Down Expand Up @@ -174,12 +168,14 @@ def get_all_user_stats(user_id):
return get_user_stats(user_id, 'artist, recording, release')


def valid_stats_exist(user_id):
def valid_stats_exist(user_id, days):
""" Returns True if statistics for a user have been calculated in
the last week, and are present in the db
the last X days (where x is passed to the function), and are present in the db

Args:
user_id (int): the row ID of the user
days (int): the number of days in which stats should have been calculated
to consider them valid

Returns:
bool value signifying if valid stats exist for the user in the db
Expand All @@ -193,7 +189,7 @@ def valid_stats_exist(user_id):
AND last_updated >= NOW() - INTERVAL ':x days'
"""), {
'user_id': user_id,
'x': config.STATS_CALCULATION_INTERVAL,
'x': days,
})
row = result.fetchone()
return True if row is not None else False
20 changes: 9 additions & 11 deletions listenbrainz/db/tests/test_stats.py
Expand Up @@ -29,14 +29,13 @@ def test_insert_user_stats(self):
recordings=recordings,
releases=releases,
artist_count=2,
yearmonth='2019-01',
)

result = db_stats.get_all_user_stats(user_id=self.user['id'])
self.assertListEqual(result['artist']['top_month']['artists'], artists)
self.assertListEqual(result['artist']['all_time']['artists'], artists)
self.assertEqual(result['artist']['count'], 2)
self.assertListEqual(result['release']['top_month']['releases'], releases)
self.assertListEqual(result['recording']['top_month']['recordings'], recordings)
self.assertListEqual(result['release']['all_time']['releases'], releases)
self.assertListEqual(result['recording']['all_time']['recordings'], recordings)
self.assertGreater(int(result['last_updated'].strftime('%s')), 0)

def insert_test_data(self):
Expand All @@ -55,7 +54,6 @@ def insert_test_data(self):
recordings=recordings,
releases=releases,
artist_count=2,
yearmonth='2019-01',
)

return {
Expand All @@ -71,7 +69,7 @@ def test_get_user_stats(self):
self.assertEqual(data['artist']['count'], 2)

data = db_stats.get_user_stats(self.user['id'], 'recording')
self.assertListEqual(data['recording']['top_month']['recordings'], data_inserted['user_recordings'])
self.assertListEqual(data['recording']['all_time']['recordings'], data_inserted['user_recordings'])

def test_get_user_artists(self):
data_inserted = self.insert_test_data()
Expand All @@ -81,14 +79,14 @@ def test_get_user_artists(self):
def test_get_all_user_stats(self):
data_inserted = self.insert_test_data()
result = db_stats.get_all_user_stats(self.user['id'])
self.assertListEqual(result['artist']['top_month']['artists'], data_inserted['user_artists'])
self.assertListEqual(result['artist']['all_time']['artists'], data_inserted['user_artists'])
self.assertEqual(result['artist']['count'], 2)
self.assertListEqual(result['release']['top_month']['releases'], data_inserted['user_releases'])
self.assertListEqual(result['recording']['top_month']['recordings'], data_inserted['user_recordings'])
self.assertListEqual(result['release']['all_time']['releases'], data_inserted['user_releases'])
self.assertListEqual(result['recording']['all_time']['recordings'], data_inserted['user_recordings'])
self.assertGreater(int(result['last_updated'].strftime('%s')), 0)

def test_valid_stats_exist(self):
self.assertFalse(db_stats.valid_stats_exist(self.user['id']))
self.assertFalse(db_stats.valid_stats_exist(self.user['id'], 7))
self.insert_test_data()
self.assertTrue(db_stats.valid_stats_exist(self.user['id']))
self.assertTrue(db_stats.valid_stats_exist(self.user['id'], 7))

1 change: 0 additions & 1 deletion listenbrainz/db/tests/test_user.py
Expand Up @@ -135,7 +135,6 @@ def test_delete(self):
recordings={},
releases={},
artist_count=2,
yearmonth='2019-01',
)
user_stats = db_stats.get_all_user_stats(user_id)
self.assertIsNotNone(user_stats)
Expand Down
3 changes: 1 addition & 2 deletions listenbrainz/spark/spark_reader.py
Expand Up @@ -45,8 +45,7 @@ def callback(self, ch, method, properties, body):
recordings = metadata['recordings']
releases = metadata['releases']
artist_count = metadata['artists']['artist_count']
yearmonth = metadata['yearmonth']
db_stats.insert_user_stats(user['id'], artists, recordings, releases, artist_count, yearmonth)
db_stats.insert_user_stats(user['id'], artists, recordings, releases, artist_count)
current_app.logger.info("data for {} published".format(username))

while True:
Expand Down
4 changes: 2 additions & 2 deletions listenbrainz/webserver/views/profile.py
Expand Up @@ -91,7 +91,7 @@ def info():
# check if user is in stats calculation queue or if valid stats already exist
in_stats_queue = _redis.redis.get(construct_stats_queue_key(current_user.musicbrainz_id)) == 'queued'
try:
stats_exist = db_stats.valid_stats_exist(current_user.id)
stats_exist = db_stats.valid_stats_exist(current_user.id, current_app.config['STATS_CALCULATION_INTERVAL'])
except DatabaseException:
stats_exist = False

Expand Down Expand Up @@ -170,7 +170,7 @@ def request_stats():
status = _redis.redis.get(construct_stats_queue_key(current_user.musicbrainz_id)) == 'queued'
if status == 'queued':
flash.info('You have already been added to the stats calculation queue! Please check back later.')
elif db_stats.valid_stats_exist(current_user.id):
elif db_stats.valid_stats_exist(current_user.id, current_app.config['STATS_CALCULATION_INTERVAL']):
flash.info('Your stats were calculated in the most recent stats calculation interval,'
' please wait until the next interval! We calculate new statistics every Monday at 00:00 UTC.')
else:
Expand Down
3 changes: 0 additions & 3 deletions listenbrainz/webserver/views/test/test_user.py
Expand Up @@ -71,7 +71,6 @@ def test_user_page(self):
recordings={},
releases={},
artist_count=2,
yearmonth='2019-01',
)
response = self.client.get(url_for('user.profile', user_name=self.user.musicbrainz_id))
self.assert200(response)
Expand Down Expand Up @@ -161,7 +160,6 @@ def test_top_artists(self):
recordings={},
releases={},
artist_count=2,
yearmonth='2019-01',
)

r = self.client.get(url_for('user.artists', user_name=self.user.musicbrainz_id))
Expand All @@ -174,7 +172,6 @@ def test_top_artists(self):
recordings={},
releases={},
artist_count=2,
yearmonth='2019-01',
)

r = self.client.get(url_for('user.artists', user_name=self.user.musicbrainz_id))
Expand Down
4 changes: 2 additions & 2 deletions listenbrainz/webserver/views/user.py
Expand Up @@ -16,7 +16,7 @@
from listenbrainz.webserver.influx_connection import _influx
from listenbrainz.webserver.views.api_tools import publish_data_to_queue
import time
from datetime import datetime
from datetime import datetime
from werkzeug.exceptions import NotFound, BadRequest, RequestEntityTooLarge, InternalServerError

LISTENS_PER_PAGE = 25
Expand Down Expand Up @@ -189,7 +189,7 @@ def artists(user_name):
flash.error(msg)
return redirect(url_for('user.profile', user_name=user_name))

top_artists = data.get('artist', {}).get('top_month', {}).get('artists', [])
top_artists = data.get('artist', {}).get('all_time', {}).get('artists', [])

return render_template(
"user/artists.html",
Expand Down