2018-05-14 20:23:51 +08:00
|
|
|
import { setting } from "discourse/lib/computed";
|
2018-04-19 03:30:41 +08:00
|
|
|
import DiscourseURL from "discourse/lib/url";
|
|
|
|
import computed from "ember-addons/ember-computed-decorators";
|
2018-05-03 21:41:41 +08:00
|
|
|
import AdminDashboardNext from "admin/models/admin-dashboard-next";
|
|
|
|
import Report from "admin/models/report";
|
2018-05-14 20:23:51 +08:00
|
|
|
import VersionCheck from "admin/models/version-check";
|
2018-05-14 11:07:59 +08:00
|
|
|
|
|
|
|
const PROBLEMS_CHECK_MINUTES = 1;
|
2018-04-16 16:42:06 +08:00
|
|
|
|
|
|
|
export default Ember.Controller.extend({
|
|
|
|
queryParams: ["period"],
|
2018-05-09 16:05:40 +08:00
|
|
|
period: "monthly",
|
2018-04-19 03:30:41 +08:00
|
|
|
isLoading: false,
|
|
|
|
dashboardFetchedAt: null,
|
2018-05-03 21:41:41 +08:00
|
|
|
exceptionController: Ember.inject.controller("exception"),
|
2018-05-14 20:23:51 +08:00
|
|
|
showVersionChecks: setting("version_checks"),
|
2018-04-26 20:49:41 +08:00
|
|
|
diskSpace: Ember.computed.alias("model.attributes.disk_space"),
|
2018-05-14 20:23:51 +08:00
|
|
|
logSearchQueriesEnabled: setting("log_search_queries"),
|
2018-05-09 16:05:40 +08:00
|
|
|
availablePeriods: ["yearly", "quarterly", "monthly", "weekly"],
|
|
|
|
|
2018-05-14 20:23:51 +08:00
|
|
|
@computed("problems.length")
|
2018-05-14 11:07:59 +08:00
|
|
|
foundProblems(problemsLength) {
|
2018-05-14 20:23:51 +08:00
|
|
|
return this.currentUser.get("admin") && (problemsLength || 0) > 0;
|
2018-05-14 11:07:59 +08:00
|
|
|
},
|
|
|
|
|
2018-05-14 20:23:51 +08:00
|
|
|
@computed("foundProblems")
|
2018-05-14 11:07:59 +08:00
|
|
|
thereWereProblems(foundProblems) {
|
2018-05-14 20:23:51 +08:00
|
|
|
if (!this.currentUser.get("admin")) { return false; }
|
2018-05-14 11:07:59 +08:00
|
|
|
|
|
|
|
if (foundProblems) {
|
2018-05-14 20:23:51 +08:00
|
|
|
this.set("hadProblems", true);
|
2018-05-14 11:07:59 +08:00
|
|
|
return true;
|
|
|
|
} else {
|
2018-05-14 20:23:51 +08:00
|
|
|
return this.get("hadProblems") || false;
|
2018-05-14 11:07:59 +08:00
|
|
|
}
|
|
|
|
},
|
2018-05-09 16:05:40 +08:00
|
|
|
|
2018-04-19 03:30:41 +08:00
|
|
|
fetchDashboard() {
|
|
|
|
if (this.get("isLoading")) return;
|
|
|
|
|
|
|
|
if (!this.get("dashboardFetchedAt") || moment().subtract(30, "minutes").toDate() > this.get("dashboardFetchedAt")) {
|
|
|
|
this.set("isLoading", true);
|
|
|
|
|
2018-05-14 11:07:59 +08:00
|
|
|
const versionChecks = this.siteSettings.version_checks;
|
|
|
|
|
2018-04-26 20:49:41 +08:00
|
|
|
AdminDashboardNext.find().then(adminDashboardNextModel => {
|
2018-05-14 11:07:59 +08:00
|
|
|
|
|
|
|
if (versionChecks) {
|
2018-05-14 20:23:51 +08:00
|
|
|
this.set("versionCheck", VersionCheck.create(adminDashboardNextModel.version_check));
|
2018-05-14 11:07:59 +08:00
|
|
|
}
|
|
|
|
|
2018-05-03 21:41:41 +08:00
|
|
|
this.setProperties({
|
|
|
|
dashboardFetchedAt: new Date(),
|
|
|
|
model: adminDashboardNextModel,
|
|
|
|
reports: adminDashboardNextModel.reports.map(x => Report.create(x))
|
|
|
|
});
|
2018-04-19 03:30:41 +08:00
|
|
|
}).catch(e => {
|
|
|
|
this.get("exceptionController").set("thrown", e.jqXHR);
|
|
|
|
this.replaceRoute("exception");
|
|
|
|
}).finally(() => {
|
|
|
|
this.set("isLoading", false);
|
|
|
|
});
|
2018-04-20 00:19:21 +08:00
|
|
|
}
|
2018-05-14 11:07:59 +08:00
|
|
|
|
2018-05-14 20:23:51 +08:00
|
|
|
if (!this.get("problemsFetchedAt") || moment().subtract(PROBLEMS_CHECK_MINUTES, "minutes").toDate() > this.get("problemsFetchedAt")) {
|
2018-05-14 11:07:59 +08:00
|
|
|
this.loadProblems();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
loadProblems() {
|
2018-05-14 20:23:51 +08:00
|
|
|
this.set("loadingProblems", true);
|
|
|
|
this.set("problemsFetchedAt", new Date());
|
2018-05-14 11:07:59 +08:00
|
|
|
AdminDashboardNext.fetchProblems().then(d => {
|
2018-05-14 20:23:51 +08:00
|
|
|
this.set("problems", d.problems);
|
2018-05-14 11:07:59 +08:00
|
|
|
}).finally(() => {
|
2018-05-14 20:23:51 +08:00
|
|
|
this.set("loadingProblems", false);
|
2018-05-14 11:07:59 +08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-05-14 20:23:51 +08:00
|
|
|
@computed("problemsFetchedAt")
|
2018-05-14 11:07:59 +08:00
|
|
|
problemsTimestamp(problemsFetchedAt) {
|
2018-05-14 20:23:51 +08:00
|
|
|
return moment(problemsFetchedAt).locale("en").format("LLL");
|
2018-04-19 03:30:41 +08:00
|
|
|
},
|
2018-04-16 16:42:06 +08:00
|
|
|
|
|
|
|
@computed("period")
|
|
|
|
startDate(period) {
|
2018-05-14 20:23:51 +08:00
|
|
|
let fullDay = moment().locale("en").utc().subtract(1, "day");
|
2018-05-11 11:30:21 +08:00
|
|
|
|
2018-04-16 16:42:06 +08:00
|
|
|
switch (period) {
|
|
|
|
case "yearly":
|
2018-05-11 11:30:21 +08:00
|
|
|
return fullDay.subtract(1, "year").startOf("day");
|
2018-04-16 16:42:06 +08:00
|
|
|
break;
|
|
|
|
case "quarterly":
|
2018-05-11 11:30:21 +08:00
|
|
|
return fullDay.subtract(3, "month").startOf("day");
|
2018-04-16 16:42:06 +08:00
|
|
|
break;
|
|
|
|
case "weekly":
|
2018-05-11 11:30:21 +08:00
|
|
|
return fullDay.subtract(1, "week").startOf("day");
|
2018-04-16 16:42:06 +08:00
|
|
|
break;
|
|
|
|
case "monthly":
|
2018-05-11 11:30:21 +08:00
|
|
|
return fullDay.subtract(1, "month").startOf("day");
|
2018-04-16 16:42:06 +08:00
|
|
|
break;
|
2018-04-16 22:01:29 +08:00
|
|
|
default:
|
2018-05-16 22:45:21 +08:00
|
|
|
return fullDay.subtract(1, "month").startOf("day");
|
2018-04-16 16:42:06 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-05-11 11:30:21 +08:00
|
|
|
@computed()
|
|
|
|
lastWeek() {
|
2018-05-14 20:23:51 +08:00
|
|
|
return moment().locale("en").utc().endOf("day").subtract(1, "week");
|
2018-05-11 11:30:21 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
@computed()
|
|
|
|
endDate() {
|
2018-05-14 20:23:51 +08:00
|
|
|
return moment().locale("en").utc().subtract(1, "day").endOf("day");
|
2018-04-16 16:42:06 +08:00
|
|
|
},
|
|
|
|
|
2018-04-19 03:30:41 +08:00
|
|
|
@computed("updated_at")
|
|
|
|
updatedTimestamp(updatedAt) {
|
|
|
|
return moment(updatedAt).format("LLL");
|
|
|
|
},
|
|
|
|
|
|
|
|
@computed("last_backup_taken_at")
|
|
|
|
backupTimestamp(lastBackupTakenAt) {
|
|
|
|
return moment(lastBackupTakenAt).format("LLL");
|
|
|
|
},
|
|
|
|
|
2018-04-16 16:42:06 +08:00
|
|
|
actions: {
|
|
|
|
changePeriod(period) {
|
|
|
|
DiscourseURL.routeTo(this._reportsForPeriodURL(period));
|
2018-05-14 11:07:59 +08:00
|
|
|
},
|
|
|
|
refreshProblems() {
|
|
|
|
this.loadProblems();
|
|
|
|
},
|
2018-04-16 16:42:06 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
_reportsForPeriodURL(period) {
|
2018-05-14 14:31:50 +08:00
|
|
|
return Discourse.getURL(`/admin?period=${period}`);
|
2018-04-16 16:42:06 +08:00
|
|
|
}
|
|
|
|
});
|