<commit_before>import Response from 'ember-cli-mirage/response';

export default function() {
    this.namespace = '/api/v1';

    this.get('/summary', function(schema) {
        let crates = schema.crates.all();

        let just_updated = crates.sort((a, b) => compareIsoDates(b.updated_at, a.updated_at)).slice(0, 10);
        let most_downloaded = crates.sort((a, b) => b.downloads - a.downloads).slice(0, 10);
        let new_crates = crates.sort((a, b) => compareIsoDates(b.created_at, a.created_at)).slice(0, 10);
        let most_recently_downloaded = crates.sort((a, b) => b.recent_downloads - a.recent_downloads).slice(0, 10);

        let num_crates = crates.length;
        let num_downloads = crates.models.reduce((sum, crate) => sum + crate.downloads, 0);

        let popular_categories = schema.categories.all().sort((a, b) => b.crates_cnt - a.crates_cnt).slice(0, 10);
        let popular_keywords = schema.keywords.all().sort((a, b) => b.crates_cnt - a.crates_cnt).slice(0, 10);

        return {
            just_updated: this.serialize(just_updated).crates
                .map(it => ({ ...it, versions: null })),
            most_downloaded: this.serialize(most_downloaded).crates
                .map(it => ({ ...it, versions: null })),
            new_crates: this.serialize(new_crates).crates
                .map(it => ({ ...it, versions: null })),
            most_recently_downloaded: this.serialize(most_recently_downloaded).crates
                .map(it => ({ ...it, versions: null })),
            num_crates,
            num_downloads,
            popular_categories: this.serialize(popular_categories).categories,
            popular_keywords: this.serialize(popular_keywords).keywords,
        };
    });

    this.get('/crates', function(schema, request) {
        const { start, end } = pageParams(request);

        let crates = schema.crates.all();

        if (request.queryParams.letter) {
            let letter = request.queryParams.letter.toLowerCase();
            crates = crates.filter(crate => crate.id[0].toLowerCase() === letter);
        }

        if (request.queryParams.q) {
            let q = request.queryParams.q.toLowerCase();
            crates = crates.filter(crate => crate.id.toLowerCase().indexOf(q) !== -1);
        }

        if (request.queryParams.user_id) {
            let userId = parseInt(request.queryParams.user_id, 10);
            crates = crates.filter(crate => (crate._owner_users || []).indexOf(userId) !== -1);
        }

        if (request.queryParams.team_id) {
            let teamId = parseInt(request.queryParams.team_id, 10);
            crates = crates.filter(crate => (crate._owner_teams || []).indexOf(teamId) !== -1);
        }

        if (request.queryParams.sort === 'alpha') {
            crates = crates.sort((a, b) => compareStrings(a.id.toLowerCase(), b.id.toLowerCase()));
        }

        return withMeta(this.serialize(crates.slice(start, end)), { total: crates.length });
    });

    this.get('/crates/:crate_id', function(schema, request) {
        let crateId = request.params.crate_id;
        let crate = schema.crates.find(crateId);
        let categories = schema.categories.all()
            .filter(category => (crate.categories || []).indexOf(category.id) !== -1);
        let keywords = schema.keywords.all()
            .filter(keyword => (crate.keywords || []).indexOf(keyword.id) !== -1);
        let versions = schema.versions.all()
            .filter(version => (crate.versions || []).indexOf(parseInt(version.id, 10)) !== -1);

        return {
            ...this.serialize(crate),
            ...this.serialize(categories),
            ...this.serialize(keywords),
            ...this.serialize(versions),
        };
    });

    this.get('/crates/:crate_id/versions', (schema, request) => {
        let crate = request.params.crate_id;
        return schema.versions.where({ crate }).sort((a, b) => compareIsoDates(b.created_at, a.created_at));
    });

    this.get('/crates/:crate_id/:version_num/authors', (schema, request) => {
        let crate = request.params.crate_id;
        let num = request.params.version_num;
        let version = schema.versions.findBy({ crate, num });
        return { meta: { names: version._authors }, users: [] };
    });

    this.get('/crates/:crate_id/:version_num/dependencies', (schema, request) => {
        let crate = request.params.crate_id;
        let num = request.params.version_num;
        let version_id = schema.versions.findBy({ crate, num }).id;
        return schema.dependencies.where({ version_id });
    });<commit_msg>mirage: Add empty handler for `/crates/:crate_id/following` route<commit_after>import Response from 'ember-cli-mirage/response';

export default function() {
    this.namespace = '/api/v1';

    this.get('/summary', function(schema) {
        let crates = schema.crates.all();

        let just_updated = crates.sort((a, b) => compareIsoDates(b.updated_at, a.updated_at)).slice(0, 10);
        let most_downloaded = crates.sort((a, b) => b.downloads - a.downloads).slice(0, 10);
        let new_crates = crates.sort((a, b) => compareIsoDates(b.created_at, a.created_at)).slice(0, 10);
        let most_recently_downloaded = crates.sort((a, b) => b.recent_downloads - a.recent_downloads).slice(0, 10);

        let num_crates = crates.length;
        let num_downloads = crates.models.reduce((sum, crate) => sum + crate.downloads, 0);

        let popular_categories = schema.categories.all().sort((a, b) => b.crates_cnt - a.crates_cnt).slice(0, 10);
        let popular_keywords = schema.keywords.all().sort((a, b) => b.crates_cnt - a.crates_cnt).slice(0, 10);

        return {
            just_updated: this.serialize(just_updated).crates
                .map(it => ({ ...it, versions: null })),
            most_downloaded: this.serialize(most_downloaded).crates
                .map(it => ({ ...it, versions: null })),
            new_crates: this.serialize(new_crates).crates
                .map(it => ({ ...it, versions: null })),
            most_recently_downloaded: this.serialize(most_recently_downloaded).crates
                .map(it => ({ ...it, versions: null })),
            num_crates,
            num_downloads,
            popular_categories: this.serialize(popular_categories).categories,
            popular_keywords: this.serialize(popular_keywords).keywords,
        };
    });

    this.get('/crates', function(schema, request) {
        const { start, end } = pageParams(request);

        let crates = schema.crates.all();

        if (request.queryParams.letter) {
            let letter = request.queryParams.letter.toLowerCase();
            crates = crates.filter(crate => crate.id[0].toLowerCase() === letter);
        }

        if (request.queryParams.q) {
            let q = request.queryParams.q.toLowerCase();
            crates = crates.filter(crate => crate.id.toLowerCase().indexOf(q) !== -1);
        }

        if (request.queryParams.user_id) {
            let userId = parseInt(request.queryParams.user_id, 10);
            crates = crates.filter(crate => (crate._owner_users || []).indexOf(userId) !== -1);
        }

        if (request.queryParams.team_id) {
            let teamId = parseInt(request.queryParams.team_id, 10);
            crates = crates.filter(crate => (crate._owner_teams || []).indexOf(teamId) !== -1);
        }

        if (request.queryParams.sort === 'alpha') {
            crates = crates.sort((a, b) => compareStrings(a.id.toLowerCase(), b.id.toLowerCase()));
        }

        return withMeta(this.serialize(crates.slice(start, end)), { total: crates.length });
    });

    this.get('/crates/:crate_id', function(schema, request) {
        let crateId = request.params.crate_id;
        let crate = schema.crates.find(crateId);
        let categories = schema.categories.all()
            .filter(category => (crate.categories || []).indexOf(category.id) !== -1);
        let keywords = schema.keywords.all()
            .filter(keyword => (crate.keywords || []).indexOf(keyword.id) !== -1);
        let versions = schema.versions.all()
            .filter(version => (crate.versions || []).indexOf(parseInt(version.id, 10)) !== -1);

        return {
            ...this.serialize(crate),
            ...this.serialize(categories),
            ...this.serialize(keywords),
            ...this.serialize(versions),
        };
    });

    this.get('/crates/:crate_id/following', (/* schema, request */) => {
        // TODO
    });

    this.get('/crates/:crate_id/versions', (schema, request) => {
        let crate = request.params.crate_id;
        return schema.versions.where({ crate }).sort((a, b) => compareIsoDates(b.created_at, a.created_at));
    });

    this.get('/crates/:crate_id/:version_num/authors', (schema, request) => {
        let crate = request.params.crate_id;
        let num = request.params.version_num;
        let version = schema.versions.findBy({ crate, num });
        return { meta: { names: version._authors }, users: [] };
    });

    this.get('/crates/:crate_id/:version_num/dependencies', (schema, request) => {
        let crate = request.params.crate_id;
        let num = request.params.version_num;
        let version_id = schema.versions.findBy({ crate, num }).id;
        return schema.dependencies.where({ version_id });
    });
{'fraction_non_alphanumeric': 0.16132135984605517, 'fraction_numerical': 0.007055805003207184, 'mean_word_length': 2.486768542676109, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2080010', 'n_tokens_mistral': 2883, 'n_tokens_neox': 2847, 'n_words': 502}