Skip to content

Commit

Permalink
'#43 Add new metadata to identify if item is a plugin item and add log
Browse files Browse the repository at this point in the history
execution time info.
  • Loading branch information
patrickdalla committed Jan 19, 2024
1 parent 6b60491 commit 7672ace
Showing 1 changed file with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
Expand Down Expand Up @@ -50,13 +51,16 @@

public class LeappBridgeTask extends AbstractPythonTask {

AtomicInteger pluginTimeCounter = new AtomicInteger(0);

public static Logger logger = LoggerFactory.getLogger(LeappBridgeTask.class);

public static MediaType DEVICEDETAILS = MediaType.application("x-leapp-devicedetails");

private static final String DEVICE_DETAILS_HTML = "DeviceDetails.html";

public static final String ALEAPP_METADATA_PREFIX = "ALEAPP";
public static final String ALEAPP_ISPLUGIN = ALEAPP_METADATA_PREFIX + ":isPlugin";
public static final String ALEAPP_PLUGIN = ALEAPP_METADATA_PREFIX + ":PLUGIN";

static final String REPORT_EVIDENCE_NAME = "LEAPP_Reports";
Expand All @@ -66,7 +70,7 @@ public class LeappBridgeTask extends AbstractPythonTask {

private static final String PLUGIN_EXECUTION_MESSAGE = "ALeapp plugin execution";

static AtomicInteger count = new AtomicInteger(0);
static AtomicInteger taskCount = new AtomicInteger(0);

HashMap<String, Item> mappedEvidences = new HashMap<String, Item>();

Expand All @@ -90,13 +94,6 @@ public LeappBridgeTask() {

static private int START_QUEUE_PRIORITY = 3;

@Override
public List<Configurable<?>> getConfigurables() {
ArrayList<Configurable<?>> c = new ArrayList<Configurable<?>>();
c.add(new ALeappConfig());
return c;
}

public static void logfunc(String message) {
logger.info(message);
}
Expand Down Expand Up @@ -139,12 +136,13 @@ public static Object open(Collection args, Map kargs) {

@Override
public void init(ConfigurationManager configurationManager) throws Exception {
int incremented = count.incrementAndGet();
int incremented = taskCount.incrementAndGet();

moduleName = "JLeapp";
if (incremented == 1) {
super.init(configurationManager);
}

Jep jep = super.getJep();

File aleappPath = getAleappScriptsDir();
Expand Down Expand Up @@ -177,10 +175,15 @@ public void init(ConfigurationManager configurationManager) throws Exception {

@Override
public void finish() throws Exception {
int decremented = taskCount.decrementAndGet();
if (decremented == 0) {
logger.warn("ALeapp total plugin execution time:" + pluginTimeCounter.get());
}
}

public void executePlugin(IItem evidence, LeapArtifactsPlugin p, List<String> filesFound, File reportDumpPath) {
Jep jep = getJep();
Date plugginStart = new Date();
try {
// some plugins depend on a sorted list
Collections.sort(filesFound);
Expand Down Expand Up @@ -239,6 +242,11 @@ public void executePlugin(IItem evidence, LeapArtifactsPlugin p, List<String> fi
}

} finally {
Date plugginEnd = new Date();
long delta = plugginEnd.getTime() - plugginStart.getTime();
pluginTimeCounter.addAndGet((int) delta);
logger.warn("ALeapp plugin "+p.getName()+" execution time:"+delta);

// jep.close();
}
}
Expand Down Expand Up @@ -289,7 +297,7 @@ public void process(IItem evidence) throws Exception {
subItem.setExtraAttribute(ExtraProperties.DECODED_DATA, true);
worker.processNewItem(subItem);

// creates on subitem for each plugin execution
// creates one subitem for each plugin execution
for (LeapArtifactsPlugin p : pluginsManager.getPlugins()) {
Item psubItem = (Item) subItem.createChildItem();
ParentInfo pparentInfo = new ParentInfo(subItem);
Expand All @@ -300,6 +308,7 @@ public void process(IItem evidence) throws Exception {
psubItem.setSubItem(true);
psubItem.setSubitemId(1);
psubItem.getMetadata().set(ALEAPP_PLUGIN, moduleName);
psubItem.getMetadata().set(ALEAPP_ISPLUGIN, "true");
psubItem.setExtraAttribute(ExtraProperties.DECODED_DATA, true);
worker.processNewItem(psubItem);
}
Expand All @@ -316,13 +325,14 @@ public void process(IItem evidence) throws Exception {
worker.processNewItem(devDetailsSubItem);
}

String pluginName = evidence.getMetadata().get(ALEAPP_PLUGIN);
if (pluginName != null) {
String isPlugin = evidence.getMetadata().get(ALEAPP_ISPLUGIN);
if (isPlugin != null && isPlugin.equals("true")) {
int priority = worker.manager.getProcessingQueues().getCurrentQueuePriority();
if (priority < START_QUEUE_PRIORITY) {
reEnqueueItem(evidence, START_QUEUE_PRIORITY);
throw new ItemReEnqueuedException();
} else {
String pluginName = evidence.getMetadata().get(ALEAPP_PLUGIN);
LeapArtifactsPlugin p = pluginsManager.getPlugin(pluginName);
processEvidence(evidence, p);
}
Expand Down Expand Up @@ -582,4 +592,11 @@ protected void loadScript(Jep jep, boolean init) throws JepException {
jep.eval("import sys");
}

@Override
public List<Configurable<?>> getConfigurables() {
ArrayList<Configurable<?>> c = new ArrayList<Configurable<?>>();
c.add(new ALeappConfig());
return c;
}

}

0 comments on commit 7672ace

Please sign in to comment.