forked from aerogear/aerogear-unifiedpush-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
activity.js
118 lines (106 loc) · 4.17 KB
/
activity.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
angular.module('upsConsole')
.controller('ActivityController', function ( $log, $timeout, $interval, $modal, variantModal, $scope, metricsEndpoint ) {
var self = this;
this.app = $scope.$parent.$parent.appDetail.app;
this.metrics = [];
this.totalCount;
this.currentPage = 1;
this.currentStart = 0;
this.currentEnd = 0;
this.perPage = 10;
this.searchString = '';
this.activeSearch = '';
var refreshInterval;
/**
* Fetches new data, reflecting provided page and searchString
*/
function fetchMetrics( page, searchString ) {
return metricsEndpoint.fetchApplicationMetrics(self.app.pushApplicationID, searchString, page, self.perPage)
.then(function( data ) {
self.activeSearch = searchString;
self.metrics.forEach(function( originalMetric ) {
data.pushMetrics.some(function ( newMetric ) {
if (originalMetric.id === newMetric.id && originalMetric.$toggled) {
newMetric.$toggled = true;
return true;
}
});
});
self.metrics = data.pushMetrics;
self.totalCount = data.totalItems;
self.currentStart = self.perPage * (self.currentPage - 1) + 1;
self.currentEnd = self.perPage * (self.currentPage - 1) + self.metrics.length;
self.metrics.forEach(function( metric ) {
try {
metric.$message = JSON.parse(metric.rawJsonMessage);
} catch (err) {
console.log('failed to parse metric');
metric.$message = {};
}
metric.variantInformations.forEach(function( variantInformation ) {
variantInformation.$variant = getVariantByID( variantInformation.variantID );
});
});
});
}
/**
* Determines whether search is active - either the user typed search string or the data doesn't reflect the search string yet.
*
* @return false if searchString if false and data reflects that searchString; true otherwise
*/
this.isSearchActive = function() {
return self.searchString || self.activeSearch;
};
/**
* Fetches new data on page change
*/
this.onPageChange = function ( page ) {
fetchMetrics( page, self.searchString );
};
function getVariantByID ( variantID ) {
return self.app.variants.filter(function( variant ) {
return variant.variantID == variantID;
})[0];
}
function refreshUntilAllServed() {
fetchMetrics( self.currentPage, self.searchString )
.then(function() {
$log.debug('refreshed');
var isPending = self.metrics.some(function(metric) {
return metric.servedVariants < metric.totalVariants;
});
if (isPending) {
if (!refreshInterval) {
$log.debug('scheduling refresh');
refreshInterval = $interval(refreshUntilAllServed, 1000);
}
} else {
$log.debug('clearing refresh');
$interval.cancel(refreshInterval);
refreshInterval = null;
}
});
}
// initial load
refreshUntilAllServed();
$scope.$on('upsNotificationSent', function() {
var timer1 = $timeout(refreshUntilAllServed, 500); // artificial delay - refresh after 0.5sec to ensure server has time to load some batches; prevents situation when totalBatches = 0 for all variants
var timer2 = $timeout(refreshUntilAllServed, 3000); // refresh again to be double-sure ;-) note: should be addressed as part of https://issues.jboss.org/browse/AGPUSH-1513
// destroy timeouts
$scope.$on("$destroy", function() {
$log.debug('cancelling refreshUntilAllServed timeouts');
$timeout.cancel( timer1 );
$timeout.cancel( timer2 );
});
});
$scope.$on('$destroy', function () {
if (refreshInterval) {
$log.debug('cancelling refreshInterval');
$interval.cancel(refreshInterval);
}
});
$scope.$watch(function() { return self.searchString }, function( searchString ) {
self.currentPage = 1;
fetchMetrics( self.currentPage, self.searchString );
});
});