Commit a86d71b5c7c3d87068c24270675ebed6a05e1eb0

Authored by Adhidarma Hadiwinoto
1 parent ee50fa5a04
Exists in master

hanya push healthy jika blm terdaftar

Showing 1 changed file with 6 additions and 3 deletions Inline Diff

1 'use strict'; 1 'use strict';
2 2
3 var moment = require('moment'); 3 var moment = require('moment');
4 var momentFormat = 'YYYY-MM-DD HH:mm:ss'; 4 var momentFormat = 'YYYY-MM-DD HH:mm:ss';
5 var maxHealthyWaitMs = 60 * 1000; 5 var maxHealthyWaitMs = 60 * 1000;
6 6
7 module.exports = MatrixUtil; 7 module.exports = MatrixUtil;
8 8
9 function _cleanPartnerId(partnerId) { 9 function _cleanPartnerId(partnerId) {
10 let cleaned = partnerId; 10 let cleaned = partnerId;
11 11
12 try { 12 try {
13 cleaned = cleaned.toLocaleString(); 13 cleaned = cleaned.toLocaleString();
14 cleaned = cleaned.trim().toLowerCase(); 14 cleaned = cleaned.trim().toLowerCase();
15 } catch(e) { 15 } catch(e) {
16 return partnerId; 16 return partnerId;
17 } 17 }
18 18
19 return cleaned; 19 return cleaned;
20 } 20 }
21 21
22 function MatrixUtil(options) { 22 function MatrixUtil(options) {
23 if (!options) { 23 if (!options) {
24 console.trace('Undefined options'); 24 console.trace('Undefined options');
25 process.exit(1); 25 process.exit(1);
26 } 26 }
27 27
28 this.matrix = options.matrix; 28 this.matrix = options.matrix;
29 if (!this.matrix) { 29 if (!this.matrix) {
30 console.trace("Matrix not set"); 30 console.trace("Matrix not set");
31 process.exit(1); 31 process.exit(1);
32 } 32 }
33 33
34 this.logger = options.logger; 34 this.logger = options.logger;
35 if (!this.logger) { 35 if (!this.logger) {
36 console.trace("Logger not set"); 36 console.trace("Logger not set");
37 process.exit(1); 37 process.exit(1);
38 } 38 }
39 } 39 }
40 40
41 MatrixUtil.prototype.updateBuddyState = function(jid, state, statusText, resource) { 41 MatrixUtil.prototype.updateBuddyState = function(jid, state, statusText, resource) {
42 if (!jid) {return; } 42 if (!jid) {return; }
43 if (jid == 'undefined') {return; } 43 if (jid == 'undefined') {return; }
44 44
45 jid = _cleanPartnerId(jid); 45 jid = _cleanPartnerId(jid);
46 46
47 if (!resource) { 47 if (!resource) {
48 resource = 'undefined'; 48 resource = 'undefined';
49 } 49 }
50 50
51 let logger = this.logger; 51 let logger = this.logger;
52 let matrix = this.matrix; 52 let matrix = this.matrix;
53 53
54 logger.verbose('Buddy state change', {jid: jid, state: state, statusText: statusText, resource: resource}); 54 logger.verbose('Buddy state change', {jid: jid, state: state, statusText: statusText, resource: resource});
55 55
56 if (!matrix) { 56 if (!matrix) {
57 return; 57 return;
58 } 58 }
59 59
60 if (!matrix.buddies) { 60 if (!matrix.buddies) {
61 matrix.buddies = {}; 61 matrix.buddies = {};
62 } 62 }
63 63
64 if (!matrix.buddies[jid]) { 64 if (!matrix.buddies[jid]) {
65 matrix.buddies[jid] = {resources: {}}; 65 matrix.buddies[jid] = {resources: {}};
66 } 66 }
67 67
68 try { 68 try {
69 matrix.buddies[jid]['resources'][resource] = { 69 matrix.buddies[jid]['resources'][resource] = {
70 state: state, 70 state: state,
71 statusText: statusText, 71 statusText: statusText,
72 last_update: moment().format(momentFormat) 72 last_update: moment().format(momentFormat)
73 } 73 }
74 } 74 }
75 catch(e) { 75 catch(e) {
76 logger.warn('MatrixUtil: Exception on update resources on matrix', {jid: jid, state: state, statusText: statusText, resource: resource}); 76 logger.warn('MatrixUtil: Exception on update resources on matrix', {jid: jid, state: state, statusText: statusText, resource: resource});
77 } 77 }
78 78
79 if (resource != 'undefined' && matrix.buddies[jid].resources['undefined']) { 79 if (resource != 'undefined' && matrix.buddies[jid].resources['undefined']) {
80 try { 80 try {
81 delete matrix.buddies[jid].resources['undefined']; 81 delete matrix.buddies[jid].resources['undefined'];
82 } 82 }
83 catch(e) {}; 83 catch(e) {};
84 } 84 }
85 } 85 }
86 86
87 MatrixUtil.prototype.isPartnerOffline = function(partner) { 87 MatrixUtil.prototype.isPartnerOffline = function(partner) {
88 if (!partner) { return; } 88 if (!partner) { return; }
89 89
90 partner = _cleanPartnerId(partner); 90 partner = _cleanPartnerId(partner);
91 91
92 let matrix = this.matrix; 92 let matrix = this.matrix;
93 let logger = this.logger; 93 let logger = this.logger;
94 94
95 if (!matrix) { return false; } 95 if (!matrix) { return false; }
96 96
97 if (!matrix.buddies[partner]) { return true; } 97 if (!matrix.buddies[partner]) { return true; }
98 if (!matrix.buddies[partner].resources) { return true; }; 98 if (!matrix.buddies[partner].resources) { return true; };
99 99
100 let resources = matrix.buddies[partner].resources; 100 let resources = matrix.buddies[partner].resources;
101 for (let key in resources) { 101 for (let key in resources) {
102 if (resources.hasOwnProperty(key)) { 102 if (resources.hasOwnProperty(key)) {
103 let resource = resources[key]; 103 let resource = resources[key];
104 if (resources[key].state == 'online') { 104 if (resources[key].state == 'online') {
105 return false; 105 return false;
106 } 106 }
107 } 107 }
108 } 108 }
109 logger.verbose('Offline partner detected: ' + partner); 109 logger.verbose('Offline partner detected: ' + partner);
110 return true; 110 return true;
111 } 111 }
112 112
113 MatrixUtil.prototype._isPartnerHealthy = function(partner) { 113 MatrixUtil.prototype._isPartnerHealthy = function(partner) {
114 if (!partner) { return; } 114 if (!partner) { return; }
115 partner = _cleanPartnerId(partner); 115 partner = _cleanPartnerId(partner);
116 116
117 if (this.isPartnerOffline(partner)) { 117 if (this.isPartnerOffline(partner)) {
118 return; 118 return;
119 } 119 }
120 120
121 let matrix = this.matrix; 121 let matrix = this.matrix;
122 let logger = this.logger; 122 let logger = this.logger;
123 123
124 if (!matrix) { return false; } 124 if (!matrix) { return false; }
125 125
126 if (!matrix.buddies[partner]) { return false; } 126 if (!matrix.buddies[partner]) { return false; }
127 if (!matrix.buddies[partner]['waiting_for_response']) { return true; } 127 if (!matrix.buddies[partner]['waiting_for_response']) { return true; }
128 if (!matrix.buddies[partner]['last_outgoing']) { return true; } 128 if (!matrix.buddies[partner]['last_outgoing']) { return true; }
129 if (!matrix.buddies[partner]['last_outgoing']['last_update_ts']) { return true; } 129 if (!matrix.buddies[partner]['last_outgoing']['last_update_ts']) { return true; }
130 130
131 let delta = Date.now() - Number(matrix.buddies[partner]['last_outgoing']['last_update_ts']); 131 let delta = Date.now() - Number(matrix.buddies[partner]['last_outgoing']['last_update_ts']);
132 let isHealthy = delta <= maxHealthyWaitMs; 132 let isHealthy = delta <= maxHealthyWaitMs;
133 133
134 logger.verbose('Partner healthy analized', {partner: partner, isHealthy: isHealthy, delta: delta, maxHealthyWaitMs: maxHealthyWaitMs}); 134 logger.verbose('Partner healthy analized', {partner: partner, isHealthy: isHealthy, delta: delta, maxHealthyWaitMs: maxHealthyWaitMs});
135 return isHealthy; 135 return isHealthy;
136 } 136 }
137 137
138 MatrixUtil.prototype.isPartnerHealthy = function(partner) { 138 MatrixUtil.prototype.isPartnerHealthy = function(partner) {
139 let matrix = this.matrix; 139 let matrix = this.matrix;
140 140
141 let isHealthy = this._isPartnerHealthy(partner); 141 let isHealthy = this._isPartnerHealthy(partner);
142 142
143 if (!matrix.healthy_partners) { 143 if (!matrix.healthy_partners) {
144 matrix.healthy_partners = []; 144 matrix.healthy_partners = [];
145 } 145 }
146 146
147 // update matrix 147 // update matrix
148 let idx = matrix.healthy_partners.indexOf(partner);
149
148 if (isHealthy) { 150 if (isHealthy) {
149 matrix.healthy_partners.push(partner); 151 if (idx < 0) {
152 matrix.healthy_partners.push(partner);
153 }
150 } 154 }
151 else { 155 else {
152 let idx = matrix.healthy_partners.indexOf(partner);
153 if (idx > -1) { 156 if (idx > -1) {
154 matrix.healthy_partners.splice(idx, 1); 157 matrix.healthy_partners.splice(idx, 1);
155 } 158 }
156 } 159 }
157 160
158 return isHealthy; 161 return isHealthy;
159 } 162 }
160 163
161 MatrixUtil.prototype._updateLastResponseTime = function(partner) { 164 MatrixUtil.prototype._updateLastResponseTime = function(partner) {
162 let matrix = this.matrix; 165 let matrix = this.matrix;
163 let logger = this.logger; 166 let logger = this.logger;
164 167
165 if (!matrix.buddies[partner]['last_outgoing']) { 168 if (!matrix.buddies[partner]['last_outgoing']) {
166 logger.verbose('No outgoing yet, skip updateLastResponseTime'); 169 logger.verbose('No outgoing yet, skip updateLastResponseTime');
167 return; 170 return;
168 } 171 }
169 172
170 if (!matrix.buddies[partner]['last_outgoing']['last_update_ts']) { 173 if (!matrix.buddies[partner]['last_outgoing']['last_update_ts']) {
171 logger.verbose('No outgoing timestamp yet, skip updateLastResponseTime'); 174 logger.verbose('No outgoing timestamp yet, skip updateLastResponseTime');
172 return; 175 return;
173 } 176 }
174 177
175 if ( 178 if (
176 matrix.buddies[partner]['last_incoming'] 179 matrix.buddies[partner]['last_incoming']
177 && (Number(matrix.buddies[partner]['last_incoming']['last_update_ts']) > Number(matrix.buddies[partner]['last_outgoing']['last_update_ts'])) 180 && (Number(matrix.buddies[partner]['last_incoming']['last_update_ts']) > Number(matrix.buddies[partner]['last_outgoing']['last_update_ts']))
178 ) { 181 ) {
179 return; 182 return;
180 } 183 }
181 184
182 let delta = Date.now() - Number(matrix.buddies[partner]['last_outgoing']['last_update_ts']); 185 let delta = Date.now() - Number(matrix.buddies[partner]['last_outgoing']['last_update_ts']);
183 delta = (delta / 1000).toFixed(2); 186 delta = (delta / 1000).toFixed(2);
184 logger.verbose('MatrixUtil: Response time in ' + delta + ' seconds', {partner: partner}); 187 logger.verbose('MatrixUtil: Response time in ' + delta + ' seconds', {partner: partner});
185 matrix.buddies[partner]['last_response_time_in_secs'] = delta; 188 matrix.buddies[partner]['last_response_time_in_secs'] = delta;
186 } 189 }
187 190
188 MatrixUtil.prototype._updateLastMessage = function(partner, msg, direction) { 191 MatrixUtil.prototype._updateLastMessage = function(partner, msg, direction) {
189 if (!partner) { return; } 192 if (!partner) { return; }
190 partner = _cleanPartnerId(partner); 193 partner = _cleanPartnerId(partner);
191 194
192 let matrix = this.matrix; 195 let matrix = this.matrix;
193 let logger = this.logger; 196 let logger = this.logger;
194 197
195 if (!matrix) { 198 if (!matrix) {
196 return; 199 return;
197 } 200 }
198 201
199 if (!matrix.buddies) { 202 if (!matrix.buddies) {
200 matrix.buddies = {}; 203 matrix.buddies = {};
201 } 204 }
202 205
203 if (!matrix.buddies[partner]) { 206 if (!matrix.buddies[partner]) {
204 matrix.buddies[partner] = {}; 207 matrix.buddies[partner] = {};
205 } 208 }
206 209
207 if (direction == 'incoming') { 210 if (direction == 'incoming') {
208 try { 211 try {
209 this._updateLastResponseTime(partner); 212 this._updateLastResponseTime(partner);
210 } 213 }
211 catch(e) { 214 catch(e) {
212 logger.warn('Exception when updateLastResponseTime', {err: e}); 215 logger.warn('Exception when updateLastResponseTime', {err: e});
213 } 216 }
214 } 217 }
215 218
216 matrix.buddies[partner]['waiting_for_response'] = (direction == 'outgoing'); 219 matrix.buddies[partner]['waiting_for_response'] = (direction == 'outgoing');
217 220
218 matrix.buddies[partner]['last_' + direction] = { 221 matrix.buddies[partner]['last_' + direction] = {
219 msg: msg, 222 msg: msg,
220 last_update: moment().format(momentFormat), 223 last_update: moment().format(momentFormat),
221 last_update_ts: Date.now() 224 last_update_ts: Date.now()
222 } 225 }
223 226
224 if (direction == 'outgoing') { 227 if (direction == 'outgoing') {
225 return; 228 return;
226 } 229 }
227 } 230 }
228 231
229 232
230 MatrixUtil.prototype.updateLastIncoming = function(partner, msg) { 233 MatrixUtil.prototype.updateLastIncoming = function(partner, msg) {
231 this._updateLastMessage(partner, msg, 'incoming'); 234 this._updateLastMessage(partner, msg, 'incoming');
232 } 235 }
233 236
234 MatrixUtil.prototype.updateLastOutgoing = function(partner, msg) { 237 MatrixUtil.prototype.updateLastOutgoing = function(partner, msg) {
235 this._updateLastMessage(partner, msg, 'outgoing'); 238 this._updateLastMessage(partner, msg, 'outgoing');
236 } 239 }