calc only avg build duration at collection, without getting builds

This commit is contained in:
oleg 2016-01-07 22:02:59 +03:00
parent 6a44a5f955
commit cbee3ecf85
4 changed files with 26 additions and 51 deletions

View File

@ -15,15 +15,21 @@ exports.init = function(app, callback) {
Steppy( Steppy(
function() { function() {
if (_(build.project).has('avgBuildDuration')) { if (_(build.project).has('avgBuildDuration')) {
this.pass(build.project.avgBuildDuration); this.pass(null);
} else { } else {
app.builds.getProjectAvgBuildDuration({ app.builds.getRecent({
projectName: build.project.name projectName: build.project.name,
status: 'done',
limit: 10
}, this.slot()); }, this.slot());
} }
}, },
function(err, avgBuildDuration) { function(err, doneBuilds) {
build.project.avgBuildDuration = avgBuildDuration; if (doneBuilds) {
build.project.avgBuildDuration = (
app.builds.getAvgBuildDuration(doneBuilds)
);
}
db.builds.put(build, this.slot()); db.builds.put(build, this.slot());
}, },

View File

@ -99,38 +99,11 @@ BuildsCollection.prototype.getLogLinesTail = function(params, callback) {
); );
}; };
/** BuildsCollection.prototype.getAvgBuildDuration = function(builds) {
* Calculate average build duration (in ms) for the given project. var durationsSum = _(builds).reduce(function(sum, build) {
* return sum + (build.endDate - build.startDate);
* @param {String} params.projectName
* @param {Number} [params.buildsCount]
* @param {Function} callback(err,duration)
*/
BuildsCollection.prototype.getProjectAvgBuildDuration = function(params, callback) {
params.buildsCount = params.buildsCount || 10;
var self = this;
Steppy(
function() {
// get last done builds to calc avg build time
self.db.builds.find({
start: {
projectName: params.projectName,
status: 'done',
descCreateDate: ''
},
limit: params.buildsCount
}, this.slot());
},
function(err, doneBuilds) {
var durationsSum = _(doneBuilds).reduce(function(memo, build) {
return memo + (build.endDate - build.startDate);
}, 0); }, 0);
return Math.round(durationsSum / builds.length);
this.pass(Math.round(durationsSum / doneBuilds.length));
},
callback
);
}; };
BuildsCollection.prototype.getRecent = function(params, callback) { BuildsCollection.prototype.getRecent = function(params, callback) {

View File

@ -2,7 +2,7 @@
var _ = require('underscore'), var _ = require('underscore'),
errorHandler = require('./errorHandler'), errorHandler = require('./errorHandler'),
createBuildDataResource = require('./helpers').createBuildDataResource; helpers = require('./helpers');
module.exports = function(app) { module.exports = function(app) {
_(['builds', 'projects']).each(function(resource) { _(['builds', 'projects']).each(function(resource) {
@ -14,7 +14,7 @@ module.exports = function(app) {
app.builds.on('buildUpdated', function(build, changes) { app.builds.on('buildUpdated', function(build, changes) {
if (build.status === 'queued') { if (build.status === 'queued') {
createBuildDataResource(app, build.id); helpers.createBuildDataResource(app, build.id);
} }
// notify about build's project change, coz building affects project // notify about build's project change, coz building affects project

View File

@ -2,14 +2,14 @@
var Steppy = require('twostep').Steppy, var Steppy = require('twostep').Steppy,
_ = require('underscore'), _ = require('underscore'),
createBuildDataResource = require('./helpers').createBuildDataResource, helpers = require('./helpers'),
logger = require('../lib/logger')('projects resource'); logger = require('../lib/logger')('projects resource');
module.exports = function(app) { module.exports = function(app) {
var resource = app.dataio.resource('projects'); var resource = app.dataio.resource('projects');
resource.use('createBuildDataResource', function(req, res) { resource.use('createBuildDataResource', function(req, res) {
createBuildDataResource(app, req.data.buildId); helpers.createBuildDataResource(app, req.data.buildId);
res.send(); res.send();
}); });
@ -28,28 +28,24 @@ module.exports = function(app) {
res.send(filteredProjects); res.send(filteredProjects);
}); });
// get project with additional fields
var getProject = function(name, callback) { var getProject = function(name, callback) {
var project; var project;
Steppy( Steppy(
function() { function() {
project = app.projects.get(name); project = _(app.projects.get(name)).clone();
app.builds.getProjectAvgBuildDuration({
projectName: project.name
}, this.slot());
// get last done build
app.builds.getRecent({ app.builds.getRecent({
projectName: project.name, projectName: project.name,
status: 'done', status: 'done',
limit: 1 limit: 10
}, this.slot()); }, this.slot());
app.builds.getDoneStreak({projectName: project.name}, this.slot()); app.builds.getDoneStreak({projectName: project.name}, this.slot());
}, },
function(err, avgProjectBuildDuration, lastDoneBuilds, doneBuildsStreak) { function(err, doneBuilds, doneBuildsStreak) {
project.lastDoneBuild = lastDoneBuilds[0]; project.avgBuildDuration = app.builds.getAvgBuildDuration(doneBuilds);
project.avgBuildDuration = avgProjectBuildDuration; project.lastDoneBuild = doneBuilds[0];
project.doneBuildsStreak = doneBuildsStreak; project.doneBuildsStreak = doneBuildsStreak;
this.pass(project); this.pass(project);