<commit_before>var ageDistribution = {
    urls: {
        survey: '/dashboard/aggregatesurveydatajsonview/',
        teams: '/dashboard/teamsjsonview/',
        states: '/dashboard/statesjsonview/'
    },
    initiate: function() {
        var selectors = jQuery('#age_distribution_teams,#age_distribution_states');
        selectors.selectpicker();
        selectors.on('change', ageDistribution.changeStateOrTeam);
        //jQuery('#age_distribution_teams,#age_distribution_states').selectmenu({
        //    change: ageDistribution.changeStateOrTeam
        //});

        jQuery('#age_distribution_households_download').on('click', ageDistribution.downloadHouseholdAgeListCSV);
        jQuery('#age_distribution_children_download').on('click', ageDistribution.downloadChildrenAgeListCSV);

        ageDistribution.drawHouseholdAgeDistribution();
        ageDistribution.drawChildrenAgeDistribution();
        dataGetter.addNew(ageDistribution.urls.teams, ageDistribution.fillTeamsList, false);
        dataGetter.addNew(ageDistribution.urls.states, ageDistribution.fillStatesList, false);
        dataGetter.addNew(ageDistribution.urls.survey, ageDistribution.updateHouseholdAgeDistribution, true);
        dataGetter.addNew(ageDistribution.urls.survey, ageDistribution.updateChildrenAgeDistribution, true);
    },
    fillTeamsList: function(data) {
        var selector = jQuery('#age_distribution_teams');
        _.each(data.teams, function(names, id) {
            selector.append(ageDistribution.teamOptionTmp({
                id: id,
                names: names
            }));
        });
        selector.selectpicker('refresh');
    },
    teamOptionTmp: _.template('<option value="<%- id %>"><%- names %></option>'),
    fillStatesList: function(data) {
        var selector = jQuery('#age_distribution_states');
        _.each(data.states.sort(), function(state) {
            selector.append(ageDistribution.stateOptionTmp({
                state: state
            }));
        });
        selector.selectpicker('refresh');
    },
    stateOptionTmp: _.template('<option value="<%- state %>" ><%- state %></option>'),
    changeStateOrTeam: function () {
        var data = dataGetter.downloads[ageDistribution.urls.survey].data,
            team = jQuery('#age_distribution_teams').val(),
            state = jQuery('#age_distribution_states').val();
        ageDistribution.updateHouseholdAgeDistribution(data,team,state);
        ageDistribution.updateChildrenAgeDistribution(data,team,state);
    },
    householdAgeDistributionPlot: false,
    drawHouseholdAgeDistribution: function() {
        ageDistribution.householdAgeDistributionPlot = jQuery.plot('#age_distribution_household_members_chart', [], {
            series: {
                color: "#2779AA",
                bars: {
                    show: true,
                    fillColor: "#D7EBF9",
                },
            },
            yaxis: {
                tickDecimals: 0,
                min: 0
            },
            xaxis: {
                tickDecimals: 0,
                min: 0
            }
        });
    },
    childrenAgeDistributionPlot: false,
    drawChildrenAgeDistribution: function() {
        ageDistribution.childrenAgeDistributionPlot = jQuery.plot('#age_distribution_children_chart', [], {
            series: {
                color: "#2779AA",
                bars: {
                    show: true,
                    fillColor: "#D7EBF9",
                },
            },
            yaxis: {
                tickDecimals: 0,
                min: 0
            },
            xaxis: {
                tickDecimals: 0,
                min: 0
            }
        });
    },
    householdAgeList: false,
    updateHouseholdAgeDistribution: function(data, team, state) {
        var ages = {};

        _.each(data.survey_data, function(survey) {
            if (team && team > 0 && team != survey.team) {
                return true;
            }
            if (state && state != 'All states' && state != clusterInfo.findState(survey.cluster)) {
                return true;
            }
            _.each(survey.members, function(member) {
                if (ages.hasOwnProperty(member.age)) {
                    ages[member.age] ++;
                } else {
                    ages[member.age] = 1;
                }
            });
        });
        ageDistribution.householdAgeList = _.map(ages, function(num, age) {
            return [parseInt(age), num];
        });
        ageDistribution.householdAgeDistributionPlot.setData([ageDistribution.householdAgeList]);
        ageDistribution.householdAgeDistributionPlot.setupGrid();
        ageDistribution.householdAgeDistributionPlot.draw();
    },
    downloadHouseholdAgeListCSV: function () {
        if (!ageDistribution.householdAgeList) {
            return false;
        }
        var output = 'age (years),count\n';

        _.each(ageDistribution.householdAgeList, function(age) {
            output += age[0] + ',' + age[1] + '\n';
        });

        saveAs(
            new Blob( [output], {type : 'text/csv'}),
            'households_age_list.csv'
        );
    },
    childrenAgeList: false,
    updateChildrenAgeDistribution: function(data, team, state) {
        var ages = {};

        _.each(data.survey_data, function(survey) {
            var childSurveys;

            if (team && team > 0 && team != survey.team) {
                return true;
            }
            if (state && state != 'All states' && state != clusterInfo.findState(survey.cluster)) {
                return true;
            }

            childSurveys = _.pluck(_.where(survey.members, {'surveyType': 'child'}), 'survey');

            _.each(childSurveys, function(child) {
                var childAge, months;
                if (child.hasOwnProperty('birthDate')) {
                    childAge = new Date(new Date()-new Date(child.birthDate));
                    months = (childAge.getYear()-70)*12+childAge.getMonth();
                } else {
                    months = child.ageInMonths;
                }
                if (ages.hasOwnProperty(months)) {
                    ages[months] ++;
                } else {
                    ages[months] = 1;
                }
            });

        });<commit_msg>handle child/adult ages not beign defined or equal to 99/999 (not measured), fixes #197<commit_after>var ageDistribution = {
    urls: {
        survey: '/dashboard/aggregatesurveydatajsonview/',
        teams: '/dashboard/teamsjsonview/',
        states: '/dashboard/statesjsonview/'
    },
    initiate: function() {
        var selectors = jQuery('#age_distribution_teams,#age_distribution_states');
        selectors.selectpicker();
        selectors.on('change', ageDistribution.changeStateOrTeam);
        //jQuery('#age_distribution_teams,#age_distribution_states').selectmenu({
        //    change: ageDistribution.changeStateOrTeam
        //});

        jQuery('#age_distribution_households_download').on('click', ageDistribution.downloadHouseholdAgeListCSV);
        jQuery('#age_distribution_children_download').on('click', ageDistribution.downloadChildrenAgeListCSV);

        ageDistribution.drawHouseholdAgeDistribution();
        ageDistribution.drawChildrenAgeDistribution();
        dataGetter.addNew(ageDistribution.urls.teams, ageDistribution.fillTeamsList, false);
        dataGetter.addNew(ageDistribution.urls.states, ageDistribution.fillStatesList, false);
        dataGetter.addNew(ageDistribution.urls.survey, ageDistribution.updateHouseholdAgeDistribution, true);
        dataGetter.addNew(ageDistribution.urls.survey, ageDistribution.updateChildrenAgeDistribution, true);
    },
    fillTeamsList: function(data) {
        var selector = jQuery('#age_distribution_teams');
        _.each(data.teams, function(names, id) {
            selector.append(ageDistribution.teamOptionTmp({
                id: id,
                names: names
            }));
        });
        selector.selectpicker('refresh');
    },
    teamOptionTmp: _.template('<option value="<%- id %>"><%- names %></option>'),
    fillStatesList: function(data) {
        var selector = jQuery('#age_distribution_states');
        _.each(data.states.sort(), function(state) {
            selector.append(ageDistribution.stateOptionTmp({
                state: state
            }));
        });
        selector.selectpicker('refresh');
    },
    stateOptionTmp: _.template('<option value="<%- state %>" ><%- state %></option>'),
    changeStateOrTeam: function () {
        var data = dataGetter.downloads[ageDistribution.urls.survey].data,
            team = jQuery('#age_distribution_teams').val(),
            state = jQuery('#age_distribution_states').val();
        ageDistribution.updateHouseholdAgeDistribution(data,team,state);
        ageDistribution.updateChildrenAgeDistribution(data,team,state);
    },
    householdAgeDistributionPlot: false,
    drawHouseholdAgeDistribution: function() {
        ageDistribution.householdAgeDistributionPlot = jQuery.plot('#age_distribution_household_members_chart', [], {
            series: {
                color: "#2779AA",
                bars: {
                    show: true,
                    fillColor: "#D7EBF9",
                },
            },
            yaxis: {
                tickDecimals: 0,
                min: 0
            },
            xaxis: {
                tickDecimals: 0,
                min: 0
            }
        });
    },
    childrenAgeDistributionPlot: false,
    drawChildrenAgeDistribution: function() {
        ageDistribution.childrenAgeDistributionPlot = jQuery.plot('#age_distribution_children_chart', [], {
            series: {
                color: "#2779AA",
                bars: {
                    show: true,
                    fillColor: "#D7EBF9",
                },
            },
            yaxis: {
                tickDecimals: 0,
                min: 0
            },
            xaxis: {
                tickDecimals: 0,
                min: 0
            }
        });
    },
    householdAgeList: false,
    updateHouseholdAgeDistribution: function(data, team, state) {
        var ages = {};

        _.each(data.survey_data, function(survey) {
            if (team && team > 0 && team != survey.team) {
                return true;
            }
            if (state && state != 'All states' && state != clusterInfo.findState(survey.cluster)) {
                return true;
            }
            _.each(survey.members, function(member) {
                if (member.hasOwnProperty('age')) {
                  if (member.age == 999) {
                    console.warn('Member age: 999');
                    return;
                  }
                    if (ages.hasOwnProperty(member.age)) {
                        ages[member.age] ++;
                    } else {
                        ages[member.age] = 1;
                    }
                } else {
                    console.warn('No member age defined');
                    return;
                }
            });
        });
        ageDistribution.householdAgeList = _.map(ages, function(num, age) {
            return [parseInt(age), num];
        });
        ageDistribution.householdAgeDistributionPlot.setData([ageDistribution.householdAgeList]);
        ageDistribution.householdAgeDistributionPlot.setupGrid();
        ageDistribution.householdAgeDistributionPlot.draw();
    },
    downloadHouseholdAgeListCSV: function () {
        if (!ageDistribution.householdAgeList) {
            return false;
        }
        var output = 'age (years),count\n';

        _.each(ageDistribution.householdAgeList, function(age) {
            output += age[0] + ',' + age[1] + '\n';
        });

        saveAs(
            new Blob( [output], {type : 'text/csv'}),
            'households_age_list.csv'
        );
    },
    childrenAgeList: false,
    updateChildrenAgeDistribution: function(data, team, state) {
        var ages = {};

        _.each(data.survey_data, function(survey) {
            var childSurveys;

            if (team && team > 0 && team != survey.team) {
                return true;
            }
            if (state && state != 'All states' && state != clusterInfo.findState(survey.cluster)) {
                return true;
            }

            childSurveys = _.pluck(_.where(survey.members, {'surveyType': 'child'}), 'survey');


            _.each(childSurveys, function(child) {
                var childAge, months;
                if (child.hasOwnProperty('birthDate')) {
                    childAge = new Date(new Date()-new Date(child.birthDate));
                    months = (childAge.getYear()-70)*12+childAge.getMonth();
                } else if (child.hasOwnProperty('ageInMonths')) {
                    months = child.ageInMonths;
                    if (months == 99) {
                        console.warn('Child age: 99');
                        return;
                    }
                } else {
                    console.warn('No child age defined');
                    return;
                }

                if (ages.hasOwnProperty(months)) {
                    ages[months] ++;
                } else {
                    ages[months] = 1;
                }
            });

        });
{'fraction_non_alphanumeric': 0.12, 'fraction_numerical': 0.0058317757009345795, 'mean_word_length': 1.8094938038227264, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5099766', 'n_tokens_mistral': 3764, 'n_tokens_neox': 3394, 'n_words': 561}