def get_top_releases(musicbrainz_id, time_interval=None):
    """ Get top releases for user with given MusicBrainz ID over a particular period of time

        Args: musicbrainz_id (str): the MusicBrainz ID of the user
              time_interval  (str): the time interval over which top releases should be returned
                                    (defaults to all time)

        Returns: A sorted list of dicts with the following structure
                [
                    {
                        'artist_name' (str),
                        'artist_msid' (uuid),
                        'artist_mbids' (string of comma seperated uuids),
                        'release_name' (str),
                        'release_msid' (uuid),
                        'release_mbid' (uuid),
                        'listen_count' (int)
                    }
                ]
    """

    filter_clause = ""
    if time_interval:
        filter_clause = "AND listened_at >= TIMESTAMP_SUB(CURRENT_TIME(), INTERVAL {})".format(
            time_interval)

    query = """SELECT artist_name
                    , artist_msid
                    , artist_mbids
                    , release_name
                    , release_msid
                    , release_mbid
                    , COUNT(release_msid) as listen_count
                 FROM {dataset_id}.{table_id}
                WHERE user_name = @musicbrainz_id
                {time_filter_clause}
             GROUP BY artist_msid, artist_mbids, artist_name, release_name, release_msid, release_mbid
             ORDER BY listen_count DESC
                LIMIT {limit}
            """.format(
        dataset_id=config.BIGQUERY_DATASET_ID,
        table_id=config.BIGQUERY_TABLE_ID,
        time_filter_clause=filter_clause,
        limit=config.STATS_ENTITY_LIMIT,
    )

    parameters = [{
        'type': 'STRING',
        'name': 'musicbrainz_id',
        'value': musicbrainz_id
    }]

    return stats.run_query(query, parameters)
def get_artist_count():
    """ Calculates the total number of artists submitted to ListenBrainz.

        Returns:
            artist_count (int)
    """

    query = """SELECT COUNT(DISTINCT(artist_msid)) as artist_count
                 FROM {dataset_id}.{table_id}
            """.format(
        dataset_id=config.BIGQUERY_DATASET_ID,
        table_id=config.BIGQUERY_TABLE_ID,
    )

    return stats.run_query(query)[0]['artist_count']
    def test_run_query_done(self, mock_bigquery):
        """ Test run_query when the result is directly returned by the first api call to bigquery.jobs.query """

        # set the value returned by call to bigquery to a response which signifies completed query
        mock_bigquery.jobs.return_value.query.return_value.execute.return_value = bigquery_responses[
            'done']

        # construct query and parameters
        query = """SELECT artist_msid, artist_name
                     FROM {dataset_id}.{table_id}
                    WHERE user_name = @username
                """.format(dataset_id=config.BIGQUERY_DATASET_ID,
                           table_id=config.BIGQUERY_TABLE_ID)

        parameters = [{
            'name': 'username',
            'type': 'STRING',
            'value': 'testuser'
        }]

        result = stats.run_query(query, parameters)
        self.assertListEqual(result, expected_results['done'])