move calculating of average build duration for the project to builds collenction

This commit is contained in:
oleg 2016-01-07 11:25:06 +03:00
parent 4da569083b
commit 3858c3a0d0
6 changed files with 42 additions and 51 deletions

View File

@ -17,7 +17,9 @@ exports.init = function(app, callback) {
if (_(build.project).has('avgBuildDuration')) {
this.pass(build.project.avgBuildDuration);
} else {
app.projects.getAvgBuildDuration(build.project.name, this.slot());
app.builds.getProjectAvgBuildDuration({
projectName: build.project.name
}, this.slot());
}
},
function(err, avgBuildDuration) {

View File

@ -1,14 +1,3 @@
- [ProjectsCollection()](#projectscollection)
- [ProjectsCollection.validateConfig():Function)](#projectscollectionvalidateconfigconfigobjectcallbackerrconfigfunction)
- [ProjectsCollection.load()]:Function)](#projectscollectionloadnamestringcallbackerrfunction)
- [ProjectsCollection.loadAll()]:Function)](#projectscollectionloadallcallbackerrfunction)
- [ProjectsCollection.unload()]:Function)](#projectscollectionunloadnamestringcallbackerrfunction)
- [ProjectsCollection.get()](#projectscollectiongetnamestring)
- [ProjectsCollection.getAll()](#projectscollectiongetall)
- [ProjectsCollection.filter()](#projectscollectionfilterpredicatefunction)
- [ProjectsCollection.getAvgBuildDuration():Function)](#projectscollectiongetavgbuilddurationnamestringcallbackerrdurationfunction)
- [ProjectsCollection.remove()]:Function)](#projectscollectionremovenamestringcallbackerrfunction)
- [ProjectsCollection.rename()]:Function)](#projectscollectionrenamenamestringcallbackerrfunction)
## ProjectsCollection()
@ -20,7 +9,7 @@
## ProjectsCollection.validateConfig(config:Object, callback(err,config):Function)
Validate and return given config
Validate and return given config.
## ProjectsCollection.load(name:String, [callback(err)]:Function)
@ -55,10 +44,6 @@
Returns array of config objects or empty array if there is no matched
project.
## ProjectsCollection.getAvgBuildDuration(name:String, callback(err,duration):Function)
Calculates average build duration (in ms) for the given project
## ProjectsCollection.remove(name:String, [callback(err)]:Function)
Remove project by name.

View File

@ -98,3 +98,37 @@ BuildsCollection.prototype.getLogLinesTail = function(params, callback) {
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
);
};

View File

@ -240,38 +240,6 @@ ProjectsCollection.prototype.filter = function(predicate) {
return _(this.configs).filter(predicate);
};
/**
* Calculate average build duration (in ms) for the given project.
*
* @param {String} name
* @param {Function} callback(err,duration)
*/
ProjectsCollection.prototype.getAvgBuildDuration = function(name, callback) {
var self = this;
Steppy(
function() {
// get last done builds to calc avg build time
self.db.builds.find({
start: {
projectName: name,
status: 'done',
descCreateDate: ''
},
limit: 10
}, 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
);
};
/**
* Remove project by name.
* Calls `unload`, removes project from disk and db.

View File

@ -10,7 +10,7 @@
"test": "npm run makeTestRepos && mocha --bail --reporter=spec --timeout 10000",
"dev": "gulp",
"sync": "npm install && npm prune && bower install && bower prune",
"docProjectsCollection": "dox --api --skipSingleStar < lib/project.js > docs/developing-plugins/projects-collection.md",
"docProjectsCollection": "dox --api --skipSingleStar < lib/project.js | sed '/^ - \\[ProjectsCollection/ d' > docs/developing-plugins/projects-collection.md",
"buildJs": "r.js -o static/js/requirejs/buid.js",
"buildClean": "rm static/index.html",
"buildHtml": "jade views/index.jade --obj '{\"env\": \"production\"}' -o static/",

View File

@ -34,7 +34,9 @@ module.exports = function(app) {
function() {
project = app.projects.get(name);
app.projects.getAvgBuildDuration(project.name, this.slot());
app.builds.getProjectAvgBuildDuration({
projectName: project.name
}, this.slot());
// get last done build
app.builds.find({