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

extracted column names to fields and provide getters and setters

parent 2b33cd58
......@@ -18,12 +18,11 @@ public class CassandraDriver implements StorageDriver {
private final Session session;
private final String table;
// TODO use this values
private final String seriesIdColumn = "series_id";
private final String timeColumn = "time";
private final String measurementColumn = "measurement";
private final String predictionColumn = "prediction";
private final String anomalyscoreColumn = "anomalyscore";
private String seriesIdColumn = "series_id";
private String timeColumn = "time";
private String measurementColumn = "measurement";
private String predictionColumn = "prediction";
private String anomalyscoreColumn = "anomalyscore";
public CassandraDriver(final Session session, final String table) {
this.session = session;
......@@ -34,21 +33,61 @@ public class CassandraDriver implements StorageDriver {
createTableIfNotExists();
}
public String getSeriesIdColumn() {
return seriesIdColumn;
}
public void setSeriesIdColumn(final String seriesIdColumn) {
this.seriesIdColumn = seriesIdColumn;
}
public String getTimeColumn() {
return timeColumn;
}
public void setTimeColumn(final String timeColumn) {
this.timeColumn = timeColumn;
}
public String getMeasurementColumn() {
return measurementColumn;
}
public void setMeasurementColumn(final String measurementColumn) {
this.measurementColumn = measurementColumn;
}
public String getPredictionColumn() {
return predictionColumn;
}
public void setPredictionColumn(final String predictionColumn) {
this.predictionColumn = predictionColumn;
}
public String getAnomalyscoreColumn() {
return anomalyscoreColumn;
}
public void setAnomalyscoreColumn(final String anomalyscoreColumn) {
this.anomalyscoreColumn = anomalyscoreColumn;
}
@Override
public TimeSeries retrieveTimeSeries(final String seriesId, final Instant start, final Instant end) {
final Select statement = QueryBuilder.select("time", "measurement")
final Select statement = QueryBuilder.select(this.timeColumn, this.measurementColumn)
.from(this.table)
.where(QueryBuilder.eq("series_id", seriesId))
.and(QueryBuilder.gte("time", start.toEpochMilli()))
.and(QueryBuilder.lte("time", end.toEpochMilli()))
.orderBy(QueryBuilder.asc("time"));
.where(QueryBuilder.eq(this.seriesIdColumn, seriesId))
.and(QueryBuilder.gte(this.timeColumn, start.toEpochMilli()))
.and(QueryBuilder.lte(this.timeColumn, end.toEpochMilli()))
.orderBy(QueryBuilder.asc(this.timeColumn));
final ResultSet results = session.execute(statement);
final TimeSeries timeSeries = new TimeSeries();
for (Row row : results) {
final Instant time = row.getTimestamp("time").toInstant();
final double value = row.getDouble("measurement");
final Instant time = row.getTimestamp(this.timeColumn).toInstant();
final double value = row.getDouble(this.measurementColumn);
timeSeries.appendEnd(new TimeSeriesPoint(time, value));
}
......@@ -59,23 +98,23 @@ public class CassandraDriver implements StorageDriver {
public void storeMeasurement(final String seriesId, final AnomalyScoredMeasurement measurement) {
final Insert statement = QueryBuilder
.insertInto(this.table)
.value("series_id", seriesId)
.value("time", measurement.getTime().toEpochMilli())
.value("measurement", measurement.getValue())
.value("prediction", measurement.getPrediction())
.value("anomalyscore", measurement.getAnomalyScore());
.value(this.seriesIdColumn, seriesId)
.value(this.timeColumn, measurement.getTime().toEpochMilli())
.value(this.measurementColumn, measurement.getValue())
.value(this.predictionColumn, measurement.getPrediction())
.value(this.anomalyscoreColumn, measurement.getAnomalyScore());
session.execute(statement);
}
private void createTableIfNotExists() {
session.execute(
"CREATE TABLE IF NOT EXISTS " + this.table + " (" +
"series_id text," +
"time timestamp," +
"measurement double," +
"prediction double," +
"anomalyscore double," +
"PRIMARY KEY (series_id, time)" +
this.seriesIdColumn + " text," +
this.timeColumn + " timestamp," +
this.measurementColumn + " double," +
this.predictionColumn + " double," +
this.anomalyscoreColumn + " double," +
"PRIMARY KEY (" + this.seriesIdColumn + ", " + this.timeColumn + ")" +
");");
}
......
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