]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5329 - Added details to Log and Json
authorStephane Gamard <stephane.gamard@searchbox.com>
Wed, 11 Jun 2014 16:10:01 +0000 (18:10 +0200)
committerStephane Gamard <stephane.gamard@searchbox.com>
Wed, 11 Jun 2014 16:10:01 +0000 (18:10 +0200)
sonar-server/src/main/java/org/sonar/server/log/index/LogNormalizer.java
sonar-server/src/main/java/org/sonar/server/log/ws/LogMapping.java
sonar-server/src/main/java/org/sonar/server/qualityprofile/ActiveRuleChange.java
sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java
sonar-server/src/test/java/org/sonar/server/qualityprofile/ActiveRuleChangeMediumTest.java

index c71926ef8f86171589242d140735a63dd9d6610b..83921b80e85af59e174d88396d8907e1a6be106a 100644 (file)
@@ -99,6 +99,9 @@ public class LogNormalizer extends BaseNormalizer<LogDto, LogKey> {
     logDoc.put(LogFields.MESSAGE.field(), dto.getMessage());
     logDoc.put(LogFields.EXECUTION.field(), dto.getExecutionTime());
     logDoc.put(LogFields.DATE.field(), dto.getCreatedAt());
+
+    System.out.println(" KeyValueFormat.parse(dto.getData()) = " + KeyValueFormat.parse(dto.getData()));
+
     logDoc.put(LogFields.DETAILS.field(), KeyValueFormat.parse(dto.getData()));
 
    /* Creating updateRequest */
index abbec40df76030a7eca8c2741d757036476727f2..534ef4234f6af3e35b00f2a2aed9bf450dfbe2a3 100644 (file)
 package org.sonar.server.log.ws;
 
 import org.sonar.api.resources.Languages;
+import org.sonar.api.utils.text.JsonWriter;
+import org.sonar.core.log.Log;
 import org.sonar.server.log.index.LogNormalizer;
 import org.sonar.server.search.ws.BaseMapping;
 import org.sonar.server.text.MacroInterpreter;
 
+import java.util.Map;
+
 /**
  * Conversion between Log and WS JSON response
  */
@@ -38,5 +42,21 @@ public class LogMapping extends BaseMapping {
     addIndexStringField("userLogin", LogNormalizer.LogFields.AUTHOR.field());
     addIndexStringField("message", LogNormalizer.LogFields.MESSAGE.field());
     addIndexStringField("executionTime", LogNormalizer.LogFields.EXECUTION.field());
+    addField("details", new DetailField());
+  }
+
+  private static class DetailField extends IndexField<Log> {
+    DetailField() {
+      super(LogNormalizer.LogFields.DETAILS.field());
+    }
+
+    @Override
+    public void write(JsonWriter json, Log log) {
+      json.name("details").beginObject();
+      for (Map.Entry<String, String> detail : log.details().entrySet()) {
+        json.prop(detail.getKey(), detail.getValue());
+      }
+      json.endObject();
+    }
   }
 }
