1
0
mirror of https://github.com/ohwgiles/laminar.git synced 2024-10-27 20:34:20 +00:00

resolves #28: compensate for server/client clock skew

This commit is contained in:
Oliver Giles 2018-02-03 16:52:46 +02:00
parent 46efb07285
commit 30f2203a3b
3 changed files with 15 additions and 2 deletions

View File

@ -169,6 +169,7 @@ void Laminar::sendStatus(LaminarClient* client) {
Json j; Json j;
j.set("type", "status"); j.set("type", "status");
j.set("title", getenv("LAMINAR_TITLE") ?: "Laminar"); j.set("title", getenv("LAMINAR_TITLE") ?: "Laminar");
j.set("time", time(nullptr));
j.startObject("data"); j.startObject("data");
if(client->scope.type == MonitorScope::RUN) { if(client->scope.type == MonitorScope::RUN) {
db->stmt("SELECT queuedAt,startedAt,completedAt, result, reason FROM builds WHERE name = ? AND number = ?") db->stmt("SELECT queuedAt,startedAt,completedAt, result, reason FROM builds WHERE name = ? AND number = ?")

View File

@ -24,6 +24,8 @@ const WebsocketHandler = function() {
comp.ws = this; comp.ws = this;
// Update html and nav titles // Update html and nav titles
document.title = comp.$root.title = msg.title; document.title = comp.$root.title = msg.title;
// Calculate clock offset (used by ProgressUpdater)
comp.$root.clockSkew = msg.time - Math.floor((new Date()).getTime()/1000);
comp.$root.connected = true; comp.$root.connected = true;
// Component-specific callback handler // Component-specific callback handler
comp[msg.type](msg.data); comp[msg.type](msg.data);
@ -98,7 +100,7 @@ const Utils = {
}, },
formatDuration: function(start, end) { formatDuration: function(start, end) {
if(!end) if(!end)
end = Math.floor(Date.now()/1000); end = Math.floor(Date.now()/1000) + this.$root.clockSkew;
if(end - start > 3600) if(end - start > 3600)
return Math.floor((end-start)/3600) + ' hours, ' + Math.floor(((end-start)%3600)/60) + ' minutes'; return Math.floor((end-start)/3600) + ' hours, ' + Math.floor(((end-start)%3600)/60) + ' minutes';
else if(end - start > 60) else if(end - start > 60)
@ -114,7 +116,7 @@ const ProgressUpdater = {
methods: { methods: {
updateProgress(o) { updateProgress(o) {
if (o.etc) { if (o.etc) {
var p = ((new Date()).getTime() / 1000 - o.started) / (o.etc - o.started); var p = (Math.floor(Date.now()/1000) + this.$root.clockSkew - o.started) / (o.etc - o.started);
if (p > 1.2) { if (p > 1.2) {
o.overtime = true; o.overtime = true;
} else if (p >= 1) { } else if (p >= 1) {
@ -524,6 +526,7 @@ new Vue({
el: '#app', el: '#app',
data: { data: {
title: '', // populated by status ws message title: '', // populated by status ws message
clockSkew: 0,
connected: false, connected: false,
notify: 'localStorage' in window && localStorage.getItem('showNotifications') == 1 notify: 'localStorage' in window && localStorage.getItem('showNotifications') == 1
}, },

View File

@ -32,3 +32,12 @@ protected:
Laminar laminar; Laminar laminar;
}; };
TEST_F(LaminarTest, StatusMessageContainsTime) {
TestLaminarClient testClient;
laminar.sendStatus(&testClient);
rapidjson::Document d;
d.Parse(testClient.payload.c_str());
ASSERT_TRUE(d.IsObject());
ASSERT_TRUE(d.HasMember("time"));
EXPECT_GE(1, d["time"].GetInt() - time(nullptr));
}