From 0a27086764a90e62c0b598c390c50dcc4974daad Mon Sep 17 00:00:00 2001 From: Bianca Nenciu Date: Tue, 21 Jan 2020 18:43:19 +0200 Subject: [PATCH] FEATURE: Export all types of reports (#8748) There is a single stacked_chart which was not exportable --- .../javascripts/admin/models/report.js.es6 | 5 ----- .../templates/components/admin-report.hbs | 18 +++++++-------- app/jobs/regular/export_csv_file.rb | 19 +++++++++++++++- spec/jobs/export_csv_file_spec.rb | 22 +++++++++++++++++++ 4 files changed, 48 insertions(+), 16 deletions(-) diff --git a/app/assets/javascripts/admin/models/report.js.es6 b/app/assets/javascripts/admin/models/report.js.es6 index 91a5f54002b..bd7074ced2a 100644 --- a/app/assets/javascripts/admin/models/report.js.es6 +++ b/app/assets/javascripts/admin/models/report.js.es6 @@ -22,11 +22,6 @@ const Report = EmberObject.extend({ percent: false, higher_is_better: true, - @discourseComputed("modes") - isTable(modes) { - return modes.some(mode => mode === "table"); - }, - @discourseComputed("type", "start_date", "end_date") reportUrl(type, start_date, end_date) { start_date = moment diff --git a/app/assets/javascripts/admin/templates/components/admin-report.hbs b/app/assets/javascripts/admin/templates/components/admin-report.hbs index 8d9729dc82b..bdc3b5b12a7 100644 --- a/app/assets/javascripts/admin/templates/components/admin-report.hbs +++ b/app/assets/javascripts/admin/templates/components/admin-report.hbs @@ -160,17 +160,15 @@ {{/each}} - {{#if model.isTable}} -
-
- {{d-button - class="btn-default export-csv-btn" - action=(action "exportCsv") - label="admin.export_csv.button_text" - icon="download"}} -
+
+
+ {{d-button + class="btn-default export-csv-btn" + action=(action "exportCsv") + label="admin.export_csv.button_text" + icon="download"}}
- {{/if}} +
{{#if showRefresh}}
diff --git a/app/jobs/regular/export_csv_file.rb b/app/jobs/regular/export_csv_file.rb index 2ab94729cf2..6798d683a2b 100644 --- a/app/jobs/regular/export_csv_file.rb +++ b/app/jobs/regular/export_csv_file.rb @@ -198,8 +198,25 @@ module Jobs end end + if report.modes == [:stacked_chart] + header = [:x] + data = {} + + report.data.map do |series| + header << series[:label] + series[:data].each do |datapoint| + data[datapoint[:x]] ||= { x: datapoint[:x] } + data[datapoint[:x]][series[:label]] = datapoint[:y] + end + end + + data = data.values + else + data = report.data + end + yield header.map { |k| titles[k] || k } - report.data.each { |row| yield row.values_at(*header).map(&:to_s) } + data.each { |row| yield row.values_at(*header).map(&:to_s) } end def get_header diff --git a/spec/jobs/export_csv_file_spec.rb b/spec/jobs/export_csv_file_spec.rb index 79bdff73361..9c4b04e5acf 100644 --- a/spec/jobs/export_csv_file_spec.rb +++ b/spec/jobs/export_csv_file_spec.rb @@ -89,6 +89,28 @@ describe Jobs::ExportCsvFile do expect(report.second).to contain_exactly(user.username, "Earth", "2010-01-01 00:00:00 UTC") end + it 'works with stacked_chart reports' do + ApplicationRequest.create!(date: '2010-01-01', req_type: 'page_view_logged_in', count: 1) + ApplicationRequest.create!(date: '2010-01-02', req_type: 'page_view_logged_in', count: 2) + ApplicationRequest.create!(date: '2010-01-03', req_type: 'page_view_logged_in', count: 3) + + ApplicationRequest.create!(date: '2010-01-01', req_type: 'page_view_anon', count: 4) + ApplicationRequest.create!(date: '2010-01-02', req_type: 'page_view_anon', count: 5) + ApplicationRequest.create!(date: '2010-01-03', req_type: 'page_view_anon', count: 6) + + ApplicationRequest.create!(date: '2010-01-01', req_type: 'page_view_crawler', count: 7) + ApplicationRequest.create!(date: '2010-01-02', req_type: 'page_view_crawler', count: 8) + ApplicationRequest.create!(date: '2010-01-03', req_type: 'page_view_crawler', count: 9) + + exporter.instance_variable_get(:@extra)['name'] = 'consolidated_page_views' + report = exporter.report_export.to_a + + expect(report[0]).to contain_exactly("Day", "Logged in users", "Anonymous users", "Crawlers") + expect(report[1]).to contain_exactly("2010-01-01", "1", "4", "7") + expect(report[2]).to contain_exactly("2010-01-02", "2", "5", "8") + expect(report[3]).to contain_exactly("2010-01-03", "3", "6", "9") + end + end let(:user_list_header) {