<commit_before>//Make sure to load uuid.js and uuidhelpers.js into mongo before running this!

mapFunction = function() {
    var level1 = level2 = level3 = level4 = 0;
    switch(parseInt(this.body.performanceLevel))
    {
        case 1: level1++; break;
        case 2: level2++; break;
        case 3: level3++; break;
        case 4: level4++; break;
        default: break;
    }
    
    var values = {level1:level1,level2:level2,level3:level3,level4:level4};
    var studentId = this.body.studentId;
    var ssaArray = db.studentschoolassociation.find({"body.studentId":studentId}).toArray();
    var districts = new Array();
    
    for( var i=0; i<ssaArray.length; i++) {    
        var schoolId = ssaArray[i].body.schoolId;
        emit({"schoolId":schoolId,"assessmentType":aggregation_name}, values); 
        
        var eosaArray = db.educationOrganizationschoolassociation.find({"body.schoolId":schoolId}).toArray();
        
        for( var j=0; j<eosaArray.length; j++) {    
            districts[eosaArray[j].body.educationOrganizationId] = eosaArray[j].body.educationOrganizationId;
        }
    }
    
    for( districtId in districts) {    
        emit({"districtId":districtId,"assessmentType":aggregation_name}, values); 
    }
};

reduceFunction = function(key,values) {
    var level1total = level2total = level3total = level4total = 0;
    
    for( var i=0; i<values.length; i++) {    
        level1total+=values[i].level1;
        level2total+=values[i].level2;
        level3total+=values[i].level3;
        level4total+=values[i].level4;
    }
    
    return {level1:level1total,level2:level2total,level3:level3total,level4:level4total}; 
    
};

finalizeFunction = function(key,value) {
    
    var total = value.level1 + value.level2 + value.level3 + value.level4;
    
    var level1percentage = value.level1*100.0/total;
    var level2percentage = value.level2*100.0/total;
    var level3percentage = value.level3*100.0/total;
    var level4percentage = value.level4*100.0/total;
    
    return {
        ts: new Date(),
        level1:value.level1,
        level2:value.level2,
        level3:value.level3,
        level4:value.level4,
        level1percentage:Math.round(level1percentage*10)/10,
        level2percentage:Math.round(level2percentage*10)/10,
        level3percentage:Math.round(level3percentage*10)/10,
        level4percentage:Math.round(level4percentage*10)/10,
        groupBy: key,
        raw:"true"
    }; 
    
};


var cleanUp = function() {
	db.aggregation.find({"value.raw":"true"}).forEach(function (data) {
		var old_id = data._id;
		var doc = data;
		
		doc._id = JUUID(UUID.generate());
		doc.type = "aggregation";
		doc.body = doc.value;

		delete doc.value;
		delete doc.body.raw;
		
		db.aggregation.insert(doc);
		db.aggregation.remove({ _id: old_id});
	});
}

db.aggregation.drop()

db.runCommand( { mapreduce:"studentassessmentassociation",
                                                      map:mapFunction,
                                                      reduce:reduceFunction,
                                                      finalize:finalizeFunction,
                                                      query: {"body.assessmentId":{$in: ["67ce204b-9999-4a11-bfea-000000004682","67ce204b-9999-4a11-bfea-000000004683","67ce204b-9999-4a11-bfea-000000004684"]}},
                                                      scope: { aggregation_name : "8th Grade EOG" },
                                                      out: { reduce: "aggregation" }
                                                    });
cleanUp();
db.aggregation.find()


<commit_msg>Modified function names and parameters<commit_after>//Make sure to load uuid.js and uuidhelpers.js into mongo before running this!

mapDistrictPerf1to4 = function() {
    
    //initialize result to all zeroes
    var level1 = level2 = level3 = level4 = 0;
    //increment variable representing this record's performance level
    switch(parseInt(this.body.performanceLevel))
    {
        case 1: level1++; break;
        case 2: level2++; break;
        case 3: level3++; break;
        case 4: level4++; break;
        default: break;
    }
    
    //create new result
    var values = {level1:level1,level2:level2,level3:level3,level4:level4};
    //new array to track associated districts (cannot hold duplicates)
    var districts = new Array();
    
    //list schools for student
    db.studentschoolassociation.find({"body.studentId":this.body.studentId}).forEach(
        function (ssa) {
            //list districts for school
            db.educationOrganizationschoolassociation.find({"body.schoolId":ssa.body.schoolId}).forEach(
                function (eosa) {
                    //register district as associated
                    districts[eosa.body.educationOrganizationId] = eosa.body.educationOrganizationId;
                }
            );
        }
    );
    
    //for each associated district
    for( districtId in districts) {    
        emit({"districtId":districtId,"assessmentType":aggregation_name}, values); 
    }
};

reducePerf1to4 = function(key,values) {
    
    //initialize sum variables to zero
    var level1total = level2total = level3total = level4total = 0;
    
    //for each value provided
    for( var i=0; i<values.length; i++) {    
        //add each value to sum
        level1total+=values[i].level1;
        level2total+=values[i].level2;
        level3total+=values[i].level3;
        level4total+=values[i].level4;
    }
    
    //return sum document
    return {level1:level1total,level2:level2total,level3:level3total,level4:level4total}; 
};

finalizePerf1to4 = function(key,value) {
    
    //determine total number of values
    var total = value.level1 + value.level2 + value.level3 + value.level4;
    
    //compute percentages, rounded to 1 decimal place
    var level1percentage = Math.round(10*value.level1*100.0/total)/10;
    var level2percentage = Math.round(10*value.level2*100.0/total)/10;
    var level3percentage = Math.round(10*value.level3*100.0/total)/10;
    var level4percentage = Math.round(10*value.level4*100.0/total)/10;
    
    return {
        ts: execution_time,
        level1:value.level1,
        level2:value.level2,
        level3:value.level3,
        level4:value.level4,
        level1percentage:level1percentage,
        level2percentage:level2percentage,
        level3percentage:level3percentage,
        level4percentage:level4percentage,
        groupBy: key,
        raw:"true"
    }; 
    
};

var cleanupBodyAndId = function() {
    //list aggregates needing cleanup
    db.aggregation.find({"value.raw":"true"}).forEach(
        function (data) {
            var old_id = data._id;
            var doc = data;
            
            doc._id = JUUID(UUID.generate());
            doc.type = "aggregation";
            doc.body = doc.value;

            delete doc.value;
            delete doc.body.raw;
            
            db.aggregation.insert(doc);
            db.aggregation.remove({ _id: old_id});
        }
    );
}

db.aggregation.drop()

var mapFunction = mapDistrictPerf1to4;
var reduceFunction = reducePerf1to4;
var finalizeFunction = finalizePerf1to4;
var collectionName = "aggregation";
var aggregationName = "8th Grade EOG";

db.runCommand( { 
    mapreduce:"studentassessmentassociation",
    map:mapFunction,
    reduce:reduceFunction,
    finalize:finalizeFunction,
    query: {"body.assessmentId":{$in: ["67ce204b-9999-4a11-bfea-000000004682","67ce204b-9999-4a11-bfea-000000004683","67ce204b-9999-4a11-bfea-000000004684"]}},
    scope: { aggregation_name : aggregationName, execution_time : new Date() },
    out: { reduce: collectionName }
});

cleanupBodyAndId();

db.aggregation.find()



{'fraction_non_alphanumeric': 0.11810821231204216, 'fraction_numerical': 0.04716617401362293, 'mean_word_length': 2.4161545215100966, 'pattern_counts': {'":': 14, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 6, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3822337', 'n_tokens_mistral': 2557, 'n_tokens_neox': 2367, 'n_words': 430}