Commit 8d26cdec authored by Adam Kučera's avatar Adam Kučera
Browse files

finished the API definition

parent 34a64cbe
...@@ -21,6 +21,7 @@ import cz.muni.fi.lasaris.sbms.data.api.request.GroupedAddressRequest; ...@@ -21,6 +21,7 @@ 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.request.AddressRequest;
import cz.muni.fi.lasaris.sbms.data.api.response.AggregateResponse; import cz.muni.fi.lasaris.sbms.data.api.response.AggregateResponse;
import cz.muni.fi.lasaris.sbms.data.api.response.SnapshotResponse; import cz.muni.fi.lasaris.sbms.data.api.response.SnapshotResponse;
import cz.muni.fi.lasaris.sbms.data.api.response.ValueResponse;
import cz.muni.fi.lasaris.sbms.data.entities.Address; 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.AggregationFunction;
import cz.muni.fi.lasaris.sbms.data.entities.values.RawValue; import cz.muni.fi.lasaris.sbms.data.entities.values.RawValue;
...@@ -131,12 +132,12 @@ public class DataPointsEndpoint { ...@@ -131,12 +132,12 @@ public class DataPointsEndpoint {
@GET @GET
@Path("/{id}") @Path("/{id}")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public SnapshotResponse getDataPoint( public ValueResponse getDataPoint(
@PathParam("id") String idP, @PathParam("id") String idP,
@QueryParam("cache") String cache) { @QueryParam("cache") String cache) {
Address a = new Address(idP); Address a = new Address(idP);
RawValue v = ProviderManager.getDataPointsProvider(a.getProtocol()).getDataPointValue(a, allowCache(cache)); RawValue v = ProviderManager.getDataPointsProvider(a.getProtocol()).getDataPointValue(a, allowCache(cache));
return new SnapshotResponse(a, v); return new ValueResponse(v);
} }
private boolean allowCache(String cache) { private boolean allowCache(String cache) {
......
...@@ -4,14 +4,11 @@ import java.io.IOException; ...@@ -4,14 +4,11 @@ import java.io.IOException;
import java.time.Duration; import java.time.Duration;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit; import java.util.Map;
import java.util.Arrays;
import javax.annotation.security.RolesAllowed; import javax.annotation.security.RolesAllowed;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.POST; import javax.ws.rs.POST;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
...@@ -27,102 +24,442 @@ import cz.muni.fi.lasaris.sbms.data.api.request.GroupedAddressRequest; ...@@ -27,102 +24,442 @@ 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.TrendsRequest;
import cz.muni.fi.lasaris.sbms.data.api.request.AddressRequest; 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.AggregateResponse;
import cz.muni.fi.lasaris.sbms.data.api.response.SetOfSeriesOfValuesResponse; import cz.muni.fi.lasaris.sbms.data.api.response.SeriesOfSlicesResponse;
import cz.muni.fi.lasaris.sbms.data.api.response.SetOfAggregatesResponse;
import cz.muni.fi.lasaris.sbms.data.api.response.SetOfTrendsResponse;
import cz.muni.fi.lasaris.sbms.data.api.response.SliceResponse; import cz.muni.fi.lasaris.sbms.data.api.response.SliceResponse;
import cz.muni.fi.lasaris.sbms.data.api.response.TrendResponse;
import cz.muni.fi.lasaris.sbms.data.api.response.ValueResponse;
import cz.muni.fi.lasaris.sbms.data.entities.Address; import cz.muni.fi.lasaris.sbms.data.entities.Address;
import cz.muni.fi.lasaris.sbms.data.entities.AddressGroup;
import cz.muni.fi.lasaris.sbms.data.entities.AggregationFunction; 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.AggregationWindow;
import cz.muni.fi.lasaris.sbms.data.entities.Interpolation; import cz.muni.fi.lasaris.sbms.data.entities.Interpolation;
import cz.muni.fi.lasaris.sbms.data.entities.SeriesSpecs; import cz.muni.fi.lasaris.sbms.data.entities.SeriesSpecs;
import cz.muni.fi.lasaris.sbms.data.entities.containers.Aggregation;
import cz.muni.fi.lasaris.sbms.data.entities.containers.Series;
import cz.muni.fi.lasaris.sbms.data.entities.containers.Slice;
import cz.muni.fi.lasaris.sbms.data.entities.containers.Trend; import cz.muni.fi.lasaris.sbms.data.entities.containers.Trend;
import cz.muni.fi.lasaris.sbms.data.entities.values.AggregatedValue;
import cz.muni.fi.lasaris.sbms.data.entities.values.InterpolatedValue;
import cz.muni.fi.lasaris.sbms.data.logic.TrendsDataCollector; import cz.muni.fi.lasaris.sbms.data.logic.TrendsDataCollector;
public class TrendsEndpoint { public class TrendsEndpoint {
final static Logger logger = Logger.getLogger(TrendsEndpoint.class); final static Logger logger = Logger.getLogger(TrendsEndpoint.class);
private TrendsDataCollector dc; private TrendsDataCollector dc;
private ObjectMapper m; private ObjectMapper m;
public TrendsEndpoint() { public TrendsEndpoint() {
dc = new TrendsDataCollector(); dc = new TrendsDataCollector();
m = new ObjectMapper(); m = new ObjectMapper();
m.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); m.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
} }
// 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
/* // Method 4
// Methods 4, 6
@RolesAllowed({"user","admin"}) @RolesAllowed({"user","admin"})
@GET @POST
@Path("/serie/{id}") @Path("/si")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public SeriesOfValuesResponse getFunc( public TrendResponse getSI(
@QueryParam("id") String id,
@QueryParam("start") String start,
@QueryParam("end") String end
) {
try {
TrendsRequest r = getReadSpecsSingle(id, start, end, null, null, null, null, null);
Trend v = dc.getTrend(r);
return new TrendResponse(v);
} catch (Exception e) {
return TrendResponse.getErrorResponse(e.getMessage());
}
}
// Method 5
@RolesAllowed({"user","admin"})
@POST
@Path("/sts")
@Produces(MediaType.APPLICATION_JSON)
public ValueResponse getSTS(
@QueryParam("id") String id,
@QueryParam("timestamp") String timestamp,
@QueryParam("sampling.cron") String samplingCron,
@QueryParam("sampling.duration") Long samplingDur,
@QueryParam("sampling.interpolation") Interpolation interpolation
) {
try {
TrendsRequest r = getReadSpecsSingle(id, timestamp, timestamp, samplingCron, samplingDur, interpolation, null, null);
InterpolatedValue v = dc.getValue(r);
return new ValueResponse(v);
} catch (Exception e) {
return ValueResponse.getErrorResponse(e.getMessage());
}
}
// Method 6
@RolesAllowed({"user","admin"})
@POST
@Path("/sis")
@Produces(MediaType.APPLICATION_JSON)
public TrendResponse getSIS(
@QueryParam("id") String id, @QueryParam("id") String id,
@QueryParam("start") String start, @QueryParam("start") String start,
@QueryParam("end") String end, @QueryParam("end") String end,
@QueryParam("sampling.cron") String samplingCron, @QueryParam("sampling.cron") String samplingCron,
@QueryParam("sampling.duration") Long samplingDur, @QueryParam("sampling.duration") Long samplingDur,
@QueryParam("sampling.interpolation") Interpolation interpolation, @QueryParam("sampling.interpolation") Interpolation interpolation
@QueryParam("aggregation.function") String aggregation,
@QueryParam("aggregation.window") String window,
) { ) {
Address a = new Address(id); try {
Trend v = ProviderManager.getTrendsProvider(a.getProtocol()).getTrend(a, getSeriesSpecs(start, end, samplingCron, samplingDur, interpolation)); TrendsRequest r = getReadSpecsSingle(id, start, end, samplingCron, samplingDur, interpolation, null, null);
return new SeriesOfValuesResponse(a, v); Trend v = dc.getTrend(r);
return new TrendResponse(v);
} catch (Exception e) {
return TrendResponse.getErrorResponse(e.getMessage());
}
} }
*/
// Method 7
// Method 4
@RolesAllowed({"user","admin"}) @RolesAllowed({"user","admin"})
@POST @POST
@Path("/si") @Path("/sas")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public SetOfSeriesOfValuesResponse getSI( public ValueResponse getSAS(
@QueryParam("id") String id, @QueryParam("id") String id,
@QueryParam("start") String start, @QueryParam("start") String start,
@QueryParam("end") String end,
@QueryParam("aggregation.function") AggregationFunction aggregation
) {
try {
TrendsRequest r = getReadSpecsSingle(id, start, end, null, null, null, aggregation, null);
AggregatedValue v = dc.getAggregate(r);
return new ValueResponse(v);
} catch (Exception e) {
return ValueResponse.getErrorResponse(e.getMessage());
}
}
// Method 8
@RolesAllowed({"user","admin"})
@POST
@Path("/siw")
@Produces(MediaType.APPLICATION_JSON)
public TrendResponse getSIW(
@QueryParam("id") String id,
@QueryParam("start") String start,
@QueryParam("end") String end,
@QueryParam("aggregation.function") AggregationFunction aggregation,
@QueryParam("aggregation.window") AggregationWindow window
) {
try {
TrendsRequest r = getReadSpecsSingle(id, start, end, null, null, null, aggregation, window);
Trend v = dc.getTrend(r);
return new TrendResponse(v);
} catch (Exception e) {
return TrendResponse.getErrorResponse(e.getMessage());
}
}
// Method 9
@RolesAllowed({"user","admin"})
@POST
@Path("/mt")
@Produces(MediaType.APPLICATION_JSON)
public SliceResponse getMT(
@QueryParam("ids") String ids,
@QueryParam("grouping") boolean grouping,
@QueryParam("timestamp") String timestamp
) {
try {
TrendsRequest r = getReadSpecsMulti(ids, grouping, timestamp, timestamp, null, null, null, null, null);
Slice v = dc.getSlice(r);
return new SliceResponse(v);
} catch (Exception e) {
return SliceResponse.getErrorResponse(e.getMessage());
}
}
// Method 10
@RolesAllowed({"user","admin"})
@POST
@Path("/mta")
@Produces(MediaType.APPLICATION_JSON)
public AggregateResponse getMTA(
@QueryParam("ids") String ids,
@QueryParam("grouping") boolean grouping,
@QueryParam("timestamp") String timestamp,
@QueryParam("aggregation.function") AggregationFunction aggregation
) {
try {
TrendsRequest r = getReadSpecsMulti(ids, grouping, timestamp, timestamp, null, null, null, null, null);
if(grouping) {
Aggregation<AddressGroup> v = dc.getGroupedAggregatesForSlice(r);
return new AggregateResponse(v);
} else {
AggregatedValue v = dc.getAggregateForSlice(r);
return new AggregateResponse(v);
}
} catch (Exception e) {
return AggregateResponse.getErrorResponse(e.getMessage());
}
}
// Method 11
@RolesAllowed({"user","admin"})
@POST
@Path("/mi")
@Produces(MediaType.APPLICATION_JSON)
public SetOfTrendsResponse getMI(
@QueryParam("ids") String ids,
@QueryParam("grouping") boolean grouping,
@QueryParam("start") String start,
@QueryParam("end") String end @QueryParam("end") String end
) { ) {
try {
TrendsRequest r = getReadSpecsMulti(ids, grouping, start, end, null, null, null, null, null);
if(grouping) {
Map<String, Map<Address, Trend>> v = dc.getGroupedTrends(r);
return new SetOfTrendsResponse(v, true);
} else {
Map<Address, Trend> v = dc.getTrends(r);
return new SetOfTrendsResponse(v);
}
} catch (Exception e) {
return SetOfTrendsResponse.getErrorResponse(e.getMessage());
}
}
// Method 12
@RolesAllowed({"user","admin"})
@POST
@Path("/mia")
@Produces(MediaType.APPLICATION_JSON)
public SetOfAggregatesResponse getMIA(
@QueryParam("ids") String ids,
@QueryParam("grouping") boolean grouping,
@QueryParam("start") String start,
@QueryParam("end") String end,
@QueryParam("aggregation.function") AggregationFunction aggregation
) {
try {
TrendsRequest r = getReadSpecsMulti(ids, grouping, start, end, null, null, null, aggregation, null);
if(grouping) {
Map<String, Aggregation<Address>> v = dc.getGroupedAggregates(r);
return new SetOfAggregatesResponse(v);
} else {
Aggregation<Address> v = dc.getAggregates(r);
return new SetOfAggregatesResponse(v);
}
} catch (Exception e) {
return SetOfAggregatesResponse.getErrorResponse(e.getMessage());
}
}
// Method 13
@RolesAllowed({"user","admin"})
@POST
@Path("/miaa")
@Produces(MediaType.APPLICATION_JSON)
public AggregateResponse getMIAA(
@QueryParam("ids") String ids,
@QueryParam("grouping") boolean grouping,
@QueryParam("start") String start,
@QueryParam("end") String end,
@QueryParam("aggregation.function") AggregationFunction aggregation
) {
try {
TrendsRequest r = getReadSpecsMulti(ids, grouping, start, end, null, null, null, aggregation, null);
if(grouping) {
Aggregation<AddressGroup> v = dc.getGroupedTrendsAggregate(r);
return new AggregateResponse(v);
} else {
AggregatedValue v = dc.getTrendsAggregate(r);
return new AggregateResponse(v);
}
} catch (Exception e) {
return AggregateResponse.getErrorResponse(e.getMessage());
}
}
TrendsRequest r = getReadSpecsSingle(id, start, end, null, null, null, null, null); // Method 14
Trend v = dc.getTrend(r); @RolesAllowed({"user","admin"})
return new SetOfSeriesOfValuesResponse(r.getAddress(), v); @POST
@Path("/misss")
@Produces(MediaType.APPLICATION_JSON)
public SetOfTrendsResponse getMISSS(
@QueryParam("ids") String ids,
@QueryParam("grouping") boolean grouping,
@QueryParam("start") String start,
@QueryParam("end") String end,
@QueryParam("sampling.cron") String samplingCron,
@QueryParam("sampling.duration") Long samplingDur,
@QueryParam("sampling.interpolation") Interpolation interpolation
) {
try {
TrendsRequest r = getReadSpecsMulti(ids, grouping, start, end, samplingCron, samplingDur, interpolation, null, null);
if(grouping) {
Map<String, Map<Address, Trend>> v = dc.getGroupedTrends(r);
return new SetOfTrendsResponse(v, true);
} else {
Map<Address, Trend> v = dc.getTrends(r);
return new SetOfTrendsResponse(v);
}
} catch (Exception e) {
return SetOfTrendsResponse.getErrorResponse(e.getMessage());
}
}
// Method 15
@RolesAllowed({"user","admin"})
@POST
@Path("/missl")
@Produces(MediaType.APPLICATION_JSON)
public SeriesOfSlicesResponse getMISSL(
@QueryParam("ids") String ids,
@QueryParam("grouping") boolean grouping,
@QueryParam("start") String start,
@QueryParam("end") String end,
@QueryParam("sampling.cron") String samplingCron,
@QueryParam("sampling.duration") Long samplingDur,
@QueryParam("sampling.interpolation") Interpolation interpolation
) {
try {
TrendsRequest r = getReadSpecsMulti(ids, grouping, start, end, samplingCron, samplingDur, interpolation, null, null);
if(grouping) {
Map<String, Series<Slice>> v = dc.getGroupedSeriesOfSlices(r);
return new SeriesOfSlicesResponse(v);
} else {
Series<Slice> v = dc.getSeriesOfSlices(r);
return new SeriesOfSlicesResponse(v);
}
} catch (Exception e) {
return SeriesOfSlicesResponse.getErrorResponse(e.getMessage());
}
} }
// Method 16 // Method 16
@RolesAllowed({"user","admin"}) @RolesAllowed({"user","admin"})
@POST @POST
@Path("/mias") @Path("/mias")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public SetOfSeriesOfValuesResponse getMIAS( public TrendResponse getMIAS(
@QueryParam("ids") String ids, @QueryParam("ids") String ids,
@QueryParam("grouping") boolean grouping, @QueryParam("grouping") boolean grouping,
@QueryParam("start") String start, @QueryParam("start") String start,
@QueryParam("end") String end, @QueryParam("end") String end,
@QueryParam("sampling.cron") String samplingCron, @QueryParam("sampling.cron") String samplingCron,
@QueryParam("sampling.duration") Long samplingDur, @QueryParam("sampling.duration") Long samplingDur,
@QueryParam("sampling.interpolation") Interpolation interpolation, @QueryParam("sampling.interpolation") Interpolation interpolation,
@QueryParam("aggregation.function") AggregationFunction aggregation @QueryParam("aggregation.function") AggregationFunction aggregation
) { ) {
try { try {
TrendsRequest r = getReadSpecsMulti(ids, grouping, start, end, samplingCron, samplingDur, interpolation, aggregation, null); TrendsRequest r = getReadSpecsMulti(ids, grouping, start, end, samplingCron, samplingDur, interpolation, aggregation, null);
Trend v = dc.getTrend(r); if(grouping) {
return new SetOfSeriesOfValuesResponse(r.getAddress(), v); Map<String, Trend> v = dc.getGroupedSeriesOfAggregates(r);
} catch(Exception e) { return new TrendResponse(v);
return SetOfSeriesOfValuesResponse.getErrorResponse(e.getMessage()); } else {
Trend v = dc.getSeriesOfAggregates(r);
return new TrendResponse(v);
} }
} catch(Exception e) {
return TrendResponse.getErrorResponse(e.getMessage());
} }
}
// Method 17
@RolesAllowed({"user","admin"})
@POST
@Path("/miw")
@Produces(MediaType.APPLICATION_JSON)
public SetOfTrendsResponse getMIW(
@QueryParam("ids") String ids,
@QueryParam("grouping") boolean grouping,
@QueryParam("start") String start,
@QueryParam("end") String end,
@QueryParam("aggregation.function") AggregationFunction aggregation,
@QueryParam("aggregation.window") AggregationWindow window
) {
try {
TrendsRequest r = getReadSpecsMulti(ids, grouping, start, end, null, null, null, aggregation, window);
if(grouping) {
Map<String, Map<Address, Trend>> v = dc.getGroupedTrends(r);
return new SetOfTrendsResponse(v, true);
} else {
Map<Address, Trend> v = dc.getTrends(r);
return new SetOfTrendsResponse(v);
}
} catch(Exception e) {
return SetOfTrendsResponse.getErrorResponse(e.getMessage());
}
}
// Method 18
@RolesAllowed({"user","admin"})
@POST
@Path("/miwa")
@Produces(MediaType.APPLICATION_JSON)
public TrendResponse getMIWA(
@QueryParam("ids") String ids,
@QueryParam("grouping") boolean grouping,
@QueryParam("start") String start,
@QueryParam("end") String end,
@QueryParam("aggregation.function") AggregationFunction aggregation,
@QueryParam("aggregation.window") AggregationWindow window
) {
try {
TrendsRequest r = getReadSpecsMulti(ids, grouping, start, end, null, null, null, aggregation, window);
if(grouping) {
Map<String, Trend> v = dc.getGroupedWindowedTrendsAggregate(r);
return new TrendResponse(v);
} else {
Trend v = dc.getWindowedTrendsAggregate(r);
return new TrendResponse(v);
}
} catch(Exception e) {
return TrendResponse.getErrorResponse(e.getMessage());
}
}
/* TEMPLATE
@RolesAllowed({"user","admin"})
@GET
@Path("/serie/{id}")
@Produces(MediaType.APPLICATION_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,
@QueryParam("sampling.interpolation") Interpolation interpolation,
@QueryParam("aggregation.function") AggregationFunction aggregation,
@QueryParam("aggregation.window") AggregationWindow window,
) {
Address a = new Address(id);
Trend v = ProviderManager.getTrendsProvider(a.getProtocol()).getTrend(a, getSeriesSpecs(start, end, samplingCron, samplingDur, interpolation));
return new SeriesOfValuesResponse(a, v);
}
*/
private TrendsRequest getReadSpecsSingle(String id, String start, String end, String samplingCron, private TrendsRequest getReadSpecsSingle(String id, String start, String end, String samplingCron,
Long samplingDur, Interpolation interpolation, AggregationFunction aggregation, AggregationWindow window) { Long samplingDur, Interpolation interpolation, AggregationFunction aggregation, AggregationWindow window) {