Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Motech 1658 #316

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
@@ -1,20 +1,12 @@
package org.motechproject.eventlogging.converter.impl;

import org.joda.time.DateTime;
import org.motechproject.commons.api.MotechException;
import org.motechproject.event.MotechEvent;
import org.motechproject.eventlogging.converter.EventToLogConverter;
import org.motechproject.eventlogging.matchers.LogMappings;
import org.motechproject.eventlogging.matchers.DbLoggableEvent;
import org.motechproject.eventlogging.domain.EventLog;
import org.motechproject.eventlogging.matchers.KeyValue;
import org.motechproject.eventlogging.matchers.LoggableEvent;
import org.motechproject.eventlogging.matchers.MappedLoggableEvent;
import org.springframework.stereotype.Component;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* Implementaton of the {@link EventToLogConverter} interface.
* This class is responsible for converting {@link MotechEvent}s to the
Expand All @@ -34,54 +26,16 @@ public EventLog convertToLog(MotechEvent eventToLog) {

/**
* Converts an event to a {@link EventLog} object, which represents a single
* logged event in database. Uses {@link DbLoggableEvent} implementation of LoggableEvent
* logged event in database. Uses {@link MappedLoggableEvent} implementation of LoggableEvent
* to configure the returned object.
*
* @param eventToLog the incoming motech event to be converted to a database event log
* @param loggableEvent contains data about an event to log, used to configure returned log instance
* @return EventLog object that is used to log the event to the database
* @throws org.motechproject.commons.api.MotechException if the passed LoggableEvent is not an instance of DbLoggableEvent
* @throws org.motechproject.commons.api.MotechException if the passed LoggableEvent is not an instance of MappedLoggableEvent
*/
public EventLog configuredConvertEventToDbLog(MotechEvent eventToLog, LoggableEvent loggableEvent) {
if (!(loggableEvent instanceof DbLoggableEvent)) {
throw new MotechException("Error: unexpected loggable event type " + loggableEvent.getClass().getName());
}
DbLoggableEvent dbLoggableEvent = (DbLoggableEvent) loggableEvent;

LogMappings mappings = dbLoggableEvent.getMappings();

List<KeyValue> keyValueList = mappings.getMappings();

List<String> exclusions = mappings.getExclusions();

List<String> inclusions = mappings.getInclusions();

Map<String, Object> initialParameters = eventToLog.getParameters();

Map<String, Object> finalParameters = new LinkedHashMap<String, Object>(initialParameters);

for (KeyValue keyValue : keyValueList) {
if (initialParameters.containsKey(keyValue.getStartKey())) {
if (keyValue.getStartValue().equals(initialParameters.get(keyValue.getStartKey()))) {
finalParameters.put(keyValue.getEndKey(), keyValue.getEndValue());
exclusions.add(keyValue.getStartKey());
}
}
}

for (String excludeParameter : exclusions) {
if (initialParameters.containsKey(excludeParameter)) {
finalParameters.remove(excludeParameter);
}
}

for (String includeParameter : inclusions) {
if (initialParameters.containsKey(includeParameter)) {
finalParameters.put(includeParameter, initialParameters.get(includeParameter));
}
}

EventLog eventLog = new EventLog(eventToLog.getSubject(), finalParameters, DateTime.now());
public EventLog configuredConvertEventToDbLog(MotechEvent eventToLog, MappedLoggableEvent loggableEvent) {
EventLog eventLog = new EventLog(eventToLog.getSubject(), loggableEvent.filterParams(eventToLog), DateTime.now());

return eventLog;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.joda.time.DateTime;
import org.motechproject.event.MotechEvent;
import org.motechproject.eventlogging.converter.EventToLogConverter;
import org.motechproject.eventlogging.matchers.MappedLoggableEvent;
import org.springframework.stereotype.Component;

import java.util.Map;
Expand All @@ -17,11 +18,25 @@ public class DefaultFileToLogConverter implements EventToLogConverter<String> {

@Override
public String convertToLog(MotechEvent event) {
StringBuilder log = new StringBuilder("EVENT: ");
return convertToLogString(event.getSubject(), event.getParameters());
}

log.append(event.getSubject() + " at TIME: " + DateTime.now());
/**
* Converts an event into a {@link String} with parameters configured by {@link MappedLoggableEvent}.
* @param event Motech event to be converted into a file event log
* @param loggableEvent Used to configure parameter conversion
* @return String used to log into file.
*/
public String convertToLogMapped(MotechEvent event, MappedLoggableEvent loggableEvent) {
Map<String, Object> finalParameters = loggableEvent.filterParams(event);

Map<String, Object> parameters = event.getParameters();
return convertToLogString(event.getSubject(), finalParameters);
}

private String convertToLogString(String subject, Map<String, Object> parameters) {
StringBuilder log = new StringBuilder("EVENT: ");

log.append(subject + " at TIME: " + DateTime.now());

if (parameters.size() > 0) {
log.append(" with PARAMETERS: ");
Expand All @@ -33,4 +48,5 @@ public String convertToLog(MotechEvent event) {

return log.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import org.motechproject.event.MotechEvent;
import org.motechproject.eventlogging.converter.impl.DefaultDbToLogConverter;
import org.motechproject.eventlogging.matchers.DbLoggableEvent;
import org.motechproject.eventlogging.domain.EventLog;
import org.motechproject.eventlogging.matchers.LoggableEvent;
import org.motechproject.eventlogging.loggers.EventLogger;
import org.motechproject.eventlogging.matchers.LoggableEvent;
import org.motechproject.eventlogging.matchers.MappedLoggableEvent;
import org.motechproject.eventlogging.service.EventLogService;

/**
Expand Down Expand Up @@ -36,8 +36,8 @@ public void log(MotechEvent eventToLog) {
if (loggableEvent.isLoggableEvent(eventToLog)) {
if (eventConverter != null) {
EventLog eventLog;
if (loggableEvent instanceof DbLoggableEvent) {
eventLog = eventConverter.configuredConvertEventToDbLog(eventToLog, loggableEvent);
if (loggableEvent instanceof MappedLoggableEvent) {
eventLog = eventConverter.configuredConvertEventToDbLog(eventToLog, (MappedLoggableEvent) loggableEvent);
} else {
eventLog = eventConverter.convertToLog(eventToLog);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.motechproject.eventlogging.converter.impl.DefaultFileToLogConverter;
import org.motechproject.eventlogging.matchers.LoggableEvent;
import org.motechproject.eventlogging.loggers.EventLogger;
import org.motechproject.eventlogging.matchers.MappedLoggableEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -61,7 +62,12 @@ public void log(MotechEvent eventToLog) {
for (LoggableEvent loggableEvent : getLoggableEvents()) {
if (loggableEvent.isLoggableEvent(eventToLog)) {
if (eventConverter != null) {
String logString = eventConverter.convertToLog(eventToLog);
String logString;
if (loggableEvent instanceof MappedLoggableEvent) {
logString = eventConverter.convertToLogMapped(eventToLog, (MappedLoggableEvent) loggableEvent);
} else {
logString = eventConverter.convertToLog(eventToLog);
}
log(logString);
} else {
return;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.motechproject.eventlogging.matchers;

import java.util.Map;

/**
* Class representing mapping from one parameter key-value pair to another.
* Used in {@link org.motechproject.eventlogging.converter.impl.DefaultDbToLogConverter}
Expand Down Expand Up @@ -72,4 +74,22 @@ public void setEndValue(Object endValue) {
this.endValue = endValue;
}

public static KeyValue buildFromMap(Map<String, String> map) {
String startKey = null;
String startValue = null;
String endKey = null;
String endValue = null;

for (Map.Entry<String, String> entry : map.entrySet()) {
if (startKey == null) {
startKey = entry.getKey();
startValue = entry.getValue();
} else {
endKey = entry.getKey();
endValue = entry.getValue();
}
}

return new KeyValue(startKey, startValue, endKey, endValue, true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.motechproject.eventlogging.matchers;

import org.motechproject.event.MotechEvent;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* Version of {@link LoggableEvent} class used when working with mappings. Additionaly to LoggableEvent it
* stores {@link LogMappings} used for manipulating event parameters before persisting log.
*/
public class MappedLoggableEvent extends LoggableEvent {

private LogMappings mappings;

/**
* Creates an instance of MappedLoggableEvent using passed parameters
*
* @param eventSubjects list of event subjects to log
* @param flags event flags used for filtering events by parameters
* @param mappings {@link LogMappings} object used for replacing, including and excluding parameters before logging to the database
*/
public MappedLoggableEvent(List<String> eventSubjects, List<? extends EventFlag> flags, LogMappings mappings) {
super(eventSubjects, flags);
this.mappings = mappings;
}

public LogMappings getMappings() {
return mappings;
}

public void setMappings(LogMappings mappings) {
this.mappings = mappings;
}

public Map<String, Object> filterParams(MotechEvent event) {
Map<String, Object> initialParameters = event.getParameters();

LogMappings logMappings = getMappings();
List<KeyValue> keyValueList = logMappings.getMappings();
List<String> exclusions = logMappings.getExclusions();
List<String> inclusions = logMappings.getInclusions();
Map<String, Object> finalParameters = new LinkedHashMap<String, Object>(initialParameters);

for (KeyValue keyValue : keyValueList) {
if (initialParameters.containsKey(keyValue.getStartKey())) {
if (keyValue.getStartValue().equals(initialParameters.get(keyValue.getStartKey()))) {
finalParameters.put(keyValue.getEndKey(), keyValue.getEndValue());
exclusions.add(keyValue.getStartKey());
}
}
}

for (String excludeParameter : exclusions) {
if (initialParameters.containsKey(excludeParameter)) {
finalParameters.remove(excludeParameter);
}
}

for (String includeParameter : inclusions) {
if (initialParameters.containsKey(includeParameter)) {
finalParameters.put(includeParameter, initialParameters.get(includeParameter));
}
}

return finalParameters;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@

import com.google.gson.reflect.TypeToken;
import org.motechproject.commons.api.json.MotechJsonReader;
import org.motechproject.eventlogging.matchers.KeyValue;
import org.motechproject.eventlogging.matchers.LogMappings;
import org.motechproject.eventlogging.matchers.LoggableEvent;
import org.motechproject.eventlogging.matchers.MappedLoggableEvent;
import org.motechproject.eventlogging.matchers.MappingsJson;
import org.motechproject.config.SettingsFacade;
import org.motechproject.eventlogging.matchers.ParametersPresentEventFlag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* Class used for retrieving event mappings from the json configuration file.
Expand Down Expand Up @@ -52,4 +59,56 @@ public List<MappingsJson> getAllMappings() {

return mappings;
}

/**
* Returns loggable events from event mappings stored in configuration file.
* @return list of loggable events
*/
public List<LoggableEvent> converToLoggableEvents() {
List<MappingsJson> allMappings = getAllMappings();
List<LoggableEvent> loggableEvents = new ArrayList<>();
for (MappingsJson mapping : allMappings) {
if (mapping.getMappings() == null && mapping.getIncludes() == null && mapping.getExcludes() == null) {
LoggableEvent event = new LoggableEvent(mapping.getSubjects(), mapping.getFlags());
loggableEvents.add(event);
} else {
MappedLoggableEvent mappedEvent = new MappedLoggableEvent(mapping.getSubjects(), null, null);

List<KeyValue> mappings = null;

if (mapping.getMappings() != null) {
List<Map<String, String>> mappingList = mapping.getMappings();
mappings = new ArrayList<>();

List<String> subjects = mapping.getSubjects();

if (subjects != null) {
for (Map<String, String> map : mappingList) {
KeyValue keyValue = KeyValue.buildFromMap(map);
mappings.add(keyValue);
}
}
}

List<String> inclusions = mapping.getIncludes();
List<String> exclusions = mapping.getExcludes();

LogMappings logMappings = new LogMappings(mappings, exclusions, inclusions);

mappedEvent.setMappings(logMappings);

List<ParametersPresentEventFlag> eventFlags = mapping.getFlags();

if (eventFlags != null) {
mappedEvent.setFlags(eventFlags);
}


loggableEvents.add(mappedEvent);
}
}

return loggableEvents;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.motechproject.eventlogging.service;


public final class Constants {

private Constants() {

}

public static final String DB_ENABLED_PROPERTY = "eventlogging.db.enabled";
public static final String FILE_ENABLED_PROPERTY = "eventlogging.file.enabled";
public static final String LOGFILE_PATH_PROPERTY = "eventlogging.logfile.path";

}
Loading