<commit_before>import fs       from 'fs';
import path     from 'path';
import google   from 'googleapis';
import examples from '../../config/examples';
import Promise  from 'bluebird';

const GA_ID           = 'ga:98310771';
const EXAMPLE_QUERIES = examples.map((q) => q.sort().join(','));
const AUTH_PATH       = path.resolve(__dirname, '../../config/google.json');
const ENABLED         = fs.existsSync(AUTH_PATH);

var jwt, analytics;

function getAnalytics() {
    if (!(jwt && analytics) && ENABLED) {
        jwt = new google.auth.JWT();
        jwt.fromJSON(require(AUTH_PATH));
        jwt = Promise.promisifyAll(jwt.createScoped('https://www.googleapis.com/auth/analytics.readonly'));
        analytics = google.analytics({ version: 'v3', auth: jwt });

        Promise.promisifyAll(analytics.data.ga);
        Promise.promisifyAll(analytics.data.realtime);
    }

    return jwt.authorizeAsync().then(() => analytics);
}

function topSearches(params) {
    return getAnalytics()
        .then((an) => {
            return an.data.ga.getAsync({
                ids: GA_ID,
                'start-date': params.days + 'daysAgo',
                'end-date': 'today',
                'max-results': params.limit,
                metrics: 'ga:totalEvents',
                dimensions: 'ga:eventAction',
                sort: '-ga:totalEvents',
                filters: 'ga:eventCategory==hits'
            });
        })
        .spread((data) => {
            let result = parseResponse(data);

            if (!params.examples) {
                result = result.filter(r => EXAMPLE_QUERIES.indexOf(r['ga:eventAction']) === -1);
            }

            if (params.summary) {
                let summary = {total: 0, words: {}};

                result.forEach(r => {
                    let term = r['ga:eventAction'].toLowerCase();
                    let count = r['ga:totalEvents'];

                    summary.words[term]  = summary.words[term] || 0;
                    summary.words[term] += count;
                    summary.total += count;
                });

                return {summary: summary};
            }

            return {searches: result};
        });
}

function imageErrors() {
    return getAnalytics().then((an) => {
        return an.data.ga.getAsync({
            ids: GA_ID,
            'start-date': '30daysAgo',
            'end-date': 'today',
            'max-results': 500,
            metrics: 'ga:totalEvents',
            dimensions: 'ga:eventAction',
            sort: '-ga:totalEvents',
            filters: 'ga:eventCategory==image-error'<commit_msg>Fix bug in analytics API<commit_after>import fs       from 'fs';
import path     from 'path';
import google   from 'googleapis';
import examples from '../../config/examples';
import Promise  from 'bluebird';

const GA_ID           = 'ga:98310771';
const EXAMPLE_QUERIES = examples.map((q) => q.sort().join(','));
const AUTH_PATH       = path.resolve(__dirname, '../../config/google.json');
const ENABLED         = fs.existsSync(AUTH_PATH);

var jwt, analytics;

function getAnalytics() {
    if (!(jwt && analytics) && ENABLED) {
        jwt = new google.auth.JWT();
        jwt.fromJSON(require(AUTH_PATH));
        jwt = Promise.promisifyAll(jwt.createScoped('https://www.googleapis.com/auth/analytics.readonly'));
        analytics = google.analytics({ version: 'v3', auth: jwt });

        Promise.promisifyAll(analytics.data.ga);
        Promise.promisifyAll(analytics.data.realtime);
    }

    return jwt.authorizeAsync().then(() => analytics);
}

function topSearches(params) {
    return getAnalytics()
        .then((an) => {
            return an.data.ga.getAsync({
                ids: GA_ID,
                'start-date': params.days + 'daysAgo',
                'end-date': 'today',
                'max-results': params.limit,
                metrics: 'ga:totalEvents',
                dimensions: 'ga:eventAction',
                sort: '-ga:totalEvents',
                filters: 'ga:eventCategory==hits'
            });
        })
        .spread((data) => {
            let result = parseResponse(data);

            if (!params.examples) {
                result = result.filter(r => EXAMPLE_QUERIES.indexOf(r['ga:eventAction']) === -1);
            }

            if (params.summary) {
                let summary = {total: 0, words: {}};

                result
                    .sort((a, b) => b['ga:totalEvents'] - a['ga:totalEvents'])
                    .forEach(r => {
                        let term = r['ga:eventAction'].toString().toLowerCase();
                        let count = r['ga:totalEvents'];

                        summary.words[term]  = summary.words[term] || 0;
                        summary.words[term] += count;
                        summary.total += count;
                    });

                return {summary: summary};
            }

            return {searches: result};
        });
}

function imageErrors() {
    return getAnalytics().then((an) => {
        return an.data.ga.getAsync({
            ids: GA_ID,
            'start-date': '30daysAgo',
            'end-date': 'today',
            'max-results': 500,
            metrics: 'ga:totalEvents',
            dimensions: 'ga:eventAction',
            sort: '-ga:totalEvents',
            filters: 'ga:eventCategory==image-error'
{'fraction_non_alphanumeric': 0.1486183719193428, 'fraction_numerical': 0.006348020911127707, 'mean_word_length': 1.788651743883394, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 18, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1984982', 'n_tokens_mistral': 1580, 'n_tokens_neox': 1507, 'n_words': 294}