Commit 5ef24f0d authored by Admin's avatar Admin
Browse files

refactoring + response types added

parent acae79c0
......@@ -19,8 +19,8 @@ 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.AggregationResponse;
import cz.muni.fi.lasaris.sbms.data.api.response.SnapshotResponse;
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.entities.Address;
import cz.muni.fi.lasaris.sbms.data.entities.AggregationFunction;
import cz.muni.fi.lasaris.sbms.data.entities.values.RawValue;
......@@ -39,7 +39,7 @@ public class DataPointsEndpoint {
@RolesAllowed({"user","admin"})
@GET
@Produces(MediaType.APPLICATION_JSON)
public SnapshotResponse getDataPoints(
public SliceResponse 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);
SnapshotResponse result = dc.getDataPointValues(readSpecs, allowCache(cache));
SliceResponse result = dc.getDataPointValues(readSpecs, allowCache(cache));
logger.debug(result);
return result;
} catch (IOException e) {
return SnapshotResponse.getErrorResponse("Unable to parse query:" + e);
return SliceResponse.getErrorResponse("Unable to parse query:" + e);
}
......@@ -66,7 +66,7 @@ public class DataPointsEndpoint {
@GET
@Path("/aggregated")
@Produces(MediaType.APPLICATION_JSON)
public AggregationResponse getAggregatedDataPoints(
public AggregateResponse getAggregatedDataPoints(
@QueryParam("ids") String json,
@DefaultValue("NONE") @QueryParam("aggregation") AggregationFunction agg,
@QueryParam("cache") String cache
......@@ -79,7 +79,7 @@ public class DataPointsEndpoint {
logger.debug(readSpecs);
return dc.getDataPointsAggregation(readSpecs, agg, allowCache(cache));
} catch (IOException e) {
return AggregationResponse.getErrorResponse("Unable to parse query:" + e);
return AggregateResponse.getErrorResponse("Unable to parse query:" + e);
}
}
......@@ -88,7 +88,7 @@ public class DataPointsEndpoint {
@GET
@Path("/grouped")
@Produces(MediaType.APPLICATION_JSON)
public SnapshotResponse getGroupedDataPoints(
public SliceResponse 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 SnapshotResponse.getErrorResponse("Unable to parse query:" + e);
return SliceResponse.getErrorResponse("Unable to parse query:" + e);
}
}
......@@ -108,7 +108,7 @@ public class DataPointsEndpoint {
@GET
@Path("/group-aggregated")
@Produces(MediaType.APPLICATION_JSON)
public AggregationResponse getGroupedAggregatedDataPoints(
public AggregateResponse getGroupedAggregatedDataPoints(
@QueryParam("ids") String json,
@DefaultValue("NONE") @QueryParam("aggregation") AggregationFunction agg,
@QueryParam("cache") String cache
......@@ -119,11 +119,11 @@ public class DataPointsEndpoint {
logger.debug(json);
GroupedAddressRequest readSpecs = m.readValue(json, GroupedAddressRequest.class);
logger.debug(readSpecs);
AggregationResponse result = dc.getDataPointGroupAggregations(readSpecs, agg, allowCache(cache));
AggregateResponse result = dc.getDataPointGroupAggregations(readSpecs, agg, allowCache(cache));
logger.debug(result);
return result;
} catch (IOException e) {
return AggregationResponse.getErrorResponse("Unable to parse query:" + e);
return AggregateResponse.getErrorResponse("Unable to parse query:" + e);
}
}
......@@ -131,12 +131,12 @@ public class DataPointsEndpoint {
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public SnapshotResponse getDataPoint(
public SliceResponse 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 SnapshotResponse(a, v);
return new SliceResponse(a, v);
}
private boolean allowCache(String cache) {
......
......@@ -22,9 +22,9 @@ 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.AggregationResponse;
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.AggregateResponse;
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.entities.Address;
import cz.muni.fi.lasaris.sbms.data.entities.AggregationFunction;
import cz.muni.fi.lasaris.sbms.data.entities.Interpolation;
......@@ -55,7 +55,7 @@ public class TrendsEndpoint {
@RolesAllowed({"user","admin"})
@GET
@Produces(MediaType.APPLICATION_JSON)
public SnapshotResponse getTrends(
public SliceResponse getTrends(
@QueryParam("ids") String json,
@QueryParam("start") String start,
@QueryParam("end") String end,
......@@ -68,11 +68,11 @@ public class TrendsEndpoint {
logger.debug(json);
TrendsRequest readSpecs = getReadSpecs(json, start, end, samplingCron, samplingDur, interpolation, null);
logger.debug(readSpecs);
SnapshotResponse result = dc.getTrends(readSpecs);
SliceResponse result = dc.getTrends(readSpecs);
logger.debug(result);
return result;
} catch (IOException e) {
return SnapshotResponse.getErrorResponse("Unable to parse query:" + e);
return SliceResponse.getErrorResponse("Unable to parse query:" + e);
}
......@@ -83,8 +83,8 @@ public class TrendsEndpoint {
@GET
@Path("/aggregated")
@Produces(MediaType.APPLICATION_JSON)
public AggregationResponse getAggregatedDataPoints(
@QueryParam("json") String json,
public AggregateResponse getAggregatedTrends(
@QueryParam("ids") String json,
@DefaultValue("NONE") @QueryParam("aggregation") AggregationFunction agg
) {
ObjectMapper m = new ObjectMapper();
......@@ -95,7 +95,7 @@ public class TrendsEndpoint {
logger.debug(readSpecs);
return dc.getDataPointsAggregation(readSpecs, agg);
} catch (IOException e) {
return AggregationResponse.getErrorResponse("Unable to parse query:" + e);
return AggregateResponse.getErrorResponse("Unable to parse query:" + e);
}
}
......@@ -104,7 +104,7 @@ public class TrendsEndpoint {
@GET
@Path("/grouped")
@Produces(MediaType.APPLICATION_JSON)
public SnapshotResponse getGroupedDataPoints(
public SliceResponse getGroupedDataPoints(
@QueryParam("json") String json
) {
ObjectMapper m = new ObjectMapper();
......@@ -115,7 +115,7 @@ public class TrendsEndpoint {
logger.debug(readSpecs);
return dc.getDataPointGroupedValues(readSpecs);
} catch (IOException e) {
return SnapshotResponse.getErrorResponse("Unable to parse query:" + e);
return SliceResponse.getErrorResponse("Unable to parse query:" + e);
}
}
......@@ -123,7 +123,7 @@ public class TrendsEndpoint {
@GET
@Path("/group-aggregated")
@Produces(MediaType.APPLICATION_JSON)
public AggregationResponse getGroupedAggregatedDataPoints(
public AggregateResponse getGroupedAggregatedDataPoints(
@QueryParam("json") String json,
@DefaultValue("NONE") @QueryParam("aggregation") AggregationFunction agg
) {
......@@ -133,11 +133,11 @@ public class TrendsEndpoint {
logger.debug(json);
GroupedAddressRequest readSpecs = m.readValue(json, GroupedAddressRequest.class);
logger.debug(readSpecs);
AggregationResponse result = dc.getDataPointGroupAggregations(readSpecs, agg);
AggregateResponse result = dc.getDataPointGroupAggregations(readSpecs, agg);
logger.debug(result);
return result;
} catch (IOException e) {
return AggregationResponse.getErrorResponse("Unable to parse query:" + e);
return AggregateResponse.getErrorResponse("Unable to parse query:" + e);
}
}
......@@ -145,7 +145,7 @@ public class TrendsEndpoint {
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public SnapshotResponse getTrend(
public SliceResponse getTrend(
@QueryParam("id") String id,
@QueryParam("start") String start,
@QueryParam("end") String end,
......@@ -155,7 +155,7 @@ public class TrendsEndpoint {
) {
Address a = new Address(id);
Trend v = ProviderManager.getTrendsProvider(a.getProtocol()).getTrend(a, getSeriesSpecs(start, end, samplingCron, samplingDur, interpolation));
return new SnapshotResponse(a, v);
return new SliceResponse(a, v);
}
private TrendsRequest getReadSpecs(String json, String start, String end, String samplingCron,
......
......@@ -7,7 +7,7 @@ import cz.muni.fi.lasaris.sbms.data.entities.AddressGroup;
import cz.muni.fi.lasaris.sbms.data.entities.containers.Aggregation;
import cz.muni.fi.lasaris.sbms.data.entities.values.AggregatedValue;
public class AggregationResponse {
public class AggregateResponse {
private String error;
private Map<String, AggregatedValue> results;
......@@ -15,26 +15,26 @@ public class AggregationResponse {
return this.results;
}
public static AggregationResponse getErrorResponse(String error) {
AggregationResponse r = new AggregationResponse();
public static AggregateResponse getErrorResponse(String error) {
AggregateResponse r = new AggregateResponse();
r.setError(error);
return r;
}
private AggregationResponse() {
private AggregateResponse() {
this.results = null;
}
public AggregationResponse(Map<String, AggregatedValue> data) {
public AggregateResponse(Map<String, AggregatedValue> data) {
this.results = data;
}
public AggregationResponse(AggregatedValue v) {
public AggregateResponse(AggregatedValue v) {
this.results = new LinkedHashMap<String, AggregatedValue>();
this.results.put("value", v);
}
public AggregationResponse(Aggregation<AddressGroup> a) {
public AggregateResponse(Aggregation<AddressGroup> a) {
this.results = new LinkedHashMap<String, AggregatedValue>();
a.forEach((k,v) -> this.results.put(k.getName(), 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.Series;
import cz.muni.fi.lasaris.sbms.data.entities.values.Value;
public class SeriesOfValuesResponse {
private static final String DEFAULT_GROUPING = "noGrouping";
private String error;
private Map<String, Map<Address, Series<? extends Value>>> results;
public Map<String, Map<Address, Series<? extends Value>>> getResults() {
return this.results;
}
public static SeriesOfValuesResponse getErrorResponse(String error) {
SeriesOfValuesResponse r = new SeriesOfValuesResponse();
r.setError(error);
return r;
}
private SeriesOfValuesResponse() {
this.results = null;
}
public SeriesOfValuesResponse(Map<String, Map<Address, Series<? extends Value>>> data, boolean groups) {
this.results = data;
}
public SeriesOfValuesResponse(Map<Address, Series<? extends Value>> data) {
this.results = new LinkedHashMap<String, Map<Address, Series<? extends Value>>>();
this.results.put(DEFAULT_GROUPING, data);
}
public SeriesOfValuesResponse(Address a, Series<? extends Value> v) {
this(new LinkedHashMap<String, Map<Address, Series<? extends Value>>>(), true);
this.results.put(DEFAULT_GROUPING, new LinkedHashMap<Address, Series<? extends Value>>());
this.results.get(DEFAULT_GROUPING).put(a, v);
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
}
......@@ -7,7 +7,7 @@ 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 {
public class SliceResponse {
private static final String DEFAULT_GROUPING = "noGrouping";
......@@ -18,26 +18,26 @@ public class SnapshotResponse {
return this.results;
}
public static SnapshotResponse getErrorResponse(String error) {
SnapshotResponse r = new SnapshotResponse();
public static SliceResponse getErrorResponse(String error) {
SliceResponse r = new SliceResponse();
r.setError(error);
return r;
}
private SnapshotResponse() {
private SliceResponse() {
this.results = null;
}
public SnapshotResponse(Map<String, Snapshot> data) {
public SliceResponse(Map<String, Snapshot> data) {
this.results = data;
}
public SnapshotResponse(Snapshot data) {
public SliceResponse(Snapshot data) {
this.results = new LinkedHashMap<String, Snapshot>();
this.results.put(DEFAULT_GROUPING, data);
}
public SnapshotResponse(Address a, RawValue v) {
public SliceResponse(Address a, RawValue v) {
this(new Snapshot());
this.results.get(DEFAULT_GROUPING).add(a, v);
......
......@@ -12,8 +12,8 @@ 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.AggregationResponse;
import cz.muni.fi.lasaris.sbms.data.api.response.SnapshotResponse;
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.entities.Address;
import cz.muni.fi.lasaris.sbms.data.entities.AggregationFunction;
import cz.muni.fi.lasaris.sbms.data.entities.containers.Snapshot;
......@@ -27,8 +27,8 @@ public class DataCollector {
final static Logger logger = Logger.getLogger(DataCollector.class);
public SnapshotResponse getDataPointValues(AddressRequest readSpecs, boolean allowCache) {
return new SnapshotResponse(getDataPointValuesSnapshot(readSpecs, allowCache));
public SliceResponse getDataPointValues(AddressRequest readSpecs, boolean allowCache) {
return new SliceResponse(getDataPointValuesSnapshot(readSpecs, allowCache));
}
private Snapshot getDataPointValuesSnapshot(AddressRequest readSpecs, boolean allowCache) {
......@@ -80,25 +80,25 @@ NavigableMap<String, AddressRequest> protocols = readSpecs.getProtocols();
return result;
}
public AggregationResponse getDataPointsAggregation(AddressRequest readSpecs, AggregationFunction aggregation,
public AggregateResponse getDataPointsAggregation(AddressRequest readSpecs, AggregationFunction aggregation,
boolean allowCache) {
NavigableMap<String, AddressRequest> protocols = readSpecs.getProtocols();
if(protocols.size() == 1) {
return new AggregationResponse(ProviderManager.getDataPointsProvider(protocols.firstKey())
return new AggregateResponse(ProviderManager.getDataPointsProvider(protocols.firstKey())
.getDataPointsAggregation(protocols.get(protocols.firstKey()).getAddresses(), aggregation, allowCache));
}
Snapshot data = getDataPointValuesSnapshot(readSpecs, allowCache);
return new AggregationResponse(computeAggregation(data.getData(), aggregation));
return new AggregateResponse(computeAggregation(data.getData(), aggregation));
}
public SnapshotResponse getDataPointGroupedValues(GroupedAddressRequest readSpecs,
public SliceResponse getDataPointGroupedValues(GroupedAddressRequest readSpecs,
boolean allowCache) {
return new SnapshotResponse(getDataPointGroupedValuesMap(readSpecs, allowCache));
return new SliceResponse(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<SnapshotResponse> results = Collections.synchronizedList(new LinkedList<SnapshotResponse>());
List<SliceResponse> results = Collections.synchronizedList(new LinkedList<SliceResponse>());
for(String protocol : protocols.keySet()) {
new Thread(new Runnable() {
@Override
public void run() {
SnapshotResponse rs = new SnapshotResponse(ProviderManager.getDataPointsProvider(protocol).getDataPointGroupedValues(protocols.get(protocol).getGroups(), allowCache));
SliceResponse rs = new SliceResponse(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(SnapshotResponse r : results) {
for(SliceResponse r : results) {
for(String group : r.getResults().keySet()) {
result.get(group).addAll(r.getResults().get(group).getData());
}
......@@ -155,19 +155,19 @@ NavigableMap<String, AddressRequest> protocols = readSpecs.getProtocols();
return result;
}
public AggregationResponse getDataPointGroupAggregations(GroupedAddressRequest readSpecs,
public AggregateResponse getDataPointGroupAggregations(GroupedAddressRequest readSpecs,
AggregationFunction aggregation, boolean allowCache) {
NavigableMap<String, GroupedAddressRequest> protocols = readSpecs.getProtocols();
if(protocols.size() == 1) {
return new AggregationResponse(ProviderManager.getDataPointsProvider(protocols.firstKey())
return new AggregateResponse(ProviderManager.getDataPointsProvider(protocols.firstKey())
.getDataPointGroupAggregations(protocols.get(protocols.firstKey()).getGroups(), aggregation, allowCache));
}
Map<String, Snapshot> data = getDataPointGroupedValuesMap(readSpecs, allowCache);
return new AggregationResponse(computeGroupAggregations(data, aggregation));
return new AggregateResponse(computeGroupAggregations(data, aggregation));
}
private Map<String, AggregatedValue> computeGroupAggregations(Map<String, Snapshot> data, AggregationFunction aggregation) {
......
package cz.muni.fi.lasaris.sbms.data.entities;
public enum AggregationWindow {
NONE,
DAY,
HOUR,
WEEK,
MONTH,
YEAR;
public static AggregationWindow fromString(String s) {
s = s.toUpperCase().trim();
if(s.equals("DAY")) {
return AggregationWindow.DAY;
}
if(s.equals("HOUR")) {
return AggregationWindow.HOUR;
}
if(s.equals("WEEK")) {
return AggregationWindow.WEEK;
}
if(s.equals("MONTH")) {
return AggregationWindow.MONTH;
}
if(s.equals("YEAR")) {
return AggregationWindow.YEAR;
}
if(s.equals("NONE")) {
return AggregationWindow.NONE;
}
throw new IllegalArgumentException("Unknown window type.");
}
}
Supports Markdown
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