<commit_before>var mongoose = require('../libs/mongoose');

var Module = require('../models/module').Module;
var Page = require('../models/page').Page;
var log = require('../libs/log')(module);
var async = require('async');
var _ = require('underscore');
var statusExport = require('../public/jsc/models/statusList');
var statusList = new statusExport.statuses();

exports.getData = function (req, res) {
    parsePages(function (moduledata) {
        res.json(moduledata);
    });
};

function moduleData() {
    this.module = [
                    {
                        key: "",
                        reportedSP: 0,
                        summarySP: 0,
                        progress: 0,
                        teamnames: [],
                        smename: "",
                        duedate: Date.parse("1/1/1970"),
                        accepted: false,
                        status: "",
                        modulestatus: "",
                        moduleresolution: "",
                        name: "",
                        moduleGroup: "",
                        pagescount: 0,
                        endOfYearDelivery: false,
                        q1Delivery: false,
                        dueDateConfirmed: false,
                        uri: ""
                    }
                ];
}

function getTeamName(labels) {
    var index = labels.indexOf("Team");
    if(index < 0) {
        return "";
    }
    var index2 = labels.indexOf(',', index);
    if(index2 < 0) {
        index2 = labels.length;
    }

    return labels.substring(index+4,index2);
}

function getDueDateConfirmed(labels) {
    var index = labels.indexOf("DueDateConfirmed");
    if(index < 0) {
        return false;
    }
    return true;
}

function getStreamName(labels) {
    var index = labels.indexOf("Stream");
    if(index < 0) {
        return "";
    }
    var index2 = labels.indexOf(',', index);
    if(index2 < 0) {
        index2 = labels.length;
    }

    return labels.substring(index+6,index2);
}

function getModuleGroupName(labels) {
    var groupName = "Unknown Module Group";
    var index = labels.indexOf("PageModuleGroup_");
    if(index < 0) {
        return groupName;
    }
    var index2 = labels.indexOf(',', index);
    if(index2 < 0) {
        index2 = labels.length;
    }

    if(labels.substring(index+16,index2) != "") {
        groupName = labels.substring(index+16,index2);
    }
    return groupName;
}

function getSizeName(labels) {
    var sizeName = "Unknown";
    var index = labels.indexOf("PageSize");
    if(index < 0) {
        return sizeName;
    }
    var index2 = labels.indexOf(',', index);
    if(index2 < 0) {
        index2 = labels.length;
    }

    if(labels.substring(index+8,index2) != "") {
        sizeName = labels.substring(index+8,index2);
    }
    return sizeName;
}

function SortData(moduledata) {
    moduledata.module.sort(function (a, b) {
        a = a.name;
        b = b.name;
        return a > b ? 1 : a < b ? -1 : 0;
    });
}

