<commit_before>
/**
 * Module dependencies.
 */

var fs = require('fs')
  , path = require('path');

var MongoClient = require('mongodb').MongoClient
  , ObjectID = require('mongodb').ObjectID

/*
 * GET description of a simulation.
 */

exports.index = function(req, res, next) {
  res.render('graph/graph');
};

function findAllTeams(teams, next) {
  var res = [];

  teams.find({}, { sort: 'name' }).each(function(err, doc) {
    if (err) {
      return next(err);
    }

    // Check if have exhausted cursor
    if (doc === null) {
      return next(null, res);
    }

    res.push(doc.name);
  });
};

function findAllGraphs(graphs, next) {
  var group = { $group: { _id: '$category', graph: { $push: '$name' } } }
    , sort = { $sort : { '_id' : 1 } };

  graphs.aggregate([ group, sort ], function(err, res) {
    if (err) {
      return next(err);
    }

    var categories = []
      , graphs = [];

    res.forEach(function(value) {
      var category = {};
      category[value._id] = value.graph;

      categories.push(category);
      graphs.push.apply(graphs, value.graph)
    });

    return next(null, { categories: categories, graphs: graphs });
  });
};

function findAllRuns(runs, next) {
  runs.aggregate([{ $unwind: '$teams' }], next);
};

/*
 * GET listing of simulations.
 */
exports.list = function(req, res, next) {
  MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    if (err) {
      return next(err);
    }

    var teams = db.collection('teams')
      , graphs = db.collection('graphs')
      , runs = db.collection('runs');

    function doNext(err) {
      db.close();
      next(err);
    };

    // Get all teams
    findAllTeams(teams, function(err, teams) {
      if (err) {
        return doNext(err);
      }

      // Get all graphs, grouped by category
      findAllGraphs(graphs, function(err, value) {
        if (err) {
          doNext(err);
        }

        var categories = value.categories
          , graphs = value.graphs;

        // Iterate through all runs, unwound by team
        findAllRuns(runs, function(err, runs) {
          if (err) {
            doNext(err);
          }

          var invTeams = {};
          teams.forEach(function(value, index) {
            invTeams[value] = index;
          });

          var invGraphs = {};
          graphs.forEach(function(value, index) {
            invGraphs[value] = index;
          });

          var matrix = [];

          // Initialize empty matrix
          // number of teams by number of graphs
          teams.forEach(function() {
            var row = [];

            graphs.forEach(function() {
              row.push(null);
            });

            matrix.push(row);
          });

          runs.forEach(function(doc) {
            var i = invTeams[doc.teams];
            var j = invGraphs[doc.graph];

            matrix[i][j] = doc._id;
          });

          res.render('graph/dashboard', { matrix: matrix
                                        , teams: teams
                                        , categories: categories
                                        , graphs: graphs });
        });
      });
    });
  });
};

/*<commit_msg>Handle case where no teams or graphs are present.<commit_after>
/**
 * Module dependencies.
 */

var fs = require('fs')
  , path = require('path');

var MongoClient = require('mongodb').MongoClient
  , ObjectID = require('mongodb').ObjectID

/*
 * GET description of a simulation.
 */

exports.index = function(req, res, next) {
  res.render('graph/graph');
};

function findAllTeams(teams, next) {
  var res = [];

  teams.find({}, { sort: 'name' }).each(function(err, doc) {
    if (err) {
      return next(err);
    }

    // Check if have exhausted cursor
    if (doc === null) {
      return next(null, res);
    }

    res.push(doc.name);
  });
};

function findAllGraphs(graphs, next) {
  var group = { $group: { _id: '$category', graph: { $push: '$name' } } }
    , sort = { $sort : { '_id' : 1 } };

  graphs.aggregate([ group, sort ], function(err, res) {
    if (err) {
      return next(err);
    }

    var categories = []
      , graphs = [];

    res.forEach(function(value) {
      var category = {};
      category[value._id] = value.graph;

      categories.push(category);
      graphs.push.apply(graphs, value.graph)
    });

    return next(null, { categories: categories, graphs: graphs });
  });
};

function findAllRuns(runs, next) {
  runs.aggregate([{ $unwind: '$teams' }], next);
};

/*
 * GET listing of simulations.
 */
exports.list = function(req, res, next) {
  MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    if (err) {
      return next(err);
    }

    var teams = db.collection('teams')
      , graphs = db.collection('graphs')
      , runs = db.collection('runs');

    function doNext(err) {
      db.close();
      next(err);
    };

    // Get all teams
    findAllTeams(teams, function(err, teams) {
      if (err) {
        return doNext(err);
      }

      // Get all graphs, grouped by category
      findAllGraphs(graphs, function(err, value) {
        if (err) {
          doNext(err);
        }

        var categories = value.categories
          , graphs = value.graphs;

        // Iterate through all runs, unwound by team
        findAllRuns(runs, function(err, runs) {
          if (err) {
            doNext(err);
          }

          var invTeams = {};
          teams.forEach(function(value, index) {
            invTeams[value] = index;
          });

          var invGraphs = {};
          graphs.forEach(function(value, index) {
            invGraphs[value] = index;
          });

          var matrix = [];

          // Initialize empty matrix
          // number of teams by number of graphs
          teams.forEach(function() {
            var row = [];

            graphs.forEach(function() {
              row.push(null);
            });

            matrix.push(row);
          });

          runs.forEach(function(doc) {
            var i = invTeams[doc.teams];
            var j = invGraphs[doc.graph];

            if (i !== undefined && j !== undefined) {
              matrix[i][j] = doc._id;
            }
          });

          res.render('graph/dashboard', { matrix: matrix
                                        , teams: teams
                                        , categories: categories
                                        , graphs: graphs });
        });
      });
    });
  });
};

/*
{'fraction_non_alphanumeric': 0.15033783783783783, 'fraction_numerical': 0.0018427518427518428, 'mean_word_length': 1.7809564474807857, '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': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2681644', 'n_tokens_mistral': 1977, 'n_tokens_neox': 1910, 'n_words': 435}