From 9d97e1244ebebe3d83ae2467cd0411300fab87da Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 14 May 2018 11:12:52 +1000 Subject: [PATCH] correct it so when we have no dates we still return graphs --- .../admin/components/dashboard-mini-chart.js.es6 | 3 +-- app/assets/javascripts/admin/mixins/async-report.js.es6 | 1 - app/assets/javascripts/admin/models/report.js.es6 | 7 ++++--- app/assets/javascripts/discourse/lib/utilities.js.es6 | 2 +- app/jobs/regular/retrieve_report.rb | 2 +- app/models/report.rb | 4 ++-- 6 files changed, 9 insertions(+), 10 deletions(-) diff --git a/app/assets/javascripts/admin/components/dashboard-mini-chart.js.es6 b/app/assets/javascripts/admin/components/dashboard-mini-chart.js.es6 index d1885cddcb8..02f4ad39433 100644 --- a/app/assets/javascripts/admin/components/dashboard-mini-chart.js.es6 +++ b/app/assets/javascripts/admin/components/dashboard-mini-chart.js.es6 @@ -83,7 +83,6 @@ export default Ember.Component.extend(AsyncReport, { ajax(this.get("dataSource"), payload) .then((response) => { this.set('reportKey', response.report.report_key); - this.loadReport(response.report); }) .finally(() => { @@ -100,7 +99,7 @@ export default Ember.Component.extend(AsyncReport, { }, loadReport(report) { - if (report.data) { + if (_.isArray(report.data)) { Report.fillMissingDates(report); if (report.data && report.data.length > 40) { diff --git a/app/assets/javascripts/admin/mixins/async-report.js.es6 b/app/assets/javascripts/admin/mixins/async-report.js.es6 index 31c9c501895..96f541935ad 100644 --- a/app/assets/javascripts/admin/mixins/async-report.js.es6 +++ b/app/assets/javascripts/admin/mixins/async-report.js.es6 @@ -14,7 +14,6 @@ export default Ember.Mixin.create({ Em.run.next(() => { if (report.report_key = this.get("reportKey")) { this.loadReport(report); - console.log(report); this.set("isLoading", false); this.renderReport(); } diff --git a/app/assets/javascripts/admin/models/report.js.es6 b/app/assets/javascripts/admin/models/report.js.es6 index 6d32554aff4..557657c84eb 100644 --- a/app/assets/javascripts/admin/models/report.js.es6 +++ b/app/assets/javascripts/admin/models/report.js.es6 @@ -97,7 +97,7 @@ const Report = Discourse.Model.extend({ @computed('data', 'currentTotal') currentAverage(data, total) { - return parseFloat((total / parseFloat(data.length)).toFixed(1)); + return data.length === 0 ? 0 : parseFloat((total / parseFloat(data.length)).toFixed(1)); }, @computed('prev_period', 'currentTotal', 'currentAverage') @@ -172,7 +172,7 @@ const Report = Discourse.Model.extend({ let percent = this.percentChangeString(current, prev); if (this.get('average')) { - prev = prev.toFixed(1); + prev = prev ? prev.toFixed(1) : "0"; current += '%'; prev += '%'; } @@ -219,7 +219,8 @@ const Report = Discourse.Model.extend({ Report.reopenClass({ fillMissingDates(report) { - if (report.data.length > 0) { + if (_.isArray(report.data)) { + const startDateFormatted = moment.utc(report.start_date).format('YYYY-MM-DD'); const endDateFormatted = moment.utc(report.end_date).format('YYYY-MM-DD'); report.data = fillMissingDates(report.data, startDateFormatted, endDateFormatted); diff --git a/app/assets/javascripts/discourse/lib/utilities.js.es6 b/app/assets/javascripts/discourse/lib/utilities.js.es6 index 0e14caabc95..91a48e0de6e 100644 --- a/app/assets/javascripts/discourse/lib/utilities.js.es6 +++ b/app/assets/javascripts/discourse/lib/utilities.js.es6 @@ -494,7 +494,7 @@ export function fillMissingDates(data, startDate, endDate) { for (let i = 0; i <= countDays; i++) { let date = (data[i]) ? moment(data[i].x, "YYYY-MM-DD") : null; - if (i === 0 && date.isAfter(startMoment)) { + if (i === 0 && (!date || date.isAfter(startMoment))) { data.splice(i, 0, { "x" : startMoment.format("YYYY-MM-DD"), 'y': 0 }); } else { if (!date || date.isAfter(moment(currentMoment))) { diff --git a/app/jobs/regular/retrieve_report.rb b/app/jobs/regular/retrieve_report.rb index 9a7f26ccdb4..54cac2b8312 100644 --- a/app/jobs/regular/retrieve_report.rb +++ b/app/jobs/regular/retrieve_report.rb @@ -16,8 +16,8 @@ module Jobs report.facets = args['facets'].map(&:to_sym) if args['facets'] Report.send("report_#{type}", report) - json = report.as_json + Discourse.cache.write(Report.cache_key(report), json, force: true, expires_in: 30.minutes) MessageBus.publish("/admin/reports/#{type}", json, user_ids: User.staff.pluck(:id)) diff --git a/app/models/report.rb b/app/models/report.rb index 8abfd668e17..9876610426a 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -42,8 +42,8 @@ class Report yaxis: I18n.t("reports.#{type}.yaxis"), description: I18n.t("reports.#{type}.description"), data: data, - start_date: start_date, - end_date: end_date, + start_date: start_date&.iso8601, + end_date: end_date&.iso8601, category_id: category_id, group_id: group_id, prev30Days: self.prev30Days,