Skip to content

Commit

Permalink
Fixed #154 and issue #157 : added enrichment, also replaced the use o…
Browse files Browse the repository at this point in the history
…f Map<String, Object> <ith Map<String, Matcher> for all search methods
  • Loading branch information
baubakg committed Aug 6, 2024
1 parent 688372b commit 70d6111
Show file tree
Hide file tree
Showing 6 changed files with 292 additions and 93 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ Below is a diagram representing the class structure:
![The Class relationship](diagrams/Log_Parser-Classes.drawio.png)

## Searching and organizing log data
As of versions 1.0.4 & 1.0.5 we have a series of search and organizing the log data.
We have a series of search and organizing the log data.

### Search and Filter Mechanisms
We have introduced the filter and search mechanisms. These allow you to search the LogData for values for a given ParseDefinitionEntry. For this we have introduced the following methods:
Expand Down Expand Up @@ -398,6 +398,7 @@ All reports are stored in the directory `./log-parser-reports/export/`.
- **(new feature)** [#117](https://github.com/adobe/log-parser/issues/117) You can now include the file name in the result of the analysis.
- **(new feature)** [#141](https://github.com/adobe/log-parser/issues/141) You can now export a LogData as a table in a HTML file.
- **(new feature)** [#123](https://github.com/adobe/log-parser/issues/123) We now log the total number and size of the parsed files.
- **(new feature)** [#154](https://github.com/adobe/log-parser/issues/154) We have a data enrichment feature, where you can enrich the log data with additional information.
- [#110](https://github.com/adobe/log-parser/issues/110) Moved to Java 11
- [#112](https://github.com/adobe/log-parser/issues/112) Updating License Headers
- [#119](https://github.com/adobe/log-parser/issues/119) Cleanup of deprecated methods, and the consequences thereof.
Expand Down
39 changes: 29 additions & 10 deletions src/main/java/com/adobe/campaign/tests/logparser/core/LogData.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;

import java.io.File;
import java.io.FileWriter;
Expand Down Expand Up @@ -293,10 +295,10 @@ public LogData<GenericEntry> groupBy(String in_parseDefinitionEntryKey)
* <p>
* Author : gandomi
*
* @param in_filterKeyValues A map of &lt;String,Object&gt; representation the values we want to find
* @param in_filterKeyValues A map of &lt;String,Matcher&gt; representation the values we want to find
* @return a new LogDataObject containing only the filtered values
*/
public LogData<T> filterBy(Map<String, Object> in_filterKeyValues) {
public LogData<T> filterBy(Map<String, Matcher> in_filterKeyValues) {
LogData<T> lr_filteredLogData = new LogData<>();

for (String lt_logDataKey : this.getEntries().keySet()) {
Expand All @@ -314,11 +316,11 @@ public LogData<T> filterBy(Map<String, Object> in_filterKeyValues) {
* Author : gandomi
*
* @param in_parseDefinitionName The name of the parse definition entry under which we search for a value
* @param in_searchValue The search value
* @param in_searchValue The matcher
* @return a new LogDataObject containing only the searched values
*/
public LogData<T> searchEntries(String in_parseDefinitionName, String in_searchValue) {
Map<String, Object> l_filterProperties = new HashMap<>();
public LogData<T> searchEntries(String in_parseDefinitionName, Matcher in_searchValue) {
Map<String, Matcher> l_filterProperties = new HashMap<>();
l_filterProperties.put(in_parseDefinitionName, in_searchValue);

return this.filterBy(l_filterProperties);
Expand All @@ -329,10 +331,10 @@ public LogData<T> searchEntries(String in_parseDefinitionName, String in_searchV
* <p>
* Author : gandomi
*
* @param in_searchKeyValues A map of &lt;String,Object&gt; representation the values we want to find
* @param in_searchKeyValues A map of &lt;String,Matcher&gt; representation the values we want to find
* @return a new LogDataObject containing only the filtered values
*/
public LogData<T> searchEntries(Map<String, Object> in_searchKeyValues) {
public LogData<T> searchEntries(Map<String, Matcher> in_searchKeyValues) {

return filterBy(in_searchKeyValues);
}
Expand All @@ -347,8 +349,8 @@ public LogData<T> searchEntries(Map<String, Object> in_searchKeyValues) {
* @return true if the search terms could be found. Otherwise false
*/
public boolean isEntryPresent(String in_parseDefinitionName, String in_searchValue) {
Map<String, Object> l_searchProperties = new HashMap<>();
l_searchProperties.put(in_parseDefinitionName, in_searchValue);
Map<String, Matcher> l_searchProperties = new HashMap<>();
l_searchProperties.put(in_parseDefinitionName, Matchers.equalTo(in_searchValue));

return isEntryPresent(l_searchProperties);
}
Expand All @@ -361,7 +363,7 @@ public boolean isEntryPresent(String in_parseDefinitionName, String in_searchVal
* @param in_searchKeyValues A map of &lt;String,Object&gt; representation the values we want to find
* @return true if the search terms could be found. Otherwise false
*/
public boolean isEntryPresent(Map<String, Object> in_searchKeyValues) {
public boolean isEntryPresent(Map<String, Matcher> in_searchKeyValues) {
return searchEntries(in_searchKeyValues).getEntries().size() > 0;
}

Expand Down Expand Up @@ -540,4 +542,21 @@ public ParseDefinition fetchParseDefinition() {

return l_firstEntry.getParseDefinition();
}

/**
* Enriches the log data with the given values provided there are lines that match the query map
* @param in_queryMap A map definition entry and Matchers
* @param in_entryName The name of the entry to be added
* @param in_entryValue The value of the entry to be added
*/
public void enrichData(Map<String, Matcher> in_queryMap, String in_entryName, String in_entryValue) {
//add the entry to the definition
fetchParseDefinition().addEntry(new ParseDefinitionEntry(in_entryName));

//Iterate over the entries
getEntries().entrySet().stream().filter(e -> e.getValue().matches(in_queryMap)).forEach(e -> {
e.getValue().getValuesMap().put(in_entryName, in_entryValue);
});
//for each entry meeting the querymap enrich with the given value
}
}
115 changes: 58 additions & 57 deletions src/main/java/com/adobe/campaign/tests/logparser/core/StdLogEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,32 @@
*/
package com.adobe.campaign.tests.logparser.core;

import java.util.*;
import java.util.stream.Collectors;

import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hamcrest.Matcher;

import java.util.*;
import java.util.stream.Collectors;

/**
* Abstract class for multiple definitions
*
*
* <p>
* <p>
* Author : gandomi
*
*/
public abstract class StdLogEntry {
public static final String STD_DATA_KEY = "key";
public static final String STD_DATA_FREQUENCE = "frequence";
public static final String STD_DATA_FILE_NAME = "fileName";
public static final String STD_DATA_FILE_PATH = "filePath";
protected static Logger log = LogManager.getLogger();

Map<String, Object> valuesMap = new HashMap<>();
private Integer frequence = 1;
private ParseDefinition parseDefinition;
Map<String, Object> valuesMap = new HashMap<>();
private String fileName;
private String filePath;


public static final String STD_DATA_KEY = "key";
public static final String STD_DATA_FREQUENCE = "frequence";
public static final String STD_DATA_FILE_NAME = "fileName";
public static final String STD_DATA_FILE_PATH = "filePath";

/**
* A method that creates the key to identify each stored entry
*
* @return A constructed key
*/
public abstract String makeKey();

public StdLogEntry(ParseDefinition in_definition) {
this.parseDefinition = in_definition;
}
Expand All @@ -59,6 +49,13 @@ public StdLogEntry() {
parseDefinition = new ParseDefinition("Created By Default");
}

/**
* A method that creates the key to identify each stored entry
*
* @return A constructed key
*/
public abstract String makeKey();

/**
* Creates a clone of the current LogEntry. This requires that each child defines a copy constructor
* <p>
Expand All @@ -77,6 +74,13 @@ public Map<String, Object> getValuesMap() {
return valuesMap;
}

/**
* @param valuesMap the valuesMap to set
*/
protected void setValuesMap(Map<String, Object> valuesMap) {
this.valuesMap = valuesMap;
}

/**
* Fetches a print out for listing purposes. It uses the header list as an index to fetch the correct order of the
* values
Expand All @@ -103,11 +107,11 @@ protected List<String> fetchValuesAsList() {
*/
public abstract Set<String> fetchHeaders();

;

/**
* Returns a set of objects you have defined for your log class. When using Generic Object no changes are made to
* it.
* When defining an SDK you should override this method.
* Author : gandomi
* it. When defining an SDK you should override this method. Author : gandomi
*
* @return A Maps of values for the LogEntry
*/
Expand All @@ -123,10 +127,10 @@ public Map<String, Object> fetchValueMap() {
l_valueMap.put(STD_DATA_FILE_PATH, getFilePath());
}

l_valueMap.put(STD_DATA_FREQUENCE , getFrequence().toString());
l_valueMap.put(STD_DATA_FREQUENCE, getFrequence().toString());

return valuesMap;
};
}

/**
* Increments the frequence
Expand Down Expand Up @@ -161,13 +165,6 @@ protected void setFrequence(Integer frequence) {
this.frequence = frequence;
}

/**
* @param valuesMap the valuesMap to set
*/
protected void setValuesMap(Map<String, Object> valuesMap) {
this.valuesMap = valuesMap;
}

public ParseDefinition getParseDefinition() {
return parseDefinition;
}
Expand Down Expand Up @@ -218,44 +215,45 @@ public Object get(String in_dataTitle) {
* @param in_filterMap A map of filter values
* @return True if all of the values can be found in the valueMap
*/
public boolean matches(Map<String, Object> in_filterMap) {
public boolean matches(Map<String, Matcher> in_filterMap) {

for (String lt_filterKey : in_filterMap.keySet()) {
if (!this.fetchHeaders().contains(lt_filterKey)) {
log.warn("The filter key {} could not be found among the log entry headers.", lt_filterKey);
return false;
}
if (!this.fetchValueMap().get(lt_filterKey).equals(in_filterMap.get(lt_filterKey))) {
return false;
}
}
return true;
return in_filterMap.entrySet().stream().allMatch(e -> e.getValue().matches(this.get(e.getKey())));
}


@Override
public boolean equals(Object obj) {
if (this == obj)
if (this == obj) {
return true;
if (obj == null)
}
if (obj == null) {
return false;
if (getClass() != obj.getClass())
}
if (getClass() != obj.getClass()) {
return false;
}
StdLogEntry other = (StdLogEntry) obj;
if (frequence == null) {
if (other.frequence != null)
if (other.frequence != null) {
return false;
} else if (!frequence.equals(other.frequence))
}
} else if (!frequence.equals(other.frequence)) {
return false;
}
if (parseDefinition == null) {
if (other.parseDefinition != null)
if (other.parseDefinition != null) {
return false;
} else if (!parseDefinition.equals(other.parseDefinition))
}
} else if (!parseDefinition.equals(other.parseDefinition)) {
return false;
}
if (valuesMap == null) {
if (other.valuesMap != null)
if (other.valuesMap != null) {
return false;
} else if (!valuesMap.equals(other.valuesMap))
}
} else if (!valuesMap.equals(other.valuesMap)) {
return false;
}
return true;
}

Expand All @@ -266,6 +264,7 @@ public int hashCode() {

/**
* Returns the headers as they are stored
*
* @return An ordered set of the value names that are stored
*/
public Set<String> fetchStoredHeaders() {
Expand All @@ -284,16 +283,17 @@ public String getFilePath() {
return filePath;
}

public void setLogFileName(String in_logFile) {
this.fileName = in_logFile;
}

public void setFilePath(String in_logFile) {
this.filePath = in_logFile;
}

public void setLogFileName(String in_logFile) {
this.fileName = in_logFile;
}

/**
* Updates the store path of the log entry. We also remove training "/" to have a clean path
*
* @param in_newPath The path we want to store
*/
public void updatePath(String in_newPath) {
Expand All @@ -303,5 +303,6 @@ public void updatePath(String in_newPath) {
l_pathDelta = (l_pathDelta.endsWith("/")) ? l_pathDelta.substring(0, l_pathDelta.length() - 1) : l_pathDelta;
setFilePath(l_pathDelta);
}

}

Loading

0 comments on commit 70d6111

Please sign in to comment.