Skip to content
Snippets Groups Projects
Commit ff58ec6e authored by Sören Henning's avatar Sören Henning
Browse files

created basic application structure

parent 0d1b0e41
No related branches found
No related tags found
No related merge requests found
package kiekpad.vizprovider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(final String[] args) {
SpringApplication.run(Application.class, args);
}
}
package kiekpad.vizprovider.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.datastax.driver.core.querybuilder.Select;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import kiekpad.vizprovider.service.CassandraService;
@RestController
public class MeasurementsController {
private final CassandraService cassandraService;
@Autowired
public MeasurementsController(final CassandraService cassandraService) {
this.cassandraService = cassandraService;
}
@CrossOrigin // TODO Cross Origin is just for development
@RequestMapping("/measurements")
public ArrayNode measurements(@RequestParam(value = "after", defaultValue = "0") final long after) {
JsonNodeFactory jsonNodeFactory = JsonNodeFactory.instance;
ArrayNode array = jsonNodeFactory.arrayNode();
final Select statement = QueryBuilder.select("time", "measurement", "prediction", "anomalyscore")
.from("measurements")
.where(QueryBuilder.eq("series_id", "temp"))
.and(QueryBuilder.gt("time", after))
.orderBy(QueryBuilder.asc("time"));
final ResultSet results = this.cassandraService.getSession().execute(statement);
for (Row row : results) {
ObjectNode node = jsonNodeFactory.objectNode();
node.set("time", jsonNodeFactory.numberNode(row.getTimestamp("time").toInstant().toEpochMilli()));
node.set("measurement", jsonNodeFactory.numberNode(row.getDouble("measurement")));
node.set("prediction", jsonNodeFactory.numberNode(row.getDouble("prediction")));
node.set("anomalyscore", jsonNodeFactory.numberNode(row.getDouble("anomalyscore")));
array.add(node);
}
return array;
}
}
package kiekpad.vizprovider.service;
import org.springframework.stereotype.Service;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;
@Service
public class CassandraService {
private static final String IP_ADDRESS = "192.168.99.100";
private static final int PORT = 32770;
private static final String KEYSPACE = "demo3";
private final Session session;
public CassandraService() {
final Cluster cluster = Cluster.builder().addContactPoint(IP_ADDRESS).withPort(PORT).build();
this.session = cluster.connect(KEYSPACE);
}
public Session getSession() {
return session;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment