Repository navigation
Expand file tree
/
Copy pathSprintController.js
More file actions
1127 lines (975 loc) · 41.4 KB
/
Copy pathSprintController.js
File metadata and controls
1127 lines (975 loc) · 41.4 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* SprintController
*
* @module :: Controller
* @description :: A set of functions called `actions`.
*
* Actions contain code telling Sails how to respond to a certain type of request.
* (i.e. do stuff, then send some JSON, show an HTML page, or redirect to another URL)
*
* You can configure the blueprint URLs which trigger these actions (`config/controllers.js`)
* and/or override them with custom routes (`config/routes.js`)
*
* NOTE: The code you write here supports both HTTP and Socket.io automatically.
*
* @docs :: http://sailsjs.org/#!documentation/controllers
*/
var async = require("async");
var moment = require("moment-timezone");
module.exports = {
/**
* Overrides for the settings in `config/controllers.js`
* (specific to SprintController)
*/
_config: {},
/**
* Sprint add action. This will render a GUI where user can add new sprint to specified project.
*
* @param {Request} request Request object
* @param {Response} response Response object
*/
add: function(request, response) {
var projectId = parseInt(request.param("projectId"), 10);
response.view({
projectId: projectId
});
},
/**
* Sprint edit action. This will render a GUI where user can edit specified sprint.
*
* @param {Request} request Request object
* @param {Response} response Response object
*/
edit: function(request, response) {
var sprintId = parseInt(request.param("id"), 10);
// Fetch sprint data for edit action
async.parallel(
{
// Fetch single sprint data
sprint: function(callback) {
DataService.getSprint(sprintId, callback);
},
// Determine user role in this sprint
role: function(callback) {
AuthService.hasSprintAccess(request.user, sprintId, callback, true);
}
},
/**
* Callback function which is called after all specified parallel jobs are done.
*
* @param {null|Error} error Error object
* @param {{
* sprint: sails.model.sprint,
* role: sails.helper.role
* }} data Object that contains sprint and role data
*/
function (error, data) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
response.view(data);
}
}
);
},
/**
* Sprint backlog action. This action will render a GUI where is listed all stories that are
* attached to specified sprint. Within this GUI user can change stories priority, add/edit
* those.
*
* @param {Request} request Request object
* @param {Response} response Response object
*/
backlog: function(request, response) {
var sprintId = parseInt(request.param("id"), 10);
// Initialize view data object
var data = {
stories: [],
role: 0,
sprint: {
data: {},
progressStory: 0,
progressTask: 0,
cntStoryDone: 0,
cntStoryNotDone: 0,
cntStoryTotal: 0,
cntTaskDone: 0,
cntTaskNotDone: 0,
cntTaskTotal: 0
}
};
// Fetch basic data for backlog action
async.parallel(
{
// Fetch sprint data.
sprint: function(callback) {
DataService.getSprint(sprintId, callback);
},
// Fetch sprint stories data.
stories: function(callback) {
DataService.getStories({sprintId: sprintId}, callback);
},
// Determine user role in this sprint
role: function(callback) {
AuthService.hasSprintAccess(request.user, sprintId, callback, true);
}
},
/**
* Callback function which is been called after all parallel jobs are processed.
*
* @param {null|Error} error Error object
* @param {{
* sprint: sails.model.sprint,
* stories: sails.model.story[],
* role: sails.helper.role
* }} result Result object that contains 'sprint', 'stories' and 'role' data
*/
function(error, result) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
// Store fetched data
data.sprint.data = result.sprint;
data.stories = result.stories;
data.role = result.role;
// Fetch phase data
fetchRelatedData();
}
}
);
/**
* Private function to fetch all related data for sprint backlog GUI. Note that data in here are
* depending on previously fetched data.
*
* After all jobs are done, function will render actual sprint backlog GUI for user.
*
* @return void
*/
function fetchRelatedData() {
async.waterfall(
[
// Fetch phase data
function(callback) {
DataService.getPhases({projectId: data.sprint.data.projectId}, function(error, phases) {
callback(error, phases);
});
},
// Fetch phase duration data
function(phases, callback) {
fetchPhaseDuration(phases, callback);
},
// Fetch task data
function(phases, callback) {
// Store phase data
data.phases = phases;
fetchTaskData(callback);
}
],
/**
* Main callback which is called after all waterfall jobs are done, or error occurred
* while processing those.
*
* @param {null|Error} error
* @param {sails.model.story[]} stories
*/
function(error, stories) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
data.stories = stories;
// Make necessary statistics calculations.
calculateStatisticsData();
response.view(data);
}
}
);
}
/**
* Private function to fetch sprint task phase duration times. This will sum tasks durations
* for each phase in this sprint.
*
* @param {sails.model.phase[]} phases Phase data
* @param {Function} next Callback function to call after job is done
*
* @return void
*/
function fetchPhaseDuration(phases, next) {
async.map(
phases,
/**
* Function to determine duration in specified phase.
*
* @param {sails.model.phase} phase
* @param {Function} callback
*/
function(phase, callback) {
var conditions = {
phaseId: phase.id,
sprintId: data.sprint.data.id
};
// Fetch sum of phase duration data
PhaseDuration
.find({sum: "duration"})
.where(conditions)
.done(function(error, result) {
if (error) {
callback(error, null);
} else {
phase.duration = result[0].duration ? result[0].duration : 0;
callback(null, phase);
}
});
},
/**
* Main callback function which is called after all phases are processed.
*
* @param {null|Error} error
* @param {sails.model.phase[]} phases
*/
function (error, phases) {
next(error, phases);
}
);
}
/**
* Private function to fetch task data for each story in specified sprint.
*
* @param {Function} next Callback function to call after job is done
*
* @return void
*/
function fetchTaskData(next) {
// Map each sprint story and fetch task data of those
async.map(
data.stories,
/**
* Iterator function which will fetch all tasks that are attached to current
* story and calculates some statistics data for story and sprint data.
*
* @param {sails.model.story} story Story object
* @param {Function} callback Callback function to call after job is finished
*/
function(story, callback) {
DataService.getTasks({storyId: story.id}, function(error, tasks) {
if (error) {
callback(error, null);
} else {
// Add tasks to story data
story.tasks = tasks;
story.doneTasks = _.reduce(tasks, function(memo, task) {
return (task.isDone) ? memo + 1 : memo;
}, 0);
if (story.doneTasks > 0) {
story.progress = Math.round(story.doneTasks / tasks.length * 100);
} else {
story.progress = 0;
}
// Add task counts to sprint
data.sprint.cntTaskTotal = data.sprint.cntTaskTotal + story.tasks.length;
data.sprint.cntTaskDone = data.sprint.cntTaskDone + story.doneTasks;
callback(null, story);
}
});
},
/**
* Callback function which is been called after all stories are processed.
*
* @param {null|Error} error
* @param {sails.model.story[]} stories
*/
function(error, stories) {
next(error, stories);
}
)
}
/**
* Private function to calculate some statistics data for sprint backlog GUI. This function
* is called right before actual GUI rendering. At this point we have all the necessary data
* fetched from database.
*
* @return void
*/
function calculateStatisticsData() {
var totalTime = 0;
var totalTimeNoFirst = 0;
data.sprint.cntStoryTotal = data.stories.length;
data.sprint.cntStoryDone = _.reduce(data.stories, function(memo, story) {
return (story.isDone) ? memo + 1 : memo;
}, 0);
data.sprint.cntStoryNotDone = data.sprint.cntStoryTotal - data.sprint.cntStoryDone;
if (data.sprint.cntStoryDone > 0) {
data.sprint.progressStory = Math.round(data.sprint.cntStoryDone / data.sprint.cntStoryTotal * 100);
} else {
data.sprint.progressStory = 0;
}
data.sprint.cntTaskNotDone = data.sprint.cntTaskTotal - data.sprint.cntTaskDone;
if (data.sprint.cntTaskDone > 0) {
data.sprint.progressTask = Math.round(data.sprint.cntTaskDone / data.sprint.cntTaskTotal * 100);
} else {
data.sprint.progressTask = 0;
}
if (data.phases.length > 0) {
totalTime = _.pluck(data.phases, "duration").reduce(function(memo, i) {
return memo + i;
});
var temp = _.pluck(_.reject(data.phases, function(phase) {
return phase.order === 0;
}), "duration");
if (temp.length > 0) {
totalTimeNoFirst = temp.reduce(function(memo, i) {
return memo + i;
});
}
}
data.phaseDuration = {
totalTime: totalTime,
totalTimeNoFirst: totalTimeNoFirst
};
_.each(data.phases, function(phase) {
phase.durationPercentage = (phase.duration > 0 && phase.order !== 0) ? phase.duration / totalTimeNoFirst * 100 : 0;
phase.durationPercentageTotal = (phase.duration > 0) ? phase.duration / totalTime * 100 : 0;
});
}
},
/**
* Sprint charts action. This will render a GUI that shows specified sprint charts.
*
* @param {Request} request Request object
* @param {Response} response Response object
*/
charts: function(request, response) {
var sprintId = parseInt(request.param("id"), 10);
// Get sprint and attached stories data
async.parallel(
{
// Fetch sprint data
sprint: function(callback) {
DataService.getSprint(sprintId, callback);
},
// Fetch sprint exclude days
excludeDays: function(callback) {
DataService.getSprintExcludeDays(sprintId, callback);
},
// Fetch stories that are attached to this sprint
stories: function(callback) {
DataService.getStories({sprintId: sprintId}, callback);
}
},
/**
* Callback function which is been called after all parallel jobs are processed.
*
* @param {Error} error Error object
* @param {{}} data Result data as an object that contains 'sprint' and 'stories'
*/
function(error, data) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
getTaskData(data);
}
}
);
/**
* Private function to fetch all task data that are attached specified sprint.
*
* @param {{
* sprint: sails.model.sprint,
* stories: sails.model.story[],
* excludeDays: sails.model.excludeSprintDay[]
* }} data
*/
function getTaskData(data) {
// Determine story id values for task search.
var storyIds = _.map(data.stories, function(story) {
return {storyId: story.id};
});
// We have some stories, so fetch tasks of those.
if (storyIds.length > 0) {
// Fetch stories tasks
DataService.getTasks({or: storyIds}, function(error, tasks) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
data.tasks = tasks;
response.view(data);
}
});
} else { // Otherwise we don't have any tasks so render GUI
data.tasks = [];
response.view(data);
}
}
},
/**
* Sprint chartDataTask action. This action handles the real data determination for sprint chart
* GUI. This action is quite heavy because of all calculations what are made in this action.
*
* I think that there are some optimizations that can be done to this action, but not yet :D
* Just leave this alone for a while, because it works like it should.
*
* @param {Request} request Request object
* @param {Response} response Response object
*/
chartDataTasks: function(request, response) {
var sprintId = parseInt(request.param("sprintId"), 10);
var data = {};
// Get sprint and attached stories data
async.parallel(
{
// Fetch sprint data
sprint: function(callback) {
DataService.getSprint(sprintId, callback);
},
// Fetch sprint exclude days
excludeDays: function(callback) {
DataService.getSprintExcludeDays(sprintId, callback);
},
// Fetch stories that are attached to this sprint
stories: function(callback) {
DataService.getStories({sprintId: sprintId}, callback);
},
// Fetch task types
types: function(callback) {
DataService.getTypes({}, callback);
}
},
/**
* Callback function which is been called after all parallel jobs are processed.
*
* @param {Error} error Error object
* @param {{}} results Result data as an object that contains 'sprint' and 'stories'
*/
function(error, results) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
// Store results to data
data.sprint = results.sprint;
data.excludeDays = results.excludeDays;
data.stories = results.stories;
data.types = results.types;
// Fetch task data
fetchRelatedData();
}
}
);
/**
* Private function to fetch all related data for sprint chart GUI. Note that this function
* is quite "heavy" to run. First we will need to fetch some related data that we need for
* calculations of statistic data.
*
* @return void
*/
function fetchRelatedData() {
async.waterfall(
[
// Fetch phase and task data
function(callback) {
getPhaseAndTaskData(callback);
},
/**
* Job to store task data for GUI and determine task durations in each phase.
*
* @param {{
* tasks: sails.model.task[],
* phases: sails.model.phase[]
* }} results
* @param {Function} callback
*/
function(results, callback) {
data.tasks = _.sortBy(results.tasks, function(task) {
return task.timeEnd;
});
data.tasksDone = _.filter(data.tasks, function(task) {
return task.isDone;
});
// Determine task durations in each phase
fetchTaskDuration(results.phases, callback);
}
],
/**
* Main callback function which is called after all waterfall jobs are done. This will store
* determined phase data and then calculate all necessary statistics data for GUI charts.
*
* Necessary statistics calculations are done in separate private function that will process
* all fetched data and create actual chart data.
*
* After that data is processed it's sended back to client as in JSON object.
*
* @param {null|Error} error
* @param {sails.model.phase[]} phases
*/
function(error, phases) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
data.phases = phases;
parseChartData();
response.json(data);
}
}
);
}
/**
* Private function to fetch task data. This is called after we have collect basic data for
* the sprint chart GUI.
*
* @param {Function} next Callback function that will be called after job is done.
*
* @return void
*/
function getPhaseAndTaskData(next) {
async.parallel(
{
// Fetch project phases data
phases: function(callback) {
DataService.getPhases({projectId: data.sprint.projectId}, callback);
},
// Fetch tasks that are attached to this sprint
tasks: function(callback) {
// Determine story id values for task search.
var storyIds = _.compact(_.map(data.stories, function(story) {
return story.ignoreInBurnDownChart ? false : { storyId: story.id };
}));
// Yeah, we got some stories, so fetch tasks of those.
if (storyIds.length > 0) {
DataService.getTasks({or: storyIds}, callback);
} else { // Otherwise just continue
callback(null, []);
}
}
},
/**
* Main callback function which is called after all parallel jobs are done.
*
* @param {null|Error} error
* @param {{
* phases: sails.model.phase[],
* tasks: sails.model.task[]
* }} results
*/
function(error, results) {
next(error, results);
}
);
}
/**
* Private function to determine tasks durations in each phase. This will map all provided
* phase objects and adds duration data to each of those.
*
* After all phases are mapped with duration data function will call provided callback
* function with phases data.
*
* @param {sails.model.phase[]} phases Phase data
* @param {Function} next Callback to call after job is done
*/
function fetchTaskDuration(phases, next) {
async.map(
phases,
/**
* Function to determine duration in current phase.
*
* @param {sails.model.phase} phase
* @param {Function} callback
*/
function(phase, callback) {
PhaseDuration
.find({sum: "duration"})
.where({phaseId: phase.id, sprintId: data.sprint.id})
.done(function(error, result) {
if (error) {
callback(error, null);
} else {
phase.duration = result[0].duration ? result[0].duration : 0;
callback(null, phase);
}
});
},
/**
* Main callback function which is called after all phases are processed.
*
* @param {null|Error} error
* @param {sails.model.phase[]} phases
*/
function (error, phases) {
next(error, phases);
}
);
}
/**
* Private function to parse actual data for chart.
*/
function parseChartData() {
var initTasks = 0;
var storyTasks = 0;
var totalTimeNoFirst = 0;
var totalTime = 0;
var tasksOver = [];
var taskCount = _.size(data.tasks);
var taskTypes = _.groupBy(data.tasks, function(task) {
return task.typeId;
});
if (data.phases.length > 0) {
totalTime= _.pluck(data.phases, "duration").reduce(function(memo, i) {
return memo + i;
});
var temp = _.pluck(_.reject(data.phases, function(phase) {
return phase.order === 0;
}), "duration");
if (temp.length > 0) {
totalTimeNoFirst = temp.reduce(function(memo, i) {
return memo + i;
});
}
}
data.phaseDuration = {
totalTime: totalTime,
totalTimeNoFirst: totalTimeNoFirst
};
_.each(data.phases, function(phase) {
phase.durationPercentage = (phase.duration > 0 && phase.order !== 0) ? phase.duration / totalTimeNoFirst * 100 : 0;
phase.durationPercentageTotal = (phase.duration > 0) ? phase.duration / totalTime * 100 : 0;
});
data.chartDataPhases = [];
data.chartDataPhasesTotal = [];
_.each(data.phases, function(phase) {
if (phase.durationPercentage > 0) {
data.chartDataPhases.push({
name: phase.title,
color: phase.backgroundColor,
y: phase.durationPercentage,
duration: phase.duration
});
}
if (phase.durationPercentageTotal > 0) {
data.chartDataPhasesTotal.push({
name: phase.title,
color: phase.backgroundColor,
y: phase.durationPercentageTotal,
duration: phase.duration
});
}
});
data.chartDataTaskTypes = [];
_.each(taskTypes, function(tasks, typeId) {
var type = _.find(data.types, function(type) {
return type.id == typeId;
});
var count = _.size(tasks);
if (count > 0) {
data.chartDataTaskTypes.push({
name: type.title,
color: type.chartColor,
y: count / taskCount * 100,
count: count
});
}
});
// Iterate each stories and determine initial and "over" tasks
_.each(data.stories, function(story) {
storyTasks = 0;
// Story has not yet started, so all of its tasks are in "init" data
if (!(story.timeStart && story.timeStart != '0000-00-00 00:00:00')) {
storyTasks = _.size(_.filter(data.tasks, function(task) {
return task.storyId === story.id;
}));
} else { // Otherwise story is either in process or done
// calculate task count that are created before story start time
storyTasks = _.size(_.filter(data.tasks, function(task) {
return (task.storyId === story.id && task.createdAt <= story.timeStart);
}));
// Determine tasks that are added after story start
tasksOver = tasksOver.concat(_.filter(data.tasks, function(task) {
return (task.storyId === story.id && task.createdAt > story.timeStart);
}));
}
// Add current story init task count to main task count
initTasks = initTasks + storyTasks;
});
data.tasksOver = _.sortBy(tasksOver, function(task) {
return task.createdAt;
});
data.initTasks = initTasks;
data.chartData = [];
// Initialize statistics data
data.statistics = {
sprintDays: 0,
workDays: 0,
tasksPerDayIdeal: 0,
tasksPerDayActual: 0
};
var dataActual = getActualData();
data.pointStart = data.sprint.dateStartObject();
data.pointActual = moment(_.last(dataActual).x);
// Add 'Ideal task remaining' data
data.chartData.push({
name: "Ideal",
nameShort: "Ideal",
type: "spline",
color: "#3276b1",
dashStyle: "Dash",
marker: {
enabled: false,
radius: 3
},
lineWidth: 1,
zIndex: 10,
data: getIdealData()
});
// Add 'Actual task remaining' data
data.chartData.push({
name: "Actual",
nameShort: "Actual",
type: "spline",
color: "#c9302c",
shadow: true,
marker: {
enabled: false,
radius: 3
},
zIndex: 20,
data: dataActual
});
// Add 'Done tasks' data
data.chartData.push({
name: "Done",
nameShort: "Done",
type: "column",
color: "#47a447",
zIndex: 5,
data: getDoneData()
});
// Add 'Added tasks' data
data.chartData.push({
name: "Added",
nameShort: "Added",
type: "column",
color: "#ec971f",
zIndex: 0,
data: getAddedData()
});
}
/**
* Private function to determine ideal data for charts. This is basically linear line where
* start point is at sprint start and value for that point is sprint initial task count. End
* point is at sprint end and value is zero.
*
* Output is an array of UTC date times and actual values.
*
* @returns {Array}
*/
function getIdealData() {
var output = [];
var tasks = data.initTasks;
var startTime = data.sprint.dateStartObject();
var endTime = data.sprint.dateEndObject();
var days = data.sprint.durationDays();
// Used dates within calculations
var currentDate = moment(startTime);
var previousDate = Date.UTC(startTime.year(), startTime.month(), startTime.date());
// Iterate sprint date range and remove exclude days
while (endTime.diff(currentDate, "days") > 0) {
// Exclude day founded, so decrease total day count
if (_.find(data.excludeDays, function(day) { return currentDate.isSame(day.dayObject(), "days"); })) {
days = days - 1;
}
currentDate.add("days", 1);
}
// Calculate ideal tasks per day count
var tasksPerDay = data.initTasks / (days - 1);
// Store statistics data
data.statistics.sprintDays = days;
data.statistics.tasksPerDayIdeal = tasksPerDay;
// Set current date to sprint start
currentDate = moment(startTime);
// Iterate sprint days and make ideal line data
while (endTime.diff(currentDate, "days") > 0) {
var ignore = isDateIgnored(currentDate);
// Current date is valid for ideal line data
if (!ignore) {
// Add data to output
output.push({
x: Date.UTC(currentDate.year(), currentDate.month(), currentDate.date()),
y: tasks,
previousX: previousDate ? previousDate : null,
notPlannedDay: false
});
// Store new previous date and task count for next round
previousDate = Date.UTC(currentDate.year(), currentDate.month(), currentDate.date());
tasks = tasks - tasksPerDay;
}
currentDate.add("days", 1);
}
// Add final point for ideal line
output.push({
x: Date.UTC(currentDate.year(), currentDate.month(), currentDate.date()),
y: 0,
previousX: previousDate ? previousDate : null,
notPlannedDay: false
});
return output;
}
/**
* Private function to get actual task data.
*
* @returns {Array}
*/
function getActualData() {
var output = [];
var tasks = data.initTasks;
var startTime = data.sprint.dateStartObject();
var endTime = data.sprint.dateEndObject();
var tasksDone = 0;
var tasksOver = 0;
var workDays = 0;
// Used dates within calculations
var currentDate = moment(startTime);
var previousDate = Date.UTC(startTime.year(), startTime.month(), startTime.date());
// Add first point of data
output.push({
x: Date.UTC(currentDate.year(), currentDate.month(), currentDate.date()),
y: tasks,
previousX: previousDate,
notPlannedDay: isDateIgnored(currentDate)
});
// Group tasks for calculations
var objectsTasksDone = _.groupBy(data.tasks, function(task) {
return task.isDone ? task.timeEndObject().format("YYYY-MM-DD") : null;
});
var objectsTasksOver = _.groupBy(data.tasksOver, function(task) {
return task.createdAtObject().format("YYYY-MM-DD");
});
// Iterate dates
while (endTime.diff(currentDate, "days") >= 0) {
// We are only interested days that are before current day
if (currentDate.isBefore(moment().add("days", 1), "day")) {
// Determine if current date is ignored or not
var ignore = isDateIgnored(currentDate);
var date = currentDate.format("YYYY-MM-DD");
var notPlannedDay = false;
// Determine tasks count
tasksDone = objectsTasksDone[date] ? _.size(objectsTasksDone[date]) : 0;
tasksOver = objectsTasksOver[date] ? _.size(objectsTasksOver[date]) : 0;
// If current date has some tasks done or added, we must drawn this date
if (tasksDone > 0 || tasksOver > 0) {
notPlannedDay = ignore;
ignore = false;
}
// Date is valid to show
if (!ignore) {
// Calculate new task count based to previous tasks, done and added tasks count
tasks = tasks - tasksDone + tasksOver;
// Add new point data to output
output.push({
x: Date.UTC(currentDate.year(), currentDate.month(), currentDate.date()),
y: tasks,
previousX: previousDate ? previousDate : null,
notPlannedDay: notPlannedDay
});
// Add workdays
workDays = workDays + 1;
// Store new previous date
previousDate = Date.UTC(currentDate.year(), currentDate.month(), currentDate.date());