Commit b73d9b55 authored by Adam Kucera's avatar Adam Kucera
Browse files

refactoring + preparation to final implementation of the DA API

parent 7596c4a3
......@@ -20,12 +20,12 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import cz.muni.fi.lasaris.sbms.data.api.request.GroupedAddressRequest;
import cz.muni.fi.lasaris.sbms.data.api.request.AddressRequest;
import cz.muni.fi.lasaris.sbms.data.api.response.AggregateResponse;
import cz.muni.fi.lasaris.sbms.data.api.response.SliceResponse;
import cz.muni.fi.lasaris.sbms.data.api.response.SnapshotResponse;
import cz.muni.fi.lasaris.sbms.data.entities.Address;
import cz.muni.fi.lasaris.sbms.data.entities.AggregationFunction;
import cz.muni.fi.lasaris.sbms.data.entities.values.RawValue;
import cz.muni.fi.lasaris.sbms.data.logic.ProviderManager;
import cz.muni.fi.lasaris.sbms.data.logic.DataCollector;
import cz.muni.fi.lasaris.sbms.data.logic.DPDataCollector;
//TODO http://stackoverflow.com/questions/14202257/design-restful-query-api-with-a-long-list-of-query-parameters
......@@ -34,12 +34,12 @@ import cz.muni.fi.lasaris.sbms.data.logic.DataCollector;
public class DataPointsEndpoint {
final static Logger logger = Logger.getLogger(DataPointsEndpoint.class);
private DataCollector dc = new DataCollector();
private DPDataCollector dc = new DPDataCollector();
@RolesAllowed({"user","admin"})
@GET
@Produces(MediaType.APPLICATION_JSON)
public SliceResponse getDataPoints(
public SnapshotResponse getDataPoints(
@QueryParam("ids") String json,
@QueryParam("cache") String cache
) {
......@@ -51,11 +51,11 @@ public class DataPointsEndpoint {
logger.debug(json);
AddressRequest readSpecs = m.readValue(json, AddressRequest.class);
logger.debug(readSpecs);
SliceResponse result = dc.getDataPointValues(readSpecs, allowCache(cache));
SnapshotResponse result = dc.getDataPointValues(readSpecs, allowCache(cache));
logger.debug(result);
return result;
} catch (IOException e) {
return SliceResponse.getErrorResponse("Unable to parse query:" + e);
return SnapshotResponse.getErrorResponse("Unable to parse query:" + e);
}
......@@ -88,7 +88,7 @@ public class DataPointsEndpoint {
@GET
@Path("/grouped")
@Produces(MediaType.APPLICATION_JSON)
public SliceResponse getGroupedDataPoints(
public SnapshotResponse getGroupedDataPoints(
@QueryParam("ids") String json,
@QueryParam("cache") String cache
) {
......@@ -100,7 +100,7 @@ public class DataPointsEndpoint {
logger.debug(readSpecs);
return dc.getDataPointGroupedValues(readSpecs, allowCache(cache));
} catch (IOException e) {
return SliceResponse.getErrorResponse("Unable to parse query:" + e);
return SnapshotResponse.getErrorResponse("Unable to parse query:" + e);
}
}
......@@ -131,12 +131,12 @@ public class DataPointsEndpoint {
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public SliceResponse getDataPoint(
public SnapshotResponse getDataPoint(
@PathParam("id") String idP,
@QueryParam("cache") String cache) {
Address a = new Address(idP);
RawValue v = ProviderManager.getDataPointsProvider(a.getProtocol()).getDataPointValue(a, allowCache(cache));
return new SliceResponse(a, v);
return new SnapshotResponse(a, v);
}
private boolean allowCache(String cache) {
......
......@@ -5,10 +5,12 @@ import java.time.Duration;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.Arrays;
import javax.annotation.security.RolesAllowed;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
......@@ -17,155 +19,109 @@ import javax.ws.rs.core.MediaType;
import org.apache.log4j.Logger;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import cz.muni.fi.lasaris.sbms.data.api.request.GroupedAddressRequest;
import cz.muni.fi.lasaris.sbms.data.api.request.TrendsRequest;
import cz.muni.fi.lasaris.sbms.data.api.request.AddressRequest;
import cz.muni.fi.lasaris.sbms.data.api.response.AggregateResponse;
import cz.muni.fi.lasaris.sbms.data.api.response.SliceResponse;
import cz.muni.fi.lasaris.sbms.data.api.response.SeriesOfValuesResponse;
import cz.muni.fi.lasaris.sbms.data.api.response.SliceResponse;
import cz.muni.fi.lasaris.sbms.data.entities.Address;
import cz.muni.fi.lasaris.sbms.data.entities.AggregationFunction;
import cz.muni.fi.lasaris.sbms.data.entities.AggregationWindow;
import cz.muni.fi.lasaris.sbms.data.entities.Interpolation;
import cz.muni.fi.lasaris.sbms.data.entities.Sampling;
import cz.muni.fi.lasaris.sbms.data.entities.SeriesSpecs;
import cz.muni.fi.lasaris.sbms.data.entities.containers.Trend;
import cz.muni.fi.lasaris.sbms.data.entities.values.RawValue;
import cz.muni.fi.lasaris.sbms.data.logic.DataCollector;
import cz.muni.fi.lasaris.sbms.data.logic.DPDataCollector;
import cz.muni.fi.lasaris.sbms.data.logic.ProviderManager;
import cz.muni.fi.lasaris.sbms.data.logic.TrendsDataCollector;
import cz.muni.fi.lasaris.sbms.data.util.Aggregator;
public class TrendsEndpoint {
final static Logger logger = Logger.getLogger(TrendsEndpoint.class);
private DataCollector dc;
private TrendsDataCollector dc;
private ObjectMapper m;
public TrendsEndpoint() {
dc = new DataCollector();
dc = new TrendsDataCollector();
m = new ObjectMapper();
m.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
// TODO http://stackoverflow.com/questions/14202257/design-restful-query-api-with-a-long-list-of-query-parameters
// http://stackoverflow.com/questions/14202257/design-restful-query-api-with-a-long-list-of-query-parameters
/*
// Methods 4, 6
@RolesAllowed({"user","admin"})
@GET
@Path("/serie/{id}")
@Produces(MediaType.APPLICATION_JSON)
public SliceResponse getTrends(
@QueryParam("ids") String json,
public SeriesOfValuesResponse getFunc(
@QueryParam("id") String id,
@QueryParam("start") String start,
@QueryParam("end") String end,
@QueryParam("sampling.cron") String samplingCron,
@QueryParam("sampling.duration") Long samplingDur,
@DefaultValue("NONE") @QueryParam("interpolation") Interpolation interpolation
@QueryParam("sampling.interpolation") Interpolation interpolation,
@QueryParam("aggregation.function") String aggregation,
@QueryParam("aggregation.window") String window,
) {
try {
logger.debug("Processing request...");
logger.debug(json);
TrendsRequest readSpecs = getReadSpecs(json, start, end, samplingCron, samplingDur, interpolation, null);
logger.debug(readSpecs);
SliceResponse result = dc.getTrends(readSpecs);
logger.debug(result);
return result;
} catch (IOException e) {
return SliceResponse.getErrorResponse("Unable to parse query:" + e);
}
Address a = new Address(id);
Trend v = ProviderManager.getTrendsProvider(a.getProtocol()).getTrend(a, getSeriesSpecs(start, end, samplingCron, samplingDur, interpolation));
return new SeriesOfValuesResponse(a, v);
}
*/
// Method 4
@RolesAllowed({"user","admin"})
@GET
@Path("/aggregated")
@POST
@Path("/si")
@Produces(MediaType.APPLICATION_JSON)
public AggregateResponse getAggregatedTrends(
@QueryParam("ids") String json,
@DefaultValue("NONE") @QueryParam("aggregation") AggregationFunction agg
public SeriesOfValuesResponse getSI(
@QueryParam("id") String id,
@QueryParam("start") String start,
@QueryParam("end") String end
) {
ObjectMapper m = new ObjectMapper();
try {
logger.debug("Processing request...");
logger.debug(json);
AddressRequest readSpecs = m.readValue(json, AddressRequest.class);
logger.debug(readSpecs);
return dc.getDataPointsAggregation(readSpecs, agg);
} catch (IOException e) {
return AggregateResponse.getErrorResponse("Unable to parse query:" + e);
}
}
@RolesAllowed({"user","admin"})
@GET
@Path("/grouped")
@Produces(MediaType.APPLICATION_JSON)
public SliceResponse getGroupedDataPoints(
@QueryParam("json") String json
) {
ObjectMapper m = new ObjectMapper();
try {
logger.debug("Processing request...");
logger.debug(json);
GroupedAddressRequest readSpecs = m.readValue(json,GroupedAddressRequest.class);
logger.debug(readSpecs);
return dc.getDataPointGroupedValues(readSpecs);
} catch (IOException e) {
return SliceResponse.getErrorResponse("Unable to parse query:" + e);
}
TrendsRequest r = getReadSpecsSingle(id, start, end, null, null, null, null, null);
Trend v = dc.getTrend(r);
return new SeriesOfValuesResponse(r.getAddress(), v);
}
@RolesAllowed({"user","admin"})
@GET
@Path("/group-aggregated")
@Produces(MediaType.APPLICATION_JSON)
public AggregateResponse getGroupedAggregatedDataPoints(
@QueryParam("json") String json,
@DefaultValue("NONE") @QueryParam("aggregation") AggregationFunction agg
) {
ObjectMapper m = new ObjectMapper();
try {
logger.debug("Processing request...");
logger.debug(json);
GroupedAddressRequest readSpecs = m.readValue(json, GroupedAddressRequest.class);
logger.debug(readSpecs);
AggregateResponse result = dc.getDataPointGroupAggregations(readSpecs, agg);
logger.debug(result);
return result;
} catch (IOException e) {
return AggregateResponse.getErrorResponse("Unable to parse query:" + e);
}
private TrendsRequest getReadSpecsSingle(String id, String start, String end, String samplingCron,
Long samplingDur, Interpolation interpolation, AggregationFunction aggregation, AggregationWindow window) {
TrendsRequest readSpecs = getReadSpecsBase(start, end, samplingCron, samplingDur, interpolation, aggregation, window);
AddressRequest ar = new AddressRequest();
ar.setAddresses(Arrays.asList(new Address[] { new Address(id)}));
readSpecs.setAddressSpecs(ar);
return readSpecs;
}
@RolesAllowed({"user","admin"})
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public SliceResponse getTrend(
@QueryParam("id") String id,
@QueryParam("start") String start,
@QueryParam("end") String end,
@QueryParam("sampling.cron") String samplingCron,
@QueryParam("sampling.duration") Long samplingDur,
@DefaultValue("NONE") @QueryParam("interpolation") Interpolation interpolation
) {
Address a = new Address(id);
Trend v = ProviderManager.getTrendsProvider(a.getProtocol()).getTrend(a, getSeriesSpecs(start, end, samplingCron, samplingDur, interpolation));
return new SliceResponse(a, v);
private TrendsRequest getReadSpecsMulti(String json, boolean grouping, String start, String end, String samplingCron,
Long samplingDur, Interpolation interpolation, AggregationFunction aggregation, AggregationWindow window) throws JsonParseException, JsonMappingException, IOException {
TrendsRequest readSpecs = getReadSpecsBase(start, end, samplingCron, samplingDur, interpolation, aggregation, window);
readSpecs.setAddressSpecs(m.readValue(json, AddressRequest.class));
return readSpecs;
}
private TrendsRequest getReadSpecs(String json, String start, String end, String samplingCron,
Long samplingDur, Interpolation interpolation, AggregationFunction aggregation) {
private TrendsRequest getReadSpecsBase(String start, String end, String samplingCron,
Long samplingDur, Interpolation interpolation, AggregationFunction aggregation, AggregationWindow window) {
TrendsRequest readSpecs = new TrendsRequest();
readSpecs.setAddressesSpecs(m.readValue(json, AddressRequest.class));
SeriesSpecs s = getSeriesSpecs(start, end, samplingCron, samplingDur, interpolation);
readSpecs.setSeriesSpecs(s);
readSpecs.setAggregation(aggregation);
readSpecs.setAggregation(aggregation, window);
return readSpecs;
}
......
package cz.muni.fi.lasaris.sbms.data.api.request;
import cz.muni.fi.lasaris.sbms.data.entities.Address;
import cz.muni.fi.lasaris.sbms.data.entities.AggregationFunction;
import cz.muni.fi.lasaris.sbms.data.entities.AggregationWindow;
import cz.muni.fi.lasaris.sbms.data.entities.SeriesSpecs;
public class TrendsRequest {
public void setAddressSpecs(AddressRequest ar) {
// TODO Auto-generated method stub
}
public Address getAddress() {
// TODO Auto-generated method stub
return null;
}
public void setSeriesSpecs(SeriesSpecs s) {
// TODO Auto-generated method stub
}
public void setAggregation(AggregationFunction aggregation, AggregationWindow window) {
// TODO Auto-generated method stub
}
}
......@@ -8,6 +8,8 @@ import cz.muni.fi.lasaris.sbms.data.entities.containers.Aggregation;
import cz.muni.fi.lasaris.sbms.data.entities.values.AggregatedValue;
public class AggregateResponse {
private static final String DEFAULT_GROUPING = "noGrouping";
private String error;
private Map<String, AggregatedValue> results;
......@@ -31,7 +33,7 @@ public class AggregateResponse {
public AggregateResponse(AggregatedValue v) {
this.results = new LinkedHashMap<String, AggregatedValue>();
this.results.put("value", v);
this.results.put(DEFAULT_GROUPING, v);
}
public AggregateResponse(Aggregation<AddressGroup> a) {
......
......@@ -8,7 +8,7 @@ import cz.muni.fi.lasaris.sbms.data.entities.containers.Series;
import cz.muni.fi.lasaris.sbms.data.entities.values.Value;
public class SeriesOfValuesResponse {
private static final String DEFAULT_GROUPING = "noGrouping";
private static final String DEFAULT_GROUPING = "noGrouping";
private String error;
private Map<String, Map<Address, Series<? extends Value>>> results;
......
......@@ -4,17 +4,17 @@ import java.util.LinkedHashMap;
import java.util.Map;
import cz.muni.fi.lasaris.sbms.data.entities.Address;
import cz.muni.fi.lasaris.sbms.data.entities.containers.Snapshot;
import cz.muni.fi.lasaris.sbms.data.entities.values.RawValue;
import cz.muni.fi.lasaris.sbms.data.entities.containers.Slice;
import cz.muni.fi.lasaris.sbms.data.entities.values.InterpolatedValue;
public class SliceResponse {
private static final String DEFAULT_GROUPING = "noGrouping";
private String error;
private Map<String, Snapshot> results;
private Map<String, Slice> results;
public Map<String, Snapshot> getResults() {
public Map<String, Slice> getResults() {
return this.results;
}
......@@ -28,17 +28,17 @@ public class SliceResponse {
this.results = null;
}
public SliceResponse(Map<String, Snapshot> data) {
public SliceResponse(Map<String, Slice> data) {
this.results = data;
}
public SliceResponse(Snapshot data) {
this.results = new LinkedHashMap<String, Snapshot>();
public SliceResponse(Slice data) {
this.results = new LinkedHashMap<String, Slice>();
this.results.put(DEFAULT_GROUPING, data);
}
public SliceResponse(Address a, RawValue v) {
this(new Snapshot());
public SliceResponse(Address a, InterpolatedValue v) {
this(new Slice());
this.results.get(DEFAULT_GROUPING).add(a, v);
}
......
package cz.muni.fi.lasaris.sbms.data.api.response;
import java.util.LinkedHashMap;
import java.util.Map;
import cz.muni.fi.lasaris.sbms.data.entities.Address;
import cz.muni.fi.lasaris.sbms.data.entities.containers.Snapshot;
import cz.muni.fi.lasaris.sbms.data.entities.values.RawValue;
public class SnapshotResponse {
private static final String DEFAULT_GROUPING = "noGrouping";
private String error;
private Map<String, Snapshot> results;
public Map<String, Snapshot> getResults() {
return this.results;
}
public static SnapshotResponse getErrorResponse(String error) {
SnapshotResponse r = new SnapshotResponse();
r.setError(error);
return r;
}
private SnapshotResponse() {
this.results = null;
}
public SnapshotResponse(Map<String, Snapshot> data) {
this.results = data;
}
public SnapshotResponse(Snapshot data) {
this.results = new LinkedHashMap<String, Snapshot>();
this.results.put(DEFAULT_GROUPING, data);
}
public SnapshotResponse(Address a, RawValue v) {
this(new Snapshot());
this.results.get(DEFAULT_GROUPING).add(a, v);
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
}
......@@ -13,7 +13,7 @@ import org.apache.log4j.Logger;
import cz.muni.fi.lasaris.sbms.data.api.request.GroupedAddressRequest;
import cz.muni.fi.lasaris.sbms.data.api.request.AddressRequest;
import cz.muni.fi.lasaris.sbms.data.api.response.AggregateResponse;
import cz.muni.fi.lasaris.sbms.data.api.response.SliceResponse;
import cz.muni.fi.lasaris.sbms.data.api.response.SnapshotResponse;
import cz.muni.fi.lasaris.sbms.data.entities.Address;
import cz.muni.fi.lasaris.sbms.data.entities.AggregationFunction;
import cz.muni.fi.lasaris.sbms.data.entities.containers.Snapshot;
......@@ -23,12 +23,12 @@ import cz.muni.fi.lasaris.sbms.data.util.Aggregator;
public class DataCollector {
public class DPDataCollector {
final static Logger logger = Logger.getLogger(DataCollector.class);
final static Logger logger = Logger.getLogger(DPDataCollector.class);
public SliceResponse getDataPointValues(AddressRequest readSpecs, boolean allowCache) {
return new SliceResponse(getDataPointValuesSnapshot(readSpecs, allowCache));
public SnapshotResponse getDataPointValues(AddressRequest readSpecs, boolean allowCache) {
return new SnapshotResponse(getDataPointValuesSnapshot(readSpecs, allowCache));
}
private Snapshot getDataPointValuesSnapshot(AddressRequest readSpecs, boolean allowCache) {
......@@ -96,9 +96,9 @@ NavigableMap<String, AddressRequest> protocols = readSpecs.getProtocols();
}
public SliceResponse getDataPointGroupedValues(GroupedAddressRequest readSpecs,
public SnapshotResponse getDataPointGroupedValues(GroupedAddressRequest readSpecs,
boolean allowCache) {
return new SliceResponse(getDataPointGroupedValuesMap(readSpecs, allowCache));
return new SnapshotResponse(getDataPointGroupedValuesMap(readSpecs, allowCache));
}
private Map<String, Snapshot> getDataPointGroupedValuesMap(GroupedAddressRequest readSpecs,
......@@ -113,13 +113,13 @@ NavigableMap<String, AddressRequest> protocols = readSpecs.getProtocols();
AtomicInteger counter = new AtomicInteger(0);
int count = protocols.keySet().size();
List<SliceResponse> results = Collections.synchronizedList(new LinkedList<SliceResponse>());
List<SnapshotResponse> results = Collections.synchronizedList(new LinkedList<SnapshotResponse>());
for(String protocol : protocols.keySet()) {
new Thread(new Runnable() {
@Override
public void run() {
SliceResponse rs = new SliceResponse(ProviderManager.getDataPointsProvider(protocol).getDataPointGroupedValues(protocols.get(protocol).getGroups(), allowCache));
SnapshotResponse rs = new SnapshotResponse(ProviderManager.getDataPointsProvider(protocol).getDataPointGroupedValues(protocols.get(protocol).getGroups(), allowCache));
synchronized(results) {
results.add(rs);
counter.incrementAndGet();
......@@ -145,7 +145,7 @@ NavigableMap<String, AddressRequest> protocols = readSpecs.getProtocols();
}
synchronized(results) {
for(SliceResponse r : results) {
for(SnapshotResponse r : results) {
for(String group : r.getResults().keySet()) {
result.get(group).addAll(r.getResults().get(group).getData());
}
......
package cz.muni.fi.lasaris.sbms.data.logic;
import cz.muni.fi.lasaris.sbms.data.api.request.TrendsRequest;
import cz.muni.fi.lasaris.sbms.data.entities.containers.Trend;
public class TrendsDataCollector {
public Trend getTrend(TrendsRequest r) {
// TODO Auto-generated method stub
return null;
}
}
package cz.muni.fi.lasaris.sbms.data.entities.containers;
import java.util.Map;
import cz.muni.fi.lasaris.sbms.data.entities.Address;
import cz.muni.fi.lasaris.sbms.data.entities.values.Value;
public abstract class AbstractSliceContainer<V extends Value> extends CompositeDataContainer<Address, V> {
public AbstractSliceContainer() {
super();
}
public AbstractSliceContainer(Map<Address, V> data) {
super(data);
}
}
......@@ -11,7 +11,7 @@ import cz.muni.fi.lasaris.sbms.data.entities.values.InterpolatedValue;
import cz.muni.fi.lasaris.sbms.data.entities.values.Value;
import cz.muni.fi.lasaris.sbms.data.util.Sampler;
public class Slice extends CompositeDataContainer<Address, InterpolatedValue> {
public class Slice extends AbstractSliceContainer<InterpolatedValue> {
public Slice() {
super();
......
......@@ -5,7 +5,7 @@ import java.util.Map;
import cz.muni.fi.lasaris.sbms.data.entities.Address;
import cz.muni.fi.lasaris.sbms.data.entities.values.RawValue;
public class Snapshot extends CompositeDataContainer<Address, RawValue> {
public class Snapshot extends AbstractSliceContainer<RawValue> {
public Snapshot() {
super();
}
......