blob: 7170166ec36fb78cd3b47704f897848adfb47c26 [file] [log] [blame]
package org.eclipse.app4mc.atdb._import.amalthea;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.List;
import org.eclipse.app4mc.atdb.ATDBConnection;
import org.eclipse.app4mc.atdb.MetricAggregation;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
public class EventChainMetricCalculator implements IRunnableWithProgress {
private final ATDBConnection con;
public EventChainMetricCalculator(final ATDBConnection con) {
this.con = con;
}
@Override
public void run(IProgressMonitor progressMonitor) throws InvocationTargetException, InterruptedException {
final SubMonitor subMon = SubMonitor.convert(progressMonitor, "Calculating event chain metrics...", 3);
try {
final Statement mStmt = this.con.createStatement();
final PreparedStatement metricStmt = this.con.getPreparedStatementFor("INSERT OR IGNORE INTO metric(name, dimension) VALUES(?, ?);");
// create event chain instances with separate sql program (a sequence of queries)
String ecInstCalculationSQL = "";
try {
final Bundle bundle = FrameworkUtil.getBundle(EventChainMetricCalculator.class);
final String pathInBundle = EventChainMetricCalculator.class.getPackage().getName().replaceAll("\\.", "/") + "/ecinststatements.sql";
final URL fileURL = bundle.getResource(pathInBundle);
byte[] fileContents = Files.readAllBytes(Paths.get(FileLocator.resolve(fileURL).toURI()));
ecInstCalculationSQL = new String(fileContents);
} catch (IOException | URISyntaxException e) {
// fail silently
}
if (ecInstCalculationSQL.length() == 0) return;
this.con.executeUpdate(ecInstCalculationSQL);
subMon.worked(1);
// calculate event chain metrics min max avg for age and reaction latency values
final List<String> latencyTypes = Arrays.asList("Age", "Reaction");
for (final String latencyType : latencyTypes) {
metricStmt.setString(1, latencyType.toLowerCase() + "Latency");
metricStmt.setString(2, "time");
metricStmt.addBatch();
// add latencies to entity instance metrics
mStmt.addBatch("INSERT OR IGNORE INTO entityInstanceMetricValue SELECT entityId, entityInstance, (SELECT id FROM metric WHERE "//
+ "name = '" + latencyType.toLowerCase() + "Latency'), responseTimestamp - stimulusTimestamp FROM eventChainInstanceInfo "//
+ "WHERE is" + latencyType + ";");
for (final MetricAggregation kind : MetricAggregation.values()) {
final String metricName = latencyType.toLowerCase() + "Latency" + "_" + kind;
final String aggregateFunction = kind.getSQLStr("value");
metricStmt.setString(1, metricName);
metricStmt.setString(2, "time");
metricStmt.addBatch();
mStmt.addBatch("INSERT OR IGNORE INTO entityMetricValue SELECT entityId, (SELECT id FROM metric WHERE name = '" + metricName//
+ "'), " + aggregateFunction + " FROM entityInstanceMetricValue WHERE (SELECT is" + latencyType + " FROM "//
+ "eventChainInstanceInfo WHERE eventChainInstanceInfo.entityId = entityInstanceMetricValue.entityId AND "//
+ "eventChainInstanceInfo.entityInstance = entityInstanceMetricValue.entityInstance) "//
+ "GROUP BY entityId;");
}
}
subMon.worked(1);
this.con.executeBatchStatements(metricStmt, mStmt);
subMon.worked(1);
} catch (SQLException e) {
throw new InvocationTargetException(e);
}
}
}