Esempio n. 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'))
Esempio n. 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'))
Esempio n. 3
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,
    )
    def callback(self, ch, method, properties, body):
        """ Handle the data received from the queue and calculate stats accordingly.
        """
        data = ujson.loads(body)

        entity_type = data.get('type', '')
        if entity_type == 'user':
            done = self.calculate_stats_for_user(data)
            if done:
                self.redis.set(
                    construct_stats_queue_key(data['musicbrainz_id']), 'done')
        else:
            self.log.info(
                'Cannot recognize the type of entity in queue, ignoring...')
            return

        while True:
            try:
                self.incoming_ch.basic_ack(delivery_tag=method.delivery_tag)
                break
            except pika.exceptions.ConnectionClosed:
                self.init_rabbitmq_connection()
    def callback(self, ch, method, properties, body):
        """ Handle the data received from the queue and work accordingly.
        """
        data = ujson.loads(body)

        job_type = data.get('type', '')
        if job_type == 'user':
            done = self.calculate_stats_for_user(data)
            if done:
                self.redis.set(construct_stats_queue_key(data['musicbrainz_id']), 'done')
        elif job_type == 'delete.user':
            current_app.logger.info('Deleting user %s from BigQuery', data['musicbrainz_id'])
            delete_user(self.bigquery, data['musicbrainz_id'])
            current_app.logger.info('Deletion complete!')
        else:
            current_app.logger.critical('Cannot recognize the type of entity in queue, ignoring. data: %s', json.dumps(body, indent=3))
            return

        while True:
            try:
                self.incoming_ch.basic_ack(delivery_tag=method.delivery_tag)
                break
            except pika.exceptions.ConnectionClosed:
                self.init_rabbitmq_connection()