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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
|
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube 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.
*
* SonarQube 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 this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.api.measures;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.sonar.api.batch.BatchSide;
import org.sonar.api.batch.InstantiationStrategy;
import org.sonar.api.server.ServerSide;
/**
* This class represents the definition of a metric in Sonar.
*
* @since 1.10
*/
@Table(name = "metrics")
@Entity(name = "Metric")
@BatchSide
@InstantiationStrategy(InstantiationStrategy.PER_BATCH)
@ServerSide
public class Metric<G extends Serializable> implements Serializable, org.sonar.api.batch.measure.Metric<G> {
/**
* A metric bigger value means a degradation
*/
public static final int DIRECTION_WORST = -1;
/**
* A metric bigger value means an improvement
*/
public static final int DIRECTION_BETTER = 1;
/**
* The metric direction has no meaning
*/
public static final int DIRECTION_NONE = 0;
public enum ValueType {
INT(Integer.class, "Integer"),
FLOAT(Double.class, "Float"),
PERCENT(Double.class, "Percent"),
BOOL(Boolean.class, "Yes/No"),
STRING(String.class, "String"),
MILLISEC(Integer.class, "Milliseconds"),
DATA(String.class, "Data"),
LEVEL(Metric.Level.class, "Level"),
DISTRIB(String.class, "Distribution"),
RATING(Integer.class, "Rating"),
WORK_DUR(Long.class, "Work Duration");
private final Class valueClass;
private final String description;
ValueType(Class valueClass, String description) {
this.valueClass = valueClass;
this.description = description;
}
private Class valueType() {
return valueClass;
}
public String description() {
return description;
}
public static String[] names() {
ValueType[] values = values();
String[] names = new String[values.length];
for (int i = 0; i < values.length; i += 1) {
names[i] = values[i].name();
}
return names;
}
public static String descriptionOf(String key) {
for (ValueType valueType : values()) {
if (valueType.name().equals(key)) {
return valueType.description;
}
}
throw new IllegalArgumentException(String.format("Unknown ValueType key '%s'", key));
}
}
public enum Level {
OK("Green"), WARN("Orange"), ERROR("Red");
private String colorName;
Level(String colorName) {
this.colorName = colorName;
}
public String getColorName() {
return colorName;
}
public static List<String> names() {
return Lists.transform(Arrays.asList(values()), new Function<Level, String>() {
@Nonnull
@Override
public String apply(@Nonnull Level level) {
return level.name();
}
});
}
}
@Id
@Column(name = "id")
@GeneratedValue
private Integer id;
@Transient
private transient Formula formula;
@Column(name = "name", updatable = false, nullable = false, length = 64)
private String key;
@Column(name = "description", updatable = true, nullable = true, length = 255)
private String description;
@Column(name = "val_type", updatable = true, nullable = true)
@Enumerated(EnumType.STRING)
private ValueType type;
@Column(name = "direction", updatable = true, nullable = true)
private Integer direction;
@Column(name = "domain", updatable = true, nullable = true, length = 60)
private String domain;
@Column(name = "short_name", updatable = true, nullable = true, length = 64)
private String name;
@Column(name = "qualitative", updatable = true, nullable = true)
private Boolean qualitative = Boolean.FALSE;
@Column(name = "user_managed", updatable = true, nullable = true)
private Boolean userManaged = Boolean.FALSE;
@Column(name = "enabled", updatable = true, nullable = true)
private Boolean enabled = Boolean.TRUE;
@Column(name = "worst_value", updatable = true, nullable = true, precision = 30, scale = 20)
private Double worstValue;
@Column(name = "best_value", updatable = true, nullable = true, precision = 30, scale = 20)
private Double bestValue;
@Column(name = "optimized_best_value", updatable = true, nullable = true)
private Boolean optimizedBestValue;
@Column(name = "hidden", updatable = true, nullable = true)
private Boolean hidden = Boolean.FALSE;
@Column(name = "delete_historical_data", updatable = true, nullable = true)
private Boolean deleteHistoricalData;
private Metric(Builder builder) {
this.key = builder.key;
this.name = builder.name;
this.description = builder.description;
this.type = builder.type;
this.direction = builder.direction;
this.domain = builder.domain;
this.qualitative = builder.qualitative;
this.enabled = Boolean.TRUE;
this.worstValue = builder.worstValue;
this.optimizedBestValue = builder.optimizedBestValue;
this.bestValue = builder.bestValue;
this.hidden = builder.hidden;
this.formula = builder.formula;
this.userManaged = builder.userManaged;
this.deleteHistoricalData = builder.deleteHistoricalData;
}
/**
* Creates an empty metric. Required for Hibernate.
*
* @deprecated in 1.12. Use the {@link Builder} factory.
*/
@Deprecated
public Metric() {
}
/**
* Creates a metric based on its key. Shortcut to Metric(key, ValueType.INT)
*
* @param key the metric key
* @deprecated since 2.7 use the {@link Builder} factory.
*/
@Deprecated
public Metric(String key) {
this(key, ValueType.INT);
}
/**
* Creates a metric based on a key and a type. Shortcut to
* Metric(key, key, key, type, -1, Boolean.FALSE, null, false)
*
* @param key the key
* @param type the type
* @deprecated since 2.7 use the {@link Builder} factory.
*/
@Deprecated
public Metric(String key, ValueType type) {
this(key, key, key, type, -1, Boolean.FALSE, null, false);
}
/**
* @deprecated since 2.7 use the {@link Builder} factory.
*/
@Deprecated
public Metric(String key, String name, String description, ValueType type, Integer direction, Boolean qualitative, String domain) {
this(key, name, description, type, direction, qualitative, domain, false);
}
/**
* Creates a fully qualified metric.
*
* @param key the metric key
* @param name the metric name
* @param description the metric description
* @param type the metric type
* @param direction the metric direction
* @param qualitative whether the metric is qualitative
* @param domain the metric domain
* @param userManaged whether the metric is user managed
*/
private Metric(String key, String name, String description, ValueType type, Integer direction, Boolean qualitative, @Nullable String domain,
boolean userManaged) {
this.key = key;
this.description = description;
this.type = type;
this.direction = direction;
this.domain = domain;
this.name = name;
this.qualitative = qualitative;
this.userManaged = userManaged;
if (ValueType.PERCENT.equals(this.type)) {
this.bestValue = (direction == DIRECTION_BETTER ? 100.0 : 0.0);
this.worstValue = (direction == DIRECTION_BETTER ? 0.0 : 100.0);
}
}
/**
* For internal use only
*/
public Integer getId() {
return id;
}
/**
* For internal use only
*/
public Metric setId(@Nullable Integer id) {
this.id = id;
return this;
}
/**
* @return the metric formula
*/
public Formula getFormula() {
return formula;
}
/**
* Sets the metric formula
*
* @param formula the formula
* @return this
*/
public Metric setFormula(Formula formula) {
this.formula = formula;
return this;
}
/**
* @return wether the metric is qualitative
*/
public Boolean getQualitative() {
return qualitative;
}
/**
* Sets whether the metric is qualitative
*
* @param qualitative whether the metric is qualitative
* @return this
*/
public Metric setQualitative(Boolean qualitative) {
this.qualitative = qualitative;
return this;
}
/**
* @return the metric key
*/
public String getKey() {
return key;
}
/**
* Sets the metric key
*
* @param key the key
* @return this
*/
public Metric setKey(String key) {
this.key = key;
return this;
}
/**
* @return the metric type
*/
public ValueType getType() {
return type;
}
/**
* Sets the metric type
*
* @param type the type
* @return this
*/
public Metric setType(ValueType type) {
this.type = type;
return this;
}
/**
* @return the metric description
*/
@CheckForNull
public String getDescription() {
return description;
}
/**
* Sets the metric description
*
* @param description the description
* @return this
*/
public Metric setDescription(@Nullable String description) {
this.description = description;
return this;
}
/**
* @return whether the metric is a managed by the users ("manual metric")
*/
public Boolean getUserManaged() {
return userManaged;
}
/**
* Sets whether the metric is managed by users ("manual metric")
*
* @param userManaged whether the metric is user managed
* @return this
*/
public Metric setUserManaged(Boolean userManaged) {
this.userManaged = userManaged;
return this;
}
/**
* @return whether the metric is enabled
*/
public Boolean getEnabled() {
return enabled;
}
/**
* Sets whether the metric is enabled
*
* @param enabled whether the metric is enabled
* @return this
*/
public Metric setEnabled(Boolean enabled) {
this.enabled = enabled;
return this;
}
/**
* @return the metric direction
*/
public Integer getDirection() {
return direction;
}
/**
* Sets the metric direction.
*
* @param direction the direction
*/
public Metric setDirection(Integer direction) {
this.direction = direction;
return this;
}
/**
* @return the domain of the metric
*/
public String getDomain() {
return domain;
}
/**
* Sets the domain for the metric (General, Complexity...)
*
* @param domain the domain
* @return this
*/
public Metric setDomain(String domain) {
this.domain = domain;
return this;
}
/**
* @return the metric name
*/
public String getName() {
return name;
}
/**
* Sets the metric name
*
* @param name the name
* @return this
*/
public Metric setName(String name) {
this.name = name;
return this;
}
public Double getWorstValue() {
return worstValue;
}
@CheckForNull
public Double getBestValue() {
return bestValue;
}
/**
* @return this
*/
public Metric setWorstValue(@Nullable Double d) {
this.worstValue = d;
return this;
}
/**
* @param bestValue the best value. It can be null.
* @return this
*/
public Metric setBestValue(@Nullable Double bestValue) {
this.bestValue = bestValue;
return this;
}
/**
* @return whether the metric is of a numeric type (int, percentage...)
*/
public boolean isNumericType() {
return ValueType.INT.equals(type)
|| ValueType.FLOAT.equals(type)
|| ValueType.PERCENT.equals(type)
|| ValueType.BOOL.equals(type)
|| ValueType.MILLISEC.equals(type)
|| ValueType.RATING.equals(type)
|| ValueType.WORK_DUR.equals(type);
}
/**
* @return whether the metric is of type data
*/
public boolean isDataType() {
return ValueType.DATA.equals(type) || ValueType.DISTRIB.equals(type);
}
/**
* @return whether the metric is of type percentage
*/
public boolean isPercentageType() {
return ValueType.PERCENT.equals(type);
}
public Metric setOptimizedBestValue(@Nullable Boolean b) {
this.optimizedBestValue = b;
return this;
}
/**
* @return null for manual metrics
*/
@CheckForNull
public Boolean isOptimizedBestValue() {
return optimizedBestValue;
}
public Boolean isHidden() {
return hidden;
}
public Metric setHidden(Boolean hidden) {
this.hidden = hidden;
return this;
}
public Boolean getDeleteHistoricalData() {
return deleteHistoricalData;
}
@Override
public int hashCode() {
return key.hashCode();
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Metric)) {
return false;
}
if (this == obj) {
return true;
}
Metric other = (Metric) obj;
return key.equals(other.getKey());
}
@Override
public String toString() {
return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).toString();
}
/**
* Merge with fields from other metric. All fields are copied, except the id.
*
* @return this
*/
public Metric merge(final Metric with) {
this.description = with.description;
this.domain = with.domain;
this.enabled = with.enabled;
this.qualitative = with.qualitative;
this.worstValue = with.worstValue;
this.bestValue = with.bestValue;
this.optimizedBestValue = with.optimizedBestValue;
this.direction = with.direction;
this.key = with.key;
this.type = with.type;
this.name = with.name;
this.userManaged = with.userManaged;
this.hidden = with.hidden;
this.deleteHistoricalData = with.deleteHistoricalData;
return this;
}
/**
* Metric.Builder is used to create metric definitions. It must be preferred to creating new instances of the Metric class directly.
*
* @since 2.7
*/
public static final class Builder {
private String key;
private Metric.ValueType type;
private String name;
private String description;
private Integer direction = DIRECTION_NONE;
private Boolean qualitative = Boolean.FALSE;
private String domain = null;
private Formula formula;
private Double worstValue;
private Double bestValue;
private boolean optimizedBestValue = false;
private boolean hidden = false;
private boolean userManaged = false;
private boolean deleteHistoricalData = false;
/**
* Creates a new {@link Builder} object.
*
* @param key the metric key, should be unique among all metrics
* @param name the metric name
* @param type the metric type
*/
public Builder(String key, String name, ValueType type) {
if (StringUtils.isBlank(key)) {
throw new IllegalArgumentException("Metric key can not be blank");
}
if (StringUtils.isBlank(name)) {
throw new IllegalArgumentException("Metric name can not be blank");
}
if (type == null) {
throw new IllegalArgumentException("Metric type can not be null");
}
this.key = key;
this.name = name;
this.type = type;
}
/**
* Sets the metric description.
*
* @param d the description
* @return the builder
*/
public Builder setDescription(String d) {
this.description = d;
return this;
}
/**
* Sets the metric direction (used for numeric values only), which is used in the Web UI to show if the trend of a metric is good or not.
* <ul>
* <li>Metric.DIRECTION_WORST: indicates that an increase of the metric value is not a good thing (example: the complexity of a function)</li>
* <li>Metric.DIRECTION_BETTER: indicates that an increase of the metric value is a good thing (example: the code coverage of a function)</li>
* <li>Metric.DIRECTION_NONE: indicates that the variation of the metric value is neither good nor bad (example: number of files).</li>
* </ul>
* Metric.DIRECTION_NONE is the default value.
*
* @see Metric#DIRECTION_WORST
* @see Metric#DIRECTION_BETTER
* @see Metric#DIRECTION_NONE
*
* @param d the direction
* @return the builder
*/
public Builder setDirection(Integer d) {
this.direction = d;
return this;
}
/**
* Sets whether the metric is qualitative or not. Default value is false.
* <br/>
* If set to true, then variations of this metric will be highlighted in the Web UI (for instance, trend icons will be red or green instead of default grey).
*
* @param b Boolean.TRUE if the metric is qualitative
* @return the builder
*/
public Builder setQualitative(Boolean b) {
this.qualitative = b;
return this;
}
/**
* Sets the domain for the metric (General, Complexity...). This is used to group metrics in the Web UI.
* <br/>
* By default, the metric belongs to no specific domain.
*
* @param d the domain
* @return the builder
*/
public Builder setDomain(String d) {
this.domain = d;
return this;
}
/**
* Specifies the formula used by Sonar to automatically aggregate measures stored on files up to the project level.
* <br/>
* <br/>
* By default, no formula is defined, which means that it's up to a sensor/decorator to compute measures on appropriate levels.
* <br/>
* When a formula is set, sensors/decorators just need to store measures at a specific level and let Sonar run the formula to store
* measures on the remaining levels.
*
* @see SumChildDistributionFormula
* @see SumChildValuesFormula
* @see MeanAggregationFormula
* @see WeightedMeanAggregationFormula
*
* @param f the formula
* @return the builder
*/
public Builder setFormula(Formula f) {
this.formula = f;
return this;
}
/**
* Sets the worst value that the metric can get (example: 0.0 for code coverage). No worst value is set by default.
*
* @param d the worst value
* @return the builder
*/
public Builder setWorstValue(Double d) {
this.worstValue = d;
return this;
}
/**
* Sets the best value that the metric can get (example: 100.0 for code coverage). No best value is set by default.
* <br/>
* Resources would be hidden on drilldown page, if the value of measure equals to best value.
*
* @param d the best value
* @return the builder
*/
public Builder setBestValue(Double d) {
this.bestValue = d;
return this;
}
/**
* Specifies whether file-level measures that equal to the defined best value are stored or not. Default is false.
* <br/>
* Example with the metric that stores the number of violation ({@link CoreMetrics#VIOLATIONS}):
* if a file has no violation, then the value '0' won't be stored in the database.
*
* @param b true if the measures must not be stored when they equal to the best value
* @return the builder
*/
public Builder setOptimizedBestValue(boolean b) {
this.optimizedBestValue = b;
return this;
}
/**
* Sets whether the metric should be hidden in Web UI (e.g. in Time Machine). Default is false.
*
* @param b true if the metric should be hidden.
* @return the builder
*/
public Builder setHidden(boolean b) {
this.hidden = b;
return this;
}
/**
* Specifies whether this metric can be edited online in the "Manual measures" page. Default is false.
*
* @since 2.10
*
* @param b true if the metric can be edited online.
* @return the builder
*/
public Builder setUserManaged(boolean b) {
this.userManaged = b;
return this;
}
/**
* Specifies whether measures from the past can be automatically deleted to minimize database volume.
* <br/>
* By default, historical data are kept.
*
* @since 2.14
*
* @param b true if measures from the past can be deleted automatically.
* @return the builder
*/
public Builder setDeleteHistoricalData(boolean b) {
this.deleteHistoricalData = b;
return this;
}
/**
* Creates a new metric definition based on the properties set on this metric builder.
*
* @return a new {@link Metric} object
*/
public <G extends Serializable> Metric<G> create() {
if (ValueType.PERCENT.equals(this.type)) {
this.bestValue = (direction == DIRECTION_BETTER ? 100.0 : 0.0);
this.worstValue = (direction == DIRECTION_BETTER ? 0.0 : 100.0);
}
return new Metric<>(this);
}
}
@Override
public String key() {
return getKey();
}
@Override
public Class<G> valueType() {
return getType().valueType();
}
}
|