2018-04-18 15:30:41 -04:00
|
|
|
import DiscourseURL from "discourse/lib/url";
|
|
|
|
import computed from "ember-addons/ember-computed-decorators";
|
2018-05-03 09:41:41 -04:00
|
|
|
import AdminDashboardNext from "admin/models/admin-dashboard-next";
|
|
|
|
import Report from "admin/models/report";
|
2018-04-16 04:42:06 -04:00
|
|
|
|
|
|
|
export default Ember.Controller.extend({
|
|
|
|
queryParams: ["period"],
|
2018-05-09 04:05:40 -04:00
|
|
|
period: "monthly",
|
2018-04-18 15:30:41 -04:00
|
|
|
isLoading: false,
|
|
|
|
dashboardFetchedAt: null,
|
2018-05-03 09:41:41 -04:00
|
|
|
exceptionController: Ember.inject.controller("exception"),
|
2018-04-26 08:49:41 -04:00
|
|
|
diskSpace: Ember.computed.alias("model.attributes.disk_space"),
|
|
|
|
|
2018-05-09 04:05:40 -04:00
|
|
|
availablePeriods: ["yearly", "quarterly", "monthly", "weekly"],
|
|
|
|
|
|
|
|
|
2018-04-18 15:30:41 -04: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 08:49:41 -04:00
|
|
|
AdminDashboardNext.find().then(adminDashboardNextModel => {
|
2018-05-03 09:41:41 -04:00
|
|
|
this.setProperties({
|
|
|
|
dashboardFetchedAt: new Date(),
|
|
|
|
model: adminDashboardNextModel,
|
|
|
|
reports: adminDashboardNextModel.reports.map(x => Report.create(x))
|
|
|
|
});
|
2018-04-18 15:30:41 -04:00
|
|
|
}).catch(e => {
|
|
|
|
this.get("exceptionController").set("thrown", e.jqXHR);
|
|
|
|
this.replaceRoute("exception");
|
|
|
|
}).finally(() => {
|
|
|
|
this.set("isLoading", false);
|
|
|
|
});
|
2018-04-19 12:19:21 -04:00
|
|
|
}
|
2018-04-18 15:30:41 -04:00
|
|
|
},
|
2018-04-16 04:42:06 -04:00
|
|
|
|
|
|
|
@computed("period")
|
|
|
|
startDate(period) {
|
2018-05-10 23:30:21 -04:00
|
|
|
let fullDay = moment().utc().subtract(1, "day");
|
|
|
|
|
2018-04-16 04:42:06 -04:00
|
|
|
switch (period) {
|
|
|
|
case "yearly":
|
2018-05-10 23:30:21 -04:00
|
|
|
return fullDay.subtract(1, "year").startOf("day");
|
2018-04-16 04:42:06 -04:00
|
|
|
break;
|
|
|
|
case "quarterly":
|
2018-05-10 23:30:21 -04:00
|
|
|
return fullDay.subtract(3, "month").startOf("day");
|
2018-04-16 04:42:06 -04:00
|
|
|
break;
|
|
|
|
case "weekly":
|
2018-05-10 23:30:21 -04:00
|
|
|
return fullDay.subtract(1, "week").startOf("day");
|
2018-04-16 04:42:06 -04:00
|
|
|
break;
|
|
|
|
case "monthly":
|
2018-05-10 23:30:21 -04:00
|
|
|
return fullDay.subtract(1, "month").startOf("day");
|
2018-04-16 04:42:06 -04:00
|
|
|
break;
|
2018-04-16 10:01:29 -04:00
|
|
|
default:
|
|
|
|
return null;
|
2018-04-16 04:42:06 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-05-10 23:30:21 -04:00
|
|
|
@computed()
|
|
|
|
lastWeek() {
|
|
|
|
return moment().utc().endOf("day").subtract(1, "week");
|
|
|
|
},
|
|
|
|
|
|
|
|
@computed()
|
|
|
|
endDate() {
|
|
|
|
return moment().utc().subtract(1, "day").endOf("day");
|
2018-04-16 04:42:06 -04:00
|
|
|
},
|
|
|
|
|
2018-04-18 15:30:41 -04: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 04:42:06 -04:00
|
|
|
actions: {
|
|
|
|
changePeriod(period) {
|
|
|
|
DiscourseURL.routeTo(this._reportsForPeriodURL(period));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_reportsForPeriodURL(period) {
|
2018-04-17 05:01:06 -04:00
|
|
|
return `/admin/dashboard-next?period=${period}`;
|
2018-04-16 04:42:06 -04:00
|
|
|
}
|
|
|
|
});
|