From cbee3ecf85516e635c3381e88112f51c6e015bf5 Mon Sep 17 00:00:00 2001 From: oleg Date: Thu, 7 Jan 2016 22:02:59 +0300 Subject: [PATCH] calc only avg build duration at collection, without getting builds --- distributor.js | 16 +++++++++++----- lib/build.js | 37 +++++-------------------------------- resources/index.js | 4 ++-- resources/projects.js | 20 ++++++++------------ 4 files changed, 26 insertions(+), 51 deletions(-) diff --git a/distributor.js b/distributor.js index 8cd7ecc..8bacbf1 100644 --- a/distributor.js +++ b/distributor.js @@ -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()); }, diff --git a/lib/build.js b/lib/build.js index d0f29a1..b90da0e 100644 --- a/lib/build.js +++ b/lib/build.js @@ -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); - }, 0); - - this.pass(Math.round(durationsSum / doneBuilds.length)); - }, - callback - ); +BuildsCollection.prototype.getAvgBuildDuration = function(builds) { + var durationsSum = _(builds).reduce(function(sum, build) { + return sum + (build.endDate - build.startDate); + }, 0); + return Math.round(durationsSum / builds.length); }; BuildsCollection.prototype.getRecent = function(params, callback) { diff --git a/resources/index.js b/resources/index.js index 25c7a9f..6371d08 100644 --- a/resources/index.js +++ b/resources/index.js @@ -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 diff --git a/resources/projects.js b/resources/projects.js index e89b7f2..90ba311 100644 --- a/resources/projects.js +++ b/resources/projects.js @@ -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);