Commit 24b73cd865e3624114c40d01f6f79507407bb9c3

Authored by Adhidarma Hadiwinoto
1 parent f6e5bd71be
Exists in master

matrix alias pada isPartnerHealthy

Showing 1 changed file with 2 additions and 0 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;
140
139 let isHealthy = this._isPartnerHealthy(partner); 141 let isHealthy = this._isPartnerHealthy(partner);
140 142
141 if (!matrix.healthy_partners) { 143 if (!matrix.healthy_partners) {
142 matrix.healthy_partners = []; 144 matrix.healthy_partners = [];
143 } 145 }
144 146
145 // update matrix 147 // update matrix
146 if (isHealthy) { 148 if (isHealthy) {
147 matrix.healthy_partners.push(partner); 149 matrix.healthy_partners.push(partner);
148 } 150 }
149 else { 151 else {
150 let idx = matrix.healthy_partners.indexOf(partner); 152 let idx = matrix.healthy_partners.indexOf(partner);
151 if (idx > -1) { 153 if (idx > -1) {
152 matrix.healthy_partners.splice(idx, 1); 154 matrix.healthy_partners.splice(idx, 1);
153 } 155 }
154 } 156 }
155 157
156 return isHealthy; 158 return isHealthy;
157 } 159 }
158 160
159 MatrixUtil.prototype._updateLastResponseTime = function(partner) { 161 MatrixUtil.prototype._updateLastResponseTime = function(partner) {
160 let matrix = this.matrix; 162 let matrix = this.matrix;
161 let logger = this.logger; 163 let logger = this.logger;
162 164
163 if (!matrix.buddies[partner]['last_outgoing']) { 165 if (!matrix.buddies[partner]['last_outgoing']) {
164 logger.verbose('No outgoing yet, skip updateLastResponseTime'); 166 logger.verbose('No outgoing yet, skip updateLastResponseTime');
165 return; 167 return;
166 } 168 }
167 169
168 if (!matrix.buddies[partner]['last_outgoing']['last_update_ts']) { 170 if (!matrix.buddies[partner]['last_outgoing']['last_update_ts']) {
169 logger.verbose('No outgoing timestamp yet, skip updateLastResponseTime'); 171 logger.verbose('No outgoing timestamp yet, skip updateLastResponseTime');
170 return; 172 return;
171 } 173 }
172 174
173 if ( 175 if (
174 matrix.buddies[partner]['last_incoming'] 176 matrix.buddies[partner]['last_incoming']
175 && (Number(matrix.buddies[partner]['last_incoming']['last_update_ts']) > Number(matrix.buddies[partner]['last_outgoing']['last_update_ts'])) 177 && (Number(matrix.buddies[partner]['last_incoming']['last_update_ts']) > Number(matrix.buddies[partner]['last_outgoing']['last_update_ts']))
176 ) { 178 ) {
177 return; 179 return;
178 } 180 }
179 181
180 let delta = Date.now() - Number(matrix.buddies[partner]['last_outgoing']['last_update_ts']); 182 let delta = Date.now() - Number(matrix.buddies[partner]['last_outgoing']['last_update_ts']);
181 delta = (delta / 1000).toFixed(2); 183 delta = (delta / 1000).toFixed(2);
182 logger.verbose('MatrixUtil: Response time in ' + delta + ' seconds', {partner: partner}); 184 logger.verbose('MatrixUtil: Response time in ' + delta + ' seconds', {partner: partner});
183 matrix.buddies[partner]['last_response_time_in_secs'] = delta; 185 matrix.buddies[partner]['last_response_time_in_secs'] = delta;
184 } 186 }
185 187
186 MatrixUtil.prototype._updateLastMessage = function(partner, msg, direction) { 188 MatrixUtil.prototype._updateLastMessage = function(partner, msg, direction) {
187 if (!partner) { return; } 189 if (!partner) { return; }
188 partner = _cleanPartnerId(partner); 190 partner = _cleanPartnerId(partner);
189 191
190 let matrix = this.matrix; 192 let matrix = this.matrix;
191 let logger = this.logger; 193 let logger = this.logger;
192 194
193 if (!matrix) { 195 if (!matrix) {
194 return; 196 return;
195 } 197 }
196 198
197 if (!matrix.buddies) { 199 if (!matrix.buddies) {
198 matrix.buddies = {}; 200 matrix.buddies = {};
199 } 201 }
200 202
201 if (!matrix.buddies[partner]) { 203 if (!matrix.buddies[partner]) {
202 matrix.buddies[partner] = {}; 204 matrix.buddies[partner] = {};
203 } 205 }
204 206
205 if (direction == 'incoming') { 207 if (direction == 'incoming') {
206 try { 208 try {
207 this._updateLastResponseTime(partner); 209 this._updateLastResponseTime(partner);
208 } 210 }
209 catch(e) { 211 catch(e) {
210 logger.warn('Exception when updateLastResponseTime', {err: e}); 212 logger.warn('Exception when updateLastResponseTime', {err: e});
211 } 213 }
212 } 214 }
213 215
214 matrix.buddies[partner]['waiting_for_response'] = (direction == 'outgoing'); 216 matrix.buddies[partner]['waiting_for_response'] = (direction == 'outgoing');
215 217
216 matrix.buddies[partner]['last_' + direction] = { 218 matrix.buddies[partner]['last_' + direction] = {
217 msg: msg, 219 msg: msg,
218 last_update: moment().format(momentFormat), 220 last_update: moment().format(momentFormat),
219 last_update_ts: Date.now() 221 last_update_ts: Date.now()
220 } 222 }
221 223
222 if (direction == 'outgoing') { 224 if (direction == 'outgoing') {
223 return; 225 return;
224 } 226 }
225 } 227 }
226 228
227 229
228 MatrixUtil.prototype.updateLastIncoming = function(partner, msg) { 230 MatrixUtil.prototype.updateLastIncoming = function(partner, msg) {
229 this._updateLastMessage(partner, msg, 'incoming'); 231 this._updateLastMessage(partner, msg, 'incoming');
230 } 232 }
231 233
232 MatrixUtil.prototype.updateLastOutgoing = function(partner, msg) { 234 MatrixUtil.prototype.updateLastOutgoing = function(partner, msg) {
233 this._updateLastMessage(partner, msg, 'outgoing'); 235 this._updateLastMessage(partner, msg, 'outgoing');
234 } 236 }
235 237