From b4a724e80a9099efeb9cc8b4874966142b60dd80 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Tue, 15 Sep 2015 16:45:01 -0400 Subject: [PATCH] FEATURE: export dashboard reports to csv file --- .../admin/controllers/admin-reports.js.es6 | 12 ++++++++++++ .../javascripts/admin/templates/reports.hbs | 1 + .../javascripts/discourse/lib/export-csv.js.es6 | 8 ++++---- app/controllers/export_csv_controller.rb | 16 +++++++++++----- app/jobs/regular/export_csv_file.rb | 12 ++++++++++++ 5 files changed, 40 insertions(+), 9 deletions(-) diff --git a/app/assets/javascripts/admin/controllers/admin-reports.js.es6 b/app/assets/javascripts/admin/controllers/admin-reports.js.es6 index e964d17a257..d83a031c6bc 100644 --- a/app/assets/javascripts/admin/controllers/admin-reports.js.es6 +++ b/app/assets/javascripts/admin/controllers/admin-reports.js.es6 @@ -1,3 +1,6 @@ +import { exportEntity } from 'discourse/lib/export-csv'; +import { outputExportResult } from 'discourse/lib/export-result'; + export default Ember.Controller.extend({ viewMode: 'table', viewingTable: Em.computed.equal('viewMode', 'table'), @@ -30,6 +33,15 @@ export default Ember.Controller.extend({ viewAsBarChart() { this.set('viewMode', 'barChart'); + }, + + exportCsv() { + exportEntity('report', { + name: this.get("model.type"), + start_date: this.get('startDate'), + end_date: this.get('endDate'), + category_id: this.get('categoryId') == 'all' ? undefined : this.get('categoryId') + }).then(outputExportResult); } } }); diff --git a/app/assets/javascripts/admin/templates/reports.hbs b/app/assets/javascripts/admin/templates/reports.hbs index c7383ef2c8a..5e1b406ea6a 100644 --- a/app/assets/javascripts/admin/templates/reports.hbs +++ b/app/assets/javascripts/admin/templates/reports.hbs @@ -5,6 +5,7 @@ {{i18n 'admin.dashboard.reports.end_date'}} {{input type="date" value=endDate}} {{combo-box valueAttribute="value" content=categoryOptions value=categoryId}} {{d-button action="refreshReport" class="btn-primary" label="admin.dashboard.reports.refresh_report" icon="refresh"}} + {{d-button action="exportCsv" label="admin.export_csv.button_text"}}
diff --git a/app/assets/javascripts/discourse/lib/export-csv.js.es6 b/app/assets/javascripts/discourse/lib/export-csv.js.es6 index 9681c9fc5a9..c4b74a08fb0 100644 --- a/app/assets/javascripts/discourse/lib/export-csv.js.es6 +++ b/app/assets/javascripts/discourse/lib/export-csv.js.es6 @@ -1,7 +1,7 @@ -function exportEntityByType(type, entity) { +function exportEntityByType(type, entity, args) { return Discourse.ajax("/export_csv/export_entity.json", { method: 'POST', - data: {entity_type: type, entity} + data: {entity_type: type, entity, args} }); } @@ -14,6 +14,6 @@ export function exportUserArchive() { } -export function exportEntity(entity) { - return exportEntityByType('admin', entity); +export function exportEntity(entity, args) { + return exportEntityByType('admin', entity, args); } diff --git a/app/controllers/export_csv_controller.rb b/app/controllers/export_csv_controller.rb index 74e341168cd..f632b93bee6 100644 --- a/app/controllers/export_csv_controller.rb +++ b/app/controllers/export_csv_controller.rb @@ -3,11 +3,8 @@ class ExportCsvController < ApplicationController skip_before_filter :preload_json, :check_xhr, only: [:show] def export_entity - params.require(:entity) - params.require(:entity_type) - guardian.ensure_can_export_entity!(params[:entity_type]) - - Jobs.enqueue(:export_csv_file, entity: params[:entity], user_id: current_user.id) + guardian.ensure_can_export_entity!(export_params[:entity_type]) + Jobs.enqueue(:export_csv_file, entity: export_params[:entity], user_id: current_user.id, args: export_params[:args]) render json: success_json end @@ -27,4 +24,13 @@ class ExportCsvController < ApplicationController end end + private + + def export_params + @_export_params ||= begin + params.require(:entity) + params.require(:entity_type) + params.permit(:entity, :entity_type, args: [:name, :start_date, :end_date, :category_id]) + end + end end diff --git a/app/jobs/regular/export_csv_file.rb b/app/jobs/regular/export_csv_file.rb index 8e4201453d8..d3d2d3da891 100644 --- a/app/jobs/regular/export_csv_file.rb +++ b/app/jobs/regular/export_csv_file.rb @@ -15,12 +15,14 @@ module Jobs HEADER_ATTRS_FOR['screened_email'] = ['email','action','match_count','last_match_at','created_at','ip_address'] HEADER_ATTRS_FOR['screened_ip'] = ['ip_address','action','match_count','last_match_at','created_at'] HEADER_ATTRS_FOR['screened_url'] = ['domain','action','match_count','last_match_at','created_at'] + HEADER_ATTRS_FOR['report'] = ['date', 'value'] sidekiq_options retry: false attr_accessor :current_user def execute(args) @entity = args[:entity] + @extra = HashWithIndifferentAccess.new(args[:args]) if args[:args] @file_name = @entity @current_user = User.find_by(id: args[:user_id]) @@ -93,6 +95,16 @@ module Jobs end end + def report_export + @extra[:start_date] = @extra[:start_date].to_date if @extra[:start_date].is_a?(String) + @extra[:end_date] = @extra[:end_date].to_date if @extra[:end_date].is_a?(String) + @extra[:category_id] = @extra[:category_id].to_i if @extra[:category_id] + r = Report.find(@extra[:name], @extra) + r.data.map do |row| + [row[:x].to_s(:db), row[:y].to_s(:db)] + end + end + def get_header case @entity