discourse/app/assets/javascripts/discourse/services/logs-notice.js.es6

88 lines
2.2 KiB
Plaintext
Raw Normal View History

2018-06-15 23:03:24 +08:00
import {
default as computed,
on,
observes
} from "ember-addons/ember-computed-decorators";
import { autoUpdatingRelativeAge } from "discourse/lib/formatter";
const LOGS_NOTICE_KEY = "logs-notice-text";
const LogsNotice = Ember.Object.extend({
text: "",
2018-06-15 23:03:24 +08:00
@on("init")
_setup() {
2018-06-15 23:03:24 +08:00
if (!this.get("isActivated")) return;
const text = this.keyValueStore.getItem(LOGS_NOTICE_KEY);
2018-06-15 23:03:24 +08:00
if (text) this.set("text", text);
this.messageBus.subscribe("/logs_error_rate_exceeded", data => {
const duration = data.duration;
2016-03-24 09:11:58 +08:00
const rate = data.rate;
var siteSettingLimit = 0;
2018-06-15 23:03:24 +08:00
if (duration === "minute") {
siteSettingLimit = this.siteSettings.alert_admins_if_errors_per_minute;
2018-06-15 23:03:24 +08:00
} else if (duration === "hour") {
siteSettingLimit = this.siteSettings.alert_admins_if_errors_per_hour;
}
2018-06-15 23:03:24 +08:00
var translationKey = rate === siteSettingLimit ? "reached" : "exceeded";
2016-03-24 09:11:58 +08:00
2018-06-15 23:03:24 +08:00
this.set(
"text",
2016-03-24 09:11:58 +08:00
I18n.t(`logs_error_rate_notice.${translationKey}`, {
2018-06-15 23:03:24 +08:00
relativeAge: autoUpdatingRelativeAge(
new Date(data.publish_at * 1000)
),
siteSettingRate: I18n.t("logs_error_rate_notice.rate", {
count: siteSettingLimit,
duration: duration
}),
rate: I18n.t("logs_error_rate_notice.rate", {
count: rate,
duration: duration
}),
url: Discourse.getURL("/logs")
})
);
});
},
2018-06-15 23:03:24 +08:00
@computed("text")
isEmpty(text) {
return Ember.isEmpty(text);
},
2018-06-15 23:03:24 +08:00
@computed("text")
message(text) {
return new Handlebars.SafeString(text);
},
2018-06-15 23:03:24 +08:00
@computed("currentUser")
isAdmin(currentUser) {
return currentUser && currentUser.admin;
},
2018-06-15 23:03:24 +08:00
@computed("isEmpty", "isAdmin")
hidden(isEmpty, isAdmin) {
return !isAdmin || isEmpty;
},
2018-06-15 23:03:24 +08:00
@observes("text")
_updateKeyValueStore() {
2018-06-15 23:03:24 +08:00
this.keyValueStore.setItem(LOGS_NOTICE_KEY, this.get("text"));
},
2018-06-15 23:03:24 +08:00
@computed(
"siteSettings.alert_admins_if_errors_per_hour",
"siteSettings.alert_admins_if_errors_per_minute"
)
isActivated(errorsPerHour, errorsPerMinute) {
return errorsPerHour > 0 || errorsPerMinute > 0;
}
});
export default LogsNotice;