Commit 7e224896 authored by Sören Henning's avatar Sören Henning

Implemeneted JSONExporter

parent 5b049ec2
package anomalydetection;
import java.time.Duration;
import teetime.framework.Configuration;
public class AnomalyDetectionConfiguration extends Configuration {
......@@ -7,7 +9,8 @@ public class AnomalyDetectionConfiguration extends Configuration {
public AnomalyDetectionConfiguration() {
// Create the stages
final MeassurementsGeneratorStage generator = new MeassurementsGeneratorStage();
final MeassurementsGeneratorStage generator = new MeassurementsGeneratorStage(x -> 500 * Math.sin(x / 60) + 2000, 250, 0.01, 1000, Duration.ofSeconds(1),
900);
final AnomalyDetectionStage anomalyDetector = new AnomalyDetectionStage();
final SimpleAlertStage alerter = new SimpleAlertStage();
......
......@@ -25,11 +25,12 @@ public class AnomalyDetectionStage extends CompositeStage {
// Create the stages
final Distributor<Measurement> measurementDistributor = new Distributor<>(new CopyByReferenceStrategy());
final ExtractorStage extractor = new ExtractorStage(new BoundedTimeSeries(Duration.ofHours(1)));
final NormalizerStage normalizerStage = new NormalizerStage(Duration.ofMinutes(1), new MeanAggregator());
final NormalizerStage normalizerStage = new NormalizerStage(Duration.ofSeconds(5), new MeanAggregator());
final ForecastStage forecaster = new ForecastStage(new WeightedForecaster(WeightMethod.LINEAR));
final MeasurementForecastDecorationStage measurementForecastDecorator = new MeasurementForecastDecorationStage();
final AnomalyScoreCalculatorStage anomalyScoreCalculator = new AnomalyScoreCalculatorStage();
final PrinterStage printer = new PrinterStage(); // TODO Temp
final JSONExporter jsonExporter = new JSONExporter(); // TODO Temp
final StorageStage storager = new StorageStage();
this.inputPort = measurementDistributor.getInputPort();
......@@ -43,6 +44,7 @@ public class AnomalyDetectionStage extends CompositeStage {
super.connectPorts(measurementForecastDecorator.getOutputPort(), anomalyScoreCalculator.getInputPort());
super.connectPorts(anomalyScoreCalculator.getOutputPort(), anomalyScoreDistributor.getInputPort());
super.connectPorts(anomalyScoreDistributor.getNewOutputPort(), printer.getInputPort());
super.connectPorts(anomalyScoreDistributor.getNewOutputPort(), jsonExporter.getInputPort());
super.connectPorts(anomalyScoreDistributor.getNewOutputPort(), storager.getInputPort());
}
......
package anomalydetection;
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() {
try {
this.baseWriter = new FileWriter("values.json");
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 e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
@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