csvexport.csv_export_controller.rb Maven / Gradle / Ivy
The newest version!
require 'fastercsv'
class Api::CsvExportWebServiceController < Api::ApiController
# GET /api/csv_export?resource=&qualifiers=&metrics=&includerules=
def index
resource=Project.by_key(params[:resource])
not_found('Resource not found') unless resource
snapshot=resource.last_snapshot
not_found('Snapshot not found') unless snapshot
access_denied unless has_role?(:user, snapshot)
@includerules=(params[:includerules]=='true')
if params[:qualifiers].present?
# load descendants
snapshot_conditions=['snapshots.islast=:islast']
snapshot_values={:islast => true}
snapshot_conditions << '(snapshots.id=:sid OR (snapshots.root_snapshot_id=:root_sid AND snapshots.path LIKE :path))'
snapshot_values[:sid]=snapshot.id
snapshot_values[:root_sid] = (snapshot.root_snapshot_id || snapshot.id)
snapshot_values[:path]="#{snapshot.path}#{snapshot.id}.%"
if params[:qualifiers].present?
snapshot_conditions << 'snapshots.qualifier in (:qualifiers)'
snapshot_values[:qualifiers]=params['qualifiers'].split(',')
end
# optimization: add condition on projects
conditions=snapshot_conditions.join(' AND ') + ' AND projects.qualifier IN (:qualifiers)'
@snapshots=Snapshot.find(:all, :conditions => [conditions, snapshot_values], :include => :project, :order => 'projects.long_name')
else
snapshot_conditions=['snapshots.id=:sid']
snapshot_values={:sid => snapshot.id}
@snapshots=[snapshot]
end
@metrics=[]
@measures_hash_by_sid={}
@violations_hash_by_sid={}
if params['metrics']
params['metrics'].each do |metric_key|
@metrics< :snapshot,
:select => select_columns_for_measures(),
:conditions => [(snapshot_conditions + measure_conditions).join(' AND '), snapshot_values.merge(measure_values)])
measures.each do |measure|
if measure.rule_id
@violations_hash_by_sid[measure.snapshot_id]||={}
@violations_hash_by_sid[measure.snapshot_id][measure.rule_id]=measure.value.to_i
elsif measure.rule_priority.nil?
@measures_hash_by_sid[measure.snapshot_id]||={}
@measures_hash_by_sid[measure.snapshot_id][measure.metric_id]=measure
end
end
end
@rules=[]
if @includerules
# Include rules from the profile
profile_measure=snapshot.root_snapshot.measure('profile')
profile=Profile.find(:first, :include => {:active_rules => :rule}, :conditions => ['id=?', profile_measure.value.to_i]) if profile_measure && profile_measure.value
if profile
@rules=profile.active_rules.map { |ar| ar.rule }.sort_by { |r| r.name }
end
# And potential manual rules
@rules += Rule.manual_rules()
@rules = Api::Utils.insensitive_sort(@rules) {|rule| rule.name}
end
respond_to do |format|
format.csv {
send_data(to_csv, :type => 'text/csv; charset=utf-8; header=present', :disposition => 'attachment; filename=sonar.csv')
}
end
end
private
def select_columns_for_measures
'project_measures.id,project_measures.value,project_measures.text_value,project_measures.metric_id,project_measures.snapshot_id,project_measures.rule_id,project_measures.rule_priority'
end
def to_csv
FasterCSV.generate do |csv|
header = ['Name']
@metrics.each do |metric|
header<