Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactor project aggregation

  • Loading branch information...
commit 5d46449df95b9c23b8caf7012f1bdb140a210639 1 parent 73425e9
@asplake asplake authored
Showing with 30 additions and 70 deletions.
  1. +17 −43 lib/project.js
  2. +13 −27 src/project.coffee
View
60 lib/project.js
@@ -1,6 +1,16 @@
// Generated by CoffeeScript 1.3.3
(function() {
- var Project;
+ var Project, compacted_property_op;
+
+ compacted_property_op = function(collection, property, op) {
+ var compacted_properties;
+ compacted_properties = _.compact(_.pluck(collection, property));
+ if (compacted_properties.length) {
+ return op(compacted_properties);
+ } else {
+ return null;
+ }
+ };
Project = (function() {
@@ -65,52 +75,16 @@
Project.summaryProject = function(projects, title) {
return new Project(this.headers, {
title: title,
- start_date: this.minStartDate(projects),
- end_date: this.maxEndDate(projects),
- inventory_current: this.sumProperty(projects, 'inventory_current'),
- inventory_peak: this.sumProperty(projects, 'inventory_peak'),
- carry_current: this.sumProperty(projects, 'carry_current'),
- carry_peak: this.sumProperty(projects, 'carry_peak'),
+ start_date: compacted_property_op(projects, 'start_date', _.min),
+ end_date: compacted_property_op(projects, 'end_date', _.max),
+ inventory_current: compacted_property_op(projects, 'inventory_current', _.sum),
+ inventory_peak: compacted_property_op(projects, 'inventory_peak', _.sum),
+ carry_current: compacted_property_op(projects, 'carry_current', _.sum),
+ carry_peak: compacted_property_op(projects, 'carry_peak', _.sum),
cycle_time_days: this.weightedMeanCycleTime(projects)
});
};
- Project.minStartDate = function(projects) {
- var minDate, project, _i, _len;
- minDate = null;
- for (_i = 0, _len = projects.length; _i < _len; _i++) {
- project = projects[_i];
- if (project.start_date && (!minDate || (project.start_date < minDate))) {
- minDate = project.start_date;
- }
- }
- return minDate;
- };
-
- Project.maxEndDate = function(projects) {
- var maxDate, project, _i, _len;
- maxDate = null;
- for (_i = 0, _len = projects.length; _i < _len; _i++) {
- project = projects[_i];
- if (project.start_date && (!maxDate || (project.end_date > maxDate))) {
- maxDate = project.end_date;
- }
- }
- return maxDate;
- };
-
- Project.sumProperty = function(projects, property) {
- var project, sum, _i, _len;
- sum = 0;
- for (_i = 0, _len = projects.length; _i < _len; _i++) {
- project = projects[_i];
- if (project[property]) {
- sum = sum + project[property];
- }
- }
- return sum;
- };
-
Project.weightedMeanCycleTime = function(projects) {
var project, sumProd, sumWeight, _i, _len;
sumProd = 0;
View
40 src/project.coffee
@@ -1,3 +1,10 @@
+compacted_property_op = (collection, property, op) ->
+ compacted_properties = _.compact(_.pluck(collection, property))
+ if compacted_properties.length
+ return op(compacted_properties)
+ else
+ return null
+
class Project
constructor: (@headers, data) ->
for k, v of data
@@ -31,36 +38,15 @@ class Project
@headers,
{
title: title,
- start_date: @minStartDate(projects),
- end_date: @maxEndDate(projects),
- inventory_current: @sumProperty(projects, 'inventory_current'),
- inventory_peak: @sumProperty(projects, 'inventory_peak'),
- carry_current: @sumProperty(projects, 'carry_current'),
- carry_peak: @sumProperty(projects, 'carry_peak'),
+ start_date: compacted_property_op(projects, 'start_date', _.min),
+ end_date: compacted_property_op(projects, 'end_date', _.max)
+ inventory_current: compacted_property_op(projects, 'inventory_current', _.sum),
+ inventory_peak: compacted_property_op(projects, 'inventory_peak', _.sum),
+ carry_current: compacted_property_op(projects, 'carry_current', _.sum),
+ carry_peak: compacted_property_op(projects, 'carry_peak', _.sum),
cycle_time_days: @weightedMeanCycleTime(projects)
})
- @minStartDate: (projects) ->
- minDate = null
- for project in projects
- if project.start_date and (not minDate or (project.start_date < minDate))
- minDate = project.start_date
- return minDate
-
- @maxEndDate: (projects) ->
- maxDate = null
- for project in projects
- if project.start_date and (not maxDate or (project.end_date > maxDate))
- maxDate = project.end_date
- return maxDate
-
- @sumProperty: (projects, property) ->
- sum = 0
- for project in projects
- if project[property]
- sum = sum + project[property]
- return sum
-
@weightedMeanCycleTime: (projects) ->
sumProd = 0
sumWeight = 0
Please sign in to comment.
Something went wrong with that request. Please try again.