nci/resources/projects.js

93 lines
2.1 KiB
JavaScript
Raw Normal View History

2015-04-10 19:23:52 +00:00
'use strict';
var Steppy = require('twostep').Steppy,
2015-07-09 20:12:24 +00:00
_ = require('underscore'),
createBuildDataResource = require('../distributor').createBuildDataResource,
logger = require('../lib/logger')('projects resource'),
db = require('../db');
2015-04-12 22:19:42 +00:00
2015-05-12 20:12:28 +00:00
module.exports = function(app) {
2015-04-12 22:41:09 +00:00
var resource = app.dataio.resource('projects'),
distributor = app.distributor;
2015-05-10 10:04:54 +00:00
2015-05-14 20:37:44 +00:00
resource.use('createBuildDataResource', function(req, res) {
createBuildDataResource(req.data.buildId);
2015-05-14 20:37:44 +00:00
res.send();
});
2015-05-17 13:48:16 +00:00
resource.use('readAll', function(req, res) {
res.send(app.projects);
2015-04-12 22:41:09 +00:00
});
2015-07-09 20:12:24 +00:00
resource.use('read', function(req, res) {
var project;
Steppy(
function() {
project = _(app.projects).findWhere(req.data);
// get last done builds to calc avg build time
db.builds.find({
start: {
projectName: project.name,
status: 'done',
descCreateDate: ''
},
2015-07-15 07:54:50 +00:00
limit: 10
}, this.slot());
2015-07-15 07:54:50 +00:00
// tricky but effective streak counting inside filter goes below
var doneBuildsStreakCallback = _(this.slot()).once(),
doneBuildsStreak = 0;
db.builds.find({
start: {
projectName: project.name,
descCreateDate: ''
},
filter: function(build) {
2015-07-15 07:54:50 +00:00
// error exits streak
if (build.status === 'error') {
doneBuildsStreakCallback(null, doneBuildsStreak);
return true;
}
2015-07-15 07:54:50 +00:00
if (build.status === 'done') {
doneBuildsStreak++;
}
},
limit: 1
}, function(err) {
doneBuildsStreakCallback(err, doneBuildsStreak);
});
},
2015-07-15 07:54:50 +00:00
function(err, doneBuilds, doneBuildsStreak) {
project.lastDoneBuild = doneBuilds[0];
var durationsSum = _(doneBuilds).reduce(function(memo, build) {
return memo + (build.endDate - build.startDate);
}, 0);
project.avgBuildDuration = Math.round(
durationsSum / doneBuilds.length
);
2015-07-15 07:54:50 +00:00
project.doneBuildsStreak = doneBuildsStreak;
res.send(project);
}
);
2015-07-09 20:12:24 +00:00
});
resource.use('run', function(req, res) {
2015-06-13 18:59:32 +00:00
var projectName = req.data.projectName;
2015-07-05 18:03:58 +00:00
logger.log('Run the project: "%s"', projectName);
2015-06-14 14:21:04 +00:00
distributor.run({
projectName: projectName,
initiator: {type: 'user'}
2015-07-05 18:03:58 +00:00
});
res.send();
});
return resource;
2015-04-10 19:23:52 +00:00
};