]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-1937 violations tab : display violation age in days to current date
authorsimonbrandhof <simon.brandhof@gmail.com>
Wed, 8 Dec 2010 13:23:18 +0000 (13:23 +0000)
committersimonbrandhof <simon.brandhof@gmail.com>
Wed, 8 Dec 2010 13:23:18 +0000 (13:23 +0000)
plugins/sonar-core-gwt/src/main/java/org/sonar/plugins/core/violationsviewer/client/ViolationsPanel.java
sonar-batch/src/main/java/org/sonar/batch/index/MeasurePersister.java
sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/ViolationUnmarshaller.java
sonar-plugin-api/src/main/java/org/sonar/api/measures/CoreMetrics.java
sonar-server/src/main/webapp/WEB-INF/app/models/rule_failure.rb
sonar-ws-client/src/main/java/org/sonar/wsclient/services/Violation.java
sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ViolationUnmarshaller.java

index 14cf189a9f0132cdac94cf2fc63241e14c8bdd62..b5122536668976d74118a2baebe1add2f7f228bc 100644 (file)
@@ -29,15 +29,13 @@ import org.sonar.wsclient.services.Resource;
 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);
@@ -140,8 +138,12 @@ public class ViolationsPanel extends SourcePanel {
     @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)
@@ -150,6 +152,24 @@ public class ViolationsPanel extends SourcePanel {
           + 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) {
index 921195a904622cf040e433fc0eb927a39a8eeb52..19d118db5cae1eab4facad262ad557ce62092359 100644 (file)
@@ -94,6 +94,7 @@ public final class MeasurePersister {
     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 &&
index d5fff51f259590e9e7a2fadb146bdfce8547124c..bf0194820e327eae8d99b4bfafbaf26832a5c881 100644 (file)
@@ -31,7 +31,6 @@ public class ViolationUnmarshaller extends AbstractUnmarshaller<Violation> {
     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) {
index 09d826610ca01f7aca47e30329c5898a484f8650..4c812dcc659893e310c381bf9b548fed7b007535 100644 (file)
@@ -375,27 +375,27 @@ public final class CoreMetrics {
 
   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 */
 
index 3b4e73b4de7b8bccda3b620d380bc1744be84707..64a2517af132292fc4b2c816bba09f6744f43189 100644 (file)
@@ -23,14 +23,6 @@ class RuleFailure < ActiveRecord::Base
   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
@@ -38,7 +30,6 @@ class RuleFailure < ActiveRecord::Base
     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,
@@ -61,7 +52,6 @@ class RuleFailure < ActiveRecord::Base
       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)
index c984d15a3bf4b3e13b80f014c670e633f83e7d60..ff0c3f22ce9289b329f96519d2613275be6e138d 100644 (file)
@@ -33,7 +33,6 @@ public class Violation extends Model {
   private String resourceScope = null;
   private String resourceQualifier = null;
   private Date createdAt = null;
-  private Integer age = null;
 
   public String getMessage() {
     return message;
@@ -134,18 +133,6 @@ public class Violation extends Model {
     return this;
   }
 
-  /**
-   * @since 2.5
-   */
-  public Integer getAge() {
-    return age;
-  }
-
-  public Violation setAge(Integer age) {
-    this.age = age;
-    return this;
-  }
-
   /**
    * @since 2.5
    */
index c0c5aa538cc732cd52b64a9e29e0c1342d4b2a09..258e5938570a2db796ac1f1cd867971178a471ca 100644 (file)
@@ -31,7 +31,6 @@ public class ViolationUnmarshaller extends AbstractUnmarshaller<Violation> {
     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) {