<commit_before>var express = require('express');
var mongoose = require('mongoose');
var router = express.Router();
const Application = mongoose.model('Application');
const Dependency = mongoose.model('Dependency');
const DependencyVersion = mongoose.model('DependencyVersion');

var renderIndex = function(req, res, next) {
  console.log("Render index!");
  Application.find({ hidden: false }).sort({ type: 1, title : -1 }).then(function(apps) {
    return res.render('index', {applications: apps});
  }, next);
};

var resultOrError = function(res) {
    return function(result) {
        if (!result) return res.sendStatus(404);
        return result;
    }
};

var findApplication = function(id, match, res) {
    return Application.findOne({ id: id })
        .populate({
            path: 'versions',
            populate: { path : 'compatible', populate: { path: 'type', select: 'name' } },
            select: 'name changelog filename compatible sortingCode',
            match: match,
            options: {
                sort: { sortingCode: -1 }
            }
        }).then(resultOrError(res));
};

var renderApp = function(req, res, next, showNightly, showAll) {
  res.locals.nightly = showNightly;

    findApplication(req.params.id, { hidden: { $ne: true }, nightly: showNightly }, res)
  .then(function(app) {
    if (!showAll) {
      var versionsCode = [];
      var versionsToShow = [];
      app.versions.forEach(function(ver) {
        console.log(Math.floor(ver.sortingCode / 100) * 100);
        if (versionsCode.indexOf(Math.floor(ver.sortingCode / 100) * 100) === -1) {
          versionsToShow.push(ver);
          versionsCode.push(Math.floor(ver.sortingCode / 100) * 100);
        }
      });

      app.versions = versionsToShow;
    }

    app.dependencies = [];
    app.versions.forEach(function(version) {
      version.compatible.forEach(function(dep) {
        if (app.dependencies.indexOf(dep) === -1)
          app.dependencies.push(dep);
      });
    });

    console.log(app);

    return res.render("application", app);
  })
}

/* GET home page. */
router.get('/', renderIndex);

router.get('/nightly', function(req, res, next) {
  res.locals.nightly = true;

  renderIndex(req, res, next);
});

router.get("/icon/:id", function(req, res, next) {
  Application.findOne({ id: req.params.id }).then(function(app) {
    if (!app) return res.sendStatus(404);

    return res.sendFile(__dirname.replace("routes", "") + "/"+ app.icon);
  })
});

router.get("/app/:id", function(req, res, next) {
  renderApp(req, res, next, false);
});

router.get("/app/:id/all", function(req, res, next) {
  renderApp(req, res, next, false, true);
});

router.get("/nightly/:id", function(req, res, next) {
  renderApp(req, res, next, true, true);
});

router.get("/app/:id/:deptype/:depver", function(req, res, next) {
  Dependency.findOne({name: req.params.deptype })
      .then(resultOrError(res))
      .then(function(dep) {
          return DependencyVersion.findOne({ version: req.params.depver });
      })
      .then(resultOrError(res))
      .then(function(depVer) {
        return findApplication(req.params.id, { hidden: { $ne: true }, compatible : { $in : [depVer._id]} }, res);
      })
      .then(function(app) {
        app.dependencies = [];
        app.versions.forEach(function(version) {
          version.compatible.forEach(function(dep) {
            if (app.dependencies.indexOf(dep) === -1)
              app.dependencies.push(dep);
          });
        });

        //app.versions.filter(function(version) {
        //  return
        //});

        return res.render("application", app);
      });
});

module.exports = router;


<commit_msg>Code format and hopefully fix dependency filtering<commit_after>var express = require('express');
var mongoose = require('mongoose');
var router = express.Router();
const Application = mongoose.model('Application');
const Dependency = mongoose.model('Dependency');
const DependencyVersion = mongoose.model('DependencyVersion');

var renderIndex = function (req, res, next) {
  console.log("Render index!");
  Application.find({hidden: false}).sort({type: 1, title: -1}).then(function (apps) {
    return res.render('index', {applications: apps});
  }, next);
};

var resultOrError = function (res) {
  return function (result) {
    if (!result) return res.sendStatus(404);
    return result;
  }
};

var findApplication = function (id, match, res) {
  return Application.findOne({id: id})
    .populate({
      path: 'versions',
      populate: {path: 'compatible', populate: {path: 'type', select: 'name'}},
      select: 'name changelog filename compatible sortingCode',
      match: match,
      options: {
        sort: {sortingCode: -1}
      }
    }).then(resultOrError(res))
    .then(buildDependencyList);
};

var buildDependencyList = function (app) {
  // Build list of dependencies
  app.dependencies = [];
  app.versions.forEach(function (version) {
    version.compatible.forEach(function (dep) {
      if (app.dependencies.indexOf(dep) === -1)
        app.dependencies.push(dep);
    });
  });

  return app;
};

var renderApp = function (req, res, next, showNightly, showAll) {
  res.locals.nightly = showNightly;

  findApplication(req.params.id, {hidden: {$ne: true}, nightly: showNightly}, res)
    .then(function (app) {
      if (!showAll) {
        var versionsCode = [];
        var versionsToShow = [];

        // Filter show only latest of each minor version.
        app.versions.forEach(function (ver) {
          if (versionsCode.indexOf(Math.floor(ver.sortingCode / 100) * 100) === -1) {
            versionsToShow.push(ver);
            versionsCode.push(Math.floor(ver.sortingCode / 100) * 100);
          }
        });

        app.versions = versionsToShow;
      }

      return res.render("application", app);
    })
};

/* GET home page. */
router.get('/', renderIndex);

router.get('/nightly', function (req, res, next) {
  res.locals.nightly = true;

  renderIndex(req, res, next);
});

router.get("/icon/:id", function (req, res, next) {
  Application.findOne({id: req.params.id}).then(function (app) {
    if (!app) return res.sendStatus(404);

    return res.sendFile(__dirname.replace("routes", "") + "/" + app.icon);
  })
});

router.get("/app/:id", function (req, res, next) {
  renderApp(req, res, next, false);
});

router.get("/app/:id/all", function (req, res, next) {
  renderApp(req, res, next, false, true);
});

router.get("/nightly/:id", function (req, res, next) {
  renderApp(req, res, next, true, true);
});

router.get("/app/:id/:deptype/:depver", function (req, res, next) {
  Dependency.findOne({name: req.params.deptype})
    .then(resultOrError(res))
    .then(function (dep) {
      return DependencyVersion.findOne({version: req.params.depver});
    })
    .then(resultOrError(res))
    .then(function (depVer) {
      console.log("Search fo rapp!");
      return findApplication(req.params.id, {hidden: {$ne: true}, nightly: false}, res).then(function (app) {
        return {app: app, depVer: depVer}
      });
    })
    .then(function (obj) {
      console.log("Found stuff!");
      var app = obj.app;
      var depVer = obj.depVer;

      var versionsToShow = [];

      // Filter show only latest of each minor version.
      app.versions.forEach(function (ver) {
        console.log("Foreach!");
        if (ver.compatible.indexOf(depVer) === -1) {
          versionsToShow.push(ver);
        }
      });

      app.versions = versionsToShow;

      return res.render("application", app);
    });
});

module.exports = router;



{'fraction_non_alphanumeric': 0.16934421912035819, 'fraction_numerical': 0.0071108770081643406, 'mean_word_length': 3.0206458443620963, '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': '3573652', 'n_tokens_mistral': 2429, 'n_tokens_neox': 2359, 'n_words': 509}