index da012888e9ba2e42c6b7252ec386cd8f39d53ac5..0126c0fe7914bb82a497c8bdd62f80d2ae66eb24 100644 (file)
@@ -41,14 +41,12 @@ public class ActiveRuleChange implements Loggable {
   private ActiveRule.Inheritance previousInheritance = null, inheritance = null;
   private Map<String, String> parameters = Maps.newHashMap();
 
-  public ActiveRuleChange() {
-    type = null;
-    key = null;
-  }
+  private long start;
 
-  ActiveRuleChange(Type type, ActiveRuleKey key) {
+  private ActiveRuleChange(Type type, ActiveRuleKey key) {
     this.type = type;
     this.key = key;
+    this.start = System.currentTimeMillis();
   }
 
   public ActiveRuleKey getKey() {
@@ -123,12 +121,15 @@ public class ActiveRuleChange implements Loggable {
     if (this.getKey() != null) {
       details.put("key", this.getKey().toString());
     }
-
     return details.build();
   }
 
   @Override
   public Integer getExecutionTime() {
-    return null;
+    return (int) (System.currentTimeMillis() - start);
+  }
+
+  public static ActiveRuleChange createFor(Type type, ActiveRuleKey key) {
+    return new ActiveRuleChange(type, key);
   }
 }
index 0e390f03c1d346bd4b5daec8e7c6fdc4f69f7a1c..3cdc6e4e98fd2cb07963e3d079d31aaf4ab0642f 100644 (file)
@@ -30,7 +30,11 @@ import org.sonar.api.server.rule.RuleParamType;
 import org.sonar.core.log.Log;
 import org.sonar.core.persistence.DbSession;
 import org.sonar.core.preview.PreviewCache;
-import org.sonar.core.qualityprofile.db.*;
+import org.sonar.core.qualityprofile.db.ActiveRuleDto;
+import org.sonar.core.qualityprofile.db.ActiveRuleKey;
+import org.sonar.core.qualityprofile.db.ActiveRuleParamDto;
+import org.sonar.core.qualityprofile.db.QualityProfileDto;
+import org.sonar.core.qualityprofile.db.QualityProfileKey;
 import org.sonar.core.rule.RuleDto;
 import org.sonar.core.rule.RuleParamDto;
 import org.sonar.server.db.DbClient;
@@ -47,7 +51,6 @@ import org.sonar.server.search.QueryOptions;
 import org.sonar.server.util.TypeValidations;
 
 import javax.annotation.Nullable;
-
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -107,7 +110,7 @@ public class RuleActivator implements ServerComponent {
 
     if (context.activeRule() == null) {
       // new activation
-      change = new ActiveRuleChange(ActiveRuleChange.Type.ACTIVATED, activation.getKey());
+      change = ActiveRuleChange.createFor(ActiveRuleChange.Type.ACTIVATED, activation.getKey());
       if (activation.isCascade() || context.isSameAsParent(activation)) {
         change.setInheritance(ActiveRule.Inheritance.INHERITED);
       }
@@ -120,7 +123,7 @@ public class RuleActivator implements ServerComponent {
         // propagating to descendants, but child profile already overrides rule -> stop propagation
         return changes;
       }
-      change = new ActiveRuleChange(ActiveRuleChange.Type.UPDATED, activation.getKey());
+      change = ActiveRuleChange.createFor(ActiveRuleChange.Type.UPDATED, activation.getKey());
       if (activation.isCascade() && context.activeRule().getInheritance() == null) {
         // activate on child, then on parent -> mark child as overriding parent
         change.setInheritance(ActiveRule.Inheritance.OVERRIDES);
@@ -303,7 +306,7 @@ public class RuleActivator implements ServerComponent {
     if (!force && !isCascade && context.activeRule().getInheritance() != null) {
       throw new IllegalStateException("Cannot deactivate inherited rule '" + key.ruleKey() + "'");
     }
-    change = new ActiveRuleChange(ActiveRuleChange.Type.DEACTIVATED, key);
+    change = ActiveRuleChange.createFor(ActiveRuleChange.Type.DEACTIVATED, key);
     changes.add(change);
     persist(change, context, dbSession);
 
index 38813d810e9708f3e2bc33e25277f9aa3b6648e8..a90898cadec7b59b256046427f72a87fcedfce7f 100644 (file)
@@ -24,8 +24,11 @@ import org.junit.After;
 import org.junit.Before;
 import org.junit.ClassRule;
 import org.junit.Test;
+import org.sonar.api.rule.RuleKey;
 import org.sonar.core.log.Log;
 import org.sonar.core.persistence.DbSession;
+import org.sonar.core.qualityprofile.db.ActiveRuleKey;
+import org.sonar.core.qualityprofile.db.QualityProfileKey;
 import org.sonar.server.db.DbClient;
 import org.sonar.server.log.LogService;
 import org.sonar.server.log.index.LogIndex;
@@ -58,7 +61,11 @@ public class ActiveRuleChangeMediumTest {
 
   @Test
   public void insert_find_active_rule_change() {
-    ActiveRuleChange change = new ActiveRuleChange()
+    ActiveRuleKey key = ActiveRuleKey.of(
+      QualityProfileKey.of("profile", "java"),
+      RuleKey.of("repository", "rule"));
+    ActiveRuleChange change = ActiveRuleChange
+      .createFor(ActiveRuleChange.Type.ACTIVATED, key)
       .setInheritance(ActiveRule.Inheritance.INHERITED)
       .setSeverity("BLOCKER")
       .setParameter("param1", "value1");
@@ -71,5 +78,6 @@ public class ActiveRuleChangeMediumTest {
 
     Log log = Iterables.getFirst(index.findAll().getHits(), null);
     assertThat(log).isNotNull();
+    assertThat(log.details().get("key")).isEqualTo(key.toString());
   }
 }
\ No newline at end of file