public class RuleImpl implements Rule {
+
+
@Override
public RuleKey key() {
// TODO Auto-generated method stub
package org.sonar.server.rule2;
import org.sonar.core.qualityprofile.db.ActiveRuleDto;
-
import org.sonar.core.qualityprofile.db.ActiveRuleDao;
import org.apache.commons.beanutils.BeanUtils;
import org.elasticsearch.common.xcontent.XContentBuilder;
}
}
+ private void addMatchField(XContentBuilder mapping, String field, String type) throws IOException {
+ mapping.startObject(field)
+ .field("type", type)
+ .field("index", "not_analyzed")
+ .endObject();
+ }
+
@Override
protected XContentBuilder getMapping() {
try {
- return jsonBuilder().startObject()
+ XContentBuilder mapping = jsonBuilder().startObject()
.startObject(this.getType())
- .field("dynamic",true)
- .startObject("properties")
- .startObject("id")
- .field("type", "string")
- .field("index", "not_analyzed")
- .endObject()
- .startObject("key")
- .field("type", "string")
- .field("index", "not_analyzed")
- .endObject()
- .startObject("repositoryKey")
- .field("type", "string")
- .field("index", "not_analyzed")
- .endObject()
- .startObject("severity")
- .field("type", "string")
- .field("index", "not_analyzed")
- .endObject()
- .endObject()
+ .field("dynamic", true)
+ .startObject("properties");
+
+ addMatchField(mapping, "id", "string");
+ addMatchField(mapping, "key", "string");
+ addMatchField(mapping, "repositoryKey", "string");
+ addMatchField(mapping, "severity", "string");
+
+ mapping.startObject("active")
+ .field("type", "nested")
+ .field("dynamic", true)
+ .endObject();
+
+ return mapping.endObject()
.endObject().endObject();
// return jsonBuilder().startObject()
@Override
public XContentBuilder normalize(RuleKey key) {
- RuleDto rule = dao.getByKey(key);
-
try {
+ long start = System.currentTimeMillis();
+ RuleDto rule = dao.getByKey(key);
+ LOG.info("Action dao.getByKey(key) in {} took {}ms",
+ this.getIndexName(), (System.currentTimeMillis() - start));
+
XContentBuilder document = jsonBuilder().startObject();
Map<String, Object> properties = BeanUtils.describe(rule);
for (Entry<String, Object> property : properties.entrySet()) {
- LOG.info("NORMALIZING: {} -> {}",property.getKey(), property.getValue());
+ LOG.trace("NORMALIZING: {} -> {}", property.getKey(), property.getValue());
document.field(property.getKey(), property.getValue());
}
+ start = System.currentTimeMillis();
- for(ActiveRuleDto activeRule:activeRuleDao.selectByRuleId(rule.getId())){
+ document.startArray("active");
+ for (ActiveRuleDto activeRule : activeRuleDao.selectByRuleId(rule.getId())) {
+ document.startObject();
Map<String, Object> activeRuleProperties = BeanUtils.describe(activeRule);
for (Entry<String, Object> activeRuleProp : activeRuleProperties.entrySet()) {
- LOG.info("NORMALIZING: --- {} -> {}",activeRuleProp.getKey(), activeRuleProp.getValue());
+ LOG.trace("NORMALIZING: --- {} -> {}", activeRuleProp.getKey(), activeRuleProp.getValue());
document.field(activeRuleProp.getKey(), activeRuleProp.getValue());
}
+ document.endObject();
}
+ document.endArray();
+
+ LOG.info("Action activeRuleDao.selectByRuleId(rule.getId()) in {} took {}ms",
+ this.getIndexName(), (System.currentTimeMillis() - start));
return document.endObject();
} catch (IOException e) {
- LOG.error("Could not normalize {} in {}", key, this.getClass().getSimpleName());
- e.printStackTrace();
+ LOG.error("Could not normalize {} in {} because {}",
+ key, this.getClass().getSimpleName(), e.getMessage());
} catch (IllegalAccessException e) {
- LOG.error("Could not normalize {} in {}", key, this.getClass().getSimpleName());
- e.printStackTrace();
+ LOG.error("Could not normalize {} in {} because {}",
+ key, this.getClass().getSimpleName(), e.getMessage());
} catch (InvocationTargetException e) {
- LOG.error("Could not normalize {} in {}", key, this.getClass().getSimpleName());
- e.printStackTrace();
+ LOG.error("Could not normalize {} in {} because {}",
+ key, this.getClass().getSimpleName(), e.getMessage());
} catch (NoSuchMethodException e) {
- LOG.error("Could not normalize {} in {}", key, this.getClass().getSimpleName());
- e.printStackTrace();
+ LOG.error("Could not normalize {} in {} because {}",
+ key, this.getClass().getSimpleName(), e.getMessage());
}
return null;
}
*/
package org.sonar.server.rule2;
+import org.apache.commons.beanutils.BeanUtils;
+
+import org.sonar.api.ServerComponent;
import org.sonar.api.rule.RuleKey;
import org.sonar.core.rule.RuleDao;
import org.sonar.core.rule.RuleDto;
/**
* @since 4.4
*/
-public class RuleService {
+public class RuleService implements ServerComponent {
private RuleDao dao;
private RuleIndex index;
@CheckForNull
public Rule getByKey(RuleKey key) {
- return null;
+ Hit hit = index.getByKey(key);
+ if(hit != null){
+ return toRule(hit);
+ } else {
+ return null;
+ }
}
public Collection<Hit> search(RuleQuery query){
+
return Collections.emptyList();
}
}
public static Rule toRule(Hit hit){
+ for()
+ BeanUtils.setProperty(bean, name, value);
return new RuleImpl();
}
}
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
+import org.elasticsearch.action.update.UpdateRequest;
+import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.slf4j.Logger;
public void connect() {
this.client = this.node.client();
-// /* Settings to access our local ES node */
-// Settings settings = ImmutableSettings.settingsBuilder()
-// .put("client.transport.sniff", true)
-// .put("cluster.name", ES_CLUSTER_NAME)
-// .put("node.name", "localclient_")
-// .build();
-//
-// this.client = new TransportClient(settings)
-// .addTransportAddress(new InetSocketTransportAddress(LOCAL_ES_NODE_HOST, LOCAL_ES_NODE_PORT));
-//
-// /*
-// * Cannot do that yet, need version >= 1.0
-// * ImmutableList<DiscoveryNode> nodes = client.connectedNodes();
-// * if (nodes.isEmpty()) {
-// * throw new ElasticSearchUnavailableException("No nodes available. Verify ES is running!");
-// * } else {
-// * log.info("connected to nodes: " + nodes.toString());
-// * }
-// */
+ // /* Settings to access our local ES node */
+ // Settings settings = ImmutableSettings.settingsBuilder()
+ // .put("client.transport.sniff", true)
+ // .put("cluster.name", ES_CLUSTER_NAME)
+ // .put("node.name", "localclient_")
+ // .build();
+ //
+ // this.client = new TransportClient(settings)
+ // .addTransportAddress(new InetSocketTransportAddress(LOCAL_ES_NODE_HOST, LOCAL_ES_NODE_PORT));
+ //
+ // /*
+ // * Cannot do that yet, need version >= 1.0
+ // * ImmutableList<DiscoveryNode> nodes = client.connectedNodes();
+ // * if (nodes.isEmpty()) {
+ // * throw new ElasticSearchUnavailableException("No nodes available. Verify ES is running!");
+ // * } else {
+ // * log.info("connected to nodes: " + nodes.toString());
+ // * }
+ // */
}
/* Cluster And ES Stats/Client methods */
if (!indexExistsResponse.isExists()) {
- LOG.info("Setup of index {}",this.getIndexName());
+ LOG.info("Setup of index {}", this.getIndexName());
try {
- LOG.info("Settings: {}",getIndexSettings().string());
+ LOG.info("Settings: {}", getIndexSettings().string());
LOG.info("Mapping: {}", getMapping().string());
} catch (IOException e) {
// TODO Auto-generated catch block
@Override
public void executeAction(IndexAction<K> action) {
+ StopWatch watch = this.createWatch();
+ long start = System.currentTimeMillis();
if (action.getMethod().equals(Method.DELETE)) {
this.delete(action.getKey());
} else if (action.getMethod().equals(Method.INSERT)) {
} else if (action.getMethod().equals(Method.UPDATE)) {
this.update(action.getKey());
}
+ LOG.info("Action {} in {} took {}ms", action.getMethod(),
+ this.getIndexName(), (System.currentTimeMillis() - start));
+ watch.stop("Action {} in {}", action.getMethod(), this.getIndexName());
}
/* Index management methods */
@Override
public void insert(K key) {
- this.update(key);
+ try {
+ XContentBuilder doc = this.normalize(key);
+ String keyvalue = this.getKeyValue(key);
+ if (doc != null && keyvalue != null && !keyvalue.isEmpty()) {
+ LOG.info("Update document with key {}", key);
+ IndexResponse result = getClient().index(
+ new IndexRequest(this.getIndexName(),
+ this.getType(), keyvalue)
+ .source(this.normalize(key))).get();
+ } else {
+ LOG.error("Could not normalize document {} for insert in ",
+ key, this.getIndexName());
+ }
+ } catch (Exception e) {
+ LOG.error("Could not update documet for index {}: {}",
+ this.getIndexName(), e.getMessage());
+ }
}
@Override
public void update(K key) {
- LOG.info("Update document with key {}", key);
- IndexResponse result = getClient().index(new IndexRequest()
- .type(this.getType())
- .index(this.getIndexName())
- .id(this.getKeyValue(key))
- .source(this.normalize(key))).actionGet();
-
+ try {
+ LOG.info("Update document with key {}", key);
+ UpdateResponse result = getClient().update(
+ new UpdateRequest(this.getIndexName(),
+ this.getType(), this.getKeyValue(key))
+ .doc(this.normalize(key))).get();
+ } catch (Exception e) {
+ LOG.error("Could not update documet for index {}: {}",
+ this.getIndexName(), e.getMessage());
+ }
}
@Override
@Override
public Long getLastSynchronization() {
- // need to read that in the admin index;
+ // TODO need to read that in the admin index;
return 0l;
}
}
*/
package org.sonar.server.rule2;
+import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.rule.Severity;
public class RuleMediumTest {
@Rule
- public ServerTester tester = new ServerTester();
+ public ServerTester tester = new ServerTester()
+ //.setProperty("sonar.log.profilingLevel", "FULL")
+ .setProperty("sonar.es.http.port", "8888");
+
+ private RuleDto dto;
+ private ActiveRuleDto adto;
+
+ @Before
+ public void setup(){
+ dto = getRuleDto(1);
+ adto = getActiveRuleDto(dto);
+ }
private ActiveRuleDto getActiveRuleDto(RuleDto dto){
return new ActiveRuleDto()
.setSeverity(3);
}
- private RuleDto getRuleDto() {
+ private RuleDto getRuleDto(int id) {
return new RuleDto()
- .setId(1)
- .setRuleKey("NewRuleKey")
+ .setId(id)
+ .setRuleKey("NewRuleKey="+id)
.setRepositoryKey("plugin")
.setName("new name")
.setDescription("new description")
@Test
public void test_dao_queue_es_search_loop() {
+
RuleDao dao = tester.get(RuleDao.class);
ActiveRuleDao adao = tester.get(ActiveRuleDao.class);
RuleIndex index = tester.get(RuleIndex.class);
- RuleDto dto = getRuleDto();
dao.insert(dto);
-
- adao.insert(getActiveRuleDto(dto));
+ adao.insert(adto);
try {
- Thread.sleep(100);
+ Thread.sleep(200);
} catch (InterruptedException e) {
;
}
Hit hit = index.getByKey(dto.getKey());
assertThat(hit.getFields().get("ruleKey")).isEqualTo(dto.getRuleKey());
}
+
+ @Test
+ public void test_ruleservice_getByKey() {
+ RuleService service = tester.get(RuleService.class);
+ RuleDao dao = tester.get(RuleDao.class);
+
+ dao.insert(dto);
+
+ //org.sonar.server.rule2.Rule rule = service.getByKey(dto.getKey());
+
+ //assertThat(rule.key()).isEqualTo(dto.getKey());
+
+
+ }
}