001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2009 SonarSource SA
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.api.web.gwt.client.webservices;
021
022 import com.google.gwt.core.client.JavaScriptObject;
023
024 import java.util.ArrayList;
025 import java.util.List;
026
027 public final class WSMetrics {
028
029 private WSMetrics() {
030 }
031
032 private final static List<Metric> DICTIONNARY = new ArrayList<Metric>();
033
034 public static final Metric NCLOC = add(new Metric("ncloc"));
035 public static final Metric LINES = add(new Metric("lines"));
036 public static final Metric CLASSES = add(new Metric("classes"));
037 public static final Metric PACKAGES = add(new Metric("packages"));
038 public static final Metric FUNCTIONS = add(new Metric("functions"));
039 public static final Metric ACCESSORS = add(new Metric("accessors"));
040 public static final Metric FILES = add(new Metric("files"));
041 public static final Metric DIRECTORIES = add(new Metric("directories"));
042 public static final Metric PUBLIC_API = add(new Metric("public_api"));
043
044 /* complexity */
045 public static final Metric COMPLEXITY = add(new Metric("complexity"));
046 public static final Metric CLASS_COMPLEXITY = add(new Metric("class_complexity"));
047 public static final Metric FUNCTION_COMPLEXITY = add(new Metric("function_complexity"));
048 public static final Metric FILE_COMPLEXITY = add(new Metric("file_complexity"));
049 public static final Metric STATEMENTS = add(new Metric("statements"));
050
051 public static final Metric CLASS_COMPLEXITY_DISTRIBUTION = add(new Metric("class_complexity_distribution"));
052 public static final Metric FUNCTION_COMPLEXITY_DISTRIBUTION = add(new Metric("function_complexity_distribution"));
053 public static final Metric UNCOVERED_COMPLEXITY_BY_TESTS = add(new Metric("uncovered_complexity_by_tests"));
054
055 /* comments */
056 public static final Metric COMMENT_LINES = add(new Metric("comment_lines"));
057 public static final Metric COMMENT_LINES_DENSITY = add(new Metric("comment_lines_density"));
058 public static final Metric PUBLIC_DOCUMENTED_API_DENSITY = add(new Metric("public_documented_api_density"));
059 public static final Metric PUBLIC_UNDOCUMENTED_API = add(new Metric("public_undocumented_api"));
060 public static final Metric COMMENTED_OUT_CODE_LINES = add(new Metric("commented_out_code_lines"));
061
062 /* unit tests */
063 public static final Metric TESTS = add(new Metric("tests"));
064 public static final Metric TESTS_EXECUTION_TIME = add(new Metric("test_execution_time"));
065 public static final Metric TEST_ERRORS = add(new Metric("test_errors"));
066 public static final Metric SKIPPED_TESTS = add(new Metric("skipped_tests"));
067 public static final Metric TEST_FAILURES = add(new Metric("test_failures"));
068 public static final Metric TEST_SUCCESS_DENSITY = add(new Metric("test_success_density"));
069 public static final Metric TEST_DATA = add(new Metric("test_data"));
070
071 public static final Metric COVERAGE = add(new Metric("coverage"));
072 public static final Metric LINE_COVERAGE = add(new Metric("line_coverage"));
073 public static final Metric BRANCH_COVERAGE = add(new Metric("branch_coverage"));
074 public static final Metric COVERAGE_LINE_HITS_DATA = add(new Metric("coverage_line_hits_data"));
075 public static final Metric BRANCH_COVERAGE_HITS_DATA = add(new Metric("branch_coverage_hits_data"));
076
077 /* duplicated lines */
078 public static final Metric DUPLICATED_LINES = add(new Metric("duplicated_lines"));
079 public static final Metric DUPLICATED_BLOCKS = add(new Metric("duplicated_blocks"));
080 public static final Metric DUPLICATED_FILES = add(new Metric("duplicated_files"));
081 public static final Metric DUPLICATED_LINES_DENSITY = add(new Metric("duplicated_lines_density"));
082 public static final Metric DUPLICATIONS_DATA = add(new Metric("duplications_data"));
083
084 /* coding rules */
085 public static final Metric VIOLATIONS_DENSITY = add(new Metric("violations_density"));
086 public static final Metric VIOLATIONS = add(new Metric("violations"));
087 public static final Metric WEIGHTED_VIOLATIONS = add(new Metric("weighted_violations"));
088
089 public static class MetricsList extends ResponsePOJO {
090
091 private List<Metric> metrics = new ArrayList<Metric>();
092
093 public List<Metric> getMetrics() {
094 return metrics;
095 }
096 }
097
098 /**
099 * Generates a callback that will update the metrics definitions from the WSMetrics metrics constants list with data
100 * received from a MetricsQuery call
101 *
102 * @param callback
103 * @return
104 */
105 public static QueryCallBack<MetricsList> getUpdateMetricsFromServer(final QueryCallBack<MetricsList> callback) {
106 return new QueryCallBack<MetricsList>() {
107 public void onResponse(MetricsList response, JavaScriptObject jsonRawResponse) {
108 for (Metric metric : response.getMetrics()) {
109 Metric WSMetricConstant = get(metric.getKey());
110 if (WSMetricConstant != null) {
111 WSMetricConstant.updateFrom(metric);
112 } else {
113 add(metric);
114 }
115 }
116 callback.onResponse(response, jsonRawResponse);
117 }
118
119 public void onError(int errorCode, String errorMessage) {
120 callback.onError(errorCode, errorMessage);
121 }
122
123 public void onTimeout() {
124 callback.onTimeout();
125 }
126 };
127 }
128
129 public static class Metric {
130 public enum ValueType {
131 INT, FLOAT, PERCENT, BOOL, STRING, MILLISEC, DATA, LEVEL, DISTRIB
132 }
133
134 private String key;
135 private String name;
136 private String description;
137 private String domain;
138 private boolean qualitative;
139 private boolean userManaged;
140 private int direction;
141 private ValueType type;
142
143 public Metric(String key) {
144 super();
145 this.key = key;
146 }
147
148 public Metric(String key, String name, String description, String domain,
149 boolean qualitative, boolean userManaged, int direction, ValueType type) {
150 super();
151 this.key = key;
152 this.name = name;
153 this.description = description;
154 this.domain = domain;
155 this.qualitative = qualitative;
156 this.userManaged = userManaged;
157 this.direction = direction;
158 this.type = type;
159 }
160
161 public void updateFrom(Metric metric) {
162 this.name = metric.getName();
163 this.description = metric.getDescription();
164 this.domain = metric.getDomain();
165 this.qualitative = metric.isQualitative();
166 this.userManaged = metric.isUserManaged();
167 this.direction = metric.getDirection();
168 this.type = metric.getType();
169 }
170
171 public String getName() {
172 return name;
173 }
174
175 public ValueType getType() {
176 return type;
177 }
178
179 public String getDescription() {
180 return description;
181 }
182
183 public String getDomain() {
184 return domain;
185 }
186
187 public boolean isQualitative() {
188 return qualitative;
189 }
190
191 public boolean isUserManaged() {
192 return userManaged;
193 }
194
195 public int getDirection() {
196 return direction;
197 }
198
199 public String getKey() {
200 return key;
201 }
202
203 @Override
204 public int hashCode() {
205 return key.hashCode();
206 }
207
208 @Override
209 public boolean equals(Object obj) {
210 if (!(obj instanceof Metric)) {
211 return false;
212 }
213 if (this == obj) {
214 return true;
215 }
216 Metric other = (Metric) obj;
217 return key.equals(other.getKey());
218 }
219 }
220
221 public static Metric add(Metric metric) {
222 if (!DICTIONNARY.contains(metric)) {
223 DICTIONNARY.add(metric);
224 }
225 return metric;
226 }
227
228 public static Metric get(String metricKey) {
229 for (Metric metric : DICTIONNARY) {
230 if (metric.getKey().equals(metricKey)) {
231 return metric;
232 }
233 }
234 return new Metric(metricKey);
235 }
236
237 }