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
054 /* comments */
055 public static final Metric COMMENT_LINES = add(new Metric("comment_lines"));
056 public static final Metric COMMENT_LINES_DENSITY = add(new Metric("comment_lines_density"));
057 public static final Metric PUBLIC_DOCUMENTED_API_DENSITY = add(new Metric("public_documented_api_density"));
058 public static final Metric PUBLIC_UNDOCUMENTED_API = add(new Metric("public_undocumented_api"));
059 public static final Metric COMMENTED_OUT_CODE_LINES = add(new Metric("commented_out_code_lines"));
060
061 /* unit tests */
062 public static final Metric TESTS = add(new Metric("tests"));
063 public static final Metric TESTS_EXECUTION_TIME = add(new Metric("test_execution_time"));
064 public static final Metric TEST_ERRORS = add(new Metric("test_errors"));
065 public static final Metric SKIPPED_TESTS = add(new Metric("skipped_tests"));
066 public static final Metric TEST_FAILURES = add(new Metric("test_failures"));
067 public static final Metric TEST_SUCCESS_DENSITY = add(new Metric("test_success_density"));
068 public static final Metric TEST_DATA = add(new Metric("test_data"));
069
070 /* coverage */
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 UNCOVERED_LINES = add(new Metric("uncovered_lines"));
074 public static final Metric BRANCH_COVERAGE = add(new Metric("branch_coverage"));
075 public static final Metric UNCOVERED_CONDITIONS = add(new Metric("uncovered_conditions"));
076 public static final Metric COVERAGE_LINE_HITS_DATA = add(new Metric("coverage_line_hits_data"));
077 public static final Metric BRANCH_COVERAGE_HITS_DATA = add(new Metric("branch_coverage_hits_data"));
078
079 /* duplicated lines */
080 public static final Metric DUPLICATED_LINES = add(new Metric("duplicated_lines"));
081 public static final Metric DUPLICATED_BLOCKS = add(new Metric("duplicated_blocks"));
082 public static final Metric DUPLICATED_FILES = add(new Metric("duplicated_files"));
083 public static final Metric DUPLICATED_LINES_DENSITY = add(new Metric("duplicated_lines_density"));
084 public static final Metric DUPLICATIONS_DATA = add(new Metric("duplications_data"));
085
086 /* coding rules */
087 public static final Metric VIOLATIONS_DENSITY = add(new Metric("violations_density"));
088 public static final Metric VIOLATIONS = add(new Metric("violations"));
089 public static final Metric WEIGHTED_VIOLATIONS = add(new Metric("weighted_violations"));
090
091 public static class MetricsList extends ResponsePOJO {
092
093 private List<Metric> metrics = new ArrayList<Metric>();
094
095 public List<Metric> getMetrics() {
096 return metrics;
097 }
098 }
099
100 /**
101 * Generates a callback that will update the metrics definitions from the WSMetrics metrics constants list with data
102 * received from a MetricsQuery call
103 *
104 * @param callback
105 * @return
106 */
107 public static QueryCallBack<MetricsList> getUpdateMetricsFromServer(final QueryCallBack<MetricsList> callback) {
108 return new QueryCallBack<MetricsList>() {
109 public void onResponse(MetricsList response, JavaScriptObject jsonRawResponse) {
110 for (Metric metric : response.getMetrics()) {
111 Metric WSMetricConstant = get(metric.getKey());
112 if (WSMetricConstant != null) {
113 WSMetricConstant.updateFrom(metric);
114 } else {
115 add(metric);
116 }
117 }
118 callback.onResponse(response, jsonRawResponse);
119 }
120
121 public void onError(int errorCode, String errorMessage) {
122 callback.onError(errorCode, errorMessage);
123 }
124
125 public void onTimeout() {
126 callback.onTimeout();
127 }
128 };
129 }
130
131 public static class Metric {
132 public enum ValueType {
133 INT, FLOAT, PERCENT, BOOL, STRING, MILLISEC, DATA, LEVEL, DISTRIB
134 }
135
136 private String key;
137 private String name;
138 private String description;
139 private String domain;
140 private boolean qualitative;
141 private boolean userManaged;
142 private int direction;
143 private ValueType type;
144
145 public Metric(String key) {
146 super();
147 this.key = key;
148 }
149
150 public Metric(String key, String name, String description, String domain,
151 boolean qualitative, boolean userManaged, int direction, ValueType type) {
152 super();
153 this.key = key;
154 this.name = name;
155 this.description = description;
156 this.domain = domain;
157 this.qualitative = qualitative;
158 this.userManaged = userManaged;
159 this.direction = direction;
160 this.type = type;
161 }
162
163 public void updateFrom(Metric metric) {
164 this.name = metric.getName();
165 this.description = metric.getDescription();
166 this.domain = metric.getDomain();
167 this.qualitative = metric.isQualitative();
168 this.userManaged = metric.isUserManaged();
169 this.direction = metric.getDirection();
170 this.type = metric.getType();
171 }
172
173 public String getName() {
174 return name;
175 }
176
177 public ValueType getType() {
178 return type;
179 }
180
181 public String getDescription() {
182 return description;
183 }
184
185 public String getDomain() {
186 return domain;
187 }
188
189 public boolean isQualitative() {
190 return qualitative;
191 }
192
193 public boolean isUserManaged() {
194 return userManaged;
195 }
196
197 public int getDirection() {
198 return direction;
199 }
200
201 public String getKey() {
202 return key;
203 }
204
205 @Override
206 public int hashCode() {
207 return key.hashCode();
208 }
209
210 @Override
211 public boolean equals(Object obj) {
212 if (!(obj instanceof Metric)) {
213 return false;
214 }
215 if (this == obj) {
216 return true;
217 }
218 Metric other = (Metric) obj;
219 return key.equals(other.getKey());
220 }
221 }
222
223 public static Metric add(Metric metric) {
224 if (!DICTIONNARY.contains(metric)) {
225 DICTIONNARY.add(metric);
226 }
227 return metric;
228 }
229
230 public static Metric get(String metricKey) {
231 for (Metric metric : DICTIONNARY) {
232 if (metric.getKey().equals(metricKey)) {
233 return metric;
234 }
235 }
236 return new Metric(metricKey);
237 }
238
239 }