import org.sonar.wsclient.services.Violation;
import org.sonar.wsclient.services.ViolationQuery;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
public class ViolationsPanel extends SourcePanel {
private boolean expand = false;
private List<Violation> violations;
private Map<Integer, List<Violation>> filteredViolationsByLine = new HashMap<Integer, List<Violation>>();
+ private final static Date now = new Date();
public ViolationsPanel(Resource resource, String filter) {
super(resource);
@Override
public String getColumn4() {
String age = "";
- if (violation.getAge()!=null && violation.getAge()>0) {
- age = " <span class='note'>(" + violation.getAge() + " days)</span>";
+ if (violation.getCreatedAt() != null) {
+ if (sameDay(now, violation.getCreatedAt())) {
+ age = " <span class='note'>(today)</span>";
+ } else {
+ age = " <span class='note'>(" + diffInDays(violation.getCreatedAt(), now) + " days)</span>";
+ }
}
return "<div class=\"warn\">" + Icons.forPriority(violation.getPriority()).getHTML() + "</img> "
+ " <a href=\"" + Links.urlForRule(violation.getRuleKey(), false)
+ Utils.escapeHtml(violation.getRuleName()) + "</b></a> : "
+ Utils.escapeHtml(violation.getMessage()) + age + "</div>";
}
+
+ @SuppressWarnings("deprecation")
+ static boolean sameDay(Date d1, Date d2) {
+ return d1.getDate() == d2.getDate() && d1.getMonth() == d2.getMonth() && d1.getYear() == d2.getYear();
+ }
+
+ @SuppressWarnings(value = "deprecation")
+ static int diffInDays(Date d1, Date d2) {
+ int difference = 0;
+ int endDateOffset = -(d1.getTimezoneOffset() * 60 * 1000);
+ long endDateInstant = d1.getTime() + endDateOffset;
+ int startDateOffset = -(d2.getTimezoneOffset() * 60 * 1000);
+ long startDateInstant = d2.getTime() + startDateOffset;
+ double differenceDouble = (double) Math.abs(endDateInstant - startDateInstant) / (1000.0 * 60 * 60 * 24);
+ differenceDouble = Math.max(1.0D, differenceDouble);
+ difference = (int) differenceDouble;
+ return difference;
+ }
}
private boolean hasViolations(int lineIndex) {
return measure.getId() == null &&
metric.isOptimizedBestValue() == Boolean.TRUE &&
metric.getBestValue() != null &&
+ measure.getValue() != null &
NumberUtils.compare(metric.getBestValue(), measure.getValue()) == 0 &&
measure.getAlertStatus() == null &&
measure.getDescription() == null &&
violation.setLine(JsonUtils.getInteger(json, "line"));
violation.setSeverity(JsonUtils.getString(json, "priority"));
violation.setCreatedAt(JsonUtils.getDate(json, "createdAt"));
- violation.setAge(JsonUtils.getInteger(json, "age"));
JSONObject rule = (JSONObject) json.get("rule");
if (rule != null) {
public static final String NEW_VIOLATIONS_KEY = "new_violations";
public static final Metric NEW_VIOLATIONS = new Metric(NEW_VIOLATIONS_KEY, "New Violations", "New Violations", Metric.ValueType.INT,
- Metric.DIRECTION_WORST, true, DOMAIN_RULES);
+ Metric.DIRECTION_WORST, true, DOMAIN_RULES).setHidden(true);
public static final String NEW_BLOCKER_VIOLATIONS_KEY = "new_blocker_violations";
public static final Metric NEW_BLOCKER_VIOLATIONS = new Metric(NEW_BLOCKER_VIOLATIONS_KEY, "New Blocker violations", "New Blocker violations",
- Metric.ValueType.INT, Metric.DIRECTION_WORST, true, DOMAIN_RULES);
+ Metric.ValueType.INT, Metric.DIRECTION_WORST, true, DOMAIN_RULES).setHidden(true);
public static final String NEW_CRITICAL_VIOLATIONS_KEY = "new_critical_violations";
public static final Metric NEW_CRITICAL_VIOLATIONS = new Metric(NEW_CRITICAL_VIOLATIONS_KEY, "New Critical violations", "New Critical violations",
- Metric.ValueType.INT, Metric.DIRECTION_WORST, true, DOMAIN_RULES);
+ Metric.ValueType.INT, Metric.DIRECTION_WORST, true, DOMAIN_RULES).setHidden(true);
public static final String NEW_MAJOR_VIOLATIONS_KEY = "new_major_violations";
public static final Metric NEW_MAJOR_VIOLATIONS = new Metric(NEW_MAJOR_VIOLATIONS_KEY, "New Major violations", "New Major violations",
- Metric.ValueType.INT, Metric.DIRECTION_WORST, true, DOMAIN_RULES);
+ Metric.ValueType.INT, Metric.DIRECTION_WORST, true, DOMAIN_RULES).setHidden(true);
public static final String NEW_MINOR_VIOLATIONS_KEY = "new_minor_violations";
public static final Metric NEW_MINOR_VIOLATIONS = new Metric(NEW_MINOR_VIOLATIONS_KEY, "New Minor violations", "New Minor violations",
- Metric.ValueType.INT, Metric.DIRECTION_WORST, true, DOMAIN_RULES);
+ Metric.ValueType.INT, Metric.DIRECTION_WORST, true, DOMAIN_RULES).setHidden(true);
public static final String NEW_INFO_VIOLATIONS_KEY = "new_info_violations";
public static final Metric NEW_INFO_VIOLATIONS = new Metric(NEW_INFO_VIOLATIONS_KEY, "New Info violations", "New Info violations",
- Metric.ValueType.INT, Metric.DIRECTION_WORST, true, DOMAIN_RULES);
+ Metric.ValueType.INT, Metric.DIRECTION_WORST, true, DOMAIN_RULES).setHidden(true);
/* Design */
belongs_to :rule
belongs_to :snapshot
- def age_in_days
- if created_at
- ((snapshot.created_at - created_at)/(60 * 60 * 24)).to_i
- else
- nil
- end
- end
-
def to_hash_json
json = {}
json['message'] = message
json['priority'] = Sonar::RulePriority.to_s(failure_level).upcase
if created_at
json['createdAt'] = format_datetime(created_at)
- json['age'] = age_in_days
end
json['rule'] = {
:key => rule.key,
xml.priority(Sonar::RulePriority.to_s(failure_level))
if created_at
xml.createdAt(format_datetime(created_at))
- xml.age(age_in_days)
end
xml.rule do
xml.key(rule.key)
private String resourceScope = null;
private String resourceQualifier = null;
private Date createdAt = null;
- private Integer age = null;
public String getMessage() {
return message;
return this;
}
- /**
- * @since 2.5
- */
- public Integer getAge() {
- return age;
- }
-
- public Violation setAge(Integer age) {
- this.age = age;
- return this;
- }
-
/**
* @since 2.5
*/
violation.setLine(JsonUtils.getInteger(json, "line"));
violation.setSeverity(JsonUtils.getString(json, "priority"));
violation.setCreatedAt(JsonUtils.getDateTime(json, "createdAt"));
- violation.setAge(JsonUtils.getInteger(json, "age"));
JSONObject rule = (JSONObject) json.get("rule");
if (rule != null) {