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(
function() {
if (_(build.project).has('avgBuildDuration')) {
this.pass(build.project.avgBuildDuration);
this.pass(null);
} else {
app.builds.getProjectAvgBuildDuration({
projectName: build.project.name
app.builds.getRecent({
projectName: build.project.name,
status: 'done',
limit: 10
}, this.slot());
}
},
function(err, avgBuildDuration) {
build.project.avgBuildDuration = avgBuildDuration;
function(err, doneBuilds) {
if (doneBuilds) {
build.project.avgBuildDuration = (
app.builds.getAvgBuildDuration(doneBuilds)
);
}
db.builds.put(build, this.slot());
},

View File

@ -99,38 +99,11 @@ BuildsCollection.prototype.getLogLinesTail = function(params, callback) {
);
};
/**
* Calculate average build duration (in ms) for the given project.
*
* @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);
BuildsCollection.prototype.getAvgBuildDuration = function(builds) {
var durationsSum = _(builds).reduce(function(sum, build) {
return sum + (build.endDate - build.startDate);
}, 0);
this.pass(Math.round(durationsSum / doneBuilds.length));
},
callback
);
return Math.round(durationsSum / builds.length);
};
BuildsCollection.prototype.getRecent = function(params, callback) {

View File

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

View File

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