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 /* design */
092 public static final Metric LCOM4 = add(new Metric("lcom4"));
093 public static final Metric RFC = add(new Metric("rfc"));
094
095 public static class MetricsList extends ResponsePOJO {
096
097 private List<Metric> metrics = new ArrayList<Metric>();
098
099 public List<Metric> getMetrics() {
100 return metrics;
101 }
102 }
103
104 /**
105 * Generates a callback that will update the metrics definitions from the WSMetrics metrics constants list with data
106 * received from a MetricsQuery call
107 *
108 * @param callback
109 * @return
110 */
111 public static QueryCallBack<MetricsList> getUpdateMetricsFromServer(final QueryCallBack<MetricsList> callback) {
112 return new QueryCallBack<MetricsList>() {
113 public void onResponse(MetricsList response, JavaScriptObject jsonRawResponse) {
114 for (Metric metric : response.getMetrics()) {
115 Metric WSMetricConstant = get(metric.getKey());
116 if (WSMetricConstant != null) {
117 WSMetricConstant.updateFrom(metric);
118 } else {
119 add(metric);
120 }
121 }
122 callback.onResponse(response, jsonRawResponse);
123 }
124
125 public void onError(int errorCode, String errorMessage) {
126 callback.onError(errorCode, errorMessage);
127 }
128
129 public void onTimeout() {
130 callback.onTimeout();
131 }
132 };
133 }
134
135 public static class Metric {
136 public enum ValueType {
137 INT, FLOAT, PERCENT, BOOL, STRING, MILLISEC, DATA, LEVEL, DISTRIB
138 }
139
140 private String key;
141 private String name;
142 private String description;
143 private String domain;
144 private boolean qualitative;
145 private boolean userManaged;
146 private int direction;
147 private ValueType type;
148
149 public Metric(String key) {
150 super();
151 this.key = key;
152 }
153
154 public Metric(String key, String name, String description, String domain,
155 boolean qualitative, boolean userManaged, int direction, ValueType type) {
156 super();
157 this.key = key;
158 this.name = name;
159 this.description = description;
160 this.domain = domain;
161 this.qualitative = qualitative;
162 this.userManaged = userManaged;
163 this.direction = direction;
164 this.type = type;
165 }
166
167 public void updateFrom(Metric metric) {
168 this.name = metric.getName();
169 this.description = metric.getDescription();
170 this.domain = metric.getDomain();
171 this.qualitative = metric.isQualitative();
172 this.userManaged = metric.isUserManaged();
173 this.direction = metric.getDirection();
174 this.type = metric.getType();
175 }
176
177 public String getName() {
178 return name;
179 }
180
181 public ValueType getType() {
182 return type;
183 }
184
185 public String getDescription() {
186 return description;
187 }
188
189 public String getDomain() {
190 return domain;
191 }
192
193 public boolean isQualitative() {
194 return qualitative;
195 }
196
197 public boolean isUserManaged() {
198 return userManaged;
199 }
200
201 public int getDirection() {
202 return direction;
203 }
204
205 public String getKey() {
206 return key;
207 }
208
209 @Override
210 public int hashCode() {
211 return key.hashCode();
212 }
213
214 @Override
215 public boolean equals(Object obj) {
216 if (!(obj instanceof Metric)) {
217 return false;
218 }
219 if (this == obj) {
220 return true;
221 }
222 Metric other = (Metric) obj;
223 return key.equals(other.getKey());
224 }
225 }
226
227 public static Metric add(Metric metric) {
228 if (!DICTIONNARY.contains(metric)) {
229 DICTIONNARY.add(metric);
230 }
231 return metric;
232 }
233
234 public static Metric get(String metricKey) {
235 for (Metric metric : DICTIONNARY) {
236 if (metric.getKey().equals(metricKey)) {
237 return metric;
238 }
239 }
240 return new Metric(metricKey);
241 }
242
243 }