33/* Controllers */
44
55angular . module ( 'sc.controllers' , [ ] )
6- . controller ( 'heartbeatsStats' , [ '$scope' , 'streamService' , 'serviceControlService' , function ( $scope , streamService , serviceControlService ) {
6+ . controller ( 'heartbeatsStats' , [ '$scope' , 'streamService' , 'serviceControlService' , function ( $scope , streamService , serviceControlService ) {
77
8- $scope . model = { active_endpoints : '?' , number_of_failing_endpoints : '?' } ;
9-
10- serviceControlService . getHeartbeatStats ( ) . then ( function ( stat ) {
8+ $scope . model = { active_endpoints : 0 , failing_endpoints : 0 } ;
9+
10+ serviceControlService . getHeartbeatStats ( ) . then ( function ( stat ) {
1111 $scope . model . active_endpoints = stat . active_endpoints ;
12- $scope . model . number_of_failing_endpoints = stat . number_of_failing_endpoints ;
12+ $scope . model . failing_endpoints = stat . failing_endpoints ;
13+ } ) ;
14+
15+ streamService . subscribe ( $scope , 'HeartbeatGracePeriodElapsed' , function ( _ ) {
16+ $scope . model . failing_endpoints ++ ;
17+ ;
1318 } ) ;
1419
15- streamService . subscribe ( $scope , 'HeartbeatSummaryChanged' , function ( message ) {
16- $scope . model . active_endpoints = message . active_endpoints ;
17- $scope . model . number_of_failing_endpoints = message . number_of_failing_endpoints ;
20+ streamService . subscribe ( $scope , 'HeartbeatReceived' , function ( _ ) {
21+ $scope . model . active_endpoints ++ ;
1822 } ) ;
23+
24+ } ] )
25+ . controller ( 'heartbeats' , [ '$scope' , 'serviceControlService' , 'streamService' , function ( $scope , serviceControlService , streamService ) {
26+
27+ $scope . model = [ ] ;
1928
20- } ] )
21- . controller ( 'heartbeats' , [ '$scope' , 'serviceControlService' , 'streamService' , function ( $scope , serviceControlService , streamService ) {
29+ serviceControlService . getHeartbeatsList ( ) . then ( function ( heartbeats ) {
30+ $scope . model = heartbeats ;
31+ } ) ;
32+
33+ streamService . subscribe ( $scope , 'HeartbeatGracePeriodElapsed' , function ( message ) {
34+ processMessage ( message , false ) ;
35+ } ) ;
36+
37+ streamService . subscribe ( $scope , 'HeartbeatReceived' , function ( message ) {
38+ processMessage ( message , true ) ;
39+ } ) ;
2240
23- $scope . model = [ ] ;
41+ function processMessage ( message , active ) {
42+ var idx = findHeartbeat ( message . endpoint , message . machine ) ;
2443
25- function refresh ( ) {
26- serviceControlService . getHeartbeatsList ( ) . then ( function ( heartbeats ) {
27- $scope . model = heartbeats ;
28- } ) ;
29- }
44+ if ( idx == - 1 ) {
45+ $scope . model . push ( angular . extend ( { active : active } , message ) ) ;
46+ } else {
47+ $scope . model [ idx ] . active = active ;
48+ $scope . model [ idx ] . last_sent_at = message . last_sent_at ;
49+ }
50+ }
3051
31- refresh ( ) ;
52+ function findHeartbeat ( endpoint , machine ) {
53+ for ( var i = 0 ; i < $scope . model . length ; i ++ ) {
54+ if ( $scope . model [ i ] . endpoint === endpoint && $scope . model [ i ] . machine === machine ) {
55+ return i ;
56+ }
57+ }
3258
33- streamService . subscribe ( $scope , 'HeartbeatSummaryChanged' , function ( _ ) {
34- refresh ( ) ;
35- } ) ;
36- } ] ) ;
59+ return - 1 ;
60+ } ;
61+ } ] ) ;
0 commit comments