Commit ed1585a1 authored by Sören Henning's avatar Sören Henning

replaced JSONExportStage

parent 65e97636
......@@ -26,7 +26,7 @@ public class AnomalyDetectionConfiguration extends Configuration {
new RegressionForecaster());
final SimpleAlertStage alerter = new SimpleAlertStage();
final PrinterStage printer = new PrinterStage(); // TODO Temp
final BetterJSONExporter jsonExporter = new BetterJSONExporter(new File("values.json")); // TODO Temp
final JSONExportStage jsonExporter = new JSONExportStage(new File("values.json")); // TODO Temp
// Connect the stages
super.connectPorts(generator.getOutputPort(), anomalyDetector.getInputPort());
......
......@@ -10,11 +10,11 @@ import com.fasterxml.jackson.core.JsonGenerator;
import anomalydetection.measurement.AnomalyScoredMeasurement;
import teetime.framework.AbstractConsumerStage;
public class BetterJSONExporter extends AbstractConsumerStage<AnomalyScoredMeasurement> {
public class JSONExportStage extends AbstractConsumerStage<AnomalyScoredMeasurement> {
private final JsonGenerator jsonGenerator;
public BetterJSONExporter(final File file) {
public JSONExportStage(final File file) {
try {
JsonFactory jsonFactory = new JsonFactory();
......
package anomalydetection;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONWriter;
import anomalydetection.measurement.AnomalyScoredMeasurement;
import teetime.framework.AbstractConsumerStage;
public class JSONExporter extends AbstractConsumerStage<AnomalyScoredMeasurement> {
private final JSONWriter writer;
private final Writer baseWriter;
public JSONExporter(final File file) {
try {
this.baseWriter = new FileWriter(file);
this.writer = new JSONWriter(this.baseWriter);
writer.array();
} catch (JSONException | IOException e) {
throw new IllegalStateException(e);
}
}
@Override
protected void execute(final AnomalyScoredMeasurement measurement) {
try {
writer.object();
writer.key("time").value(measurement.getTime().toEpochMilli());
writer.key("measurement").value(measurement.getValue());
writer.key("prediction").value(measurement.getPrediction());
writer.key("anomalyscore").value(measurement.getAnomalyScore());
writer.endObject();
} catch (JSONException e) {
throw new IllegalStateException(e);
}
}
@Override
public void onTerminating() throws Exception {
writer.endArray();
baseWriter.close();
super.onTerminating();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment