nci/resources/projects.js

129 lines
3.0 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'),
2015-07-26 13:05:54 +00:00
getAvgProjectBuildDuration =
require('../lib/project').getAvgProjectBuildDuration,
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) {
2015-10-15 22:13:10 +00:00
var filteredProjects = app.projects,
nameQuery = req.data && req.data.nameQuery;
if (nameQuery) {
filteredProjects = _(filteredProjects).filter(function(project) {
return project.name.indexOf(nameQuery) !== -1;
});
}
res.send(filteredProjects);
2015-04-12 22:41:09 +00:00
});
2015-09-26 20:49:03 +00:00
var getProject = function(params, callback) {
var project;
Steppy(
function() {
2015-09-26 20:49:03 +00:00
project = _(app.projects).findWhere(params.condition);
2015-07-26 13:05:54 +00:00
getAvgProjectBuildDuration(project.name, this.slot());
// get last done build
db.builds.find({
start: {
projectName: project.name,
status: 'done',
descCreateDate: ''
},
2015-07-26 13:05:54 +00:00
limit: 1
}, 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-26 13:05:54 +00:00
function(err, avgProjectBuildDuration, lastDoneBuilds, doneBuildsStreak) {
project.lastDoneBuild = lastDoneBuilds[0];
project.avgBuildDuration = avgProjectBuildDuration;
2015-07-15 07:54:50 +00:00
project.doneBuildsStreak = doneBuildsStreak;
2015-09-26 20:49:03 +00:00
this.pass(project);
},
callback
);
};
// resource custom method which finds project by condition
// and emits event about it change to clients
resource.clientEmitSyncChange = function(condition) {
Steppy(
function() {
getProject({condition: condition}, this.slot());
},
function(err, project) {
resource.clientEmitSync('change', {project: project});
2015-09-26 20:49:03 +00:00
},
function(err) {
console.error(
'Error during sync project change occurred:',
err.stack || err
);
}
);
};
resource.use('read', function(req, res) {
Steppy(
function() {
getProject({condition: req.data}, this.slot());
},
function(err, project) {
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
};