示例#1
0
def request_stats():
    """ Check if the current user's statistics have been calculated and if not,
        put them in the stats queue for stats_calculator.
    """
    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):
        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:
        # publish to rabbitmq queue that the stats-calculator consumes
        data = {
            'type': 'user',
            'id': current_user.id,
            'musicbrainz_id': current_user.musicbrainz_id,
        }
        publish_data_to_queue(
            data=data,
            exchange=current_app.config['BIGQUERY_EXCHANGE'],
            queue=current_app.config['BIGQUERY_QUEUE'],
            error_msg=
            'Could not put user %s into statistics calculation queue, please try again later',
        )
        _redis.redis.set(
            construct_stats_queue_key(current_user.musicbrainz_id), 'queued')
        flash.info(
            'You have been added to the stats calculation queue! Please check back later.'
        )
    return redirect(url_for('profile.info'))
示例#2
0
def request_stats():
    """ Check if the current user's statistics have been calculated and if not,
        put them in the stats queue for stats_calculator.
    """
    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):
        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:
        # publish to rabbitmq queue that the stats-calculator consumes
        data = {
            'type': 'user',
            'id': current_user.id,
            'musicbrainz_id': current_user.musicbrainz_id,
        }
        publish_data_to_queue(
            data=data,
            exchange=current_app.config['BIGQUERY_EXCHANGE'],
            queue=current_app.config['BIGQUERY_QUEUE'],
            error_msg='Could not put user %s into statistics calculation queue, please try again later',
        )
        _redis.redis.set(construct_stats_queue_key(current_user.musicbrainz_id), 'queued')
        flash.info('You have been added to the stats calculation queue! Please check back later.')
    return redirect(url_for('profile.info'))
    def calculate_stats_for_user(self, user):
        """ Calculate statistics for specified user.

        Args:
            user: a dict which should contain the `id` and `musicbrainz_id` keys

        Returns:
            bool value specifying if user's stats were calculated or not.
        """
        try:
            user = {
                'id': user['id'],
                'musicbrainz_id': user['musicbrainz_id']
            }
        except KeyError:
            current_app.logger.error('Invalid user data sent into queue, ignoring...')
            return False


        # if this user already has recent stats, ignore
        if db_stats.valid_stats_exist(user['id']):
            current_app.logger.info('Stats already exist for user %s, moving on!', user['musicbrainz_id'])
            return False

        try:
            current_app.logger.info('Calculating statistics for user %s...', user['musicbrainz_id'])
            recordings = stats_user.get_top_recordings(self.bigquery, user['musicbrainz_id'])
            current_app.logger.info('Top recordings for user %s done!', user['musicbrainz_id'])

            artists = stats_user.get_top_artists(self.bigquery, user['musicbrainz_id'])
            current_app.logger.info('Top artists for user %s done!', user['musicbrainz_id'])

            releases = stats_user.get_top_releases(self.bigquery, user['musicbrainz_id'])
            current_app.logger.info('Top releases for user %s done!', user['musicbrainz_id'])

            artist_count = stats_user.get_artist_count(self.bigquery, user['musicbrainz_id'])
            current_app.logger.info('Artist count for user %s done!', user['musicbrainz_id'])

        except Exception as e:
            current_app.logger.error('Unable to calculate stats for user %s: %s', user['musicbrainz_id'], str(e), exc_info=True)
            raise

        current_app.logger.info('Inserting calculated stats for user %s into db', user['musicbrainz_id'])
        while True:
            try:
                db_stats.insert_user_stats(
                    user_id=user['id'],
                    artists=artists,
                    recordings=recordings,
                    releases=releases,
                    artist_count=artist_count
                )
                current_app.logger.info('Stats calculation for user %s done!', user['musicbrainz_id'])
                break

            except Exception as e:
                current_app.logger.error('Unable to insert calculated stats into db for user %s: %s', user['musicbrainz_id'], str(e), exc_info=True)
                time.sleep(3)

        return True
示例#4
0
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)
    except DatabaseException:
        stats_exist = False

    return render_template(
        "profile/info.html",
        user=current_user,
        in_stats_queue=in_stats_queue,
        stats_exist=stats_exist,
    )
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)
    except DatabaseException:
        stats_exist = False

    return render_template(
        "profile/info.html",
        user=current_user,
        in_stats_queue=in_stats_queue,
        stats_exist=stats_exist,
    )
示例#6
0
 def test_valid_stats_exist(self):
     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'], 7))
示例#7
0
 def test_delete_user_stats(self):
     self.assertFalse(db_stats.valid_stats_exist(self.user['id'], 7))
     self.insert_test_data()
     db_stats.delete_user_stats(self.user['id'])
     self.assertFalse(db_stats.valid_stats_exist(self.user['id'], 7))
 def test_valid_stats_exist(self):
     self.assertFalse(db_stats.valid_stats_exist(self.user['id']))
     self.insert_test_data()
     self.assertTrue(db_stats.valid_stats_exist(self.user['id']))