Extract sent_messages.message_state class.

This class helps us encapsulate the state of a message, with all
the dates/flags that get sent as part of /json/report_send_time.
This commit is contained in:
Steve Howell
2017-07-12 10:12:00 -04:00
committed by showell
parent f6d670ae3d
commit 25b59d0044
2 changed files with 96 additions and 41 deletions

View File

@@ -19,38 +19,87 @@ function report_send_time(send_time, receive_time, display_time, locally_echoed,
});
}
function maybe_report_send_times(message_id) {
var data = exports.send_times_data[message_id];
if (data.send_finished === undefined || data.received === undefined ||
data.displayed === undefined) {
// We report the data once we have both the send and receive times
return;
exports.message_state = function () {
var self = {};
self.data = {};
// TODO: Fix quirk that we don't know the start time sometime
// when we start recording message state.
self.data.start = undefined;
self.data.received = undefined;
self.data.displayed = undefined;
self.data.send_finished = undefined;
self.data.locally_echoed = false;
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;
};
self.process_success = function (opts) {
self.data.start = opts.start;
self.data.send_finished = opts.send_finished;
self.data.locally_echoed = opts.locally_echoed;
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;
};
exports.get_message_state = function (message_id) {
if (exports.send_times_data[message_id] === undefined) {
exports.send_times_data[message_id] = exports.message_state();
}
report_send_time(data.send_finished - data.start,
data.received - data.start,
data.displayed - data.start,
data.locally_echoed,
data.rendered_content_disparity || false);
}
return exports.send_times_data[message_id];
};
function mark_end_to_end_receive_time(message_id) {
if (exports.send_times_data[message_id] === undefined) {
exports.send_times_data[message_id] = {};
}
exports.send_times_data[message_id].received = new Date();
maybe_report_send_times(message_id);
var state = exports.get_message_state(message_id);
state.mark_received();
}
function mark_end_to_end_display_time(message_id) {
exports.send_times_data[message_id].displayed = new Date();
maybe_report_send_times(message_id);
var state = exports.get_message_state(message_id);
state.mark_displayed();
}
exports.mark_rendered_content_disparity = function (message_id, changed) {
if (exports.send_times_data[message_id] === undefined) {
exports.send_times_data[message_id] = {};
}
exports.send_times_data[message_id].rendered_content_disparity = changed;
var state = exports.get_message_state(message_id);
state.mark_disparity(changed);
};
exports.report_as_received = function report_as_received(message) {
@@ -71,18 +120,18 @@ exports.process_success = function (message_id, start_time, locally_echoed) {
if (feature_flags.collect_send_times) {
exports.send_times_log.push(send_time);
}
if (exports.send_times_data[message_id] === undefined) {
exports.send_times_data[message_id] = {};
}
exports.send_times_data[message_id].start = start_time;
exports.send_times_data[message_id].send_finished = send_finished;
exports.send_times_data[message_id].locally_echoed = locally_echoed;
maybe_report_send_times(message_id);
var state = exports.get_message_state(message_id);
state.process_success({
start: start_time,
send_finished: send_finished,
locally_echoed: locally_echoed,
});
};
exports.set_timer_for_restarting_event_loop = function (message_id) {
setTimeout(function () {
if (exports.send_times_data[message_id].received === undefined) {
if (!exports.send_times_data[message_id].was_received()) {
blueslip.log("Restarting get_events due to delayed receipt of sent message " + message_id);
server_events.restart_get_events();
}
@@ -97,9 +146,8 @@ exports.initialize = function () {
$(document).on('message_id_changed', function (event) {
if (exports.send_times_data[event.old_id] !== undefined) {
var value = exports.send_times_data[event.old_id];
exports.send_times_data[event.new_id] = value;
delete exports.send_times_data[event.old_id];
exports.send_times_data[event.new_id] =
_.extend({}, exports.send_times_data[event.old_id], value);
}
});
};