/
resultsets_store.js
1297 lines (1130 loc) · 53.1 KB
/
resultsets_store.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
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
'use strict';
treeherder.factory('ThResultSetStore', [
'$rootScope', '$q', '$location', '$interval', 'thPlatformMap',
'ThResultSetModel', 'ThJobModel', 'ThJobDetailModel', 'thEvents',
'thResultStatusObject', 'thAggregateIds', 'ThLog', 'thNotify',
'thJobFilters', 'thOptionOrder', 'ThRepositoryModel', '$timeout',
'ThRunnableJobModel',
function (
$rootScope, $q, $location, $interval, thPlatformMap, ThResultSetModel,
ThJobModel, ThJobDetailModel, thEvents, thResultStatusObject, thAggregateIds,
ThLog, thNotify, thJobFilters, thOptionOrder, ThRepositoryModel,
$timeout, ThRunnableJobModel) {
var $log = new ThLog("ThResultSetStore");
// indexOf doesn't work on objects so we need to map thPlatformMap to an array
var platformArray = _.map(thPlatformMap, function (val, idx) { return idx; });
/******
* Handle updating the resultset datamodel based on a queue of jobs
* and resultsets.
*
* manages:
* resultset array
* resultset queue
* resultset map
* job queue
* job map
*/
var defaultResultSetCount = 10;
// the primary data model
var repositories = {};
var resultSetPollInterval = 60000;
var jobPollInterval = 60000;
var maxPollInterval = 60000 * 15;
var lastPolltime;
var lastJobUpdate;
// Keys that, if present on the url, must be passed into the resultset
// polling endpoint
var rsPollingKeys = ['tochange', 'enddate', 'revision', 'author'];
// changes to the url for any of these fields should reload the page
// because it changes the query to the db
var reloadOnChangeParameters = [
'repo',
'revision',
'author',
'fromchange',
'tochange',
'startdate',
'enddate',
'exclusion_profile',
'nojobs'
];
var registerResultSetPollers = function () {
// these params will be passed in each time we poll to remain
// within the constraints of the URL params
var rsPollingParams = _.pick($location.search(), rsPollingKeys);
// Register resultset poller if it's not registered
var resultSetPoller = $interval(function () {
// resultset data for the current repository
var rsData = repositories[$rootScope.repoName];
// This case is if we already have at least 1 resultset and we're
// polling for more.
// If we already have one, and the revision param is passed in,
// don't poll for more, because "there can be only one."
if ((rsData.resultSets.length > 0) &&
(!rsData.loadingStatus.prepending)) {
if (doResultSetPolling(rsPollingParams)) {
// Get all resultsets from the oldest we have in memory
// to the newest possible. This is so that, if a
// resultset has been created on the server out of
// order with regards to its push_timestamp, we will
// still pick it up.
var fromChangeRev = rsData.resultSets[rsData.resultSets.length - 1].revision;
ThResultSetModel.getResultSetsFromChange(
$rootScope.repoName,
fromChangeRev,
rsPollingParams
).then(function (data) {
prependResultSets($rootScope.repoName, data.data);
});
} else {
// cancel the interval for the polling, because
// the parameters mean we can get no more result sets.
$interval.cancel(resultSetPoller);
}
} else if ((rsData.resultSets.length === 0) &&
(rsData.loadingStatus.prepending === false)) {
fetchResultSets($rootScope.repoName, defaultResultSetCount);
}
}, resultSetPollInterval);
};
/**
* Some URL conditions will prevent polling after the initial set of
* result sets is loaded.
*
* Formerly, we wouldn't poll for resultsets if the most recent loaded
* resultset matched the url param of ``tochange``.
* But it is now possible (however unlikely) to get resultsets out of
* order (due to resultset auto-creation or created via the resultset
* creation API).
* So we don't have that limitation any longer. You may have the latest
* resultset, but an older one comes in that is between (wrt
* push_timestamp) two resultsets you have loaded. We want to fill
* in that gap.
*/
var doResultSetPolling = function (rsParams) {
return (!_.has(rsParams, 'revision'));
};
var pollJobs = function () {
var resultSetIdList = repositories[$rootScope.repoName].resultSets
.map(x => x.id);
var jobUpdatesPromise;
if (!lastJobUpdate || (Date.now() - lastPolltime) > maxPollInterval) {
// it is possible that some pushes might not have any jobs initially
// also, if it's been too long, just refetch everything since
// getting updates can be extremely slow (and taxing on the
// server) if there are a lot of them
jobUpdatesPromise = $q.all(ThResultSetModel.getResultSetJobs(
resultSetIdList,
$rootScope.repoName,
_.pick($location.search(), ["exclusion_profile", "visibility"])
));
} else {
jobUpdatesPromise = ThResultSetModel.getResultSetJobsUpdates(
resultSetIdList,
$rootScope.repoName,
lastJobUpdate,
_.pick($location.search(), ["exclusion_profile", "visibility"]));
}
lastPolltime = Date.now();
jobUpdatesPromise
.then(function (jobList) {
jobList = _.flatten(jobList);
if (jobList.length > 0) {
lastJobUpdate = getLastModifiedJobTime(jobList);
var jobListByResultSet = _.values(
_.groupBy(jobList, 'result_set_id')
);
jobListByResultSet
.forEach((singleResultSetJobList) =>
mapResultSetJobs($rootScope.repoName,
singleResultSetJobList));
} else if (lastJobUpdate) {
// try to update the last poll interval to the greater of the
// last job update or the current time minus a small multiple of the
// job poll interval
// (this depends on the client having a reasonably accurate internal
// clock, but it should hopefully prevent us from getting too
// far behind in cases where we've stopped receiving job updates
// due e.g. to looking at a completed push)
lastJobUpdate = _.max([
new Date(Date.now() - (5 * jobPollInterval)),
lastJobUpdate
]);
}
schedulePoll();
});
};
var registerJobsPoller = function () {
if (!lastPolltime) {
lastPolltime = Date.now();
}
schedulePoll();
};
var schedulePoll = function () {
if (window.requestIdleCallback) {
$timeout(() => requestIdleCallback(pollJobs), jobPollInterval);
} else {
$timeout(pollJobs, jobPollInterval);
}
};
var mapResultSetJobs = function (repoName, jobList) {
if (jobList.length > 0) {
// jobList contains jobs belonging to the same resultset,
// so we can pick the result_set_id from the first job
var resultSetId = jobList[0].result_set_id;
var resultSet = _.find(repositories[repoName].resultSets,
{id: resultSetId});
if (_.isUndefined(resultSet)) { return $q.defer().resolve(); }
if (_.has(resultSet, 'jobList')) {
// get the new job ids
var jobIds = _.map(jobList, 'id');
// remove the elements that need to be updated
resultSet.jobList = _.filter(resultSet.jobList, function (job) {
return _.indexOf(jobIds, job.id) === -1;
});
resultSet.jobList = resultSet.jobList.concat(jobList);
} else {
resultSet.jobList = jobList;
}
var sortAndGroupJobs = _.flowRight(
sortGroupedJobs,
groupJobByPlatform
);
_.extend(resultSet, sortAndGroupJobs(resultSet.jobList));
mapPlatforms(
repoName,
resultSet
);
updateUnclassifiedFailureCountForTiers(repoName);
updateFilteredUnclassifiedFailureCount(repoName);
$rootScope.$emit(thEvents.applyNewJobs, resultSetId);
}
};
$rootScope.$on(thEvents.recalculateUnclassified, function () {
$timeout(function (repoName) {
updateUnclassifiedFailureCountForTiers(repoName);
updateFilteredUnclassifiedFailureCount(repoName);
}, 0, true, $rootScope.repoName);
});
$rootScope.$on(thEvents.jobsClassified, function () {
$timeout(function (repoName) {
updateUnclassifiedFailureCountForTiers(repoName);
updateFilteredUnclassifiedFailureCount(repoName);
}, 0, true, $rootScope.repoName);
});
$rootScope.$on(thEvents.globalFilterChanged, function () {
$timeout(updateFilteredUnclassifiedFailureCount, 0, true, $rootScope.repoName);
});
var addRepository = function (repoName) {
//Initialize a new repository in the repositories structure
// only base the locationSearch on params that are NOT filters,
// because filters don't effect the server side fetching of
// jobs.
var locationSearch = thJobFilters.stripFiltersFromQueryString(
_.clone($location.search())
);
$log.debug("locationSearch", locationSearch);
if (_.isEmpty(repositories[repoName]) ||
!_.isEqual(locationSearch, repositories[repoName].search)) {
$log.debug(
"fetching new resultset list with parameters:",
locationSearch
);
repositories[repoName] = {
name:repoName,
// This keeps track of the selected job. The selected job,
// when rendered, will be given a class of ``selected-job``,
// but the directive may lose track of that class when we are
// updating with new jobs. In the event the row with the
// selected job is being re-rendered, knowing which one is
// selected here in the model will allow us to apply the
// correct styling to it.
lastJobObjSelected:{},
// maps to help finding objects to update/add
rsMap:{},
jobMap:{},
grpMap:{},
unclassifiedFailureMap: {},
// count of unclassified for the currently enabled tiers
unclassifiedFailureCountForTiers: 0,
// count of unclassified jobs within enabled tiers and filtered out
filteredUnclassifiedFailureCount: 0,
//used as the offset in paging
rsMapOldestTimestamp:null,
resultSets:[],
// this is "watchable" by the controller now to update its scope.
loadingStatus: {
appending: false,
prepending: false
},
search: locationSearch
};
}
};
var getAllShownJobs = function (repoName, spaceRemaining, maxSize, resultsetId) {
var shownJobs = [];
var addIfShown = function (jMap) {
if (resultsetId && jMap.job_obj.result_set_id !== resultsetId) {
return;
}
if (jMap.job_obj.visible) {
shownJobs.push(jMap.job_obj);
}
if (_.size(shownJobs) === spaceRemaining) {
thNotify.send("Max pinboard size of " + maxSize + " reached.",
"danger");
return true;
}
return false;
};
_.find(getJobMap(repoName), addIfShown);
return shownJobs;
};
var getSelectedJob = function (repoName) {
return {
job:repositories[repoName].lastJobObjSelected
};
};
var setSelectedJob = function (repoName, lastJobObjSelected) {
repositories[repoName].lastJobObjSelected = lastJobObjSelected;
};
var getPlatformKey = function (name, option) {
var key = name;
if (option !== undefined) {
key += option;
}
return key;
};
var addRunnableJobs = function (repoName, resultSet) {
getGeckoDecisionTaskId(repoName, resultSet.id).then(function (decisionTaskId) {
if (repoName !== "try") {
decisionTaskId = "";
}
return ThRunnableJobModel.get_list(repoName, {"decision_task_id": decisionTaskId}).then(function (jobList) {
var id = resultSet.id;
_.each(jobList, function (job) {
job.result_set_id = id;
job.id = thAggregateIds.escape(job.result_set_id + job.ref_data_name);
});
if (jobList.length === 0) {
resultSet.isRunnableVisible = false;
thNotify.send("No new jobs available");
}
mapResultSetJobs(repoName, jobList);
}, function () {
thNotify.send("Error fetching runnable jobs", "danger");
});
}, function (reason) {
thNotify.send(`Error fetching runnable jobs: Failed to fetch task ID (${reason})`, "danger");
});
};
var deleteRunnableJobs = function (repoName, resultSet) {
repositories[repoName].rsMap[resultSet.id].selected_runnable_jobs = [];
resultSet.isRunnableVisible = false;
$rootScope.$emit(thEvents.globalFilterChanged);
};
/******
* Build the Job and Resultset object mappings to make it faster and
* easier to find and update jobs and resultsets
*
* @param data The array of resultsets to map.
*/
var mapResultSets = function (repoName, data) {
for (var rs_i = 0; rs_i < data.length; rs_i++) {
var rs_obj = data[rs_i];
// make a watch-able revisions array
rs_obj.revisions = rs_obj.revisions || [];
var rsMapElement = {
rs_obj: rs_obj,
platforms: {}
};
repositories[repoName].rsMap[rs_obj.id] = rsMapElement;
// platforms
if (rs_obj.platforms !== undefined) {
mapPlatforms(repoName, rs_obj);
}
}
$log.debug("sorting", repoName, repositories[repoName]);
repositories[repoName].resultSets.sort(rsCompare);
repositories[repoName].rsMapOldestTimestamp = _.last(repositories[repoName].resultSets).push_timestamp;
$log.debug("oldest result set: ", repositories[repoName].rsMapOldestTimestamp);
$log.debug("done mapping:", repositories[repoName].rsMap);
};
var mapPlatforms = function (repoName, rs_obj) {
for (var pl_i = 0; pl_i < rs_obj.platforms.length; pl_i++) {
var pl_obj = rs_obj.platforms[pl_i];
var plMapElement = {
pl_obj: pl_obj,
parent: repositories[repoName].rsMap[rs_obj.id],
groups: {}
};
var platformKey = getPlatformKey(pl_obj.name, pl_obj.option);
repositories[repoName].rsMap[rs_obj.id].platforms[platformKey] = plMapElement;
// groups
for (var gp_i = 0; gp_i < pl_obj.groups.length; gp_i++) {
var gr_obj = pl_obj.groups[gp_i];
gr_obj.mapKey = thAggregateIds.getGroupMapKey(rs_obj.id, gr_obj.symbol, gr_obj.tier, pl_obj.name, pl_obj.option);
var grMapElement = {
grp_obj: gr_obj,
parent: plMapElement,
jobs: {}
};
plMapElement.groups[gr_obj.symbol] = grMapElement;
// check if we need to copy groupState from an existing group
// object. This would be set if a user explicitly clicked
// a group to toggle it expanded/collapsed.
// This value will have been overwritten by the _.extend
// in mapResultSetJobs.
var oldGroup = repositories[repoName].grpMap[gr_obj.mapKey];
if (oldGroup) {
gr_obj.groupState = oldGroup.grp_obj.groupState;
}
repositories[repoName].grpMap[gr_obj.mapKey] = grMapElement;
// jobs
for (var j_i = 0; j_i < gr_obj.jobs.length; j_i++) {
var job_obj = gr_obj.jobs[j_i];
var key = thAggregateIds.getJobMapKey(job_obj);
var jobMapElement = {
job_obj: job_obj,
parent: grMapElement
};
grMapElement.jobs[key] = jobMapElement;
repositories[repoName].jobMap[key] = jobMapElement;
updateUnclassifiedFailureMap(repoName, job_obj);
}
}
}
};
var updateUnclassifiedFailureMap = function (repoName, job) {
if (thJobFilters.isJobUnclassifiedFailure(job)) {
// store a job here instead of just ``true`` so that when we
// go back and evaluate each one to see if it matches a tier,
// we can. This also allows us to check other values of the job
// to see if it matches the current filters.
repositories[repoName].unclassifiedFailureMap[job.job_guid] = job;
} else {
delete repositories[repoName].unclassifiedFailureMap[job.job_guid];
}
};
/**
* Go through map of loaded unclassified jobs and check against current
* enabled tiers to get this count.
*/
var updateUnclassifiedFailureCountForTiers = function (repoName) {
if (_.has(repositories, repoName)) {
repositories[repoName].unclassifiedFailureCountForTiers = 0;
_.forEach(repositories[repoName].unclassifiedFailureMap, function (job) {
if (thJobFilters.isFilterSetToShow("tier", job.tier)) {
repositories[repoName].unclassifiedFailureCountForTiers += 1;
}
});
}
};
/**
* Loops through the map of unlcassified failures and checks if it is
* within the enabled tiers and if the job should be shown. This essentially
* gives us the difference in unclassified failures and, of those jobs, the
* ones that have been filtered out
*
* @param {String} repoName - the name of the repository from which to match the unclassified jobs
* @private
*/
var updateFilteredUnclassifiedFailureCount = function (repoName) {
if (!_.has(repositories, repoName)) {
return;
}
var repo = repositories[repoName];
repo.filteredUnclassifiedFailureCount = 0;
_.forEach(repo.unclassifiedFailureMap, function (job) {
if (thJobFilters.showJob(job)) {
repo.filteredUnclassifiedFailureCount++;
}
});
};
var getAllUnclassifiedFailureCount = function (repoName) {
if (repositories[repoName]) {
return repositories[repoName].unclassifiedFailureCountForTiers;
}
return 0;
};
var getFilteredUnclassifiedFailureCount = function (repoName) {
if (repositories.hasOwnProperty(repoName)) {
return repositories[repoName].filteredUnclassifiedFailureCount;
}
return 0;
};
/**
* Sort the resultsets in place after updating the array
*/
var rsCompare = function (rs_a, rs_b) {
if (rs_a.push_timestamp > rs_b.push_timestamp) {
return -1;
}
if (rs_a.push_timestamp < rs_b.push_timestamp) {
return 1;
}
return 0;
};
/******
* Ensure that the platform for ``newJob`` exists. Create it if
* necessary. Add to the datamodel AND the map
* @param newJob
* @returns plMapElement
*/
var getOrCreatePlatform = function (repoName, newJob) {
var rsMapElement = repositories[repoName].rsMap[newJob.result_set_id];
var platformKey = getPlatformKey(newJob.platform, newJob.platform_option);
var plMapElement = rsMapElement.platforms[platformKey];
if (!plMapElement) {
// this platform wasn't in the resultset, so add it.
$log.debug("adding new platform");
var pl_obj = {
name: newJob.platform,
option: newJob.platform_option,
groups: []
};
// add the new platform to the datamodel and resort
if (rsMapElement.rs_obj.hasOwnProperty('platforms')) {
rsMapElement.rs_obj.platforms.push(pl_obj);
// add the new platform to the resultset map
rsMapElement.platforms[platformKey] = {
pl_obj: pl_obj,
parent: rsMapElement,
groups: {}
};
plMapElement = rsMapElement.platforms[platformKey];
}
}
return plMapElement;
};
/******
* Ensure that the group and platform for ``newJob`` exist.
* Create it if necessary. Add to the datamodel AND the map
* @param newJob
* @returns grpMapElement
*/
var getOrCreateGroup = function (repoName, newJob) {
var plMapElement = getOrCreatePlatform(repoName, newJob);
var grMapElement;
if (plMapElement) {
var groupInfo = getJobGroupInfo(newJob);
grMapElement = plMapElement.groups[groupInfo.symbol];
if (!grMapElement) {
$log.debug("adding new group");
var grp_obj = {
symbol: groupInfo.symbol,
name: groupInfo.name,
mapKey: groupInfo.mapKey,
jobs: []
};
// add the new group to the datamodel
plMapElement.pl_obj.groups.push(grp_obj);
// add the new group to the platform map
plMapElement.groups[grp_obj.symbol] = {
grp_obj: grp_obj,
parent: plMapElement,
jobs: {}
};
grMapElement = plMapElement.groups[groupInfo.symbol];
}
}
return grMapElement;
};
/**
* Fetch the job objects for the ids in ``jobFetchList`` and update them
* in the data model.
* @param {string} repoName - name of the project repository
* @param {number[]} jobFetchList - project-specific ids of the jobs to fetch
*/
var fetchJobs = function (repoName, jobFetchList) {
$log.debug("fetchJobs", repoName, jobFetchList);
// we could potentially have very large lists of jobs. So we need
// to chunk this fetching.
var count = 40;
var error_callback = function (data) {
$log.error("Error fetching jobs: " + data);
};
var unavailableJobs = [];
while (jobFetchList.length > 0) {
var jobFetchSlice = jobFetchList.splice(0, count);
ThJobModel.get_list(repoName, {
id__in: jobFetchSlice.join(),
count: count
})
.then(function (jobsFetched) {
// if there are jobs unfetched, enqueue them for the next run
var ids_unfetched = jobFetchList.splice(count);
if (ids_unfetched.length > 0) {
$log.debug("re-adding " +
ids_unfetched.length + "job to the fetch queue");
unavailableJobs.push.apply(unavailableJobs, ids_unfetched);
}
return jobsFetched;
}, error_callback)
.then(_.bind(updateJobs, $rootScope, repoName));
}
// retry to fetch the unfetched jobs later
_.delay(fetchJobs, 10000, repoName, unavailableJobs);
};
var aggregateJobPlatform = function (repoName, job, platformData) {
var resultsetId, platformName, platformOption, platformAggregateId,
platformKey, jobUpdated, resultsetAggregateId, revision,
jobGroups;
jobUpdated = updateJob(repoName, job);
//the job was not updated or added to the model, don't include it
//in the jobsLoaded broadcast
if (jobUpdated === false) {
return;
}
resultsetId = job.result_set_id;
platformName = job.platform;
platformOption = job.platform_option;
if (_.isEmpty(repositories[repoName].rsMap[ resultsetId ])) {
//We don't have this resultset
return;
}
platformAggregateId = thAggregateIds.getPlatformRowId(
repoName,
job.result_set_id,
job.platform,
job.platform_option
);
if (!platformData[platformAggregateId]) {
if (!_.isEmpty(repositories[repoName].rsMap[resultsetId])) {
revision = repositories[repoName].rsMap[resultsetId].rs_obj.revision;
resultsetAggregateId = thAggregateIds.getResultsetTableId(
$rootScope.repoName, resultsetId, revision
);
platformKey = getPlatformKey(platformName, platformOption);
$log.debug("aggregateJobPlatform", repoName, resultsetId, platformKey, repositories);
jobGroups = [];
if (repositories[repoName].rsMap[resultsetId].platforms[platformKey] !== undefined) {
jobGroups = repositories[repoName].rsMap[resultsetId].platforms[platformKey].pl_obj.groups;
}
platformData[platformAggregateId] = {
platformName:platformName,
revision:revision,
platformOrder:repositories[repoName].rsMap[resultsetId].rs_obj.platforms,
resultsetId:resultsetId,
resultsetAggregateId:resultsetAggregateId,
platformOption:platformOption,
jobGroups:jobGroups,
jobs:[]
};
}
}
platformData[platformAggregateId].jobs.push(job);
};
/***
* update resultsets and jobs with those that were in the update queue
* @param jobList List of jobs to be placed in the data model and maps
*/
var updateJobs = function (repoName, jobList) {
$log.debug("number of jobs returned for add/update: ", jobList.length);
var platformData = {};
_.forEach(jobList, function (job) {
aggregateJobPlatform(repoName, job, platformData);
});
if (!_.isEmpty(platformData) && repoName === $rootScope.repoName) {
$timeout($rootScope.$emit(thEvents.jobsLoaded, platformData));
}
};
/******
*
* Add or update a new job. Either we have it loaded already and the
* status and info need to be updated. Or we have the resultset, and
* the job needs to be added to that resultset.
*
* Check the map, and update. or add by finding the right place.
*
* Shape of the rsMap:
* -------------------
* rsMap = {
<rs_id1>: {
rs_obj: rs_obj,
platforms: {
<pl_name1 + pl_option>: {
pl_obj: pl_obj,
groups: {
<grp_name1>: {
grp_obj: grp_obj
},
<grp_name2>: {...}
}
},
<pl_name2>: {...}
},
<rs_id2>: {...}
}
}
*
*
* @param newJob The new job object that was just fetched which needs
* to be added or updated.
*/
var updateJob = function (repoName, newJob) {
var key = thAggregateIds.getJobMapKey(newJob);
var loadedJobMap = repositories[repoName].jobMap[key];
var loadedJob = loadedJobMap? loadedJobMap.job_obj: null;
var rsMapElement = repositories[repoName].rsMap[newJob.result_set_id];
//We don't have this resultset id yet
if (_.isEmpty(rsMapElement)) {
return false;
}
if (loadedJob) {
$log.debug("updating existing job", loadedJob, newJob);
_.extend(loadedJob, newJob);
} else {
// this job is not yet in the model or the map. add it to both
$log.debug("adding new job", newJob);
var grpMapElement = getOrCreateGroup(repoName, newJob);
if (grpMapElement) {
// add the job mapping to the group
grpMapElement.jobs[key] = {
job_obj: newJob,
parent: grpMapElement
};
// add the job to the datamodel
grpMapElement.grp_obj.jobs.push(newJob);
// add job to the jobmap
var jobMapElement = {
job_obj: newJob,
parent: grpMapElement
};
repositories[repoName].jobMap[key] = jobMapElement;
}
}
updateUnclassifiedFailureMap(repoName, newJob);
return true;
};
var prependResultSets = function (repoName, data) {
// prepend the resultsets because they'll be newer.
var added = [];
for (var i = data.results.length - 1; i > -1; i--) {
if (data.results[i].push_timestamp >= repositories[repoName].rsMapOldestTimestamp &&
isInResultSetRange(repoName, data.results[i].push_timestamp) &&
repositories[repoName].rsMap[data.results[i].id] === undefined) {
$log.debug("prepending resultset: ", data.results[i].id);
repositories[repoName].resultSets.push(data.results[i]);
added.push(data.results[i]);
} else {
$log.debug("not prepending. timestamp is older");
}
}
mapResultSets(repoName, added);
repositories[repoName].loadingStatus.prepending = false;
};
var appendResultSets = function (repoName, data) {
if (data.results.length > 0) {
$log.debug("appendResultSets", data.results);
var rsIds = repositories[repoName].resultSets.map((rs) => rs.id);
// ensure we only append resultsets we don't already have.
// There could be overlap with fetching "next 10" because we use
// the latest ``push_timestamp`` and theoretically we could
// get
var newResultsets = [];
_.each(data.results, function (rs) {
if (!_.includes(rsIds, rs.id)) {
newResultsets.push(rs);
}
});
Array.prototype.push.apply(
repositories[repoName].resultSets, newResultsets
);
mapResultSets(repoName, newResultsets);
// only set the meta-data on the first pull for a repo.
// because this will establish ranges from then-on for auto-updates.
if (_.isUndefined(repositories[repoName].meta)) {
repositories[repoName].meta = data.meta;
}
}
repositories[repoName].loadingStatus.appending = false;
};
/**
* Ensure the revisions for this resultset have been loaded. If this resultset
* already has revisions loaded, then this is a no-op.
*/
var loadRevisions = function (repoName, resultsetId) {
$log.debug("loadRevisions", repoName, resultsetId);
var rs = repositories[repoName].rsMap[resultsetId].rs_obj;
if (rs && rs.revisions.length === 0) {
$log.debug("loadRevisions: check out to load revisions", rs, repoName);
// these revisions have never been loaded; do so now.
return ThResultSetModel.get(rs.revisions_uri).
success(function (data) {
if (rs.revisions.length === 0) {
Array.prototype.push.apply(rs.revisions, data);
$timeout($rootScope.$emit(thEvents.revisionsLoaded, rs));
}
});
}
};
/**
* Check if ``repoName`` had a range specified in its ``meta`` data
* and whether or not ``push_timestamp`` falls within that range.
*/
var isInResultSetRange = function (repoName, push_timestamp) {
var result = true;
if (repositories[repoName] && repositories[repoName].length) {
var meta = repositories[repoName].meta;
if (_.has(meta, "push_timestamp__gte") &&
push_timestamp < meta.push_timestamp__gte) {
result = false;
}
if (_.has(meta, "push_timestamp__lte") &&
push_timestamp > meta.push_timestamp__lte) {
result = false;
}
if (_.has(meta, "push_timestamp__lt") &&
push_timestamp >= meta.push_timestamp__lt) {
result = false;
}
}
return result;
};
var getResultSetsArray = function (repoName) {
// this is "watchable" for when we add new resultsets and have to
// sort them
return repositories[repoName].resultSets;
};
var getResultSetsMap = function (repoName) {
return repositories[repoName].rsMap;
};
var getResultSet = function (repoName, resultsetId) {
return repositories[repoName].rsMap[resultsetId].rs_obj;
};
var getSelectedRunnableJobs = function (repoName, resultsetId) {
if (!repositories[repoName].rsMap[resultsetId].selected_runnable_jobs) {
repositories[repoName].rsMap[resultsetId].selected_runnable_jobs = [];
}
return repositories[repoName].rsMap[resultsetId].selected_runnable_jobs;
};
var getGeckoDecisionJob = function (repoName, resultsetId) {
let resultSet = getResultSet(repoName, resultsetId);
let platform = _.find(resultSet.platforms, {
"name": "gecko-decision",
"groups": [{"jobs": [{"state": "completed", "job_type_symbol": "D"}]}]});
if (platform) {
// Gecko Decision Task has been completed.
// Let's fetch the URL of full-tasks-graph.json
// This extra search is important to avoid confusion with Action Tasks
return _.find(platform.groups[0].jobs, {"job_type_symbol": "D"});
}
return undefined;
};
var getGeckoDecisionTaskId = function (repoName, resultsetId) {
let resultSet = getResultSet(repoName, resultsetId);
let dtid = resultSet.geckoDecisionTaskId;
// If we've retrieved it already, we can just return it again. Otherwise
// try to find it. If it doesn't exist, we set it to an empty string.
if (dtid || dtid === "") {
return $q.when(dtid);
}
let decisionTask = getGeckoDecisionJob(repoName, resultsetId);
if (decisionTask) {
return ThJobModel.get(repoName, decisionTask.id).then(
function (job) {
// this failure case is unlikely, but I guess you
// never know
if (!job.taskcluster_metadata) {
return $q.reject("Decision task missing taskcluster metadata");
}
return job.taskcluster_metadata.task_id;
});
}
// no decision task, we fail
return $q.reject("No decision task");
};
var toggleSelectedRunnableJob = function (repoName, resultsetId, buildername) {
var selectedRunnableJobs = getSelectedRunnableJobs(repoName, resultsetId);
var jobIndex = selectedRunnableJobs.indexOf(buildername);
if (jobIndex === -1) {
selectedRunnableJobs.push(buildername);
} else {
selectedRunnableJobs.splice(jobIndex, 1);
}
};
var isRunnableJobSelected = function (repoName, resultsetId, buildername) {
var selectedRunnableJobs = getSelectedRunnableJobs(repoName, resultsetId);
return _.includes(selectedRunnableJobs, buildername);
};
var getJobMap = function (repoName) {
// this is a "watchable" for jobs
return repositories[repoName].jobMap;
};
var getGroupMap = function (repoName) {
return repositories[repoName].grpMap;
};
var getLoadingStatus = function (repoName) {
return repositories[repoName].loadingStatus;
};