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-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-04-26 20:49:41 +08:00
|
|
|
diskSpace: Ember.computed.alias("model.attributes.disk_space"),
|
|
|
|
|
2018-05-09 16:05:40 +08:00
|
|
|
availablePeriods: ["yearly", "quarterly", "monthly", "weekly"],
|
|
|
|
|
|
|
|
|
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-04-26 20:49:41 +08:00
|
|
|
AdminDashboardNext.find().then(adminDashboardNextModel => {
|
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-04-19 03:30:41 +08:00
|
|
|
},
|
2018-04-16 16:42:06 +08:00
|
|
|
|
|
|
|
@computed("period")
|
|
|
|
startDate(period) {
|
2018-05-11 11:30:21 +08:00
|
|
|
let fullDay = moment().utc().subtract(1, "day");
|
|
|
|
|
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:
|
|
|
|
return null;
|
2018-04-16 16:42:06 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-05-11 11:30:21 +08:00
|
|
|
@computed()
|
|
|
|
lastWeek() {
|
|
|
|
return moment().utc().endOf("day").subtract(1, "week");
|
|
|
|
},
|
|
|
|
|
|
|
|
@computed()
|
|
|
|
endDate() {
|
|
|
|
return moment().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));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_reportsForPeriodURL(period) {
|
2018-04-17 17:01:06 +08:00
|
|
|
return `/admin/dashboard-next?period=${period}`;
|
2018-04-16 16:42:06 +08:00
|
|
|
}
|
|
|
|
});
|