function parsePages(callback) {
    var moduledata = new moduleData();
    moduledata.module = [];

    var pageByMonthArray = [[],[],[],[],[],[],[],[],[],[],[],[]];

    async.series([
        function (callback) {
            Module.find({}).exec(function(err, modules) {
                async.series([
                    async.eachSeries(modules, function(module, callback) {
                            var endOfYearDelivery = module._doc.labels != null ? module._doc.labels.indexOf('EOYDeliverable') > -1 : false;
                            var q1Delivery = module._doc.labels != null ? module._doc.labels.indexOf('Q1Deliverable') > -1 : false;
                            var q2Delivery = module._doc.labels != null ? module._doc.labels.indexOf('Q2Deliverable') > -1 : false;
                            var q2Delivery = q2Delivery || !(endOfYearDelivery || q1Delivery);
                            var dueDateConfirmed = getDueDateConfirmed(module._doc.labels);
                            var count = 0;
                            Page.find({epicKey: module.key}).exec(function (err, pages) {
                            if(pages != null && pages.length > 0) {
                                async.eachSeries(pages, function(page, callback) {
                                        var storyPoints = page.storyPoints == null ? 0 : parseFloat(page.storyPoints);

                                        if(page.devFinished != null) {
                                            var dfDate = new Date(Date.parse(page.devFinished));
                                            var dfMonth = dfDate.getMonth();
                                            var sizeName = getSizeName(page.labels);
                                            var monthItems = pageByMonthArray[dfMonth];
                                            if(monthItems[sizeName] != null) {
                                                monthItems[sizeName]++;
                                            }
                                            else {
                                                monthItems[sizeName] = 1;
                                            }
                                        }

                                        //if(page.epicKey == 'PLEXUXC-2056') {
                                        //    log.info(page.key + ', ' + page.status + ', ' + page.resolution);
                                        //}

                                        var moduleGroup = getModuleGroupName(page.labels);
                                        var labels = module._doc.labels != null ? module._doc.labels : "";
                                        var teamName = getTeamName(labels);
                                        var streamName = getStreamName(labels);

                                        var calcStoryPoints = storyPoints * page.progress / 100;

                                        var status = page.status;
                                        var resolution = page.resolution;

                                        status = status == 'Closed' && resolution == "Done" ? "Accepted" : status;
                                        status = status == 'Closed' && resolution == "Implemented" ? "Accepted" : status;

                                        var ignore = status == "Closed" && (resolution == "Out of Scope" || resolution == "Rejected" || resolution == "Canceled");

                                        if(!ignore) {
                                            putDataPoint(moduledata, endOfYearDelivery, q1Delivery, q2Delivery, dueDateConfirmed, status, moduleGroup, teamName, streamName, calcStoryPoints, storyPoints, ++count, module);
                                        }
                                        callback();
                                },
                                function(err) {
                                    callback();
                                });
                            }
                            else {
                                putDataPoint(moduledata, endOfYearDelivery, q1Delivery, q2Delivery, dueDateConfirmed, "Empty", "Unknown Module Group", "", "", 0, 0, count, module);
                                callback();
                            }
                        })<commit_msg>fix for modules without pages<commit_after>var mongoose = require('../libs/mongoose');

var Module = require('../models/module').Module;
var Page = require('../models/page').Page;
var log = require('../libs/log')(module);
var async = require('async');
var _ = require('underscore');
var statusExport = require('../public/jsc/models/statusList');
var statusList = new statusExport.statuses();

exports.getData = function (req, res) {
    parsePages(function (moduledata) {
        res.json(moduledata);
    });
};

function moduleData() {
    this.module = [
                    {
                        key: "",
                        reportedSP: 0,
                        summarySP: 0,
                        progress: 0,
                        teamnames: [],
                        smename: "",
                        duedate: Date.parse("1/1/1970"),
                        accepted: false,
                        status: "",
                        modulestatus: "",
                        moduleresolution: "",
                        name: "",
                        moduleGroup: "",
                        pagescount: 0,
                        endOfYearDelivery: false,
                        q1Delivery: false,
                        dueDateConfirmed: false,
                        uri: ""
                    }
                ];
}

function getTeamName(labels) {
    var index = labels.indexOf("Team");
    if(index < 0) {
        return "";
    }
    var index2 = labels.indexOf(',', index);
    if(index2 < 0) {
        index2 = labels.length;
    }

    return labels.substring(index+4,index2);
}

function getDueDateConfirmed(labels) {
    var index = labels.indexOf("DueDateConfirmed");
    if(index < 0) {
        return false;
    }
    return true;
}

function getStreamName(labels) {
    var index = labels.indexOf("Stream");
    if(index < 0) {
        return "";
    }
    var index2 = labels.indexOf(',', index);
    if(index2 < 0) {
        index2 = labels.length;
    }

    return labels.substring(index+6,index2);
}

function getModuleGroupName(labels) {
    var groupName = "Unknown Module Group";
    var index = labels.indexOf("PageModuleGroup_");
    if(index < 0) {
        return groupName;
    }
    var index2 = labels.indexOf(',', index);
    if(index2 < 0) {
        index2 = labels.length;
    }

    if(labels.substring(index+16,index2) != "") {
        groupName = labels.substring(index+16,index2);
    }
    return groupName;
}

function getSizeName(labels) {
    var sizeName = "Unknown";
    var index = labels.indexOf("PageSize");
    if(index < 0) {
        return sizeName;
    }
    var index2 = labels.indexOf(',', index);
    if(index2 < 0) {
        index2 = labels.length;
    }

    if(labels.substring(index+8,index2) != "") {
        sizeName = labels.substring(index+8,index2);
    }
    return sizeName;
}

function SortData(moduledata) {
    moduledata.module.sort(function (a, b) {
        a = a.name;
        b = b.name;
        return a > b ? 1 : a < b ? -1 : 0;
    });
}

function parsePages(callback) {
    var moduledata = new moduleData();
    moduledata.module = [];

    var pageByMonthArray = [[],[],[],[],[],[],[],[],[],[],[],[]];

    async.series([
        function (callback) {
            Module.find({}).exec(function(err, modules) {
                async.series([
                    async.eachSeries(modules, function(module, callback) {
                            var endOfYearDelivery = module._doc.labels != null ? module._doc.labels.indexOf('EOYDeliverable') > -1 : false;
                            var q1Delivery = module._doc.labels != null ? module._doc.labels.indexOf('Q1Deliverable') > -1 : false;
                            var q2Delivery = module._doc.labels != null ? module._doc.labels.indexOf('Q2Deliverable') > -1 : false;
                            var q2Delivery = q2Delivery || !(endOfYearDelivery || q1Delivery);
                            var dueDateConfirmed = getDueDateConfirmed(module._doc.labels);
                            var count = 0;
                            var labels = module._doc.labels != null ? module._doc.labels : "";
                            var teamName = getTeamName(labels);
                            var streamName = getStreamName(labels);

                            Page.find({epicKey: module.key}).exec(function (err, pages) {
                            if(pages != null && pages.length > 0) {
                                async.eachSeries(pages, function(page, callback) {
                                        var storyPoints = page.storyPoints == null ? 0 : parseFloat(page.storyPoints);

                                        if(page.devFinished != null) {
                                            var dfDate = new Date(Date.parse(page.devFinished));
                                            var dfMonth = dfDate.getMonth();
                                            var sizeName = getSizeName(page.labels);
                                            var monthItems = pageByMonthArray[dfMonth];
                                            if(monthItems[sizeName] != null) {
                                                monthItems[sizeName]++;
                                            }
                                            else {
                                                monthItems[sizeName] = 1;
                                            }
                                        }

                                        //if(page.epicKey == 'PLEXUXC-2056') {
                                        //    log.info(page.key + ', ' + page.status + ', ' + page.resolution);
                                        //}

                                        var moduleGroup = getModuleGroupName(page.labels);

                                        var calcStoryPoints = storyPoints * page.progress / 100;

                                        var status = page.status;
                                        var resolution = page.resolution;

                                        status = status == 'Closed' && resolution == "Done" ? "Accepted" : status;
                                        status = status == 'Closed' && resolution == "Implemented" ? "Accepted" : status;

                                        var ignore = status == "Closed" && (resolution == "Out of Scope" || resolution == "Rejected" || resolution == "Canceled");

                                        if(!ignore) {
                                            putDataPoint(moduledata, endOfYearDelivery, q1Delivery, q2Delivery, dueDateConfirmed, status, moduleGroup, teamName, streamName, calcStoryPoints, storyPoints, ++count, module);
                                        }
                                        callback();
                                },
                                function(err) {
                                    callback();
                                });
                            }
                            else {
                                putDataPoint(moduledata, endOfYearDelivery, q1Delivery, q2Delivery, dueDateConfirmed, "Empty", "Unknown Module Group", teamName, streamName, 0, 0, count, module);
                                callback();
                            }
                        })
{'fraction_non_alphanumeric': 0.11417296046812275, 'fraction_numerical': 0.010342246717017079, 'mean_word_length': 1.1898092967818832, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 100, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '888742', 'n_tokens_mistral': 3586, 'n_tokens_neox': 3392, 'n_words': 712}