aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-server/src/main/webapp/WEB-INF/app/controllers/drilldown_controller.rb
blob: 848e046f37196df76ce8d04b0dac02e4860be722 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#
# Sonar, open source software quality management tool.
# Copyright (C) 2008-2012 SonarSource
# mailto:contact AT sonarsource DOT com
#
# Sonar is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
#
# Sonar is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with Sonar; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
#
class DrilldownController < ApplicationController
  before_filter :init_resource_for_user_role

  helper ProjectHelper, DashboardHelper

  SECTION=Navigation::SECTION_RESOURCE

  def measures
    @metric = select_metric(params[:metric], 'ncloc')
    @highlighted_metric = Metric.by_key(params[:highlight]) || @metric

    # selected resources
    if params[:rids]
      selected_rids= params[:rids]
    elsif params[:resource]
      highlighted_resource=Project.by_key(params[:resource])
      selected_rids=(highlighted_resource ? [highlighted_resource.id] : [])
    else
      selected_rids=[]
    end
    selected_rids=selected_rids.map { |r| r.to_i }


    # options
    options={}
    if params[:characteristic_id]
      @characteristic=Characteristic.find(params[:characteristic_id])
    elsif params[:model] && params[:characteristic]
      @characteristic=Characteristic.find(:first, :select => 'id', :include => 'quality_model', :conditions => ['quality_models.name=? AND characteristics.kee=? AND characteristics.enabled=?', params[:model], params[:characteristic], true])
    end
    options[:characteristic]=@characteristic
    if params[:period]
      @period=params[:period].to_i
      options[:period]=@period
    end

    # load data
    @drilldown = Drilldown.new(@resource, @metric, selected_rids, options)

    @highlighted_resource=@drilldown.highlighted_resource
    if @highlighted_resource.nil? && @drilldown.columns.empty?
      @highlighted_resource=@resource
    end

    @display_viewers=display_metric_viewers?(@highlighted_resource||@resource, @highlighted_metric.key)
  end

  def violations
    @rule=Rule.by_key_or_id(params[:rule])

    # variation measures
    if params[:period].present? && params[:period].to_i>0
      @period=params[:period].to_i
      metric_prefix = 'new_'
    else
      @period=nil
      metric_prefix = ''
    end

    @severity = params[:severity] || params[:priority]
    @rule_severity = params[:rule_sev] || @severity

    if @rule && @rule_severity.blank?
      # workaround for SONAR-3255 : guess the severity
      @rule_severity=guess_rule_severity(@snapshot, @rule, metric_prefix)
    end


    if @rule_severity.present?
      # Filter resources by severity
      @metric = Metric::by_key("#{metric_prefix}#{@rule_severity.downcase}_violations")
    else
      @metric = Metric::by_key("#{metric_prefix}violations")
    end

    # selected resources
    if params[:rids]
      @selected_rids= params[:rids]
    elsif params[:resource]
      highlighted_resource=Project.by_key(params[:resource])
      @selected_rids=(highlighted_resource ? [highlighted_resource.id] : [])
    else
      @selected_rids=[]
    end
    @selected_rids=@selected_rids.map { |r| r.to_i }


    # options for Drilldown
    options={:exclude_zero_value => true, :period => @period}
    if @rule
      params[:rule]=@rule.key # workaround for SONAR-1767 : the javascript hash named "rp" in the HTML source must contain the rule key, but not the rule id
      options[:rule_id]=@rule.id
    end

    # load data
    @drilldown = Drilldown.new(@resource, @metric, @selected_rids, options)

    @highlighted_resource=@drilldown.highlighted_resource
    if @highlighted_resource.nil? && @drilldown.columns.empty?
      @highlighted_resource=@resource
    end


    #
    # Initialize filter by rule
    #
    if @severity.present?
      # Filter on severity -> filter rule measures by the selected metric
      @rule_measures = @snapshot.rule_measures(@metric)
    else
      # No filter -> loads all the rules
      metrics=[
        Metric.by_key("#{metric_prefix}blocker_violations"),
        Metric.by_key("#{metric_prefix}critical_violations"),
        Metric.by_key("#{metric_prefix}major_violations"),
        Metric.by_key("#{metric_prefix}minor_violations"),
        Metric.by_key("#{metric_prefix}info_violations")
      ]
      @rule_measures = @snapshot.rule_measures(metrics)
    end

    @display_viewers=display_violation_viewers?(@drilldown.highlighted_snapshot || @snapshot)
  end

  private

  def select_metric(metric_key, default_key)
    metric=nil
    if metric_key
      metric=Metric::by_key(metric_key)
    end
    if metric.nil?
      metric=Metric::by_key(default_key)
    end
    metric
  end

  def select_subsnapshot(snapshot, sid)
    if sid
      snapshot.children.each do |subsnapshot|
        return subsnapshot if subsnapshot.id==sid.to_i
      end
    end
    nil
  end

  def array_to_hash_by_id(array)
    hash={}
    array.each do |s|
      hash[s.id]=s
    end
    hash
  end

  def display_metric_viewers?(resource, metric_key)
    return true if resource.file?
    java_facade.getResourceTabsForMetric(resource.scope, resource.qualifier, resource.language, metric_key).each do |tab|
      tab.getUserRoles().each do |role|
        if has_role?(role, resource)
          return true
        end
      end
    end
    false
  end

  def display_violation_viewers?(snapshot)
    return true if snapshot.file?
    snapshot.violations.size>0
  end

  def guess_rule_severity(snapshot, rule, metric_prefix)
    Severity::KEYS.each do |severity|
      if snapshot.rule_measure(Metric.by_key("#{metric_prefix}#{severity.downcase}_violations"), rule)
        return severity
      end
      Severity::MAJOR
    end
  end
end