2017-07-12 12:13:56 +02:00
|
|
|
var sent_messages = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
exports.send_times_log = [];
|
|
|
|
exports.send_times_data = {};
|
|
|
|
|
2017-07-13 13:43:24 +02:00
|
|
|
exports.reset_id_state = function () {
|
|
|
|
exports.local_id_dict = new Dict();
|
|
|
|
exports.next_client_message_id = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_new_client_message_id = function (opts) {
|
|
|
|
exports.next_client_message_id += 1;
|
|
|
|
var client_message_id = exports.next_client_message_id;
|
|
|
|
exports.local_id_dict.set(client_message_id, opts.local_id);
|
|
|
|
return client_message_id;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_local_id = function (opts) {
|
|
|
|
var client_message_id = opts.client_message_id;
|
|
|
|
|
|
|
|
if (client_message_id === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return exports.local_id_dict.get(client_message_id);
|
|
|
|
};
|
|
|
|
|
2017-07-12 12:13:56 +02:00
|
|
|
function report_send_time(send_time, receive_time, display_time, locally_echoed, rendered_changed) {
|
|
|
|
var data = {time: send_time.toString(),
|
|
|
|
received: receive_time.toString(),
|
|
|
|
displayed: display_time.toString(),
|
|
|
|
locally_echoed: locally_echoed};
|
|
|
|
if (locally_echoed) {
|
|
|
|
data.rendered_content_disparity = rendered_changed;
|
|
|
|
}
|
|
|
|
channel.post({
|
|
|
|
url: '/json/report_send_time',
|
|
|
|
data: data,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-13 18:21:24 +02:00
|
|
|
exports.track_message = function (opts) {
|
|
|
|
var client_message_id = opts.client_message_id;
|
|
|
|
|
|
|
|
if (exports.send_times_data[client_message_id] !== undefined) {
|
|
|
|
blueslip.error('We are re-using a client_message_id');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var state = exports.message_state(opts);
|
|
|
|
|
|
|
|
exports.send_times_data[client_message_id] = state;
|
|
|
|
|
|
|
|
return state;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.message_state = function (opts) {
|
2017-07-12 16:12:00 +02:00
|
|
|
var self = {};
|
|
|
|
self.data = {};
|
2017-07-12 12:13:56 +02:00
|
|
|
|
2017-07-13 18:21:24 +02:00
|
|
|
self.data.start = new Date();
|
|
|
|
|
|
|
|
self.data.client_message_id = opts.client_message_id;
|
|
|
|
self.data.locally_echoed = opts.locally_echoed;
|
|
|
|
|
|
|
|
|
2017-07-12 16:12:00 +02:00
|
|
|
self.data.received = undefined;
|
|
|
|
self.data.displayed = undefined;
|
|
|
|
self.data.send_finished = undefined;
|
|
|
|
self.data.rendered_content_disparity = false;
|
|
|
|
|
|
|
|
self.maybe_report_send_times = function () {
|
|
|
|
if (!self.ready()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var data = self.data;
|
|
|
|
report_send_time(data.send_finished - data.start,
|
|
|
|
data.received - data.start,
|
|
|
|
data.displayed - data.start,
|
|
|
|
data.locally_echoed,
|
|
|
|
data.rendered_content_disparity || false);
|
|
|
|
};
|
|
|
|
|
|
|
|
self.mark_received = function () {
|
|
|
|
self.data.received = new Date();
|
|
|
|
self.maybe_report_send_times();
|
|
|
|
};
|
|
|
|
|
|
|
|
self.mark_displayed = function () {
|
|
|
|
self.data.displayed = new Date();
|
|
|
|
self.maybe_report_send_times();
|
|
|
|
};
|
|
|
|
|
|
|
|
self.mark_disparity = function (changed) {
|
|
|
|
self.data.rendered_content_disparity = changed;
|
|
|
|
};
|
|
|
|
|
2017-07-13 18:21:24 +02:00
|
|
|
self.process_success = function () {
|
|
|
|
var send_finished = new Date();
|
|
|
|
|
|
|
|
var send_time = (send_finished - self.data.start);
|
|
|
|
if (feature_flags.log_send_times) {
|
|
|
|
blueslip.log("send time: " + send_time);
|
|
|
|
}
|
|
|
|
if (feature_flags.collect_send_times) {
|
|
|
|
exports.send_times_log.push(send_time);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.data.send_finished = send_finished;
|
2017-07-12 16:12:00 +02:00
|
|
|
self.maybe_report_send_times();
|
|
|
|
};
|
|
|
|
|
|
|
|
self.was_received = function () {
|
|
|
|
return self.data.received !== undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
self.ready = function () {
|
|
|
|
return (self.data.send_finished !== undefined) &&
|
|
|
|
(self.data.received !== undefined) &&
|
|
|
|
(self.data.displayed !== undefined);
|
|
|
|
};
|
|
|
|
|
|
|
|
return self;
|
|
|
|
};
|
|
|
|
|
2017-07-13 16:21:16 +02:00
|
|
|
exports.get_message_state = function (client_message_id) {
|
2017-07-13 18:21:24 +02:00
|
|
|
var state = exports.send_times_data[client_message_id];
|
|
|
|
|
|
|
|
if (!state) {
|
|
|
|
blueslip.warn('Unknown client_message_id' + client_message_id);
|
2017-07-12 12:13:56 +02:00
|
|
|
}
|
2017-07-12 16:12:00 +02:00
|
|
|
|
2017-07-13 18:21:24 +02:00
|
|
|
return state;
|
2017-07-12 16:12:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-07-13 16:21:16 +02:00
|
|
|
function mark_end_to_end_receive_time(client_message_id) {
|
|
|
|
var state = exports.get_message_state(client_message_id);
|
2017-07-13 18:21:24 +02:00
|
|
|
if (!state) {
|
|
|
|
return;
|
|
|
|
}
|
2017-07-12 16:12:00 +02:00
|
|
|
state.mark_received();
|
2017-07-12 12:13:56 +02:00
|
|
|
}
|
|
|
|
|
2017-07-13 16:21:16 +02:00
|
|
|
function mark_end_to_end_display_time(client_message_id) {
|
|
|
|
var state = exports.get_message_state(client_message_id);
|
2017-07-13 18:21:24 +02:00
|
|
|
if (!state) {
|
|
|
|
return;
|
|
|
|
}
|
2017-07-12 16:12:00 +02:00
|
|
|
state.mark_displayed();
|
2017-07-12 12:13:56 +02:00
|
|
|
}
|
|
|
|
|
2017-07-13 16:21:16 +02:00
|
|
|
exports.mark_rendered_content_disparity = function (opts) {
|
|
|
|
var state = exports.get_message_state(opts.client_message_id);
|
2017-07-13 18:21:24 +02:00
|
|
|
if (!state) {
|
|
|
|
return;
|
|
|
|
}
|
2017-07-13 16:21:16 +02:00
|
|
|
state.mark_disparity(opts.changed);
|
2017-07-12 12:13:56 +02:00
|
|
|
};
|
|
|
|
|
2017-07-13 16:21:16 +02:00
|
|
|
exports.report_as_received = function report_as_received(client_message_id) {
|
|
|
|
if (client_message_id) {
|
|
|
|
mark_end_to_end_receive_time(client_message_id);
|
2017-07-12 12:13:56 +02:00
|
|
|
setTimeout(function () {
|
2017-07-13 16:21:16 +02:00
|
|
|
mark_end_to_end_display_time(client_message_id);
|
2017-07-12 12:13:56 +02:00
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-07-13 16:21:16 +02:00
|
|
|
exports.set_timer_for_restarting_event_loop = function (client_message_id) {
|
2017-07-12 12:13:56 +02:00
|
|
|
setTimeout(function () {
|
2017-07-13 16:21:16 +02:00
|
|
|
if (!exports.send_times_data[client_message_id].was_received()) {
|
|
|
|
blueslip.log("Restarting get_events due to delayed receipt of sent message " + client_message_id);
|
2017-07-12 12:13:56 +02:00
|
|
|
server_events.restart_get_events();
|
|
|
|
}
|
|
|
|
}, 5000);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.initialize = function () {
|
2017-07-13 13:43:24 +02:00
|
|
|
exports.reset_id_state();
|
2017-07-12 12:13:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = sent_messages;
|
|
|
|
}
|