Esempio n. 1
0
    def test_insert_user_stats_mult_ranges_listening_activity(self):
        """ Test if multiple time range data is inserted correctly """
        with open(self.path_to_data_file('user_listening_activity_db.json')) as f:
            listening_activity_data = json.load(f)

        db_stats.insert_user_listening_activity(
            user_id=self.user['id'], listening_activity=UserListeningActivityStatJson(**{'year': listening_activity_data}))
        db_stats.insert_user_listening_activity(
            self.user['id'], UserListeningActivityStatJson(**{'all_time': listening_activity_data}))

        result = db_stats.get_user_listening_activity(1, 'all_time')
        self.assertDictEqual(result.all_time.dict(), listening_activity_data)

        result = db_stats.get_user_listening_activity(1, 'year')
        self.assertDictEqual(result.year.dict(), listening_activity_data)
Esempio n. 2
0
def handle_user_listening_activity(data):
    """ Take listening activity stats for user and save it in database. """
    musicbrainz_id = data['musicbrainz_id']
    user = db_user.get_by_mb_id(musicbrainz_id)
    if not user:
        current_app.logger.critical(
            "Calculated stats for a user that doesn't exist in the Postgres database: %s",
            musicbrainz_id)
        return

    # send a notification if this is a new batch of stats
    if is_new_user_stats_batch():
        notify_user_stats_update(stat_type=data.get('type', ''))
    current_app.logger.debug("inserting stats for user %s", musicbrainz_id)

    stats_range = data['stats_range']

    # Strip extra data
    to_remove = {'musicbrainz_id', 'type', 'stats_range'}
    data_mod = {key: data[key] for key in data if key not in to_remove}

    try:
        db_stats.insert_user_listening_activity(
            user['id'],
            UserListeningActivityStatJson(**{stats_range: data_mod}))
    except ValidationError:
        current_app.logger.error(
            """ValidationError while inserting {stats_range} listening_activity for user with
                                    user_id: {user_id}. Data: {data}""".format(
                stats_range=stats_range,
                user_id=user['id'],
                data=json.dumps({stats_range: data_mod}, indent=3)),
            exc_info=True)
def insert_user_listening_activity(
        user_id: int, listening_activity: UserListeningActivityStatJson):
    """Inserts listening_activity stats calculated from Spark into the database.

       If stats are already present for some user, they are updated to the new
       values passed.

       Args: user_id: the row id of the user,
             listening_activity: the listening_activity stats of the user
    """
    _insert_user_jsonb_data(user_id=user_id,
                            column='listening_activity',
                            data=listening_activity.dict(exclude_none=True))
Esempio n. 4
0
    def test_handle_user_listening_activity(self, mock_send_mail,
                                            mock_new_user_stats,
                                            mock_get_by_mb_id, mock_db_insert):
        data = {
            'musicbrainz_id':
            'iliekcomputers',
            'type':
            'listening_activity',
            'stats_range':
            'all_time',
            'from_ts':
            1,
            'to_ts':
            10,
            'listening_activity': [{
                'from_ts': 1,
                'to_ts': 5,
                'time_range': '2020',
                'listen_count': 200,
            }],
        }
        mock_get_by_mb_id.return_value = {
            'id': 1,
            'musicbrainz_id': 'iliekcomputers'
        }
        mock_new_user_stats.return_value = True

        with self.app.app_context():
            current_app.config[
                'TESTING'] = False  # set testing to false to check the notifications
            handle_user_listening_activity(data)

        mock_db_insert.assert_called_with(
            1,
            UserListeningActivityStatJson(
                week=None,
                year=None,
                month=None,
                all_time=UserListeningActivityStatRange(
                    to_ts=10,
                    from_ts=1,
                    listening_activity=[
                        UserListeningActivityRecord(from_ts=1,
                                                    to_ts=5,
                                                    time_range='2020',
                                                    listen_count=200)
                    ])))
        mock_send_mail.assert_called_once()
Esempio n. 5
0
    def insert_test_data(self):
        """ Insert test data into the database """

        with open(self.path_to_data_file('user_top_artists_db.json')) as f:
            user_artists = json.load(f)
        with open(self.path_to_data_file('user_top_releases_db.json')) as f:
            releases = json.load(f)
        with open(self.path_to_data_file('user_top_recordings_db.json')) as f:
            recordings = json.load(f)
        with open(self.path_to_data_file('user_listening_activity_db.json')) as f:
            listening_activity = json.load(f)
        with open(self.path_to_data_file('user_daily_activity_db.json')) as f:
            daily_activity = json.load(f)
        with open(self.path_to_data_file('user_artist_map_db.json')) as f:
            artist_map = json.load(f)
        with open(self.path_to_data_file('sitewide_top_artists_db.json')) as f:
            sitewide_artists = json.load(f)

        db_stats.insert_user_artists(self.user['id'], UserArtistStatJson(**{'all_time': user_artists}))
        db_stats.insert_user_releases(self.user['id'], UserReleaseStatJson(**{'all_time': releases}))
        db_stats.insert_user_recordings(self.user['id'], UserRecordingStatJson(**{'all_time': recordings}))
        db_stats.insert_user_listening_activity(
            self.user['id'], UserListeningActivityStatJson(**{'all_time': listening_activity}))
        db_stats.insert_user_daily_activity(self.user['id'], UserDailyActivityStatJson(**{'all_time': daily_activity}))
        db_stats.insert_user_artist_map(self.user['id'], UserArtistMapStatJson(**{'all_time': artist_map}))
        db_stats.insert_sitewide_artists('all_time', SitewideArtistStatJson(**sitewide_artists))

        return {
            'user_artists': user_artists,
            'user_releases': releases,
            'user_recordings': recordings,
            'user_listening_activity': listening_activity,
            'user_daily_activity': daily_activity,
            'user_artist_map': artist_map,
            'sitewide_artists': sitewide_artists
        }
Esempio n. 6
0
 def test_insert_user_listening_activity(self):
     """ Test if listening activity stats are inserted correctly """
     with open(self.path_to_data_file('user_listening_activity_db.json')) as f:
         listening_activity_data = json.load(f)
     db_stats.insert_user_listening_activity(
         user_id=self.user['id'], listening_activity=UserListeningActivityStatJson(**{'all_time': listening_activity_data}))