Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
bb9edc0
Added fixes to MongoDB connections
SantoshNC68 Aug 23, 2020
f520587
Updated the Mongo query handling to check the object mapping
SantoshNC68 Aug 23, 2020
9f7a0df
Updated java doc for the newly created methods
SantoshNC68 Aug 23, 2020
74d6c4b
Fixed review comments
SantoshNC68 Aug 24, 2020
ab8d08f
Added to debug logs
SantoshNC68 Aug 25, 2020
84cea22
Added code changes to look in the objects instead of the eventId
SantoshNC68 Aug 25, 2020
6a6894e
removed local changes in application.properties
SantoshNC68 Aug 26, 2020
8fd5639
removed local changes in application.properties
SantoshNC68 Aug 26, 2020
2b47e2c
Updated the logger to change from ID to condition
SantoshNC68 Aug 27, 2020
a0b4b42
Added test case and updated comments
SantoshNC68 Sep 1, 2020
3a1753b
Added a new collection name for event object map test
SantoshNC68 Sep 1, 2020
ec68d4b
Added a new collection name for event object map test
SantoshNC68 Sep 1, 2020
319971a
Added a new collection name for event object map test
SantoshNC68 Sep 1, 2020
f462b80
Added a new collection name for event object map test
SantoshNC68 Sep 1, 2020
1199734
Added a new collection name for event object map test
SantoshNC68 Sep 1, 2020
e7e2318
Added loggers for testing
Sep 1, 2020
93f4395
logs for failing tescases
Sep 1, 2020
1acd190
Updated DatabaseManager to check expectedEventIds in objects
SantoshNC68 Sep 1, 2020
81da686
Updated DatabaseManager to check expectedEventIds
SantoshNC68 Sep 1, 2020
863659b
Updated DatabaseManager
SantoshNC68 Sep 1, 2020
dbec64e
Fixes in Database Manager
SantoshNC68 Sep 1, 2020
dba08e6
Fixes in Database Manager
SantoshNC68 Sep 1, 2020
3728090
Fixes in Database Manager
SantoshNC68 Sep 1, 2020
1248e62
Removed the sysouts
SantoshNC68 Sep 1, 2020
abe9240
Modified login in Database Manager
SantoshNC68 Sep 1, 2020
55f5eab
Updated to check events in objects list
SantoshNC68 Sep 1, 2020
f0f3f85
Added other comments
SantoshNC68 Sep 1, 2020
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 @@ -149,9 +149,9 @@ private List<String> compareSentEventsWithEventsInDB(List<String> checklist) {
List<Document> documents = collection.find().into(new ArrayList<>());
for (Document document : documents) {
for (String expectedID : new ArrayList<>(checklist)) {
if (expectedID.equals(document.get("_id").toString())) {
if (document.get("objects").toString().contains(expectedID)) {
checklist.remove(expectedID);
}
}
}
}
return checklist;
Expand Down
40 changes: 23 additions & 17 deletions src/main/java/com/ericsson/ei/handlers/EventToObjectMapHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,30 +81,36 @@ public ArrayList<String> getObjectsForEventId(String eventId) {
return getEventToObjectList(eventId);
}

/**
* To check and save the eventIds to the objectId in the mapped database.
* @param rulesObject
* @param event
* @param objectId aggregated event object Id
*/
public void updateEventToObjectMapInMemoryDB(RulesObject rulesObject, String event, String objectId) {
String eventId = getEventId(rulesObject, event);
String condition = "{\"_id\" : \"" + eventId + "\"}";
ArrayList<String> list = getEventToObjectList(eventId);
boolean firstTime = list.isEmpty();
list = updateList(list, eventId, objectId);
ObjectMapper mapper = new ObjectMapper();
JsonNode entry = null;

String condition = "{\"_id\" : \"" + objectId + "\"}";
LOGGER.debug("Checking document exists in the collection with condition : {}\n EventId : {}", condition, eventId);
boolean docExists = mongodbhandler.checkDocumentExists(databaseName, collectionName, condition);
try {
entry = new ObjectMapper().readValue(condition, JsonNode.class);
ArrayNode jsonNode = mapper.convertValue(list, ArrayNode.class);
((ObjectNode) entry).set(listPropertyName, mapper.readTree(jsonNode.toString()));
String mapStr = entry.toString();
LOGGER.debug("MongoDbHandler Insert/Update Event: {}\nto database: {} and to Collection: {}", mapStr, databaseName, collectionName);
if (firstTime) {
mongodbhandler.insertDocument(databaseName, collectionName, mapStr);
if (!docExists) {
ArrayList<String> list = new ArrayList<String>();
list.add(eventId);
final ObjectMapper mapper = new ObjectMapper();
JsonNode entry = new ObjectMapper().readValue(condition, JsonNode.class);
ArrayNode jsonNode = mapper.convertValue(list, ArrayNode.class);
((ObjectNode) entry).set(listPropertyName, mapper.readTree(jsonNode.toString()));
final String mapStr = entry.toString();
LOGGER.debug("MongoDbHandler Insert/Update Event: {}\nto database: {} and to Collection: {}", mapStr, databaseName, collectionName);
mongodbhandler.insertDocument(databaseName, collectionName, mapStr );
} else {
mongodbhandler.updateDocument(databaseName, collectionName, condition, mapStr);
mongodbhandler.updateDocumentAddToSet(databaseName, collectionName, condition, eventId);
}
} catch (Exception e) {
LOGGER.info("Failed to update event object list.", e);
LOGGER.error("Failed to update event object list.", e);
}
}


public String getEventId(RulesObject rulesObject, String event) {
String idRule = rulesObject.getIdRule();
Expand Down Expand Up @@ -148,7 +154,7 @@ public boolean deleteEventObjectMap(String templateName) {
}

public boolean isEventInEventObjectMap(String eventId) {
String condition = "{\"_id\" : \"" + eventId + "\"}";
String condition = "{\"objects\": { \"$in\" : [\"" + eventId + "\"]} }";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the top of this class a variable is defined for this key called 'listPropertyName' - use it here also. However, given that you've changed the structure of this document to look like:

```
"_id": "aggregated-object-id",
 "objects": ["event-id", "event-id"]
```

the key of the list should perhaps be called "events" instead of "objects"? It would look somehting like this instead:

```
"_id": "aggregated-object-id",
 "events": ["event-id", "event-id"]
```

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the top of this class a variable is defined for this key called 'listPropertyName' - use it here also. However, given that you've changed the structure of this document to look like:

"_id": "aggregated-object-id",
"objects": ["event-id", "event-id"]

the key of the list should perhaps be called "events" instead of "objects"? It would look somehting like this instead:

"_id": "aggregated-object-id",
"events": ["event-id", "event-id"]

changed "events" instead of "objects"

Copy link
Contributor

@durga-vasaadi durga-vasaadi Sep 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"events" instead of "objects" changes will come in future, this changes will be release ASAP, could you please review the current changes

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see another PR open, which duplicates this one. Please update the changes in this PR and close the other one (then we keep the review history also) It's possible for @SantoshNC68 to allow @durga-vasaadi as collaborator on your fork, and this way you can both push to this one PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I have done that and we have worked together, but because of the issues on the travis we have another PR. We will close that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also we will not make changes for events in this PR but will do it another PR as we need to see the impact of that.

List<String> documents = mongodbhandler.find(databaseName, collectionName, condition);
return !documents.isEmpty();
}
Expand Down
46 changes: 45 additions & 1 deletion src/main/java/com/ericsson/ei/handlers/MongoDBHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.IndexOptions;
import com.mongodb.client.model.Indexes;
import com.mongodb.client.model.Updates;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import com.mongodb.util.JSON;
Expand Down Expand Up @@ -137,7 +138,7 @@ public ArrayList<String> getAllDocuments(String dataBaseName, String collectionN
*
* @param dataBaseName
* @param collectionName
* @param condition string json
* @param condition a condition to find a requested object in the database
* @return
*/
public ArrayList<String> find(String dataBaseName, String collectionName, String condition) {
Expand Down Expand Up @@ -342,4 +343,47 @@ public void dropDatabase(String databaseName) {
MongoDatabase db = mongoClient.getDatabase(databaseName);
db.drop();
}

/**
* Check if the document exists
* @param databaseName
* @param collectionName
* @param condition a condition to find a requested object in the database
* @return
*/
public boolean checkDocumentExists(String databaseName, String collectionName, String condition) {
MongoDatabase db = mongoClient.getDatabase(databaseName);
MongoCollection<Document> mongoCollection = db.getCollection(collectionName);
Document doc = mongoCollection.find(BasicDBObject.parse(condition)).first();
if (doc == null || doc.isEmpty()) {
return false;
}
return true;
}


/**
* Update the existing documents with unique objects list
* Used only in EventToObjectMapHandler.java
* @param dataBaseName
* @param collectionName
* @param condition a condition to find a requested object in the database
* @param eventId eventId to update in the mapper collection
* @return
*/
public boolean updateDocumentAddToSet(String dataBaseName, String collectionName, String condition, String eventId) {
try {
MongoCollection<Document> collection = getMongoCollection(dataBaseName, collectionName);
if (collection != null) {
final Document dbObjectInput = Document.parse(condition);
UpdateResult updateMany = collection.updateOne(dbObjectInput, Updates.addToSet("objects", eventId));
LOGGER.debug("updateDocument() :: database: {} and collection: {} is document Updated : {}", dataBaseName, collectionName, updateMany.wasAcknowledged());
return updateMany.wasAcknowledged();
}
} catch (Exception e) {
LOGGER.error("Failed to update document.", e);
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public JsonNode runRuleOnEvent(String rule, String event) {
Expression<JsonNode> expression = jmespath.compile(rule);
JsonNode eventJson = objectMapper.readValue(event, JsonNode.class);
result = expression.search(eventJson);
LOGGER.debug("Expression : {} \n RESULT VALUE FROM JMESPATH : {}" , expression, result);
} catch (Exception e) {
LOGGER.error("Failed to run rule on event.\nRule: {}\nEvent: {}", rule, event, e);
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/ericsson/ei/jsonmerge/MergeHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@ public String mergeObject(String id, String mergeId, RulesObject rules, String e
try {
// lock and get the AggregatedObject
String aggregatedObject = getAggregatedObject(id, true);
LOGGER.debug("AGGREGATED OBJECT : " + aggregatedObject);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you really want to log the entire aggregation twice? They can be very big, so it might clutter the log.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed logger

String mergeRule = getMergeRules(rules);
if (mergeRule != null && !mergeRule.isEmpty()) {
String updatedRule = replaceIdMarkerInRules(mergeRule, mergeId);
String ruleForMerge = jmesPathInterface.runRuleOnEvent(updatedRule, event).toString();
String mergePath = prepareMergePrepareObject.getMergePath(aggregatedObject, ruleForMerge, false);
preparedToMergeObject = prepareMergePrepareObject.addMissingLevels(aggregatedObject,
objectToMerge.toString(), ruleForMerge, mergePath);
LOGGER.debug("PREPARE TO MERGE OBJECT : " + preparedToMergeObject);
} else {
preparedToMergeObject = objectToMerge.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class WaitListStorageHandler {
public void addEventToWaitListIfNotExisting(String event, RulesObject rulesObject) {
try {
JsonNode id = extractIdFromEventUsingRules(event, rulesObject);
String foundEvent = findEventInWaitList(id);
String foundEvent = findEventInWaitList(id.textValue());
if (foundEvent.isEmpty()) {
Date date = createCurrentTimeStamp();
BasicDBObject document = createWaitListDocument(event, id, date);
Expand All @@ -82,7 +82,7 @@ public void addEventToWaitListIfNotExisting(String event, RulesObject rulesObjec
}
}

private String findEventInWaitList(JsonNode id) {
private String findEventInWaitList(String id) {
String condition = "{\"_id\" : \"" + id + "\"}";
List<String> foundEventsInWaitList = mongoDbHandler.find(databaseName, collectionName, condition);
if (foundEventsInWaitList.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,23 @@ public class MongoDBHandlerTest {

private String dataBaseName = "MongoDBHandlerTestDB";
private String collectionName = "SampleEvents";
private String mapCollectionName = "SampleEventObjectMap";
private String input = "{\"id\":\"eventId\",\"type\":\"eventType11\",\"test_cases\" : [{\"event_id\" : \"testcaseid1\", \"test_data\" : \"testcase1data\"},{\"event_id\" : \"testcaseid2\", \"test_data\" : \"testcase2data\"}]}";
private String updateInput = "{\"id\":\"eventId\",\"type\":\"eventType11\",\"test_cases\" : [{\"event_id\" : \"testcaseid1\", \"test_data\" : \"testcase2data\"},{\"event_id\" : \"testcaseid3\", \"test_data\" : \"testcase3data\"}]}";
private String condition = "{\"test_cases.event_id\" : \"testcaseid1\"}";

//Added to test new functionality for EventToObjectMapHandler
private String conditionForEventToObjectMap = "{\"_id\" : \"testid1\"}";
private String inputForEventToObjectMap = "{\"_id\" : \"testid1\", \"objects\" : [\"eventid1\", \"eventid2\"]}";
private String updateInputForEventToObjectMap = "\"eventid3\"";

@Before
public void init() throws Exception {
TestConfigs.init();
mongoDBHandler = new MongoDBHandler();
mongoDBHandler.setMongoClient(TestConfigs.getMongoClient());
mongoDBHandler.insertDocument(dataBaseName, collectionName, input);
mongoDBHandler.insertDocument(dataBaseName, mapCollectionName, inputForEventToObjectMap);
}

@Test
Expand All @@ -69,5 +76,17 @@ public void testUpdateDocument() {
@After
public void dropCollection() {
assertTrue(mongoDBHandler.dropDocument(dataBaseName, collectionName, condition));
mongoDBHandler.dropCollection(dataBaseName, mapCollectionName);
}

//Added test cases for EventToObjectMapHandler
@Test
public void checkDocument() {
assertTrue(mongoDBHandler.checkDocumentExists(dataBaseName, mapCollectionName, conditionForEventToObjectMap));
}

@Test
public void updateEventToObjectMap() {
assertTrue(mongoDBHandler.updateDocumentAddToSet(dataBaseName, mapCollectionName, conditionForEventToObjectMap, updateInputForEventToObjectMap));
}
}