2019-10-24 00:30:52 +08:00
|
|
|
import Component from "@ember/component";
|
2018-06-15 23:03:24 +08:00
|
|
|
import debounce from "discourse/lib/debounce";
|
|
|
|
import { renderSpinner } from "discourse/helpers/loading-spinner";
|
|
|
|
import { escapeExpression } from "discourse/lib/utilities";
|
|
|
|
import { bufferedRender } from "discourse-common/lib/buffered-render";
|
2018-09-20 02:00:03 +08:00
|
|
|
import { observes, on } from "ember-addons/ember-computed-decorators";
|
2014-02-13 12:35:46 +08:00
|
|
|
|
2019-10-24 00:30:52 +08:00
|
|
|
export default Component.extend(
|
2018-06-15 23:03:24 +08:00
|
|
|
bufferedRender({
|
|
|
|
classNames: ["admin-backups-logs"],
|
2014-02-13 12:35:46 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
init() {
|
2019-01-19 17:05:51 +08:00
|
|
|
this._super(...arguments);
|
2018-06-15 23:03:24 +08:00
|
|
|
this._reset();
|
|
|
|
},
|
2014-02-13 12:35:46 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
_reset() {
|
|
|
|
this.setProperties({ formattedLogs: "", index: 0 });
|
|
|
|
},
|
2014-02-13 12:35:46 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
_scrollDown() {
|
2019-07-16 18:45:15 +08:00
|
|
|
const div = this.element;
|
|
|
|
div.scrollTop = div.scrollHeight;
|
2018-06-15 23:03:24 +08:00
|
|
|
},
|
2016-10-27 23:57:33 +08:00
|
|
|
|
2018-09-20 02:00:03 +08:00
|
|
|
@on("init")
|
|
|
|
@observes("logs.[]")
|
|
|
|
_resetFormattedLogs() {
|
2019-05-27 16:15:39 +08:00
|
|
|
if (this.logs.length === 0) {
|
2018-06-15 23:03:24 +08:00
|
|
|
this._reset(); // reset the cached logs whenever the model is reset
|
|
|
|
this.rerenderBuffer();
|
2014-02-13 12:35:46 +08:00
|
|
|
}
|
2018-09-20 02:00:03 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
@on("init")
|
|
|
|
@observes("logs.[]")
|
|
|
|
_updateFormattedLogs: debounce(function() {
|
2019-05-27 16:15:39 +08:00
|
|
|
const logs = this.logs;
|
2018-09-20 02:00:03 +08:00
|
|
|
if (logs.length === 0) return;
|
|
|
|
|
|
|
|
// do the log formatting only once for HELLish performance
|
2019-05-27 16:15:39 +08:00
|
|
|
let formattedLogs = this.formattedLogs;
|
|
|
|
for (let i = this.index, length = logs.length; i < length; i++) {
|
2018-09-20 02:00:03 +08:00
|
|
|
const date = logs[i].get("timestamp"),
|
|
|
|
message = escapeExpression(logs[i].get("message"));
|
|
|
|
formattedLogs += "[" + date + "] " + message + "\n";
|
|
|
|
}
|
|
|
|
// update the formatted logs & cache index
|
|
|
|
this.setProperties({
|
|
|
|
formattedLogs: formattedLogs,
|
|
|
|
index: logs.length
|
|
|
|
});
|
|
|
|
// force rerender
|
|
|
|
this.rerenderBuffer();
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
Ember.run.scheduleOnce("afterRender", this, this._scrollDown);
|
2018-09-20 02:00:03 +08:00
|
|
|
}, 150),
|
2014-02-13 12:35:46 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
buildBuffer(buffer) {
|
2019-05-27 16:15:39 +08:00
|
|
|
const formattedLogs = this.formattedLogs;
|
2018-06-15 23:03:24 +08:00
|
|
|
if (formattedLogs && formattedLogs.length > 0) {
|
|
|
|
buffer.push("<pre>");
|
|
|
|
buffer.push(formattedLogs);
|
|
|
|
buffer.push("</pre>");
|
|
|
|
} else {
|
|
|
|
buffer.push("<p>" + I18n.t("admin.backups.logs.none") + "</p>");
|
|
|
|
}
|
|
|
|
// add a loading indicator
|
|
|
|
if (this.get("status.isOperationRunning")) {
|
|
|
|
buffer.push(renderSpinner("small"));
|
|
|
|
}
|
2014-02-13 12:35:46 +08:00
|
|
|
}
|
2018-06-15 23:03:24 +08:00
|
|
|
})
|
|
|
|
);
|