ATEST-285

This commit is contained in:
Fabian Märki
2015-11-27 07:58:27 +01:00
parent 5e1f5ad42d
commit aebb77a61e

View File

@ -15,6 +15,7 @@ import javax.validation.Valid;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.validation.Validator; import org.springframework.validation.Validator;
import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.WebDataBinder;
@ -28,10 +29,12 @@ import org.springframework.web.bind.annotation.RestController;
import ch.psi.daq.cassandra.request.validate.RequestProviderValidator; import ch.psi.daq.cassandra.request.validate.RequestProviderValidator;
import ch.psi.daq.cassandra.util.test.CassandraDataGen; import ch.psi.daq.cassandra.util.test.CassandraDataGen;
import ch.psi.daq.common.concurrent.singleton.Deferred;
import ch.psi.daq.common.json.deserialize.AttributeBasedDeserializer; import ch.psi.daq.common.json.deserialize.AttributeBasedDeserializer;
import ch.psi.daq.common.ordering.Ordering; import ch.psi.daq.common.ordering.Ordering;
import ch.psi.daq.domain.DataEvent; import ch.psi.daq.domain.DataEvent;
import ch.psi.daq.query.analyzer.QueryAnalyzer; import ch.psi.daq.query.analyzer.QueryAnalyzer;
import ch.psi.daq.query.config.QueryConfig;
import ch.psi.daq.query.model.Aggregation; import ch.psi.daq.query.model.Aggregation;
import ch.psi.daq.query.model.AggregationType; import ch.psi.daq.query.model.AggregationType;
import ch.psi.daq.query.model.DBMode; import ch.psi.daq.query.model.DBMode;
@ -61,10 +64,14 @@ public class QueryRestController {
private ResponseStreamWriter responseStreamWriter; private ResponseStreamWriter responseStreamWriter;
@Resource @Resource
private QueryProcessor cassandraQueryProcessor; private ApplicationContext appContext;
@Resource // using Deferred ensures that data-api startup succeeds even if DataBuffer/ArchiverAppliance is
private QueryProcessor archiverApplianceQueryProcessor; // not reachable at startup
private Deferred<QueryProcessor> cassandraQueryProcessor = new Deferred<QueryProcessor>(
() -> appContext.getBean(QueryConfig.BEAN_NAME_CASSANDRA_QUERY_PROCESSOR, QueryProcessor.class));
private Deferred<QueryProcessor> archiverApplianceQueryProcessor = new Deferred<QueryProcessor>(
() -> appContext.getBean(QueryConfig.BEAN_NAME_ARCHIVER_APPLIANCE_QUERY_PROCESSOR, QueryProcessor.class));
@Resource @Resource
private Function<Query, QueryAnalyzer> queryAnalizerFactory; private Function<Query, QueryAnalyzer> queryAnalizerFactory;
@ -87,7 +94,8 @@ public class QueryRestController {
} }
} }
@RequestMapping(value = CHANNELS, method = {RequestMethod.GET, RequestMethod.POST}, produces = {MediaType.APPLICATION_JSON_VALUE}) @RequestMapping(value = CHANNELS, method = {RequestMethod.GET, RequestMethod.POST},
produces = {MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody Collection<String> getChannels(@RequestBody(required = false) ChannelsRequest request) public @ResponseBody Collection<String> getChannels(@RequestBody(required = false) ChannelsRequest request)
throws Throwable { throws Throwable {
// in case not specified use default (e.g. GET) // in case not specified use default (e.g. GET)
@ -112,7 +120,8 @@ public class QueryRestController {
* @return Collection of channel names matching the specified input channel name * @return Collection of channel names matching the specified input channel name
* @throws Throwable in case something goes wrong * @throws Throwable in case something goes wrong
*/ */
@RequestMapping(value = CHANNELS + "/{channelName}", method = {RequestMethod.GET}, produces = {MediaType.APPLICATION_JSON_VALUE}) @RequestMapping(value = CHANNELS + "/{channelName}", method = {RequestMethod.GET},
produces = {MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody Collection<String> getChannels(@PathVariable(value = "channelName") String channelName) public @ResponseBody Collection<String> getChannels(@PathVariable(value = "channelName") String channelName)
throws Throwable { throws Throwable {
return getChannels(new ChannelsRequest(channelName)); return getChannels(new ChannelsRequest(channelName));
@ -166,7 +175,8 @@ public class QueryRestController {
* *
* @return list of {@link AggregationType}s as String array * @return list of {@link AggregationType}s as String array
*/ */
@RequestMapping(value = "aggregationtypes", method = {RequestMethod.GET}, produces = {MediaType.APPLICATION_JSON_VALUE}) @RequestMapping(value = "aggregationtypes", method = {RequestMethod.GET},
produces = {MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody List<String> getAggregationTypesValues() { public @ResponseBody List<String> getAggregationTypesValues() {
List<AggregationType> orderings = Lists.newArrayList(AggregationType.values()); List<AggregationType> orderings = Lists.newArrayList(AggregationType.values());
return orderings.stream() return orderings.stream()
@ -193,9 +203,9 @@ public class QueryRestController {
/** /**
* Catch-all query method for getting data from the backend. * Catch-all query method for getting data from the backend.
* <p> * <p>
* The {@link DAQQuery} object will be a concrete subclass based on the combination of * The {@link DAQQuery} object will be a concrete subclass based on the combination of fields
* fields defined in the user's query. The {@link AttributeBasedDeserializer} decides which class * defined in the user's query. The {@link AttributeBasedDeserializer} decides which class to
* to deserialize the information into and has been configured (see * deserialize the information into and has been configured (see
* QueryRestConfig#afterPropertiesSet) accordingly. * QueryRestConfig#afterPropertiesSet) accordingly.
* *
* @param query concrete implementation of {@link DAQQuery} * @param query concrete implementation of {@link DAQQuery}
@ -230,9 +240,9 @@ public class QueryRestController {
private QueryProcessor getQueryProcessor(DBMode dbMode) { private QueryProcessor getQueryProcessor(DBMode dbMode) {
if (DBMode.databuffer.equals(dbMode)) { if (DBMode.databuffer.equals(dbMode)) {
return cassandraQueryProcessor; return cassandraQueryProcessor.get();
} else if (DBMode.archiverappliance.equals(dbMode)) { } else if (DBMode.archiverappliance.equals(dbMode)) {
return archiverApplianceQueryProcessor; return archiverApplianceQueryProcessor.get();
} else { } else {
LOGGER.error("Unknown DBMode '{}'!", dbMode); LOGGER.error("Unknown DBMode '{}'!", dbMode);
throw new IllegalArgumentException(String.format("Unknown DBMode '%s'", dbMode)); throw new IllegalArgumentException(String.format("Unknown DBMode '%s'", dbMode));