<commit_before>var mongoose = require('mongoose');
var e = require('../helpers/errorhandler');
var Group = mongoose.model('Group');
var User = mongoose.model('User');
var hash = require('../helpers/hash');
var mailer = require('../helpers/mailer');
var image = require('../helpers/image');
var socket = require('../socket');
var moment = require('moment');
var _ = require('underscore');

//Fields from user to populate into member array
var userFields = 'id name email verified hashedEmail gravatar image';
var validBodyKeys = ['name', 'description', 'startDate', 'endDate', 'public', 'minParticipants', 'maxParticipants', 'incrementDays'];
var validMemberKeys = ['status'];
var validAdminMemberKeys = ['admin'];

var filterValidKeys = function (o) {
    return _.pick(o, validBodyKeys);
};

exports.findAll = function (req, res) {
    Group.find(function (err, groups) {
        e(err, res, 'Error finding groups') || res.send(groups);
    });
};

exports.findByUser = function (req, res) {
    var user = req.user;
    var now = new Date();
    Group.find({startDate: { $gt: now } }).or([
        {'members.user': user},
        { public: true }
    ]).sort({ startDate: 'desc'}).exec(function (err, groups) {
        e(err, res, 'Error finding groups by user') || res.send(groups);
    });
};

exports.archive = function (req, res) {
    var user = req.user;
    Group.aggregate({
        $match: {
            startDate: { $lt: new Date() }
        }
    }, {
        $project: {
            key: 1,
            name: 1,
            startDate: 1,
            members: 1
        }
    }, {
        $unwind: "$members"
    }, {
        $sort: {
            startDate: -1
        }
    }, {
        $group: {
            _id: { id: "$_id", key: "$key" },
            name: {
                $last: "$name"
            },
            startDate: {
                $last: "$startDate"
            },
            participants: {
                $sum: {
                    $cond: [
                        { $eq: [ "$members.status", "Yes" ] },
                        1,
                        0
                    ]
                }
            }
        }
    }, {
        $group: {
            _id: "$_id.key",
            id: {
                $last: "$_id.id"
            },
            name: {
                $last: "$name"
            },
            startDate: {
                $last: "$startDate"
            },
            count: {
                $sum: 1
            },
            participants: {
                $avg: "$participants"
            }
        }
    }, {
        $sort: {<commit_msg>Correct sorting for archive<commit_after>var mongoose = require('mongoose');
var e = require('../helpers/errorhandler');
var Group = mongoose.model('Group');
var User = mongoose.model('User');
var hash = require('../helpers/hash');
var mailer = require('../helpers/mailer');
var image = require('../helpers/image');
var socket = require('../socket');
var moment = require('moment');
var _ = require('underscore');

//Fields from user to populate into member array
var userFields = 'id name email verified hashedEmail gravatar image';
var validBodyKeys = ['name', 'description', 'startDate', 'endDate', 'public', 'minParticipants', 'maxParticipants', 'incrementDays'];
var validMemberKeys = ['status'];
var validAdminMemberKeys = ['admin'];

var filterValidKeys = function (o) {
    return _.pick(o, validBodyKeys);
};

exports.findAll = function (req, res) {
    Group.find(function (err, groups) {
        e(err, res, 'Error finding groups') || res.send(groups);
    });
};

exports.findByUser = function (req, res) {
    var user = req.user;
    var now = new Date();
    Group.find({startDate: { $gt: now } }).or([
        {'members.user': user},
        { public: true }
    ]).sort({ startDate: 'desc'}).exec(function (err, groups) {
        e(err, res, 'Error finding groups by user') || res.send(groups);
    });
};

exports.archive = function (req, res) {
    var user = req.user;
    Group.aggregate({
        $match: {
            startDate: { $lt: new Date() }
        }
    }, {
        $project: {
            key: 1,
            name: 1,
            startDate: 1,
            members: 1
        }
    }, {
        $unwind: "$members"
    }, {
        $sort: {
            startDate: -1
        }
    }, {
        $group: {
            _id: { id: "$_id", key: "$key" },
            name: {
                $first: "$name"
            },
            startDate: {
                $first: "$startDate"
            },
            participants: {
                $sum: {
                    $cond: [
                        { $eq: [ "$members.status", "Yes" ] },
                        1,
                        0
                    ]
                }
            }
        }
    }, {
        $sort: {
            startDate: -1
        }
    }, {
        $group: {
            _id: "$_id.key",
            id: {
                $first: "$_id.id"
            },
            name: {
                $first: "$name"
            },
            startDate: {
                $first: "$startDate"
            },
            count: {
                $sum: 1
            },
            participants: {
                $avg: "$participants"
            }
        }
    }, {
        $sort: {
{'fraction_non_alphanumeric': 0.15896662266641523, 'fraction_numerical': 0.0032057326041863097, 'mean_word_length': 1.4692737430167597, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4984072', 'n_tokens_mistral': 1566, 'n_tokens_neox': 1525, 'n_words': 366}