@Override
public Date time() {
- return this.getField(ActivityNormalizer.LogFields.DATE.field());
+ return this.getField(ActivityNormalizer.LogFields.CREATED_AT.field());
}
@Override
public static final class LogFields extends Indexable {
public final static IndexField TYPE = addSortable(IndexField.Type.STRING, "type");
- public final static IndexField DATE = addSortable(IndexField.Type.DATE, "date");
+ public final static IndexField CREATED_AT = addSortable(IndexField.Type.DATE, "createdAt");
+ public final static IndexField UPDATED_AT = addSortable(IndexField.Type.DATE, BaseNormalizer.UPDATED_AT_FIELD);
public final static IndexField AUTHOR = addSearchable(IndexField.Type.STRING, "author");
public final static IndexField DETAILS = addSearchable(IndexField.Type.OBJECT, "details");
public final static IndexField MESSAGE = addSearchable(IndexField.Type.STRING, "message");
logDoc.put(LogFields.TYPE.field(), dto.getType());
logDoc.put(LogFields.AUTHOR.field(), dto.getAuthor());
logDoc.put(LogFields.MESSAGE.field(), dto.getMessage());
- logDoc.put(LogFields.DATE.field(), dto.getCreatedAt());
+ logDoc.put(LogFields.CREATED_AT.field(), dto.getCreatedAt());
+ logDoc.put(LogFields.UPDATED_AT.field(), dto.getUpdatedAt());
logDoc.put(LogFields.DETAILS.field(), KeyValueFormat.parse(dto.getData()));
public ActivityMapping(Languages languages, MacroInterpreter macroInterpreter) {
super();
addIndexStringField("type", ActivityNormalizer.LogFields.TYPE.field());
- addIndexDatetimeField("createdAt", ActivityNormalizer.LogFields.DATE.field());
+ addIndexDatetimeField("createdAt", ActivityNormalizer.LogFields.CREATED_AT.field());
addIndexStringField("userLogin", ActivityNormalizer.LogFields.AUTHOR.field());
addIndexStringField("message", ActivityNormalizer.LogFields.MESSAGE.field());
addField("details", new DetailField());
import org.sonar.server.search.es.ListUpdate;
import java.lang.reflect.Field;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> {
public static final IndexField NAME = addSortableAndSearchable(IndexField.Type.STRING, "name");
public static final IndexField CREATED_AT = addSortable(IndexField.Type.DATE, "createdAt");
- public static final IndexField UPDATED_AT = addSortable(IndexField.Type.DATE, "updatedAt");
+ public static final IndexField UPDATED_AT = addSortable(IndexField.Type.DATE, UPDATED_AT_FIELD);
public static final IndexField HTML_DESCRIPTION = addSearchable(IndexField.Type.TEXT, "htmlDesc");
public static final IndexField SEVERITY = add(IndexField.Type.STRING, "severity");
public static final IndexField STATUS = add(IndexField.Type.STRING, "status");
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.metrics.valuecount.InternalValueCount;
+import org.elasticsearch.search.sort.SortOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.core.cluster.WorkQueue;
/* Synchronization methods */
- private void setLastSynchronization() {
- Date time = new Date();
- if (time.after(getLastSynchronization())) {
- LOG.debug("Updating synchTime updating");
- getClient().prepareUpdate()
- .setId(indexDefinition.getIndexName())
- .setType(indexDefinition.getManagementType())
- .setIndex(indexDefinition.getManagementIndex())
- .setDoc("updatedAt", time)
- .get();
- }
- }
-
@Override
public Date getLastSynchronization() {
- return (java.util.Date) getClient().prepareGet()
- .setIndex(indexDefinition.getManagementIndex())
- .setId(this.getIndexName())
- .setType(indexDefinition.getManagementType())
- .get().getField("updatedAt").getValue();
+ Date date;
+ try {
+ date = getClient().prepareSearch(this.getIndexName())
+ .setTypes(this.getIndexType())
+ .setQuery(QueryBuilders.matchAllQuery())
+ .setSize(1)
+ .addField(BaseNormalizer.UPDATED_AT_FIELD)
+ .addSort(BaseNormalizer.UPDATED_AT_FIELD, SortOrder.DESC)
+ .get().getHits().getAt(0).field(BaseNormalizer.UPDATED_AT_FIELD).getValue();
+ } catch (Exception e) {
+ date = new Date(0L);
+ }
+ LOG.info("Index {}:{} has last update of {}", this.getIndexName(), this.getIndexType(), date);
+ return date;
}
/* Index management methods */
public abstract class BaseNormalizer<DTO extends Dto<KEY>, KEY extends Serializable> {
+ public static final String UPDATED_AT_FIELD = "updatedAt";
+
protected final DbClient db;
protected final IndexDefinition definition;