Skip to content

Commit

Permalink
✨ Maintenance Tracker II (#3785)
Browse files Browse the repository at this point in the history
  • Loading branch information
odeimaiz authored Jan 20, 2023
1 parent eda37dc commit cca02d0
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ qx.Class.define("osparc.component.message.FlashMessenger", {
* @param {Number} duration
*/
logAs: function(message, level="INFO", logger=null, duration=null) {
this.log({
return this.log({
message,
level: level.toUpperCase(),
logger,
Expand All @@ -93,46 +93,48 @@ qx.Class.define("osparc.component.message.FlashMessenger", {
}
const level = logMessage.level.toUpperCase(); // "DEBUG", "INFO", "WARNING", "ERROR"

const flash = new osparc.ui.message.FlashMessage(message, level, logMessage.duration);
flash.addListener("closeMessage", () => this.__removeMessage(flash), this);
this.__messages.push(flash);
const flashMessage = new osparc.ui.message.FlashMessage(message, level, logMessage.duration);
flashMessage.addListener("closeMessage", () => this.removeMessage(flashMessage), this);
this.__messages.push(flashMessage);

return flashMessage;
},

/**
* Private method to show a message to the user. It will stack it on the previous ones.
*
* @param {osparc.ui.message.FlashMessage} message FlassMessage element to show.
* @param {osparc.ui.message.FlashMessage} flashMessage FlassMessage element to show.
*/
__showMessage: function(message) {
this.__messages.remove(message);
__showMessage: function(flashMessage) {
this.__messages.remove(flashMessage);
this.__messageContainer.resetDecorator();
this.__messageContainer.add(message);
this.__messageContainer.add(flashMessage);
const {
width
} = message.getSizeHint();
} = flashMessage.getSizeHint();
if (this.__displayedMessagesCount === 0 || width > this.__messageContainer.getWidth()) {
this.__updateContainerPosition(width);
}
this.__displayedMessagesCount++;

let duration = message.getDuration();
let duration = flashMessage.getDuration();
if (duration === null) {
const wordCount = message.getMessage().split(" ").length;
const wordCount = flashMessage.getMessage().split(" ").length;
duration = Math.max(5500, wordCount*400); // An average reader takes 300ms to read a word
}
qx.event.Timer.once(() => this.__removeMessage(message), this, duration);
qx.event.Timer.once(() => this.removeMessage(flashMessage), this, duration);
},

/**
* Private method to remove a message. If there are still messages in the queue, it will show the next available one.
*
* @param {osparc.ui.message.FlashMessage} message FlassMessage element to remove.
* @param {osparc.ui.message.FlashMessage} flashMessage FlassMessage element to remove.
*/
__removeMessage: function(message) {
if (this.__messageContainer.indexOf(message) > -1) {
removeMessage: function(flashMessage) {
if (this.__messageContainer.indexOf(flashMessage) > -1) {
this.__displayedMessagesCount--;
this.__messageContainer.setDecorator("flash-container-transitioned");
this.__messageContainer.remove(message);
this.__messageContainer.remove(flashMessage);
qx.event.Timer.once(() => {
if (this.__messages.length) {
// There are still messages to show
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ qx.Class.define("osparc.component.notification.NotificationUI", {
wrap: true
});
this.bind("text", control, "value");
this._add(control);
this._add(control, {
flex: 1
});
break;
}
return control || this.base(arguments, id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ qx.Class.define("osparc.component.notification.Notifications", {
this.__notificationsContainer.addAt(notification, 0);
},

removeNotification: function(notification) {
this.__notifications.remove(notification);
this.__notificationsContainer.remove(notification);
},

getNotifications: function() {
return this.__notifications;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,25 @@ qx.Class.define("osparc.data.MaintenanceTracker", {
},

statics: {
CHECK_INTERVAL: 60*60*1000, // Check hourly
CHECK_INTERVAL: 15*60*1000, // Check every 15'
WARN_IN_ADVANCE: 20*60*1000 // Show Flash Message 20' in advance
},

members: {
__checkInternval: null,
__lastNotification: null,
__lastFlashMessage: null,
__logoutTimer: null,

startTracker: function() {
const checkMaintenance = () => {
if (this.getStart()) {
return;
}
osparc.data.Resources.get("maintenance")
.then(scheduledMaintenance => {
if (scheduledMaintenance) {
// for now it's just a string
this.__setMaintenance(JSON.parse(scheduledMaintenance));
} else {
this.__setMaintenance(null);
}
})
.catch(err => console.error(err));
Expand Down Expand Up @@ -94,39 +96,62 @@ qx.Class.define("osparc.data.MaintenanceTracker", {
},

__setMaintenance: function(maintenanceData) {
if ("start" in maintenanceData) {
const startDate = new Date(maintenanceData.start);
this.setStart(startDate);
const oldStart = this.getStart();
const oldEnd = this.getEnd();
const oldReason = this.getReason();

this.setStart(maintenanceData && "start" in maintenanceData ? new Date(maintenanceData.start) : null);
this.setEnd(maintenanceData && "end" in maintenanceData ? new Date(maintenanceData.end) : null);
this.setReason(maintenanceData && "reason" in maintenanceData ? maintenanceData.reason : null);

if (
(oldStart === null || oldStart.getTime() !== this.getStart().getTime()) ||
(oldEnd === null || oldEnd.getTime() !== this.getEnd().getTime()) ||
oldReason !== this.getReason()
) {
this.__scheduleMaintenance();
}
if ("end" in maintenanceData) {
const endDate = new Date(maintenanceData.end);
this.setEnd(new Date(endDate));
}
if ("reason" in maintenanceData) {
const reason = maintenanceData.reason;
this.setReason(reason);
},

__scheduleMaintenance: function() {
this.__scheduleStart();
},

__scheduleStart: function() {
if (this.getStart() === null) {
this.__removeNotification();
this.__removeFlashMessage();
this.__removeScheduledLogout();
} else {
this.__addNotification();
this.__scheduleFlashMessage();
this.__scheduleLogout();
}
},

__addNotification: function() {
this.__removeNotification();

const text = this.__getText();
const notification = new osparc.component.notification.NotificationUI(text);
const notification = this.__lastNotification = new osparc.component.notification.NotificationUI(text);
osparc.component.notification.Notifications.getInstance().addNotification(notification);

this.__scheduleMaintenance();

this.stopTracker();
},

__scheduleMaintenance: function() {
this.__scheduleFlashMessage();
this.__scheduleLogout();
__removeNotification: function() {
if (this.__lastNotification) {
osparc.component.notification.Notifications.getInstance().removeNotification(this.__lastNotification);
this.__lastNotification = null;
}
},

__scheduleFlashMessage: function() {
this.__removeFlashMessage();

const popupMessage = () => {
const now = new Date();
const duration = this.getStart().getTime() - now.getTime();
const text = this.__getText();
osparc.component.message.FlashMessenger.getInstance().logAs(text, "WARNING", null, duration);
this.__lastFlashMessage = osparc.component.message.FlashMessenger.getInstance().logAs(text, "WARNING", null, duration);
};
const now = new Date();
const diff = this.getStart().getTime() - now.getTime() - this.self().WARN_IN_ADVANCE;
Expand All @@ -137,14 +162,41 @@ qx.Class.define("osparc.data.MaintenanceTracker", {
}
},

__removeFlashMessage: function() {
if (this.__lastFlashMessage) {
osparc.component.message.FlashMessenger.getInstance().removeMessage(this.__lastFlashMessage);
this.__lastFlashMessage = null;
}
},

__scheduleLogout: function() {
this.__removeScheduledLogout();

const logoutUser = () => {
this.set({
start: null,
end: null,
reason: null
});
let text = qx.locale.Manager.tr("We are under maintenance.");
text += "<br>";
text += qx.locale.Manager.tr("Please, check back later");
osparc.component.message.FlashMessenger.getInstance().logAs(text, "WARNING");
qx.core.Init.getApplication().logout();
};
const now = new Date();
const diff = this.getStart().getTime() - now.getTime();
console.log("logout scheduled: ", this.getStart());
setTimeout(() => logoutUser(), diff);
if (this.getStart().getTime() > now.getTime()) {
const diff = this.getStart().getTime() - now.getTime();
this.__logoutTimer = setTimeout(() => logoutUser(), diff);
} else if (this.getStart().getTime() < now.getTime() && this.getEnd().getTime() > now.getTime()) {
logoutUser();
}
},

__removeScheduledLogout: function() {
if (this.__logoutTimer) {
clearTimeout(this.__logoutTimer);
}
}
}
});

0 comments on commit cca02d0

Please sign in to comment.