Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import javax.annotation.PostConstruct;

import org.apache.commons.lang3.StringUtils;
import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -35,6 +36,7 @@
import com.ericsson.ei.mongo.MongoDBHandler;
import com.ericsson.ei.mongo.MongoStringQuery;
import com.ericsson.ei.rules.RulesObject;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -65,14 +67,14 @@ public class EventToObjectMapHandler {
@Autowired
JmesPathInterface jmesPathInterface;

@Value("${aggregations.collection.ttl:0}")
@Value("${aggregations.collection.ttl}")
private String eventToObjectTtl;

@PostConstruct
public void init() throws AbortExecutionException {
try {
if (Integer.parseInt(eventToObjectTtl) > 0) {
mongodbhandler.createTTLIndex(databaseName, collectionName, MongoConstants.TIME, Integer.parseInt(eventToObjectTtl));
if (getTtl() > 0) {
mongodbhandler.createTTLIndex(databaseName, collectionName, MongoConstants.TIME, getTtl());
}
} catch (Exception e) {
LOGGER.error("Failed to create an index for {} due to: {}", collectionName, e);
Expand Down Expand Up @@ -135,7 +137,7 @@ public void updateEventToObjectMapInMemoryDB(RulesObject rulesObject, String eve
mapStr, databaseName, collectionName);
Document document = Document.parse(mapStr);
document.append("Time", DateUtils.getDate());
mongodbhandler.insertDocumentObject(databaseName, collectionName, document);
mongodbhandler.insertDocumentObject(databaseName, collectionName, document, condition, eventId);
} else {
mongodbhandler.updateDocumentAddToSet(databaseName, collectionName, condition,
eventId);
Expand Down Expand Up @@ -194,5 +196,24 @@ public boolean isEventInEventObjectMap(String eventId) {
List<String> documents = mongodbhandler.find(databaseName, collectionName, query);
return !documents.isEmpty();
}


/**
* This method gives the TTL (time to live) value for documents stored in the database. This
* value is set in application.properties when starting Eiffel Intelligence.
*
* @return ttl Integer value representing time to live for documents
*/
@JsonIgnore
public int getTtl() {
int ttl = 0;
if (StringUtils.isNotEmpty(eventToObjectTtl)) {
try {
ttl = Integer.parseInt(eventToObjectTtl);
} catch (NumberFormatException e) {
LOGGER.error("Failed to parse TTL value.", e);
}
}
return ttl;
}

}
11 changes: 10 additions & 1 deletion src/main/java/com/ericsson/ei/mongo/MongoDBHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.ericsson.ei.handlers.DateUtils;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.mongodb.BasicDBObject;
import com.mongodb.ErrorCategory;
import com.mongodb.MongoClientException;
import com.mongodb.MongoCommandException;
import com.mongodb.MongoConfigurationException;
Expand Down Expand Up @@ -122,9 +123,12 @@ public void insertDocument(String dataBaseName, String collectionName, String in
* @param dataBaseName
* @param collectionName
* @param document - Document object to insert
* @param condition - a condition to find a requested object in the database
* @param eventId - eventId to update in the mapper collection
* @throws MongoWriteException
*/
public void insertDocumentObject(String dataBaseName, String collectionName, Document document) throws MongoWriteException {
public void insertDocumentObject(String dataBaseName, String collectionName, Document document, MongoCondition condition, String eventId)
throws MongoWriteException {
try {
MongoCollection<Document> collection = getMongoCollection(dataBaseName, collectionName);

Expand All @@ -137,6 +141,11 @@ public void insertDocumentObject(String dataBaseName, String collectionName, Doc
collectionName, dataBaseName);
}

} catch(MongoWriteException e) {
if(e.getError().getCategory() == ErrorCategory.DUPLICATE_KEY) {
LOGGER.debug("Duplicate key insertion for {} in collection {} and Update event-to-object map collection for condition {} with eventId {}", document, collectionName, condition, eventId);
updateDocumentAddToSet(dataBaseName, collectionName, condition, eventId);
}
} catch (Exception e) {
LOGGER.error("Failed to insert Object: {} \n in collection: {} and database {}. \n {}", document,
collectionName, dataBaseName, e.getMessage());
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/com/ericsson/ei/mongo/MongoDBHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.List;

import org.bson.Document;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
Expand All @@ -49,6 +51,9 @@ public class MongoDBHandlerTest {
"testid1");
private String inputForEventToObjectMap = "{\"_id\" : \"testid1\", \"objects\" : [\"eventid1\", \"eventid2\"]}";
private String updateInputForEventToObjectMap = "\"eventid3\"";
private String inputForEventToObjectMapDuplicate = "{\"_id\" : \"testid1\", \"objects\" : [\"eventid4\"]}";
private String updateInputForEventToObjectMapDuplicate = "eventid4";
Document document = Document.parse(inputForEventToObjectMapDuplicate);

@Before
public void init() throws Exception {
Expand Down Expand Up @@ -98,6 +103,19 @@ public void updateEventToObjectMap() {
assertTrue(mongoDBHandler.updateDocumentAddToSet(dataBaseName, mapCollectionName,
conditionForEventToObjectMap, updateInputForEventToObjectMap));
}

@Test
public void insertEventToObjectMapDuplicate() {
mongoDBHandler.insertDocumentObject(dataBaseName, mapCollectionName, document, conditionForEventToObjectMap, updateInputForEventToObjectMapDuplicate);
assertTrue(isEventInEventObjectMap(updateInputForEventToObjectMapDuplicate));
}

public boolean isEventInEventObjectMap(String eventId) {
String condition = "{\"objects\": { \"$in\" : [\"" + eventId + "\"]} }";
MongoStringQuery query = new MongoStringQuery(condition);
List<String> documents = mongoDBHandler.find(dataBaseName, mapCollectionName, query);
return !documents.isEmpty();
}

@Test
public void checkMongoDBStatusUp() {
Expand Down