<commit_before>import faker from 'faker';
import moment from 'moment';
import nql from '@nexes/nql';
import {Response} from 'ember-cli-mirage';
import {extractFilterParam, paginateModelCollection} from '../utils';
import {underscore} from '@ember/string';

export function mockMembersStats(server) {
    server.get('/members/stats/count', function (db, {queryParams}) {
        let {days} = queryParams;

        let firstSubscriberDays = faker.datatype.number({min: 30, max: 600});

        if (days === 'all-time') {
            days = firstSubscriberDays;
        } else {
            days = Number(days);
        }

        let total = 0;
        if (firstSubscriberDays > days) {
            total += faker.datatype.number({max: 1000});
        }

        // simulate sql GROUP BY where days with 0 subscribers are missing
        let dateCounts = {};
        let i = 0;
        while (i < days) {
            let date = moment().subtract(i, 'days').format('YYYY-MM-DD');
            let count = faker.datatype.number({min: 0, max: 30});

            if (count !== 0) {
                dateCounts[date] = count;
            }

            i += 1;
        }

        // similar to what we'll need to do on the server
        let totalOnDate = {};
        let j = days - 1;
        while (j >= 0) {
            let date = moment().subtract(j, 'days').format('YYYY-MM-DD');
            totalOnDate[date] = total + (dateCounts[date] || 0);
            total += (dateCounts[date] || 0);
            j -= 1;
        }

        return {
            total,
            resource: 'members',
            data: Object.keys(totalOnDate).map((key, idx, arr) => {
                return {
                    date: key,
                    free: arr[key],
                    paid: 0,
                    comped: 0
                };
            })
        };
    });
}

export default function mockMembers(server) {
    server.post('/members/', function ({members}) {
        let attrs = this.normalizedRequestAttrs();

        return members.create(Object.assign({}, attrs, {id: 99}));
    });

    server.get('/members/', function ({members}, {queryParams}) {
        let {filter, search, page, limit} = queryParams;

        page = +page || 1;
        limit = +limit || 15;

        let collection = members.all();

        if (filter) {
            const nqlFilter = nql(filter, {
                expansions: [
                    {
                        key: 'label',
                        replacement: 'labels.slug'
                    }
                ]
            });

            console.log(nqlFilter.toJSON());

            collection = collection.filter((member) => {
                const serializedMember = {};

                // mirage model keys match our main model keys so we need to transform
                // camelCase to underscore to match the filter format
                Object.keys(member.attrs).forEach((key) => {
                    serializedMember[underscore(key)] = member.attrs[key];
                });

                // similar deal for associated label models
                serializedMember.labels = [];
                member.labels.models.forEach((label) => {
                    const serializedLabel = {};
                    Object.keys(label.attrs).forEach((key) => {
                        serializedLabel[underscore(key)] = label.attrs[key];
                    });
                    serializedMember.labels.push(serializedLabel);
                });

                console.log({serializedMember});

                return nqlFilter.queryJSON(serializedMember);
            });
        }

        if (search) {
            const query = search.toLowerCase();
<commit_msg>Removed errant console.log<commit_after>import faker from 'faker';
import moment from 'moment';
import nql from '@nexes/nql';
import {Response} from 'ember-cli-mirage';
import {extractFilterParam, paginateModelCollection} from '../utils';
import {underscore} from '@ember/string';

export function mockMembersStats(server) {
    server.get('/members/stats/count', function (db, {queryParams}) {
        let {days} = queryParams;

        let firstSubscriberDays = faker.datatype.number({min: 30, max: 600});

        if (days === 'all-time') {
            days = firstSubscriberDays;
        } else {
            days = Number(days);
        }

        let total = 0;
        if (firstSubscriberDays > days) {
            total += faker.datatype.number({max: 1000});
        }

        // simulate sql GROUP BY where days with 0 subscribers are missing
        let dateCounts = {};
        let i = 0;
        while (i < days) {
            let date = moment().subtract(i, 'days').format('YYYY-MM-DD');
            let count = faker.datatype.number({min: 0, max: 30});

            if (count !== 0) {
                dateCounts[date] = count;
            }

            i += 1;
        }

        // similar to what we'll need to do on the server
        let totalOnDate = {};
        let j = days - 1;
        while (j >= 0) {
            let date = moment().subtract(j, 'days').format('YYYY-MM-DD');
            totalOnDate[date] = total + (dateCounts[date] || 0);
            total += (dateCounts[date] || 0);
            j -= 1;
        }

        return {
            total,
            resource: 'members',
            data: Object.keys(totalOnDate).map((key, idx, arr) => {
                return {
                    date: key,
                    free: arr[key],
                    paid: 0,
                    comped: 0
                };
            })
        };
    });
}

export default function mockMembers(server) {
    server.post('/members/', function ({members}) {
        let attrs = this.normalizedRequestAttrs();

        return members.create(Object.assign({}, attrs, {id: 99}));
    });

    server.get('/members/', function ({members}, {queryParams}) {
        let {filter, search, page, limit} = queryParams;

        page = +page || 1;
        limit = +limit || 15;

        let collection = members.all();

        if (filter) {
            const nqlFilter = nql(filter, {
                expansions: [
                    {
                        key: 'label',
                        replacement: 'labels.slug'
                    }
                ]
            });

            collection = collection.filter((member) => {
                const serializedMember = {};

                // mirage model keys match our main model keys so we need to transform
                // camelCase to underscore to match the filter format
                Object.keys(member.attrs).forEach((key) => {
                    serializedMember[underscore(key)] = member.attrs[key];
                });

                // similar deal for associated label models
                serializedMember.labels = [];
                member.labels.models.forEach((label) => {
                    const serializedLabel = {};
                    Object.keys(label.attrs).forEach((key) => {
                        serializedLabel[underscore(key)] = label.attrs[key];
                    });
                    serializedMember.labels.push(serializedLabel);
                });

                return nqlFilter.queryJSON(serializedMember);
            });
        }

        if (search) {
            const query = search.toLowerCase();

{'fraction_non_alphanumeric': 0.12600135777325186, 'fraction_numerical': 0.007875084860828241, 'mean_word_length': 1.4893545116593443, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1064306', 'n_tokens_mistral': 2031, 'n_tokens_neox': 1871, 'n_words': 476}