ATEST-81:

- fixing serialisation
- adding tests
This commit is contained in:
Zellweger Christof Ralf
2015-06-23 11:49:08 +02:00
parent c51db19001
commit 1276e20399
12 changed files with 413 additions and 144 deletions

View File

@ -53,7 +53,7 @@ public class DaqRestController {
* @throws IOException * @throws IOException
*/ */
@RequestMapping(value = "/timerange") @RequestMapping(value = "/timerange")
public void pulseRange(@RequestBody TimeRangeQuery query, HttpServletResponse res) throws IOException { public void timeRange(@RequestBody TimeRangeQuery query, HttpServletResponse res) throws IOException {
logger.debug("TimeRangeQuery received: {}", query); logger.debug("TimeRangeQuery received: {}", query);

View File

@ -5,13 +5,17 @@ import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder; import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import ch.psi.daq.cassandra.reader.Ordering; import ch.psi.daq.cassandra.reader.Ordering;
import ch.psi.daq.hazelcast.query.Aggregation; import ch.psi.daq.hazelcast.query.Aggregation;
import ch.psi.daq.hazelcast.query.AggregationType; import ch.psi.daq.hazelcast.query.AggregationType;
import ch.psi.daq.hazelcast.query.Query; import ch.psi.daq.hazelcast.query.Query;
import ch.psi.daq.hazelcast.query.bin.BinIntervalCalculator; import ch.psi.daq.hazelcast.query.bin.BinningStrategy;
import ch.psi.daq.hazelcast.query.bin.BinningStrategyFactory;
import ch.psi.daq.hazelcast.query.range.QueryRange;
import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
@ -35,6 +39,8 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo;
}) })
public abstract class AbstractQuery implements Query { public abstract class AbstractQuery implements Query {
private static Logger logger = LoggerFactory.getLogger(AbstractQuery.class);
private List<String> channels; private List<String> channels;
private LinkedHashSet<String> fields; private LinkedHashSet<String> fields;
@ -47,37 +53,65 @@ public abstract class AbstractQuery implements Query {
private Ordering ordering; private Ordering ordering;
private BinningStrategy binningStrategy;
private BinningStrategyEnum binningStrategyEnum;
private QueryRange queryRange;
/** /**
* *
* @param isOrderDescending whether to add a 'orderBy' clause into the database query * @param ordering whether to add a 'orderBy' clause into the database query
* @param channelIds all the channelIds (channel names) we want to query * @param channels all the channelIds (channel names) we want to query
* @param fields the fields (who map to fields in the DB) we are interested in returning to the * @param fields the fields (who map to fields in the DB) we are interested in returning to the
* client, needs to be in insertion order (hence the {@link LinkedHashSet} type) * client, needs to be in insertion order (hence the {@link LinkedHashSet} type)
* @param queryRange TODO
*/ */
@JsonCreator @JsonCreator
public AbstractQuery( public AbstractQuery(
// note that those annotations are needed for the polymorphic // note that those annotations are needed for the polymorphic
// mapping to work correctly // mapping to work correctly
@JsonProperty(value = "ordering") Ordering ordering, @JsonProperty(value = "ordering") Ordering ordering,
@JsonProperty(value = "channels") List<String> channelIds, @JsonProperty(value = "channels") List<String> channels,
@JsonProperty(value = "fields") LinkedHashSet<String> fields, @JsonProperty(value = "fields") LinkedHashSet<String> fields,
@JsonProperty(value = "binningStrategy") BinningStrategyEnum binningStrategyEnum,
@JsonProperty(value = "binDuration") long binDurationOrBinCount,
@JsonProperty(value = "aggregateChannels") boolean aggregateChannels, @JsonProperty(value = "aggregateChannels") boolean aggregateChannels,
@JsonProperty(value = "aggregationType") AggregationType aggregationType, @JsonProperty(value = "aggregationType") AggregationType aggregationType,
@JsonProperty(value = "aggregations") List<Aggregation> aggregations) { @JsonProperty(value = "aggregations") List<Aggregation> aggregations,
@JsonProperty(value = "queryRange") QueryRange queryRange) {
this.ordering = ordering; this.ordering = ordering;
this.aggregateChannels = aggregateChannels; this.aggregateChannels = aggregateChannels;
this.aggregationType = aggregationType; this.aggregationType = aggregationType;
this.aggregations = aggregations;
this.queryRange = queryRange;
if (channelIds == null || fields == null) { Assert.notNull(channels, "channel name must not be null.");
throw new IllegalArgumentException("sourceIds and/or fields cannot be null.");
}
Assert.notNull(channelIds, "channel name must not be null.");
Assert.notNull(fields, "field, i.e. property, names must not be null."); Assert.notNull(fields, "field, i.e. property, names must not be null.");
this.channels = channelIds; this.channels = channels;
this.fields = fields; this.fields = fields;
this.aggregations = aggregations;
this.binningStrategyEnum = binningStrategyEnum; // can be null: default then will be BinCountBinningStrategy
if (binningStrategyEnum != null) {
switch (binningStrategyEnum) {
case bincount:
this.binningStrategy = BinningStrategyFactory.getBinningStrategy(getQueryRange(), (int) binDurationOrBinCount);
break;
case lengthpulse:
case lengthtime:
this.binningStrategy = BinningStrategyFactory.getBinningStrategy(getQueryRange(), binDurationOrBinCount);
break;
default:
logger.warn("No binning strategy has been set. Selecting BinningStrategyBinCount.");
this.binningStrategy = BinningStrategyFactory.getBinningStrategy(getQueryRange(), (int) binDurationOrBinCount);
}
} else {
this.binningStrategy = BinningStrategyFactory.getBinningStrategy(getQueryRange(), (int) binDurationOrBinCount);
}
} }
/** /**
@ -104,16 +138,19 @@ public abstract class AbstractQuery implements Query {
return aggregationType; return aggregationType;
} }
@Override
public BinIntervalCalculator getBinIntervalCalculator() {
// TODO
return null;
}
public boolean isAggregateChannels() { public boolean isAggregateChannels() {
return aggregateChannels; return aggregateChannels;
} }
public BinningStrategyEnum getBinningStrategyEnum() {
return binningStrategyEnum;
}
@Override
public BinningStrategy getBinningStrategy() {
return binningStrategy;
}
public LinkedHashSet<String> getFields() { public LinkedHashSet<String> getFields() {
return fields; return fields;
@ -123,6 +160,18 @@ public abstract class AbstractQuery implements Query {
return aggregations; return aggregations;
} }
/**
* {@inheritDoc}
*/
@Override
public QueryRange getQueryRange() {
return queryRange;
}
public void setQueryRange(QueryRange queryRange) {
this.queryRange = queryRange;
}
@Override @Override
public String toString() { public String toString() {
return ReflectionToStringBuilder.reflectionToString(this); return ReflectionToStringBuilder.reflectionToString(this);

View File

@ -0,0 +1,13 @@
package ch.psi.daq.rest.queries;
import ch.psi.daq.hazelcast.query.bin.BinningStrategy;
/**
* Defines the strategies that map to the similarly named {@link BinningStrategy} implementations.
* <p>
* This enum is used for type-safety and simplicity for the rest interface.
*
*/
public enum BinningStrategyEnum {
lengthpulse, lengthtime, bincount
}

View File

@ -18,9 +18,6 @@ import com.fasterxml.jackson.annotation.JsonProperty;
*/ */
public class PulseRangeQuery extends AbstractQuery { public class PulseRangeQuery extends AbstractQuery {
private long startPulseId;
private long endPulseId;
@JsonCreator @JsonCreator
public PulseRangeQuery( public PulseRangeQuery(
// note that those annotations are needed for the polymorphic // note that those annotations are needed for the polymorphic
@ -28,33 +25,16 @@ public class PulseRangeQuery extends AbstractQuery {
@JsonProperty(value = "ordering") Ordering ordering, @JsonProperty(value = "ordering") Ordering ordering,
@JsonProperty(value = "channels") List<String> channels, @JsonProperty(value = "channels") List<String> channels,
@JsonProperty(value = "fields") LinkedHashSet<String> fields, @JsonProperty(value = "fields") LinkedHashSet<String> fields,
@JsonProperty(value = "binningStrategy") BinningStrategyEnum binningStrategyEnum,
@JsonProperty(value = "binDuration") long binDurationOrBinCount,
@JsonProperty(value = "aggregateChannels") boolean aggregateChannels, @JsonProperty(value = "aggregateChannels") boolean aggregateChannels,
@JsonProperty(value = "aggregationType") AggregationType aggregationType, @JsonProperty(value = "aggregationType") AggregationType aggregationType,
@JsonProperty(value = "aggregations") List<Aggregation> aggregations, @JsonProperty(value = "aggregations") List<Aggregation> aggregations,
@JsonProperty(value = "startPulseId") long startPulseId, @JsonProperty(value = "queryRange") QueryRange queryRange) {
@JsonProperty(value = "endPulseId") long endPulseId)
{
super(ordering, channels, fields, aggregateChannels, aggregationType, aggregations); super(ordering, channels, fields, binningStrategyEnum, binDurationOrBinCount, aggregateChannels, aggregationType,
aggregations, queryRange);
this.startPulseId = startPulseId;
this.endPulseId = endPulseId;
}
public long getStartPulseId() {
return startPulseId;
}
public void setStartPulseId(long startPulseId) {
this.startPulseId = startPulseId;
}
public long getEndPulseId() {
return endPulseId;
}
public void setEndPulseId(long endPulseId) {
this.endPulseId = endPulseId;
} }
/** /**
@ -65,10 +45,4 @@ public class PulseRangeQuery extends AbstractQuery {
return ToStringBuilder.reflectionToString(this); return ToStringBuilder.reflectionToString(this);
} }
@Override
public QueryRange getQueryRange() {
// TODO Auto-generated method stub
return null;
}
} }

View File

@ -6,8 +6,6 @@ import java.util.Date;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import javax.validation.constraints.NotNull;
import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -23,31 +21,24 @@ import com.fasterxml.jackson.annotation.JsonProperty;
public class TimeRangeQuery extends AbstractQuery { public class TimeRangeQuery extends AbstractQuery {
private static final String DATE_FORMAT_STRING = "yyyy/MM/dd hh:mm:ss.SSS"; public static final String DATE_FORMAT_STRING = "yyyy/MM/dd hh:mm:ss.SSS";
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(DATE_FORMAT_STRING); private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(DATE_FORMAT_STRING);
private static Logger logger = LoggerFactory.getLogger(TimeRangeQuery.class); private static Logger logger = LoggerFactory.getLogger(TimeRangeQuery.class);
@NotNull
private long start;
@NotNull
private long startNanoOffset;
@NotNull
private long end;
@NotNull
private long endNanoOffset;
/** /**
* *
* @param ordering * @param ordering whether to add a 'orderBy' clause into the database query
* @param channelIds all the sourceIds (channel names) we want to query * @param channelIds all the sourceIds (channel names) we want to query
* @param fields the fields (who map to fields in the DB) we are interested in returning to the * @param fields the fields (who map to fields in the DB) we are interested in returning to the
* client, needs to be in insertion order (hence the {@link LinkedHashSet} type) * client, needs to be in insertion order (hence the {@link LinkedHashSet} type)
* @param startMillis * @param binningStrategyEnum
* @param startNanoOffset * @param binDurationOrBinCount
* @param endMillis * @param aggregateChannels
* @param endNanoOffset * @param aggregationType
* @param aggregations
* @param queryRange
* @param startDateTime
* @param endDateTime
*/ */
@JsonCreator @JsonCreator
public TimeRangeQuery( public TimeRangeQuery(
@ -56,23 +47,16 @@ public class TimeRangeQuery extends AbstractQuery {
@JsonProperty(value = "ordering") Ordering ordering, @JsonProperty(value = "ordering") Ordering ordering,
@JsonProperty(value = "channels") List<String> channels, @JsonProperty(value = "channels") List<String> channels,
@JsonProperty(value = "fields") LinkedHashSet<String> fields, @JsonProperty(value = "fields") LinkedHashSet<String> fields,
@JsonProperty(value = "binningStrategy") BinningStrategyEnum binningStrategyEnum,
@JsonProperty(value = "binDuration") long binDurationOrBinCount,
@JsonProperty(value = "aggregateChannels") boolean aggregateChannels, @JsonProperty(value = "aggregateChannels") boolean aggregateChannels,
@JsonProperty(value = "aggregationType") AggregationType aggregationType, @JsonProperty(value = "aggregationType") AggregationType aggregationType,
@JsonProperty(value = "aggregations") List<Aggregation> aggregations, @JsonProperty(value = "aggregations") List<Aggregation> aggregations,
@JsonProperty(value = "start") long startMillis, @JsonProperty(value = "queryRange") QueryRange queryRange,
@JsonProperty(value = "startNanoOffset") long startNanoOffset,
@JsonProperty(value = "startDateTime") String startDateTime, @JsonProperty(value = "startDateTime") String startDateTime,
@JsonProperty(value = "end") long endMillis,
@JsonProperty(value = "endNanoOffset") long endNanoOffset,
@JsonProperty(value = "endDateTime") String endDateTime) { @JsonProperty(value = "endDateTime") String endDateTime) {
super(ordering, channels, fields, aggregateChannels, aggregationType, aggregations); super(ordering, channels, fields, binningStrategyEnum, binDurationOrBinCount, aggregateChannels, aggregationType, aggregations, queryRange);
this.start = startMillis;
this.startNanoOffset = startNanoOffset;
this.end = endMillis;
this.endNanoOffset = endNanoOffset;
if (startDateTime != null && endDateTime != null) { if (startDateTime != null && endDateTime != null) {
logger.info("startDateTime and endDateTime specified. This takes precedence over the start / end fields."); logger.info("startDateTime and endDateTime specified. This takes precedence over the start / end fields.");
@ -80,8 +64,7 @@ public class TimeRangeQuery extends AbstractQuery {
Date startDate = DATE_FORMAT.parse(startDateTime); Date startDate = DATE_FORMAT.parse(startDateTime);
Date endDate = DATE_FORMAT.parse(endDateTime); Date endDate = DATE_FORMAT.parse(endDateTime);
this.start = startDate.getTime(); getQueryRange().setTimeRange(startDate.getTime(), queryRange.getStartNanos(), endDate.getTime(), queryRange.getEndNanos());
this.end = endDate.getTime();
} catch (ParseException e) { } catch (ParseException e) {
logger.error("Parsing the start- and/or endDate was unsuccessful. " logger.error("Parsing the start- and/or endDate was unsuccessful. "
+ "The format must be '" + DATE_FORMAT_STRING + "'", e); + "The format must be '" + DATE_FORMAT_STRING + "'", e);
@ -90,38 +73,6 @@ public class TimeRangeQuery extends AbstractQuery {
} }
public long getStart() {
return start;
}
public void setStart(long start) {
this.start = start;
}
public long getStartNanoOffset() {
return startNanoOffset;
}
public void setStartNanoOffset(long startNanoOffset) {
this.startNanoOffset = startNanoOffset;
}
public long getEnd() {
return end;
}
public void setEnd(long end) {
this.end = end;
}
public long getEndNanoOffset() {
return endNanoOffset;
}
public void setEndNanoOffset(long endNanoOffset) {
this.endNanoOffset = endNanoOffset;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -130,11 +81,4 @@ public class TimeRangeQuery extends AbstractQuery {
return ToStringBuilder.reflectionToString(this); return ToStringBuilder.reflectionToString(this);
} }
@Override
public QueryRange getQueryRange() {
// TODO Auto-generated method stub
return null;
}
} }

View File

@ -37,7 +37,8 @@ public abstract class AbstractDaqRestTest {
protected MockMvc mockMvc; protected MockMvc mockMvc;
protected ObjectMapper mapper = new ObjectMapper(); @Autowired
protected ObjectMapper mapper;
@Before @Before
public void setup() { public void setup() {

View File

@ -5,6 +5,7 @@ import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import ch.psi.daq.rest.DaqRestConfiguration;
import ch.psi.daq.test.cassandra.LocalCassandraTestConfig; import ch.psi.daq.test.cassandra.LocalCassandraTestConfig;
@Configuration @Configuration
@ -15,7 +16,7 @@ import ch.psi.daq.test.cassandra.LocalCassandraTestConfig;
// "ch.psi.daq.cassandra.reader", // "ch.psi.daq.cassandra.reader",
// "ch.psi.daq.cassandra.writer" // "ch.psi.daq.cassandra.writer"
//}) //})
@Import(value = {LocalCassandraTestConfig.class}) @Import(value = {LocalCassandraTestConfig.class, DaqRestConfiguration.class})
@EnableWebMvc @EnableWebMvc
public class DaqRestApplicationConfiguration extends WebMvcConfigurationSupport { public class DaqRestApplicationConfiguration extends WebMvcConfigurationSupport {

View File

@ -12,10 +12,14 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import ch.psi.daq.cassandra.reader.Ordering; import ch.psi.daq.cassandra.reader.Ordering;
import ch.psi.daq.hazelcast.query.AggregationType; import ch.psi.daq.hazelcast.query.AggregationType;
import ch.psi.daq.hazelcast.query.range.QueryRange;
import ch.psi.daq.hazelcast.query.range.QueryRangeImpl;
import ch.psi.daq.rest.queries.BinningStrategyEnum;
import ch.psi.daq.rest.queries.PulseRangeQuery; import ch.psi.daq.rest.queries.PulseRangeQuery;
import ch.psi.daq.rest.queries.TimeRangeQuery; import ch.psi.daq.rest.queries.TimeRangeQuery;
import ch.psi.daq.test.rest.AbstractDaqRestTest; import ch.psi.daq.test.rest.AbstractDaqRestTest;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
@ -28,16 +32,17 @@ public class DaqControllerTest extends AbstractDaqRestTest {
@Test @Test
public void testPulseRangeQuery() throws Exception { public void testPulseRangeQuery() throws Exception {
QueryRange range = new QueryRangeImpl(100l, 101l);
PulseRangeQuery request = new PulseRangeQuery( PulseRangeQuery request = new PulseRangeQuery(
Ordering.DESC, Ordering.DESC, //ordering
Lists.newArrayList(), // DummyQueryProcessor simply returns a fixed list Lists.newArrayList(), // channels, DummyQueryProcessor simply returns a fixed list
Sets.newLinkedHashSet(DEFAULT_PROPERTIES), Sets.newLinkedHashSet(DEFAULT_PROPERTIES), // fields
BinningStrategyEnum.bincount,
100,
false, false,
AggregationType.index, AggregationType.index,
null, null,
100l, range);
101l
);
String content = mapper.writeValueAsString(request); String content = mapper.writeValueAsString(request);
@ -59,18 +64,18 @@ public class DaqControllerTest extends AbstractDaqRestTest {
long startTime = new Date().getTime(); long startTime = new Date().getTime();
long endTime = startTime + TimeUnit.SECONDS.toMillis(1); long endTime = startTime + TimeUnit.SECONDS.toMillis(1);
QueryRange range = new QueryRangeImpl(startTime, 0, endTime, 0);
TimeRangeQuery request = new TimeRangeQuery( TimeRangeQuery request = new TimeRangeQuery(
Ordering.ASC, Ordering.ASC,
Lists.newArrayList(), // DummyQueryProcessor simply returns a fixed list Lists.newArrayList("test"),
Sets.newLinkedHashSet(DEFAULT_PROPERTIES), Sets.newLinkedHashSet(DEFAULT_PROPERTIES),
BinningStrategyEnum.bincount,
100,
false, false,
AggregationType.index, AggregationType.index,
null, // aggregations null, // aggregations
startTime, // startMillis range,
0, null, // startMillis
null,
endTime,
0,
null); null);
String content = mapper.writeValueAsString(request); String content = mapper.writeValueAsString(request);
@ -87,4 +92,26 @@ public class DaqControllerTest extends AbstractDaqRestTest {
.andExpect(MockMvcResultMatchers.jsonPath("$[0]").exists()) .andExpect(MockMvcResultMatchers.jsonPath("$[0]").exists())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channel").value("testChannel1")); .andExpect(MockMvcResultMatchers.jsonPath("$[0].channel").value("testChannel1"));
} }
@Test
public void testMapper() throws JsonProcessingException {
long startTime = new Date().getTime();
long endTime = startTime + TimeUnit.SECONDS.toMillis(1);
QueryRange range = new QueryRangeImpl(startTime, 0, endTime, 0);
TimeRangeQuery request = new TimeRangeQuery(
Ordering.ASC,
Lists.newArrayList("test"),
Sets.newLinkedHashSet(DEFAULT_PROPERTIES),
BinningStrategyEnum.bincount,
100,
false,
AggregationType.index,
null, // aggregations
range,
null, // startMillis
null);
String content = mapper.writeValueAsString(request);
System.out.println(content);
}
} }

View File

@ -0,0 +1,15 @@
package ch.psi.daq.test.rest.queries;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import com.google.common.collect.Lists;
public class AbstractQueryTest {
protected static final List<String> DEFAULT_PROPERTIES = Lists.newArrayList("channel", "pulseId");
protected long startMillis = new Date().getTime();
protected long endMillis = new Date().getTime() + TimeUnit.SECONDS.toMillis(10);
}

View File

@ -0,0 +1,164 @@
package ch.psi.daq.test.rest.queries;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import ch.psi.daq.cassandra.reader.Ordering;
import ch.psi.daq.hazelcast.query.AggregationType;
import ch.psi.daq.hazelcast.query.bin.BinningStrategyBinCount;
import ch.psi.daq.hazelcast.query.bin.BinningStrategyLengthPulse;
import ch.psi.daq.hazelcast.query.bin.BinningStrategyLengthTime;
import ch.psi.daq.hazelcast.query.range.QueryRange;
import ch.psi.daq.hazelcast.query.range.QueryRangeImpl;
import ch.psi.daq.rest.queries.BinningStrategyEnum;
import ch.psi.daq.rest.queries.TimeRangeQuery;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
@RunWith(BlockJUnit4ClassRunner.class)
public class AbstractQueryTestTest extends AbstractQueryTest {
@Test(expected=IllegalArgumentException.class)
public void test_NoChannels_exception() {
long startMillis = new Date().getTime();
long endMillis = new Date().getTime() + TimeUnit.SECONDS.toMillis(10);
QueryRange range = new QueryRangeImpl(startMillis, 0, endMillis, 0);
new TimeRangeQuery(
Ordering.ASC,
null, // should throw exception
Sets.newLinkedHashSet(DEFAULT_PROPERTIES),
BinningStrategyEnum.bincount,
100, // binDurationOrBincount : long
false, // isAggregateChannels
AggregationType.index,
null, // list of aggregations
range,
null, // startDateTime : String
null); // endDateTime : String
}
@Test(expected=IllegalArgumentException.class)
public void test_NoFields_exception() {
long startMillis = new Date().getTime();
long endMillis = new Date().getTime() + TimeUnit.SECONDS.toMillis(10);
QueryRange range = new QueryRangeImpl(startMillis, 0, endMillis, 0);
new TimeRangeQuery(
Ordering.ASC,
Lists.newArrayList(),
null,
BinningStrategyEnum.bincount,
100, // binDurationOrBincount : long
false, // isAggregateChannels
AggregationType.index,
null, // list of aggregations
range,
null, // startDateTime : String
null); // endDateTime : String
}
@Test
public void test_nullBinningStrategyEnum_exception() {
long startMillis = new Date().getTime();
long endMillis = new Date().getTime() + TimeUnit.SECONDS.toMillis(10);
QueryRange range = new QueryRangeImpl(startMillis, 0, endMillis, 0);
TimeRangeQuery query = new TimeRangeQuery(
Ordering.ASC,
Lists.newArrayList(),
Sets.newLinkedHashSet(DEFAULT_PROPERTIES),
null,
100, // binDurationOrBincount : long
false, // isAggregateChannels
AggregationType.index,
null, // list of aggregations
range,
null, // startDateTime : String
null); // endDateTime : String
Assert.assertTrue(query.getBinningStrategy() != null);
Assert.assertEquals(BinningStrategyBinCount.class, query.getBinningStrategy().getClass());
}
@Test
public void test_LengthBinningTimeStrategy() {
long startMillis = new Date().getTime();
long endMillis = new Date().getTime() + TimeUnit.SECONDS.toMillis(10);
QueryRange range = new QueryRangeImpl(startMillis, 0, endMillis, 0);
TimeRangeQuery query = new TimeRangeQuery(
Ordering.ASC,
Lists.newArrayList(),
Sets.newLinkedHashSet(DEFAULT_PROPERTIES),
BinningStrategyEnum.lengthpulse,
100, // binDurationOrBincount : long
false, // isAggregateChannels
AggregationType.index,
null, // list of aggregations
range,
null, // startDateTime : String
null); // endDateTime : String
Assert.assertTrue(query.getBinningStrategy() != null);
Assert.assertEquals(BinningStrategyLengthTime.class, query.getBinningStrategy().getClass());
}
@Test
public void test_LengthBinningPulseStrategy() {
long startPulse = 100l;
long endPulse = 105l;
QueryRange range = new QueryRangeImpl(startPulse, endPulse);
TimeRangeQuery query = new TimeRangeQuery(
Ordering.ASC,
Lists.newArrayList(),
Sets.newLinkedHashSet(DEFAULT_PROPERTIES),
BinningStrategyEnum.lengthpulse,
100, // binDurationOrBincount : long
false, // isAggregateChannels
AggregationType.index,
null, // list of aggregations
range,
null, // startDateTime : String
null); // endDateTime : String
Assert.assertTrue(query.getBinningStrategy() != null);
Assert.assertEquals(BinningStrategyLengthPulse.class, query.getBinningStrategy().getClass());
}
@Test
public void test_BinCountBinningStrategy() {
long startMillis = new Date().getTime();
long endMillis = new Date().getTime() + TimeUnit.SECONDS.toMillis(10);
QueryRange range = new QueryRangeImpl(startMillis, 0, endMillis, 0);
TimeRangeQuery query = new TimeRangeQuery(
Ordering.ASC,
Lists.newArrayList(),
Sets.newLinkedHashSet(DEFAULT_PROPERTIES),
BinningStrategyEnum.bincount,
100, // binDurationOrBincount : long
false, // isAggregateChannels
AggregationType.index,
null, // list of aggregations
range,
null, // startDateTime : String
null); // endDateTime : String
Assert.assertTrue(query.getBinningStrategy() != null);
Assert.assertEquals(BinningStrategyBinCount.class, query.getBinningStrategy().getClass());
}
}

View File

@ -0,0 +1,73 @@
package ch.psi.daq.test.rest.queries;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import ch.psi.daq.cassandra.reader.Ordering;
import ch.psi.daq.hazelcast.query.AggregationType;
import ch.psi.daq.hazelcast.query.range.QueryRange;
import ch.psi.daq.hazelcast.query.range.QueryRangeImpl;
import ch.psi.daq.rest.queries.BinningStrategyEnum;
import ch.psi.daq.rest.queries.TimeRangeQuery;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
@RunWith(BlockJUnit4ClassRunner.class)
public class TimeRangeQueryTest extends AbstractQueryTest {
@Test
public void test_startAndEndDateTime_asString() throws ParseException {
// allowed format: yyyy/MM/dd hh:mm:ss.SSS
String startDateTime = "2014/01/01 12:00:00.000";
String endDateTime = "2014/01/01 18:00:00.000";
QueryRange range = new QueryRangeImpl(-1, 0, -1, 0);
TimeRangeQuery query = new TimeRangeQuery(
Ordering.ASC,
Lists.newArrayList(), // DummyQueryProcessor simply returns a fixed list
Sets.newLinkedHashSet(DEFAULT_PROPERTIES),
BinningStrategyEnum.bincount,
100, // binDurationOrBincount : long
false, // isAggregateChannels
AggregationType.index,
null, // list of aggregations
range,
startDateTime, // startDateTime : String
endDateTime); // endDateTime : String
SimpleDateFormat format = new SimpleDateFormat(TimeRangeQuery.DATE_FORMAT_STRING);
Assert.assertEquals(format.parse(startDateTime).getTime(), query.getQueryRange().getStartMillis());
Assert.assertEquals(format.parse(endDateTime).getTime(), query.getQueryRange().getEndMillis());
}
@Test
public void test_wrongFormat_exception() {
String startDateTime = "2014-01-01 12:00:00.000";
String endDateTime = "2014/01/01 18:00:00.000";
QueryRange range = new QueryRangeImpl(-1, 0, -1, 0);
TimeRangeQuery query = new TimeRangeQuery(
Ordering.ASC,
Lists.newArrayList(), // DummyQueryProcessor simply returns a fixed list
Sets.newLinkedHashSet(DEFAULT_PROPERTIES),
BinningStrategyEnum.bincount,
100, // binDurationOrBincount : long
false, // isAggregateChannels
AggregationType.index,
null,
range,
startDateTime, // startDateTime : String
endDateTime); // endDateTime : String
Assert.assertEquals(-1, query.getQueryRange().getStartMillis());
Assert.assertEquals(-1, query.getQueryRange().getStartMillis());
}
}

View File

@ -6,8 +6,10 @@
"test2" "test2"
], ],
"fields": [ "fields": [
"channel", pulseId", "globalMillis", "globalNanos", "dbValueBytes" "channel", "pulseId", "globalMillis", "globalNanos", "dbValueBytes"
], ],
"binningStrategy" : "bincount",
"binDuration" : 100,
"aggregateChannels":"false", "aggregateChannels":"false",
"aggregationType": "index", "aggregationType": "index",
"aggregations": [ "aggregations": [
@ -25,7 +27,7 @@
"endPulseId" : 200 "endPulseId" : 200
} }
curl -v -X POST -H 'Content-Type: application/json' -d '{"queryType":"pulserange","ordering":"ASC","channels":["test1","test2"],"fields":["channel","pulseId"],"aggregateChannels":"false","aggregationType":"index","aggregations":[{"fieldRef":"e_val","type":"max","resultFieldName":"maximum"},{"fieldRef":"e_val","type":"min","resultFieldName":"minimum"}],"startPulseId":100,"endPulseId":200}' http://localhost:8080/pulserange curl -v -X POST -H 'Content-Type: application/json' -d '{"queryType":"pulserange","ordering":"ASC","channels":["test1","test2"],"fields":["channel","pulseId","globalMillis","globalNanos","dbValueBytes"],"binningStrategy":"bincount","binDuration":100,"aggregateChannels":"false","aggregationType":"index","aggregations":[{"fieldRef":"e_val","type":"max","resultFieldName":"maximum"},{"fieldRef":"e_val","type":"min","resultFieldName":"minimum"}],"startPulseId":100,"endPulseId":200}' http://localhost:8080/pulserange
=============================================================================================================================================== ===============================================================================================================================================
@ -40,15 +42,21 @@ curl -v -X POST -H 'Content-Type: application/json' -d '{"queryType":"pulserange
"channel", "channel",
"pulseId" "pulseId"
], ],
"binningStrategy" : "duration",
"binDuration" : 100,
"aggregateChannels":false, "aggregateChannels":false,
"aggregationType": "index",
"aggregations":null, "aggregations":null,
"start":1434717654177, "start":1434717654177,
"startNanoOffset":0, "startNanoOffset":0,
"end":1434717655177, "end":1434717655177,
"endNanoOffset":0, "endNanoOffset":0,
"valueAggregation":"index",
"binIntervalCalculator":null,
"ordering":"DESC" "ordering":"DESC"
} }
curl -v -X POST -H 'Content-Type: application/json' -d '{"queryType":"timerange","channels":["test1","test2"],"fields":["channel","pulseId"],"aggregateChannels":false,"aggregations":null,"start":1434717654177,"startNanoOffset":0,"end":1434717655177,"endNanoOffset":0,"valueAggregation":"index","ordering":"ASC"}' http://localhost:8080/timerange curl -v -X POST -H 'Content-Type: application/json' -d '{"queryType":"timerange","channels":["test1","test2"],"fields":["channel","pulseId"],"binningStrategy":"duration","binDuration":100,"aggregateChannels":false,"aggregations":null,"start":1434717654177,"startNanoOffset":0,"end":1434717655177,"endNanoOffset":0,"aggregationType":"index","ordering":"DESC"}' http://localhost:8080/timerange
curl -v -X POST -H 'Content-Type: application/json' -d '{"queryType":"timerange","ordering":"ASC","channels":["test"],"fields":["channel","pulseId"],"binningStrategy":{"combinedMarkerCalculator":{}},"aggregateChannels":false,"aggregationType":"index","queryRange":{"startMillis":1435048390294,"startNanos":0,"endMillis":1435048391294,"endNanos":0,"startPulseId":9223372036854775807,"endPulseId":9223372036854775807},"binningStrategyEnum":"bincount"}' http://localhost:8080/timerange
curl -v -X POST -H 'Content-Type: application/json' -d '{"queryType":"timerange","ordering":"ASC","channels":["test"],"fields":["channel","pulseId"],"aggregateChannels":false,"aggregationType":"index","queryRange":{"startMillis":1435049709091,"startNanos":0,"endMillis":1435049710091,"endNanos":0,"startPulseId":9223372036854775807,"endPulseId":9223372036854775807},"binningStrategyEnum":"bincount"}' http://localhost:8080/timerange