aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-server/src/main/webapp/WEB-INF/app/models/metric.rb
blob: 8212fe13a1ce24d579bf89990f937d8c744e7888 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#
# Sonar, entreprise quality control tool.
# Copyright (C) 2008-2011 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 Metric < ActiveRecord::Base

  DOMAIN_RULES = 'Rules'

  VALUE_TYPE_INT = 'INT'
  VALUE_TYPE_BOOLEAN = 'BOOL'
  VALUE_TYPE_FLOAT = 'FLOAT'
  VALUE_TYPE_PERCENT = 'PERCENT'
  VALUE_TYPE_STRING = 'STRING'
  VALUE_TYPE_DATA = 'DATA'
  VALUE_TYPE_MILLISEC = 'MILLISEC'
  VALUE_TYPE_LEVEL = 'LEVEL'
  VALUE_TYPE_DISTRIB = 'DISTRIB'
  VALUE_TYPE_RATING = 'RATING'

  TYPE_LEVEL_OK = 'OK'
  TYPE_LEVEL_WARN = 'WARN'
  TYPE_LEVEL_ERROR = 'ERROR'

  ORIGIN_GUI='GUI'
  ORIGIN_JAVA='JAV'

  CACHE_KEY='metrics'
  I18N_DOMAIN_CACHE_KEY='i18n_domains'
  I18N_SHORT_NAME_CACHE_KEY='i18n_metric_short_names'
  
  validates_length_of       :name, :within => 1..64
  validates_uniqueness_of   :name
  validates_length_of       :short_name, :within => 1..64
  validates_inclusion_of    :val_type, :in => [VALUE_TYPE_INT,VALUE_TYPE_BOOLEAN,VALUE_TYPE_FLOAT,VALUE_TYPE_PERCENT,VALUE_TYPE_STRING,VALUE_TYPE_MILLISEC,VALUE_TYPE_LEVEL, VALUE_TYPE_DATA, VALUE_TYPE_DISTRIB], :message => "wrong value type"


  @@metrics_type_names = { VALUE_TYPE_INT => 'Integer', VALUE_TYPE_FLOAT => 'Float', VALUE_TYPE_PERCENT => 'Percent',
    VALUE_TYPE_BOOLEAN => 'Yes/No', VALUE_TYPE_STRING => 'Text', VALUE_TYPE_LEVEL => 'Level' }

  attr_accessible :name, :description, :direction, :domain, :short_name, :qualitative, :val_type, :user_managed

  def ==(other)
    name==other.name
  end

  def <=>(other)
    short_name<=>other.short_name
  end

  def self.domains(translate=true)
    all.collect{|metric|
      metric.domain(translate)
    }.compact.uniq.sort
  end

  # Localized domain name
  def self.domain_for(domain_key)
    return nil if domain_key.nil?
    
    localeMap = Metric.i18n_domain_cache[domain_key]
    locale = I18n.locale
    
    return localeMap[locale] if localeMap && localeMap.has_key?(locale)
    
    i18n_key = 'metric_domain.' + domain_key
    result = Api::Utils.message(i18n_key, :default => domain_key)
    localeMap[locale] = result if localeMap
    result
  end

  def self.name_for(metric_key)
    m=by_key(metric_key)
    m ? m.short_name : nil
  end
 
  def key
    name
  end

  def domain(translate=true)
    default_string = read_attribute(:domain)
    return default_string unless translate
    Metric.domain_for(default_string)
  end
 
  def domain=(value)
    write_attribute(:domain, value)
  end
 
  def short_name(translate=true)
    default_string = read_attribute(:short_name)
    return default_string unless translate
    
    metric_key = read_attribute(:name)
    return nil if metric_key.nil?
    
    localeMap = Metric.i18n_short_name_cache[metric_key]
    locale = I18n.locale
    
    return localeMap[locale] if localeMap && localeMap.has_key?(locale)
    
    i18n_key = 'metric.' + metric_key + '.name'
    result = Api::Utils.message(i18n_key, :default => default_string)
    localeMap[locale] = result if localeMap
    result
  end
  
  def short_name=(value)
    write_attribute(:short_name, value)
  end
 
  def description(translate=true)
    default_string = read_attribute(:description)
    return default_string unless translate

    metric_name = read_attribute(:name)
      
    return nil if metric_name.nil?
    
    i18n_key = 'metric.' + metric_name + '.description'
    result = Api::Utils.message(i18n_key, :default => default_string)
    result
  end

  def description=(value)
    write_attribute(:description, value)
  end
 
  def user_managed?
    user_managed==true
  end

  def value_type
    val_type
  end

  def numeric?
    val_type==VALUE_TYPE_INT || val_type==VALUE_TYPE_FLOAT || val_type==VALUE_TYPE_PERCENT || val_type==VALUE_TYPE_MILLISEC || val_type==VALUE_TYPE_RATING
  end

  def data?
    val_type==VALUE_TYPE_DATA || val_type==VALUE_TYPE_DISTRIB
  end

  def quantitative?
    !qualitative?
  end
  
  def qualitative?
    qualitative
  end

  def hidden
    read_attribute(:hidden)
  end

  def value_type_name
    @@metrics_type_names[value_type]
  end

  def self.value_type_names
    # return a copy of the hash
    metrics_type_names_clone = {}
    @@metrics_type_names.each_pair {|type, descr|
      metrics_type_names_clone[type] = descr
    }
    metrics_type_names_clone
  end

  def timemachine?
    enabled && !hidden && (val_type != VALUE_TYPE_DATA)
  end

  def display?
    enabled && !data? && !hidden
  end

  def alertable?
    (!data?) && (!hidden) && (key!=Metric::ALERT_STATUS) && (val_type != VALUE_TYPE_RATING)
  end

  def suffix
    case val_type
      when VALUE_TYPE_PERCENT
        '%'
      when VALUE_TYPE_MILLISEC
        'ms'
      else ''
    end
  end

  def self.all
    cache['ALL']
  end

  def self.by_id(id)
    cache[id.to_s]
  end

  def self.by_name(key)
    cache[key]
  end

  def self.by_key(key)
    cache[key.to_s]
  end

  def self.by_names(names)
    metrics = names.map do |name|
      by_name(name)
    end
    metrics
  end

  def self.by_domain(domain, translate=true)
    all.select{|metric| metric.domain(translate)==domain}.sort
  end

  def self.major_metrics_id
    all.collect {|m| m.id}
  end

  def self.clear_cache
    Caches.clear(CACHE_KEY)
    Caches.clear(I18N_DOMAIN_CACHE_KEY)
    Caches.clear(I18N_SHORT_NAME_CACHE_KEY)
  end

  def self.default_time_machine_metrics
    [COMPLEXITY, COVERAGE, VIOLATIONS_DENSITY]
  end

  def self.by_keys(keys)
    result=[]
    keys.each do |k|
      result<<by_key(k)
    end
    result.compact.uniq
  end

  def self.ids_from_keys(keys_array)
    keys_array.collect{ |key| Metric.by_name(key).id if Metric.by_name(key) }
  end

  def to_hash_json(options={})
    return {'key' => name, 'name' => short_name, 'description' => description, 'domain' => domain,
      'qualitative' => qualitative, 'user_managed' => self.user_managed,
      'direction' => direction.to_i, 'val_type' => val_type.to_s, 'hidden' => hidden}
  end

  def treemap_color?
    enabled && !hidden && ((numeric? && worst_value && best_value) || val_type==Metric::VALUE_TYPE_LEVEL)
  end

  def treemap_size?
    enabled && !hidden && numeric? && !domain.blank?
  end

  # temporary method since 2.7. Will replace it by a field in database.
  def on_new_code?
    @on_new_code ||=
      begin
        key.start_with?('new_')
      end
  end

  def to_xml(options={})
    xml = Builder::XmlMarkup.new
    xml.metric do
      xml.key(name)
      xml.name(short_name)
      xml.description(description)
      xml.domain(domain)
      xml.qualitative(qualitative)
      xml.direction(direction)
      xml.user_managed(self.user_managed)
      xml.val_type(val_type)
      xml.hidden(hidden)
    end
  end

  def created_online?
    origin==ORIGIN_GUI
  end

  def updatable_online?
    origin!=ORIGIN_JAVA
  end

  # METRIC DEFINITIONS
  # WARNING if you edit this file do not forget to also change sonar-commons/src/main/java/ch/hortis/sonar/model/Metrics.java
  LINES = 'lines'
  NCLOC = 'ncloc'
  CLASSES = 'classes'
  PACKAGES = 'packages'
  FILES = 'files'
  FUNCTIONS = 'functions'
  DIRECTORIES = 'directories'
  ACCESSORS = 'accessors'
  PUBLIC_API = 'public_api'
  
  COMPLEXITY = 'complexity'
  STATEMENTS = 'statements'
  AVG_CMPX_BY_CLASS = 'class_complexity'
  AVG_CMPX_BY_FUNCTION = 'function_complexity'
  AVG_CMPX_BY_FILE = 'file_complexity'
  CLASSES_CMPX_DISTRIBUTION = 'class_complexity_distribution'
  FUNCTIONS_CMPX_DISTRIBUTION = 'function_complexity_distribution'

  DUPLICATED_FILES = 'duplicated_files'
  DUPLICATED_LINES = 'duplicated_lines'
  DUPLICATED_BLOCKS = 'duplicated_blocks'
  DUPLICATED_LINES_DENSITY = 'duplicated_lines_density'

  TESTS = 'tests'
  TEST_ERRORS = 'test_errors'
  SKIPPED_TESTS = 'skipped_tests'
  TEST_FAILURES = 'test_failures'
  TEST_EXECUTION_TIME = 'test_execution_time'
  TEST_SUCCESS_DENSITY = 'test_success_density'
  COVERAGE = 'coverage'
  LINE_COVERAGE = 'line_coverage'
  BRANCH_COVERAGE = 'branch_coverage'
  UNCOVERED_LINES='uncovered_lines'
  UNCOVERED_CONDITIONS='uncovered_conditions'
  
  VIOLATIONS = 'violations'
  VIOLATIONS_DENSITY = 'violations_density'
  WEIGHTED_VIOLATIONS = 'weighted_violations'
  BLOCKER_VIOLATIONS='blocker_violations'
  CRITICAL_VIOLATIONS='critical_violations'
  MAJOR_VIOLATIONS='major_violations'
  MINOR_VIOLATIONS='minor_violations'
  INFO_VIOLATIONS='info_violations'

  MAINTAINABILITY='maintainability'
  EFFICIENCY='efficiency'
  RELIABILITY='reliability'
  USABILITY='usability'
  PORTABILITY='portability'

  COMMENT_LINES_DENSITY = 'comment_lines_density'
  COMMENT_LINES = 'comment_lines'
  PUBLIC_DOCUMENTED_API_DENSITY = 'public_documented_api_density'
  PUBLIC_UNDOCUMENTED_API = 'public_undocumented_api'
  COMMENTED_OUT_CODE_LINES = 'commented_out_code_lines'

  ALERT_STATUS = 'alert_status'
  PROFILE='profile'

  private

  def self.cache
    c = Caches.cache(CACHE_KEY)
    if c.size==0
      metrics=Metric.find(:all, :conditions => {:enabled => true})
      metrics.each do |metric|
        c[metric.name]=metric
        c[metric.id.to_s]=metric
      end
      c['ALL']=metrics
    end
    c
  end

  def self.i18n_domain_cache
    c = Caches.cache(I18N_DOMAIN_CACHE_KEY)
    if c.size==0
      domains(false).each do |domain|
        locale_map={}
        c[domain]=locale_map
      end
    end
    c
  end
  
  def self.i18n_short_name_cache
    c = Caches.cache(I18N_SHORT_NAME_CACHE_KEY)
    if c.size==0
      all.each do |metric|
        locale_map={}
        c[metric.name]=locale_map
      end
    end
    c
  end
end