forked from RallyCommunity/CustomChart
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCalculator.js
205 lines (188 loc) · 7.66 KB
/
Calculator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
Ext.define('Calculator', {
config: {
calculationType: undefined,
field: undefined,
stackField: undefined,
stackValues: undefined,
bucketBy: undefined
},
constructor: function(config) {
this.initConfig(config);
},
prepareChartData: function(store) {
var data = this._groupData(store),
categories = _.keys(data),
seriesData;
if (!this.stackField) {
if(this.calculationType === 'count') {
seriesData = _.map(data, function(value, key) {
return [key, value.length];
});
} else {
seriesData = _.map(data, function(value, key) {
var valueTotal = _.reduce(value, function(total, r) {
var valueField = this._getValueFieldForCalculationType();
return total + r.get(valueField);
}, 0, this);
return [key, valueTotal];
}, this);
}
return {
categories: categories,
series: [
{
name: this.field,
type: this.seriesType,
data: seriesData
}
]
};
} else {
var stackField = store.model.getField(this.stackField),
stackValues;
if (this.stackValues) {
stackValues = _.map(this.stackValues, function(stackValue) {
return this._getDisplayValue(stackField, stackValue);
}, this);
} else {
var values = _.invoke(store.getRange(), 'get', this.stackField);
if (this.stackField === 'Iteration' || this.stackField === 'Release') {
values = _.sortBy(values, function(timebox) {
var dateValue = timebox && (timebox.StartDate || timebox.ReleaseStartDate || null);
return new Date(dateValue);
});
}
stackValues = _.unique(_.map(values, function(value) {
return this._getDisplayValue(stackField, value);
}, this));
}
var series = {};
_.each(categories, function(category) {
var group = data[category];
var recordsByStackValue = _.groupBy(group, function(record) {
return this._getDisplayValueForField(record, this.stackField);
}, this);
_.each(stackValues, function(stackValue) {
series[stackValue] = series[stackValue] || [];
var records = recordsByStackValue[stackValue];
if(this.calculationType === 'count') {
series[stackValue].push((records && records.length) || 0);
} else {
var valueTotal = _.reduce(records, function(total, r) {
var valueField = this._getValueFieldForCalculationType();
return total + r.get(valueField);
}, 0, this);
series[stackValue].push(valueTotal);
}
}, this);
}, this);
return {
categories: categories,
series: _.map(stackValues, function(value) {
return {
name: value,
type: this.seriesType,
data: series[value]
};
}, this)
};
}
},
_groupData: function(store) {
var field = store.model.getField(this.field),
fieldType = field.getType(),
groups = {};
if (fieldType === 'collection') {
_.each(store.getRange(), function(record) {
var value = record.get(this.field),
values = value._tagsNameArray;
if (_.isEmpty(values)) {
groups.None = groups.None || [];
groups.None.push(record);
} else {
_.each(values, function(val) {
groups[val.Name] = groups[val.Name] || [];
groups[val.Name].push(record);
});
}
}, this);
return groups;
} else {
groups = _.groupBy(store.getRange(), function(record) {
return this._getDisplayValueForField(record, this.field);
}, this);
if (fieldType === 'date') {
var dates = _.sortBy(_.compact(_.map(store.getRange(), function(record) {
return record.get(this.field);
}, this)));
var datesNoGaps = this._getDateRange(dates[0], dates[dates.length-1]);
var allGroups = {};
if (groups['-- No Entry --']) {
allGroups['-- No Entry --'] = groups['-- No Entry --'];
}
groups = _.reduce(datesNoGaps, function(accum, val) {
var group = this._getDisplayValue(field, moment(val).toDate());
accum[group] = groups[group] || [];
return accum;
}, allGroups, this);
}
return groups;
}
},
_getDateRange: function(startDate, endDate) {
var currentDate = startDate;
var datesNoGaps = [];
var unit = 'd';
if (this.bucketBy === 'week') {
unit = 'w';
} else if(this.bucketBy === 'month') {
unit = 'M';
} else if(this.bucketBy === 'quarter') {
unit = 'Q';
} else if(this.bucketBy === 'year') {
unit = 'y';
}
while(currentDate <= endDate) {
datesNoGaps.push(currentDate);
currentDate = moment(currentDate).add(1, unit).toDate();
}
datesNoGaps.push(endDate);
return datesNoGaps;
},
_getDisplayValueForField: function(record, fieldName) {
var field = record.getField(fieldName),
value = record.get(fieldName);
return this._getDisplayValue(field, value);
},
_getDisplayValue: function(field, value) {
if (_.isDate(value)) {
if (!this.bucketBy || this.bucketBy === 'day') {
return Rally.util.DateTime.formatWithDefault(value);
} else if (this.bucketBy === 'week') {
return Rally.util.DateTime.formatWithDefault(moment(value).startOf('week').toDate());
} else if (this.bucketBy === 'month') {
return moment(value).startOf('month').format('MMM \'YY');
} else if (this.bucketBy === 'quarter') {
return moment(value).startOf('quarter').format('YYYY [Q]Q');
} else if (this.bucketBy === 'year') {
return moment(value).startOf('year').format('YYYY');
}
} else if (_.isObject(value)) {
return value._refObjectName;
} else if (Ext.isEmpty(value)) {
var fieldType = field.getType();
if (field.attributeDefinition.SchemaType === 'User') {
return '-- No Owner --';
} else if (fieldType === 'rating' || fieldType === 'object') {
return 'None';
} else {
return '-- No Entry --';
}
} else {
return value;
}
},
_getValueFieldForCalculationType: function() {
return ChartUtils.getFieldForAggregationType(this.calculationType);
}
});