FEATURE: export dashboard reports to csv file

This commit is contained in:
Neil Lalonde 2015-09-15 16:45:01 -04:00
parent b12ace5f9d
commit b4a724e80a
5 changed files with 40 additions and 9 deletions

View File

@ -1,3 +1,6 @@
import { exportEntity } from 'discourse/lib/export-csv';
import { outputExportResult } from 'discourse/lib/export-result';
export default Ember.Controller.extend({ export default Ember.Controller.extend({
viewMode: 'table', viewMode: 'table',
viewingTable: Em.computed.equal('viewMode', 'table'), viewingTable: Em.computed.equal('viewMode', 'table'),
@ -30,6 +33,15 @@ export default Ember.Controller.extend({
viewAsBarChart() { viewAsBarChart() {
this.set('viewMode', 'barChart'); 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);
} }
} }
}); });

View File

@ -5,6 +5,7 @@
{{i18n 'admin.dashboard.reports.end_date'}} {{input type="date" value=endDate}} {{i18n 'admin.dashboard.reports.end_date'}} {{input type="date" value=endDate}}
{{combo-box valueAttribute="value" content=categoryOptions value=categoryId}} {{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="refreshReport" class="btn-primary" label="admin.dashboard.reports.refresh_report" icon="refresh"}}
{{d-button action="exportCsv" label="admin.export_csv.button_text"}}
</div> </div>
<div class='view-options'> <div class='view-options'>

View File

@ -1,7 +1,7 @@
function exportEntityByType(type, entity) { function exportEntityByType(type, entity, args) {
return Discourse.ajax("/export_csv/export_entity.json", { return Discourse.ajax("/export_csv/export_entity.json", {
method: 'POST', method: 'POST',
data: {entity_type: type, entity} data: {entity_type: type, entity, args}
}); });
} }
@ -14,6 +14,6 @@ export function exportUserArchive() {
} }
export function exportEntity(entity) { export function exportEntity(entity, args) {
return exportEntityByType('admin', entity); return exportEntityByType('admin', entity, args);
} }

View File

@ -3,11 +3,8 @@ class ExportCsvController < ApplicationController
skip_before_filter :preload_json, :check_xhr, only: [:show] skip_before_filter :preload_json, :check_xhr, only: [:show]
def export_entity def export_entity
params.require(:entity) guardian.ensure_can_export_entity!(export_params[:entity_type])
params.require(:entity_type) Jobs.enqueue(:export_csv_file, entity: export_params[:entity], user_id: current_user.id, args: export_params[:args])
guardian.ensure_can_export_entity!(params[:entity_type])
Jobs.enqueue(:export_csv_file, entity: params[:entity], user_id: current_user.id)
render json: success_json render json: success_json
end end
@ -27,4 +24,13 @@ class ExportCsvController < ApplicationController
end end
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 end

View File

@ -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_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_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['screened_url'] = ['domain','action','match_count','last_match_at','created_at']
HEADER_ATTRS_FOR['report'] = ['date', 'value']
sidekiq_options retry: false sidekiq_options retry: false
attr_accessor :current_user attr_accessor :current_user
def execute(args) def execute(args)
@entity = args[:entity] @entity = args[:entity]
@extra = HashWithIndifferentAccess.new(args[:args]) if args[:args]
@file_name = @entity @file_name = @entity
@current_user = User.find_by(id: args[:user_id]) @current_user = User.find_by(id: args[:user_id])
@ -93,6 +95,16 @@ module Jobs
end end
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 def get_header
case @entity case @entity