Configurable Backend.

This commit is contained in:
Fabian Märki
2017-09-21 15:19:37 +02:00
parent cfbb2705cf
commit 47c2e62fa2
35 changed files with 945 additions and 1158 deletions

View File

@ -13,16 +13,15 @@ import javax.annotation.Resource;
import org.msgpack.jackson.dataformat.MessagePackFactory; import org.msgpack.jackson.dataformat.MessagePackFactory;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.validation.Validator; import org.springframework.validation.Validator;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonFactory;
@ -31,6 +30,8 @@ import com.fasterxml.jackson.dataformat.smile.SmileFactory;
import ch.psi.daq.common.statistic.Statistics; import ch.psi.daq.common.statistic.Statistics;
import ch.psi.daq.domain.DataEvent; import ch.psi.daq.domain.DataEvent;
import ch.psi.daq.domain.backend.Backend;
import ch.psi.daq.domain.config.DomainConfig;
import ch.psi.daq.domain.config.DomainConfigCORS; import ch.psi.daq.domain.config.DomainConfigCORS;
import ch.psi.daq.domain.query.backend.BackendQuery; import ch.psi.daq.domain.query.backend.BackendQuery;
import ch.psi.daq.domain.query.backend.analyzer.BackendQueryAnalyzer; import ch.psi.daq.domain.query.backend.analyzer.BackendQueryAnalyzer;
@ -56,8 +57,10 @@ import ch.psi.daq.queryrest.response.smile.SmileTableResponseStreamWriter;
@Configuration @Configuration
@Import(value = DomainConfigCORS.class) @Import(value = DomainConfigCORS.class)
@PropertySource(value = {"classpath:queryrest.properties"}) @PropertySource(value = {"classpath:queryrest.properties"})
@PropertySource(value = {"file:${user.home}/.config/daq/queryrest.properties"}, ignoreResourceNotFound = true) @PropertySource(value = {
public class QueryRestConfig extends WebMvcConfigurerAdapter { "classpath:queryrest-${daq.config.environment}.properties",
"file:${user.home}/.config/daq/queryrest.properties"}, ignoreResourceNotFound = true)
public class QueryRestConfig { // extends WebMvcConfigurerAdapter {
private static final String QUERYREST_DEFAULT_RESPONSE_AGGREGATIONS = "queryrest.default.response.aggregations"; private static final String QUERYREST_DEFAULT_RESPONSE_AGGREGATIONS = "queryrest.default.response.aggregations";
@ -79,19 +82,38 @@ public class QueryRestConfig extends WebMvcConfigurerAdapter {
private static final Logger LOGGER = LoggerFactory.getLogger(QueryRestConfig.class); private static final Logger LOGGER = LoggerFactory.getLogger(QueryRestConfig.class);
public static final String BEAN_NAME_QUERY_MANAGER = "queryManager";
public static final String BEAN_NAME_QUERY_ANALIZER_FACTORY = "queryAnalizerFactory";
public static final String BEAN_NAME_QUERY_VALIDATOR = "queryValidator";
public static final String BEAN_NAME_JSON_FACTORY = "jsonFactory";
public static final String BEAN_NAME_MSG_PACK_FACTORY = "msgPackFactory";
public static final String BEAN_NAME_SMILE_FACTORY = "smileFactory";
public static final String BEAN_NAME_DEFAULT_RESPONSE_FIELDS = "defaultResponseFields"; public static final String BEAN_NAME_DEFAULT_RESPONSE_FIELDS = "defaultResponseFields";
public static final String BEAN_NAME_DEFAULT_RESPONSE_AGGREGATIONS = "defaultResponseAggregations"; public static final String BEAN_NAME_DEFAULT_RESPONSE_AGGREGATIONS = "defaultResponseAggregations";
// public static final String BEAN_NAME_CORS_ALLOWEDORIGINS = "corsAllowedorigins";
// public static final String BEAN_NAME_CORS_FORCEALLHEADERS = "corsForceallheaders";
@Resource @Resource
private ApplicationContext context;
private Environment env; private Environment env;
@Resource
private ObjectMapper objectMapper; private ObjectMapper objectMapper;
@SuppressWarnings("unchecked")
@PostConstruct @PostConstruct
public void afterPropertiesSet() { public void afterPropertiesSet() {
env = context.getEnvironment();
objectMapper = context.getBean(DomainConfig.BEAN_NAME_OBJECT_MAPPER, ObjectMapper.class);
initObjectMapper(objectMapper);
// init BackendAccesses
final List<Backend> backends = context.getBean(DomainConfig.BEAN_NAME_BACKENDS, List.class);
for (final Backend backend : backends) {
// backends instance is different to objectMapper above
initObjectMapper(
backend.getApplicationContext().getBean(DomainConfig.BEAN_NAME_OBJECT_MAPPER, ObjectMapper.class));
}
}
private void initObjectMapper(ObjectMapper objectMapper) {
// only include non-null values // only include non-null values
objectMapper.setSerializationInclusion(Include.NON_NULL); objectMapper.setSerializationInclusion(Include.NON_NULL);
// Mixin which is used dynamically to filter out which properties get serialised and which // Mixin which is used dynamically to filter out which properties get serialised and which
@ -104,83 +126,103 @@ public class QueryRestConfig extends WebMvcConfigurerAdapter {
objectMapper.addMixIn(Response.class, PolymorphicResponseMixIn.class); objectMapper.addMixIn(Response.class, PolymorphicResponseMixIn.class);
} }
// @Override
// public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
// /**
// * This is necessary so that the message conversion uses the configured object mapper.
// * Otherwise, a separate object mapper is instantiated for Springs message conversion.
// */
// converter.setObjectMapper(objectMapper);
// converters.add(converter);
// super.configureMessageConverters(converters);
// }
// does this work for json comming from web-requests
// -> use WebMvcConfigurationSupport?
// https://stackoverflow.com/questions/26639475/how-to-set-context-param-in-spring-boot
// @Bean
// @Lazy
// public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
// final MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
// jsonConverter.setObjectMapper(objectMapper);
// return jsonConverter;
// }
/** @Bean(name = BEAN_NAME_JSON_FACTORY)
* {@inheritDoc} @Lazy
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
/**
* This is necessary so that the message conversion uses the configured object mapper.
* Otherwise, a separate object mapper is instantiated for Springs message conversion.
*/
converter.setObjectMapper(objectMapper);
converters.add(converter);
super.configureMessageConverters(converters);
}
@Bean
public JsonFactory jsonFactory() { public JsonFactory jsonFactory() {
return new JsonFactory(); return new JsonFactory();
} }
@Bean @Bean(name = BEAN_NAME_MSG_PACK_FACTORY)
@Lazy
public MessagePackFactory messagePackFactory() { public MessagePackFactory messagePackFactory() {
return new MessagePackFactory(); return new MessagePackFactory();
} }
@Bean @Bean(name = BEAN_NAME_SMILE_FACTORY)
@Lazy
public SmileFactory smileFactory() { public SmileFactory smileFactory() {
return new SmileFactory(); return new SmileFactory();
} }
@Bean @Bean(name = BEAN_NAME_QUERY_ANALIZER_FACTORY)
@Lazy
public Function<BackendQuery, BackendQueryAnalyzer> queryAnalizerFactory() { public Function<BackendQuery, BackendQueryAnalyzer> queryAnalizerFactory() {
return (query) -> new BackendQueryAnalyzerImpl(query); return (query) -> new BackendQueryAnalyzerImpl(query);
} }
@Bean @Bean
@Lazy
public JSONResponseStreamWriter jsonResponseStreamWriter() { public JSONResponseStreamWriter jsonResponseStreamWriter() {
return new JSONResponseStreamWriter(); return new JSONResponseStreamWriter();
} }
@Bean @Bean
@Lazy
public JSONTableResponseStreamWriter jsonTableResponseStreamWriter() { public JSONTableResponseStreamWriter jsonTableResponseStreamWriter() {
return new JSONTableResponseStreamWriter(); return new JSONTableResponseStreamWriter();
} }
@Bean @Bean
@Lazy
public MsgPackResponseStreamWriter msgPackResponseStreamWriter() { public MsgPackResponseStreamWriter msgPackResponseStreamWriter() {
return new MsgPackResponseStreamWriter(); return new MsgPackResponseStreamWriter();
} }
@Bean @Bean
@Lazy
public MsgPackTableResponseStreamWriter msgPackTableResponseStreamWriter() { public MsgPackTableResponseStreamWriter msgPackTableResponseStreamWriter() {
return new MsgPackTableResponseStreamWriter(); return new MsgPackTableResponseStreamWriter();
} }
@Bean @Bean
@Lazy
public SmileResponseStreamWriter smileResponseStreamWriter() { public SmileResponseStreamWriter smileResponseStreamWriter() {
return new SmileResponseStreamWriter(); return new SmileResponseStreamWriter();
} }
@Bean @Bean
@Lazy
public SmileTableResponseStreamWriter smileTableResponseStreamWriter() { public SmileTableResponseStreamWriter smileTableResponseStreamWriter() {
return new SmileTableResponseStreamWriter(); return new SmileTableResponseStreamWriter();
} }
@Bean @Bean
@Lazy
public CSVResponseStreamWriter csvResponseStreamWriter() { public CSVResponseStreamWriter csvResponseStreamWriter() {
return new CSVResponseStreamWriter(); return new CSVResponseStreamWriter();
} }
@Bean @Bean(name = BEAN_NAME_QUERY_MANAGER)
@Lazy
public QueryManager queryManager() { public QueryManager queryManager() {
return new QueryManagerImpl(); return new QueryManagerImpl();
} }
@Bean(name = BEAN_NAME_DEFAULT_RESPONSE_FIELDS) @Bean(name = BEAN_NAME_DEFAULT_RESPONSE_FIELDS)
@Lazy
public Set<QueryField> defaultResponseFields() { public Set<QueryField> defaultResponseFields() {
String[] responseFields = String[] responseFields =
StringUtils.commaDelimitedListToStringArray(env.getProperty(QUERYREST_DEFAULT_RESPONSE_FIELDS)); StringUtils.commaDelimitedListToStringArray(env.getProperty(QUERYREST_DEFAULT_RESPONSE_FIELDS));
@ -200,6 +242,7 @@ public class QueryRestConfig extends WebMvcConfigurerAdapter {
} }
@Bean(name = BEAN_NAME_DEFAULT_RESPONSE_AGGREGATIONS) @Bean(name = BEAN_NAME_DEFAULT_RESPONSE_AGGREGATIONS)
@Lazy
public Set<Aggregation> defaultResponseAggregations() { public Set<Aggregation> defaultResponseAggregations() {
String[] responseAggregations = String[] responseAggregations =
StringUtils.commaDelimitedListToStringArray(env.getProperty(QUERYREST_DEFAULT_RESPONSE_AGGREGATIONS)); StringUtils.commaDelimitedListToStringArray(env.getProperty(QUERYREST_DEFAULT_RESPONSE_AGGREGATIONS));
@ -219,7 +262,8 @@ public class QueryRestConfig extends WebMvcConfigurerAdapter {
return defaultResponseAggregations; return defaultResponseAggregations;
} }
@Bean @Bean(name = BEAN_NAME_QUERY_VALIDATOR)
@Lazy
public Validator queryValidator() { public Validator queryValidator() {
return new QueryValidator(); return new QueryValidator();
} }

View File

@ -5,16 +5,15 @@ import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid; import javax.validation.Valid;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
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;
@ -48,37 +47,37 @@ import ch.psi.daq.domain.query.response.ResponseFormat;
import ch.psi.daq.domain.query.transform.image.color.ColorModelType; import ch.psi.daq.domain.query.transform.image.color.ColorModelType;
import ch.psi.daq.domain.query.transform.image.resize.ValueAggregation; import ch.psi.daq.domain.query.transform.image.resize.ValueAggregation;
import ch.psi.daq.domain.request.validate.RequestProviderValidator; import ch.psi.daq.domain.request.validate.RequestProviderValidator;
import ch.psi.daq.queryrest.config.QueryRestConfig;
import ch.psi.daq.queryrest.query.QueryManager; import ch.psi.daq.queryrest.query.QueryManager;
import ch.psi.daq.queryrest.response.AbstractHTTPResponse; import ch.psi.daq.queryrest.response.AbstractHTTPResponse;
import ch.psi.daq.queryrest.response.PolymorphicResponseMixIn; import ch.psi.daq.queryrest.response.PolymorphicResponseMixIn;
import ch.psi.daq.queryrest.response.json.JSONHTTPResponse; import ch.psi.daq.queryrest.response.json.JSONHTTPResponse;
@RestController @RestController
public class QueryRestController { public class QueryRestController implements ApplicationContextAware {
private static final Logger LOGGER = LoggerFactory.getLogger(QueryRestController.class); private static final Logger LOGGER = LoggerFactory.getLogger(QueryRestController.class);
public static final String PARAMETERS_ROOT_PATH = "params";
@Resource private Set<Backend> activeBackends;
private ApplicationContext appContext; private ApplicationContext context;
private ObjectMapper objectMapper;
@Resource private QueryManager queryManager;
private Validator queryValidator; private Validator queryValidator;
private Validator requestProviderValidator = new RequestProviderValidator(); private Validator requestProviderValidator = new RequestProviderValidator();
@Resource
private QueryManager queryManager;
@Resource
private ObjectMapper objectMapper;
@Resource(name = DomainConfig.BEAN_NAME_BACKENDS_ACTIVE)
private Set<Backend> activeBackends;
private Response defaultResponse = new JSONHTTPResponse(); private Response defaultResponse = new JSONHTTPResponse();
@PostConstruct @SuppressWarnings("unchecked")
public void afterPropertiesSet() {} @Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
final Backend backend = context.getBean(DomainConfig.BEAN_NAME_BACKEND_DEFAULT, Backend.class);
context = backend.getApplicationContext();
this.context = context;
activeBackends = context.getBean(DomainConfig.BEAN_NAME_BACKENDS_ACTIVE, Set.class);
objectMapper = context.getBean(DomainConfig.BEAN_NAME_OBJECT_MAPPER, ObjectMapper.class);
queryManager = context.getBean(QueryRestConfig.BEAN_NAME_QUERY_MANAGER, QueryManager.class);
queryValidator = context.getBean(QueryRestConfig.BEAN_NAME_QUERY_VALIDATOR, Validator.class);
}
@InitBinder @InitBinder
protected void initBinder(WebDataBinder binder) { protected void initBinder(WebDataBinder binder) {
@ -205,7 +204,7 @@ public class QueryRestController {
Response response = queries.getResponseOrDefault(defaultResponse); Response response = queries.getResponseOrDefault(defaultResponse);
if (response instanceof AbstractHTTPResponse) { if (response instanceof AbstractHTTPResponse) {
((AbstractHTTPResponse) response).respond(appContext, queries, res); ((AbstractHTTPResponse) response).respond(context, queries, res);
} else { } else {
String message = String message =
String.format( String.format(
@ -228,7 +227,7 @@ public class QueryRestController {
* *
* @return list of {@link Ordering}s as String array * @return list of {@link Ordering}s as String array
*/ */
@RequestMapping(value = PARAMETERS_ROOT_PATH + "/ordering", method = {RequestMethod.GET}, @RequestMapping(value = DomainConfig.PATH_PARAMETERS_ROOT + "/ordering", method = {RequestMethod.GET},
produces = {MediaType.APPLICATION_JSON_VALUE}) produces = {MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody List<Ordering> getOrderingValues() { public @ResponseBody List<Ordering> getOrderingValues() {
return Lists.newArrayList(Ordering.values()); return Lists.newArrayList(Ordering.values());
@ -239,7 +238,7 @@ public class QueryRestController {
* *
* @return list of {@link Ordering}s as String array * @return list of {@link Ordering}s as String array
*/ */
@RequestMapping(value = PARAMETERS_ROOT_PATH + "/responseformat", method = {RequestMethod.GET}, @RequestMapping(value = DomainConfig.PATH_PARAMETERS_ROOT + "/responseformat", method = {RequestMethod.GET},
produces = {MediaType.APPLICATION_JSON_VALUE}) produces = {MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody List<ResponseFormat> getResponseFormatValues() { public @ResponseBody List<ResponseFormat> getResponseFormatValues() {
return Lists.newArrayList(ResponseFormat.values()); return Lists.newArrayList(ResponseFormat.values());
@ -250,7 +249,7 @@ public class QueryRestController {
* *
* @return list of {@link QueryField}s as String array * @return list of {@link QueryField}s as String array
*/ */
@RequestMapping(value = PARAMETERS_ROOT_PATH + "/queryfields", method = {RequestMethod.GET}, @RequestMapping(value = DomainConfig.PATH_PARAMETERS_ROOT + "/queryfields", method = {RequestMethod.GET},
produces = {MediaType.APPLICATION_JSON_VALUE}) produces = {MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody List<QueryField> getQueryFieldValues() { public @ResponseBody List<QueryField> getQueryFieldValues() {
return Arrays.stream(QueryField.values()) return Arrays.stream(QueryField.values())
@ -263,7 +262,7 @@ public class QueryRestController {
* *
* @return list of {@link Aggregation}s as String array * @return list of {@link Aggregation}s as String array
*/ */
@RequestMapping(value = PARAMETERS_ROOT_PATH + "/aggregations", method = {RequestMethod.GET}, @RequestMapping(value = DomainConfig.PATH_PARAMETERS_ROOT + "/aggregations", method = {RequestMethod.GET},
produces = {MediaType.APPLICATION_JSON_VALUE}) produces = {MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody List<Aggregation> getAggregationValues() { public @ResponseBody List<Aggregation> getAggregationValues() {
return Lists.newArrayList(Aggregation.values()); return Lists.newArrayList(Aggregation.values());
@ -274,7 +273,7 @@ public class QueryRestController {
* *
* @return list of {@link AggregationType}s as String array * @return list of {@link AggregationType}s as String array
*/ */
@RequestMapping(value = PARAMETERS_ROOT_PATH + "/aggregationtypes", method = {RequestMethod.GET}, @RequestMapping(value = DomainConfig.PATH_PARAMETERS_ROOT + "/aggregationtypes", method = {RequestMethod.GET},
produces = {MediaType.APPLICATION_JSON_VALUE}) produces = {MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody List<AggregationType> getAggregationTypeValues() { public @ResponseBody List<AggregationType> getAggregationTypeValues() {
return Lists.newArrayList(AggregationType.values()); return Lists.newArrayList(AggregationType.values());
@ -285,10 +284,10 @@ public class QueryRestController {
* *
* @return list of {@link Backend}s as String array * @return list of {@link Backend}s as String array
*/ */
@RequestMapping(value = PARAMETERS_ROOT_PATH + "/backends", method = {RequestMethod.GET}, @RequestMapping(value = DomainConfig.PATH_BACKENDS, method = {RequestMethod.GET},
produces = {MediaType.APPLICATION_JSON_VALUE}) produces = {MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody List<Backend> getBackendValues() { public @ResponseBody List<Backend> getBackendValues() {
return Stream.of(Backend.values()) return Backend.getBackends().stream()
.filter(backend -> activeBackends.contains(backend)) .filter(backend -> activeBackends.contains(backend))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
@ -298,7 +297,7 @@ public class QueryRestController {
* *
* @return list of {@link Compression}s as String array * @return list of {@link Compression}s as String array
*/ */
@RequestMapping(value = PARAMETERS_ROOT_PATH + "/compression", method = {RequestMethod.GET}, @RequestMapping(value = DomainConfig.PATH_PARAMETERS_ROOT + "/compression", method = {RequestMethod.GET},
produces = {MediaType.APPLICATION_JSON_VALUE}) produces = {MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody List<Compression> getCompressionValues() { public @ResponseBody List<Compression> getCompressionValues() {
return Lists.newArrayList(Compression.values()); return Lists.newArrayList(Compression.values());
@ -309,7 +308,7 @@ public class QueryRestController {
* *
* @return list of {@link ValueAggregation}s as String array * @return list of {@link ValueAggregation}s as String array
*/ */
@RequestMapping(value = PARAMETERS_ROOT_PATH + "/valueaggregations", method = {RequestMethod.GET}, @RequestMapping(value = DomainConfig.PATH_PARAMETERS_ROOT + "/valueaggregations", method = {RequestMethod.GET},
produces = {MediaType.APPLICATION_JSON_VALUE}) produces = {MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody List<ValueAggregation> getValueAggregations() { public @ResponseBody List<ValueAggregation> getValueAggregations() {
return Lists.newArrayList(ValueAggregation.values()); return Lists.newArrayList(ValueAggregation.values());
@ -320,7 +319,7 @@ public class QueryRestController {
* *
* @return list of {@link ValueAggregation}s as String array * @return list of {@link ValueAggregation}s as String array
*/ */
@RequestMapping(value = PARAMETERS_ROOT_PATH + "/colormodeltypes", method = {RequestMethod.GET}, @RequestMapping(value = DomainConfig.PATH_PARAMETERS_ROOT + "/colormodeltypes", method = {RequestMethod.GET},
produces = {MediaType.APPLICATION_JSON_VALUE}) produces = {MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody List<ColorModelType> getColorModelTypes() { public @ResponseBody List<ColorModelType> getColorModelTypes() {
return Lists.newArrayList(ColorModelType.values()); return Lists.newArrayList(ColorModelType.values());

View File

@ -4,11 +4,14 @@ import java.util.ArrayList;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;
import javax.annotation.Resource; import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.validation.Errors; import org.springframework.validation.Errors;
import org.springframework.validation.Validator; import org.springframework.validation.Validator;
import ch.psi.daq.domain.backend.Backend;
import ch.psi.daq.domain.config.DomainConfig;
import ch.psi.daq.domain.query.DAQQueries; import ch.psi.daq.domain.query.DAQQueries;
import ch.psi.daq.domain.query.DAQQuery; import ch.psi.daq.domain.query.DAQQuery;
import ch.psi.daq.domain.query.DAQQueryElement; import ch.psi.daq.domain.query.DAQQueryElement;
@ -19,38 +22,38 @@ import ch.psi.daq.domain.query.transform.ValueTransformationSequence;
import ch.psi.daq.domain.request.Request; import ch.psi.daq.domain.request.Request;
import ch.psi.daq.queryrest.config.QueryRestConfig; import ch.psi.daq.queryrest.config.QueryRestConfig;
public class QueryValidator implements Validator { public class QueryValidator implements Validator, ApplicationContextAware {
@Resource(name = QueryRestConfig.BEAN_NAME_DEFAULT_RESPONSE_FIELDS)
private Set<QueryField> defaultResponseFields; private Set<QueryField> defaultResponseFields;
@Resource(name = QueryRestConfig.BEAN_NAME_DEFAULT_RESPONSE_AGGREGATIONS)
private Set<Aggregation> defaultResponseAggregations; private Set<Aggregation> defaultResponseAggregations;
/** @SuppressWarnings("unchecked")
* {@inheritDoc}
*/
@Override @Override
public boolean supports(Class<?> clazz) { public void setApplicationContext(ApplicationContext context) throws BeansException {
final Backend backend = context.getBean(DomainConfig.BEAN_NAME_BACKEND_DEFAULT, Backend.class);
context = backend.getApplicationContext();
defaultResponseFields = context.getBean(QueryRestConfig.BEAN_NAME_DEFAULT_RESPONSE_FIELDS, Set.class);
defaultResponseAggregations = context.getBean(QueryRestConfig.BEAN_NAME_DEFAULT_RESPONSE_AGGREGATIONS, Set.class);
}
@Override
public boolean supports(final Class<?> clazz) {
return DAQQuery.class.isAssignableFrom(clazz) || DAQQueries.class.isAssignableFrom(clazz); return DAQQuery.class.isAssignableFrom(clazz) || DAQQueries.class.isAssignableFrom(clazz);
} }
/**
* {@inheritDoc}
*/
@Override @Override
public void validate(Object target, Errors errors) { public void validate(final Object target, final Errors errors) {
if (target instanceof DAQQuery) { if (target instanceof DAQQuery) {
this.checkElement((DAQQuery) target, errors); this.checkElement((DAQQuery) target, errors);
} else if (target instanceof DAQQueries) { } else if (target instanceof DAQQueries) {
DAQQueries queries = (DAQQueries) target; final DAQQueries queries = (DAQQueries) target;
for (DAQQueryElement daqQueryElement : queries) { for (final DAQQueryElement daqQueryElement : queries) {
this.checkElement(daqQueryElement, errors); this.checkElement(daqQueryElement, errors);
} }
} }
} }
private void checkElement(DAQQueryElement query, Errors errors) { private void checkElement(final DAQQueryElement query, final Errors errors) {
// set default values (if not set) // set default values (if not set)
if (query.getFields() == null || query.getFields().isEmpty()) { if (query.getFields() == null || query.getFields().isEmpty()) {
query.setFields(new LinkedHashSet<>(defaultResponseFields)); query.setFields(new LinkedHashSet<>(defaultResponseFields));
@ -58,9 +61,9 @@ public class QueryValidator implements Validator {
if (query.getAggregation() != null) { if (query.getAggregation() != null) {
// check if only one binning element is defined // check if only one binning element is defined
long durationPerBin = query.getAggregation().getDurationPerBin(); final long durationPerBin = query.getAggregation().getDurationPerBin();
long pulsesPerBin = query.getAggregation().getPulsesPerBin(); final long pulsesPerBin = query.getAggregation().getPulsesPerBin();
int nrOfBins = query.getAggregation().getNrOfBins(); final int nrOfBins = query.getAggregation().getNrOfBins();
if ((durationPerBin != Request.NOT_SET && (pulsesPerBin != Request.NOT_SET || nrOfBins != Request.NOT_SET)) if ((durationPerBin != Request.NOT_SET && (pulsesPerBin != Request.NOT_SET || nrOfBins != Request.NOT_SET))
|| (pulsesPerBin != Request.NOT_SET || (pulsesPerBin != Request.NOT_SET
&& (durationPerBin != Request.NOT_SET || nrOfBins != Request.NOT_SET)) && (durationPerBin != Request.NOT_SET || nrOfBins != Request.NOT_SET))
@ -96,7 +99,7 @@ public class QueryValidator implements Validator {
// without this field, json will not contain transformedValue // without this field, json will not contain transformedValue
query.addField(QueryField.transformedValue); query.addField(QueryField.transformedValue);
for (ValueTransformationSequence transformationSequence : query.getValueTransformations()) { for (final ValueTransformationSequence transformationSequence : query.getValueTransformations()) {
transformationSequence.setExecutionEnvironment(ExecutionEnvironment.QUERYING); transformationSequence.setExecutionEnvironment(ExecutionEnvironment.QUERYING);
} }
} }

View File

@ -18,10 +18,10 @@ import ch.psi.daq.domain.query.channels.ChannelsResponse;
public interface QueryManager { public interface QueryManager {
List<ChannelsResponse> getChannels(ChannelsRequest request) throws Exception; List<ChannelsResponse> getChannels(final ChannelsRequest request) throws Exception;
Collection<ChannelInfos> getChannelInfos(ChannelNameRequest request) throws Exception; Collection<ChannelInfos> getChannelInfos(final ChannelNameRequest request) throws Exception;
List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> getEvents(DAQQueries queries) List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> getEvents(final DAQQueries queries)
throws Exception; throws Exception;
} }

View File

@ -8,18 +8,16 @@ import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy; import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple; import org.apache.commons.lang3.tuple.Triple;
import org.slf4j.Logger; import org.springframework.beans.BeansException;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import ch.psi.daq.domain.DataEvent; import ch.psi.daq.domain.DataEvent;
import ch.psi.daq.domain.backend.BackendAccess; import ch.psi.daq.domain.backend.Backend;
import ch.psi.daq.domain.config.DomainConfig; import ch.psi.daq.domain.config.DomainConfig;
import ch.psi.daq.domain.json.ChannelName; import ch.psi.daq.domain.json.ChannelName;
import ch.psi.daq.domain.json.channels.info.ChannelInfos; import ch.psi.daq.domain.json.channels.info.ChannelInfos;
@ -33,26 +31,23 @@ import ch.psi.daq.domain.query.channels.ChannelNameCache;
import ch.psi.daq.domain.query.channels.ChannelsRequest; import ch.psi.daq.domain.query.channels.ChannelsRequest;
import ch.psi.daq.domain.query.channels.ChannelsResponse; import ch.psi.daq.domain.query.channels.ChannelsResponse;
import ch.psi.daq.domain.query.processor.QueryProcessor; import ch.psi.daq.domain.query.processor.QueryProcessor;
import ch.psi.daq.query.config.QueryConfig;
import ch.psi.daq.queryrest.config.QueryRestConfig;
import ch.psi.daq.queryrest.query.model.ChannelInfosStreamImpl; import ch.psi.daq.queryrest.query.model.ChannelInfosStreamImpl;
public class QueryManagerImpl implements QueryManager { public class QueryManagerImpl implements QueryManager, ApplicationContextAware {
@SuppressWarnings("unused") private ChannelNameCache channelNameCache;
private static final Logger LOGGER = LoggerFactory.getLogger(QueryManagerImpl.class);
@Resource
private ApplicationContext appContext;
@Resource
private Function<BackendQuery, BackendQueryAnalyzer> queryAnalizerFactory; private Function<BackendQuery, BackendQueryAnalyzer> queryAnalizerFactory;
@Resource(name = DomainConfig.BEAN_NAME_BACKEND_ACCESS) @SuppressWarnings("unchecked")
private BackendAccess backendAccess; @Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
final Backend backend = context.getBean(DomainConfig.BEAN_NAME_BACKEND_DEFAULT, Backend.class);
context = backend.getApplicationContext();
@Resource(name = DomainConfig.BEAN_NAME_CHANNEL_NAME_CACHE) channelNameCache = context.getBean(QueryConfig.BEAN_NAME_CHANNEL_NAME_CACHE, ChannelNameCache.class);
private ChannelNameCache channelNameCache; queryAnalizerFactory = context.getBean(QueryRestConfig.BEAN_NAME_QUERY_ANALIZER_FACTORY, Function.class);
}
@PostConstruct
public void afterPropertiesSet() {}
@PreDestroy @PreDestroy
public void destroy() {} public void destroy() {}
@ -67,24 +62,21 @@ public class QueryManagerImpl implements QueryManager {
return channelNameCache.getChannels(request); return channelNameCache.getChannels(request);
} }
public Collection<ChannelInfos> getChannelInfos(ChannelNameRequest request) { public Collection<ChannelInfos> getChannelInfos(final ChannelNameRequest request) {
// set backends if not defined yet // set backends if not defined yet
channelNameCache.configureBackends(request.getChannels()); channelNameCache.configureBackends(request.getChannels());
Stream<ChannelInfos> stream = request.getRequestsByBackend().entrySet().stream() final Stream<ChannelInfos> stream = request.getRequestsByBackend().entrySet().stream()
.filter(entry -> .filter(entry -> entry.getKey().getBackendAccess().hasDataReader()
backendAccess.hasDataReader(entry.getKey()) && entry.getKey().getBackendAccess().hasChannelInfoReader())
&& backendAccess.hasChannelInfoReader(entry.getKey()))
.flatMap(entry -> { .flatMap(entry -> {
return entry.getValue().getChannelInfos(entry.getKey(), backendAccess) return entry.getValue().getChannelInfos(entry.getKey())
.entrySet().stream() .entrySet().stream()
.map(innerEntry -> { .map(innerEntry -> {
return new ChannelInfosStreamImpl( return new ChannelInfosStreamImpl(
new ChannelName(innerEntry.getKey(), entry.getKey()), new ChannelName(innerEntry.getKey(), entry.getKey()),
innerEntry.getValue() innerEntry.getValue());
); });
}
);
}); });
// materialize // materialize
@ -92,33 +84,32 @@ public class QueryManagerImpl implements QueryManager {
} }
@Override @Override
public List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> getEvents(DAQQueries queries) { public List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> getEvents(
final DAQQueries queries) {
// set backends if not defined yet // set backends if not defined yet
channelNameCache.configureBackends(queries); channelNameCache.configureBackends(queries);
List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> results = final List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> results =
new ArrayList<>(queries.getQueries().size()); new ArrayList<>(queries.getQueries().size());
for (DAQQueryElement queryElement : queries) { for (final DAQQueryElement queryElement : queries) {
Stream<Triple<BackendQuery, ChannelName, ?>> resultStreams = Stream<Triple<BackendQuery, ChannelName, ?>> resultStreams =
BackendQueryImpl BackendQueryImpl
.getBackendQueries(queryElement) .getBackendQueries(queryElement)
.stream() .stream()
.filter( .filter(
query -> query -> query.getBackend().getBackendAccess().hasDataReader()
backendAccess.hasDataReader(query.getBackend()) && query.getBackend().getBackendAccess().hasQueryProcessor())
&& backendAccess.hasQueryProcessor(query.getBackend())
)
.flatMap( .flatMap(
query -> { query -> {
QueryProcessor processor = backendAccess.getQueryProcessor(query.getBackend()); final QueryProcessor processor = query.getBackend().getBackendAccess().getQueryProcessor();
BackendQueryAnalyzer queryAnalizer = queryAnalizerFactory.apply(query); final BackendQueryAnalyzer queryAnalizer = queryAnalizerFactory.apply(query);
/* all the magic happens here */ /* all the magic happens here */
Stream<Entry<ChannelName, Stream<? extends DataEvent>>> channelToDataEvents = final Stream<Entry<ChannelName, Stream<? extends DataEvent>>> channelToDataEvents =
processor.process(queryAnalizer); processor.process(queryAnalizer);
/* do post-process */ /* do post-process */
Stream<Entry<ChannelName, ?>> channelToData = final Stream<Entry<ChannelName, ?>> channelToData =
queryAnalizer.postProcess(channelToDataEvents); queryAnalizer.postProcess(channelToDataEvents);
return channelToData.map(entry -> { return channelToData.map(entry -> {

View File

@ -15,7 +15,7 @@ public class ChannelInfosStreamImpl implements ChannelInfos {
public ChannelInfosStreamImpl() {} public ChannelInfosStreamImpl() {}
public ChannelInfosStreamImpl(ChannelName channel, Stream<? extends ChannelInfo> infos) { public ChannelInfosStreamImpl(final ChannelName channel, final Stream<? extends ChannelInfo> infos) {
this.channel = channel; this.channel = channel;
this.infos = infos; this.infos = infos;
} }

View File

@ -15,12 +15,13 @@ import ch.psi.daq.domain.query.response.ResponseImpl;
public abstract class AbstractHTTPResponse extends ResponseImpl { public abstract class AbstractHTTPResponse extends ResponseImpl {
public AbstractHTTPResponse(ResponseFormat format) { public AbstractHTTPResponse(final ResponseFormat format) {
super(format); super(format);
} }
@JsonIgnore @JsonIgnore
public abstract void respond(ApplicationContext context, DAQQueries queries, HttpServletResponse httpResponse) throws Exception; public abstract void respond(final ApplicationContext context, final DAQQueries queries,
HttpServletResponse httpResponse) throws Exception;
/** /**
* Configures the output stream and headers according to whether compression is wanted or not. * Configures the output stream and headers according to whether compression is wanted or not.
@ -38,15 +39,15 @@ public abstract class AbstractHTTPResponse extends ResponseImpl {
* @throws Exception Something goes wrong * @throws Exception Something goes wrong
*/ */
@JsonIgnore @JsonIgnore
protected OutputStream handleCompressionAndResponseHeaders(HttpServletResponse httpResponse, protected OutputStream handleCompressionAndResponseHeaders(final HttpServletResponse httpResponse,
String contentType) throws Exception { final String contentType) throws Exception {
OutputStream out = httpResponse.getOutputStream(); OutputStream out = httpResponse.getOutputStream();
httpResponse.setCharacterEncoding(JsonEncoding.UTF8.getJavaName()); httpResponse.setCharacterEncoding(JsonEncoding.UTF8.getJavaName());
httpResponse.setContentType(contentType); httpResponse.setContentType(contentType);
httpResponse.addHeader("Content-Type", contentType); httpResponse.addHeader("Content-Type", contentType);
String filename = "data." + this.getFileSuffix(); final String filename = "data." + this.getFileSuffix();
httpResponse.addHeader("Content-Disposition", "attachment; filename=" + filename); httpResponse.addHeader("Content-Disposition", "attachment; filename=" + filename);
if (this.isCompressed()) { if (this.isCompressed()) {

View File

@ -22,8 +22,9 @@ public interface ResponseStreamWriter {
* *
* @param results The results results * @param results The results results
* @param out The OutputStream * @param out The OutputStream
* @param response The Response * @param response The Response
* @throws Exception thrown if writing to the output stream fails * @throws Exception thrown if writing to the output stream fails
*/ */
public void respond(List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> results, OutputStream out, Response response) throws Exception; public void respond(final List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> results,
final OutputStream out, final Response response) throws Exception;
} }

View File

@ -6,19 +6,19 @@ import java.util.function.ToDoubleFunction;
import ch.psi.daq.domain.DataEvent; import ch.psi.daq.domain.DataEvent;
public class AggregationStringifyer implements Function<DataEvent, String> { public class AggregationStringifyer implements Function<DataEvent, String> {
private ToDoubleFunction<DataEvent> accessor; private final ToDoubleFunction<DataEvent> accessor;
private String nonValue; private final String nonValue;
public AggregationStringifyer(ToDoubleFunction<DataEvent> accessor, String nonValue) { public AggregationStringifyer(final ToDoubleFunction<DataEvent> accessor, final String nonValue) {
this.accessor = accessor; this.accessor = accessor;
this.nonValue = nonValue; this.nonValue = nonValue;
} }
@Override @Override
public String apply(DataEvent event) { public String apply(final DataEvent event) {
if (event == null) { if (event == null) {
return nonValue; return nonValue;
}else{ } else {
return Double.toString(accessor.applyAsDouble(event)); return Double.toString(accessor.applyAsDouble(event));
} }
} }

View File

@ -36,15 +36,15 @@ public class CSVHTTPResponse extends AbstractHTTPResponse {
super(ResponseFormat.CSV); super(ResponseFormat.CSV);
} }
public CSVHTTPResponse(Compression compression) { public CSVHTTPResponse(final Compression compression) {
this(); this();
setCompression(compression); setCompression(compression);
} }
@Override @Override
public void respond(ApplicationContext context, DAQQueries queries, HttpServletResponse httpResponse) public void respond(final ApplicationContext context, final DAQQueries queries, final HttpServletResponse httpResponse)
throws Exception { throws Exception {
OutputStream out = handleCompressionAndResponseHeaders(httpResponse, CONTENT_TYPE); final OutputStream out = handleCompressionAndResponseHeaders(httpResponse, CONTENT_TYPE);
// do csv specific validations // do csv specific validations
validateQueries(queries); validateQueries(queries);
@ -52,11 +52,11 @@ public class CSVHTTPResponse extends AbstractHTTPResponse {
try { try {
LOGGER.debug("Executing query '{}'", queries); LOGGER.debug("Executing query '{}'", queries);
QueryManager queryManager = context.getBean(QueryManager.class); final QueryManager queryManager = context.getBean(QueryManager.class);
CSVResponseStreamWriter streamWriter = context.getBean(CSVResponseStreamWriter.class); final CSVResponseStreamWriter streamWriter = context.getBean(CSVResponseStreamWriter.class);
// execute query // execute query
List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> result = final List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> result =
queryManager.getEvents(queries); queryManager.getEvents(queries);
// write the response back to the client using java 8 streams // write the response back to the client using java 8 streams
streamWriter.respond(result, out, this); streamWriter.respond(result, out, this);
@ -66,14 +66,14 @@ public class CSVHTTPResponse extends AbstractHTTPResponse {
} }
} }
protected void validateQueries(DAQQueries queries) { protected void validateQueries(final DAQQueries queries) {
for (DAQQueryElement query : queries) { for (final DAQQueryElement query : queries) {
if (!(query.getAggregation() == null || AggregationType.value.equals(query.getAggregation() if (!(query.getAggregation() == null || AggregationType.value.equals(query.getAggregation()
.getAggregationType()))) { .getAggregationType()))) {
// We allow only no aggregation or value aggregation as // We allow only no aggregation or value aggregation as
// extrema: nested structure and not clear how to map it to one line // extrema: nested structure and not clear how to map it to one line
// index: value is an array of Statistics whose size is not clear at initialization time // index: value is an array of Statistics whose size is not clear at initialization time
String message = "CSV export does not support '" + query.getAggregation().getAggregationType() + "'"; final String message = "CSV export does not support '" + query.getAggregation().getAggregationType() + "'";
LOGGER.warn(message); LOGGER.warn(message);
throw new IllegalArgumentException(message); throw new IllegalArgumentException(message);
} }

View File

@ -19,7 +19,6 @@ import java.util.function.Function;
import java.util.function.ToLongFunction; import java.util.function.ToLongFunction;
import java.util.stream.Stream; import java.util.stream.Stream;
import javax.annotation.Resource;
import javax.servlet.ServletResponse; import javax.servlet.ServletResponse;
import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVFormat;
@ -28,7 +27,9 @@ import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple; import org.apache.commons.lang3.tuple.Triple;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import ch.psi.daq.common.stream.StreamIterable; import ch.psi.daq.common.stream.StreamIterable;
import ch.psi.daq.common.stream.match.MapCreator; import ch.psi.daq.common.stream.match.MapCreator;
@ -36,23 +37,26 @@ import ch.psi.daq.common.stream.match.MapFiller;
import ch.psi.daq.common.stream.match.Padder; import ch.psi.daq.common.stream.match.Padder;
import ch.psi.daq.common.stream.match.StreamMatcher; import ch.psi.daq.common.stream.match.StreamMatcher;
import ch.psi.daq.domain.DataEvent; import ch.psi.daq.domain.DataEvent;
import ch.psi.daq.domain.backend.Backend;
import ch.psi.daq.domain.config.DomainConfig;
import ch.psi.daq.domain.json.ChannelName; import ch.psi.daq.domain.json.ChannelName;
import ch.psi.daq.domain.query.DAQQueryElement; import ch.psi.daq.domain.query.DAQQueryElement;
import ch.psi.daq.domain.query.backend.BackendQuery; import ch.psi.daq.domain.query.backend.BackendQuery;
import ch.psi.daq.domain.query.backend.analyzer.BackendQueryAnalyzer; import ch.psi.daq.domain.query.backend.analyzer.BackendQueryAnalyzer;
import ch.psi.daq.domain.query.mapping.Mapping;
import ch.psi.daq.domain.query.mapping.IncompleteStrategy; import ch.psi.daq.domain.query.mapping.IncompleteStrategy;
import ch.psi.daq.domain.query.mapping.Mapping;
import ch.psi.daq.domain.query.operation.Aggregation; import ch.psi.daq.domain.query.operation.Aggregation;
import ch.psi.daq.domain.query.operation.Extrema; import ch.psi.daq.domain.query.operation.Extrema;
import ch.psi.daq.domain.query.operation.QueryField; import ch.psi.daq.domain.query.operation.QueryField;
import ch.psi.daq.domain.query.response.Response; import ch.psi.daq.domain.query.response.Response;
import ch.psi.daq.queryrest.config.QueryRestConfig;
import ch.psi.daq.queryrest.response.ResponseStreamWriter; import ch.psi.daq.queryrest.response.ResponseStreamWriter;
/** /**
* Takes a Java 8 stream and writes it to the output stream provided by the {@link ServletResponse} * Takes a Java 8 stream and writes it to the output stream provided by the {@link ServletResponse}
* of the current request. * of the current request.
*/ */
public class CSVResponseStreamWriter implements ResponseStreamWriter { public class CSVResponseStreamWriter implements ResponseStreamWriter, ApplicationContextAware {
private static final Logger LOGGER = LoggerFactory.getLogger(CSVResponseStreamWriter.class); private static final Logger LOGGER = LoggerFactory.getLogger(CSVResponseStreamWriter.class);
public static final Mapping DEFAULT_MAPPING = new Mapping(IncompleteStrategy.FILL_NULL); public static final Mapping DEFAULT_MAPPING = new Mapping(IncompleteStrategy.FILL_NULL);
@ -67,12 +71,18 @@ public class CSVResponseStreamWriter implements ResponseStreamWriter {
// buckets. // buckets.
private static final ToLongFunction<DataEvent> MATCHER_PROVIDER = (event) -> event.getGlobalMillis() / 10L; private static final ToLongFunction<DataEvent> MATCHER_PROVIDER = (event) -> event.getGlobalMillis() / 10L;
@Resource
private ApplicationContext context;
@Resource
private Function<BackendQuery, BackendQueryAnalyzer> queryAnalizerFactory; private Function<BackendQuery, BackendQueryAnalyzer> queryAnalizerFactory;
@SuppressWarnings("unchecked")
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
final Backend backend = context.getBean(DomainConfig.BEAN_NAME_BACKEND_DEFAULT, Backend.class);
context = backend.getApplicationContext();
queryAnalizerFactory = context.getBean(QueryRestConfig.BEAN_NAME_QUERY_ANALIZER_FACTORY, Function.class);
}
@SuppressWarnings("unchecked")
@Override @Override
public void respond(final List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> results, public void respond(final List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> results,
final OutputStream out, final Response response) throws Exception { final OutputStream out, final Response response) throws Exception {
@ -80,7 +90,7 @@ public class CSVResponseStreamWriter implements ResponseStreamWriter {
throw new IllegalStateException("CSV format does not allow for multiple queries."); throw new IllegalStateException("CSV format does not allow for multiple queries.");
} }
AtomicReference<Exception> exception = new AtomicReference<>(); final AtomicReference<Exception> exception = new AtomicReference<>();
final Map<ChannelName, Stream<DataEvent>> streams = new LinkedHashMap<>(results.size()); final Map<ChannelName, Stream<DataEvent>> streams = new LinkedHashMap<>(results.size());
final List<String> header = new ArrayList<>(); final List<String> header = new ArrayList<>();
@ -110,11 +120,11 @@ public class CSVResponseStreamWriter implements ResponseStreamWriter {
}); });
}); });
Mapping mapping = daqQueryRef.get().getMappingOrDefault(DEFAULT_MAPPING); final Mapping mapping = daqQueryRef.get().getMappingOrDefault(DEFAULT_MAPPING);
Padder<ChannelName, DataEvent> padder = mapping.getIncomplete().getPadder(context, backendQueryRef.get()); final Padder<ChannelName, DataEvent> padder = mapping.getIncomplete().getPadder(backendQueryRef.get());
// online matching of the stream's content // online matching of the stream's content
StreamMatcher<ChannelName, DataEvent, Map<ChannelName, DataEvent>> streamMatcher = final StreamMatcher<ChannelName, DataEvent, Map<ChannelName, DataEvent>> streamMatcher =
new StreamMatcher<>( new StreamMatcher<>(
KEY_PROVIDER, KEY_PROVIDER,
MATCHER_PROVIDER, MATCHER_PROVIDER,
@ -123,10 +133,10 @@ public class CSVResponseStreamWriter implements ResponseStreamWriter {
null, null,
padder, padder,
streams.values()); streams.values());
Iterator<Map<ChannelName, DataEvent>> streamsMatchIter = streamMatcher.iterator(); final Iterator<Map<ChannelName, DataEvent>> streamsMatchIter = streamMatcher.iterator();
// prepare csv output // prepare csv output
CSVFormat csvFormat = CSVFormat.EXCEL.withDelimiter(DELIMITER_CVS); final CSVFormat csvFormat = CSVFormat.EXCEL.withDelimiter(DELIMITER_CVS);
Writer writer = null; Writer writer = null;
CSVPrinter csvFilePrinter = null; CSVPrinter csvFilePrinter = null;
@ -142,7 +152,7 @@ public class CSVResponseStreamWriter implements ResponseStreamWriter {
final Map<ChannelName, DataEvent> match = streamsMatchIter.next(); final Map<ChannelName, DataEvent> match = streamsMatchIter.next();
// ensure correct order // ensure correct order
Stream<String> rowStream = accessors.stream().sequential() final Stream<String> rowStream = accessors.stream().sequential()
.map(accessorPair -> { .map(accessorPair -> {
DataEvent event = match.get(accessorPair.getKey()); DataEvent event = match.get(accessorPair.getKey());
if (event != null) { if (event != null) {
@ -187,18 +197,19 @@ public class CSVResponseStreamWriter implements ResponseStreamWriter {
} }
private void setupChannelColumns(DAQQueryElement daqQuery, BackendQuery backendQuery, ChannelName channelName, private void setupChannelColumns(final DAQQueryElement daqQuery, final BackendQuery backendQuery,
Collection<String> header, Collection<Pair<ChannelName, Function<DataEvent, String>>> accessors) { final ChannelName channelName,
Set<QueryField> queryFields = daqQuery.getFields(); final Collection<String> header, Collection<Pair<ChannelName, Function<DataEvent, String>>> accessors) {
List<Aggregation> aggregations = final Set<QueryField> queryFields = daqQuery.getFields();
final List<Aggregation> aggregations =
daqQuery.getAggregation() != null ? daqQuery.getAggregation().getAggregations() : null; daqQuery.getAggregation() != null ? daqQuery.getAggregation().getAggregations() : null;
List<Extrema> extrema = daqQuery.getAggregation() != null ? daqQuery.getAggregation().getExtrema() : null; final List<Extrema> extrema = daqQuery.getAggregation() != null ? daqQuery.getAggregation().getExtrema() : null;
BackendQueryAnalyzer queryAnalyzer = queryAnalizerFactory.apply(backendQuery); final BackendQueryAnalyzer queryAnalyzer = queryAnalizerFactory.apply(backendQuery);
for (QueryField field : queryFields) { for (final QueryField field : queryFields) {
if (!(QueryField.value.equals(field) && queryAnalyzer.isAggregationEnabled())) { if (!(QueryField.value.equals(field) && queryAnalyzer.isAggregationEnabled())) {
StringBuilder buf = new StringBuilder(3) final StringBuilder buf = new StringBuilder(3)
.append(channelName.getName()) .append(channelName.getName())
.append(DELIMITER_CHANNELNAME_FIELDNAME) .append(DELIMITER_CHANNELNAME_FIELDNAME)
.append(field.name()); .append(field.name());
@ -210,8 +221,8 @@ public class CSVResponseStreamWriter implements ResponseStreamWriter {
} }
if (aggregations != null && queryAnalyzer.isAggregationEnabled()) { if (aggregations != null && queryAnalyzer.isAggregationEnabled()) {
for (Aggregation aggregation : aggregations) { for (final Aggregation aggregation : aggregations) {
StringBuilder buf = new StringBuilder(5) final StringBuilder buf = new StringBuilder(5)
.append(channelName.getName()) .append(channelName.getName())
.append(DELIMITER_CHANNELNAME_FIELDNAME) .append(DELIMITER_CHANNELNAME_FIELDNAME)
.append(QueryField.value.name()) .append(QueryField.value.name())
@ -224,11 +235,11 @@ public class CSVResponseStreamWriter implements ResponseStreamWriter {
} }
if (extrema != null && queryAnalyzer.isAggregationEnabled()) { if (extrema != null && queryAnalyzer.isAggregationEnabled()) {
for (Extrema extremum : extrema) { for (final Extrema extremum : extrema) {
for (QueryField field : queryFields) { for (final QueryField field : queryFields) {
Function<DataEvent, Object> accessor = extremum.getAccessor(field); final Function<DataEvent, Object> accessor = extremum.getAccessor(field);
if (accessor != null) { if (accessor != null) {
StringBuilder buf = new StringBuilder(7) final StringBuilder buf = new StringBuilder(7)
.append(channelName.getName()) .append(channelName.getName())
.append(DELIMITER_CHANNELNAME_FIELDNAME) .append(DELIMITER_CHANNELNAME_FIELDNAME)
.append(FIELDNAME_EXTREMA) .append(FIELDNAME_EXTREMA)

View File

@ -37,22 +37,22 @@ public class JSONHTTPResponse extends AbstractHTTPResponse {
super(ResponseFormat.JSON); super(ResponseFormat.JSON);
} }
public JSONHTTPResponse(Compression compression) { public JSONHTTPResponse(final Compression compression) {
this(); this();
setCompression(compression); setCompression(compression);
} }
@Override @Override
public void respond(ApplicationContext context, DAQQueries queries, HttpServletResponse response) throws Exception { public void respond(final ApplicationContext context, final DAQQueries queries, final HttpServletResponse response) throws Exception {
OutputStream out = handleCompressionAndResponseHeaders(response, CONTENT_TYPE); final OutputStream out = handleCompressionAndResponseHeaders(response, CONTENT_TYPE);
boolean hasMapping = JSONHTTPResponse.validateQueries(queries); final boolean hasMapping = JSONHTTPResponse.validateQueries(queries);
try { try {
LOGGER.debug("Executing query '{}'", queries); LOGGER.debug("Executing query '{}'", queries);
QueryManager queryManager = context.getBean(QueryManager.class); final QueryManager queryManager = context.getBean(QueryManager.class);
ResponseStreamWriter streamWriter; final ResponseStreamWriter streamWriter;
if (hasMapping) { if (hasMapping) {
streamWriter = context.getBean(JSONTableResponseStreamWriter.class); streamWriter = context.getBean(JSONTableResponseStreamWriter.class);
} else { } else {
@ -60,7 +60,7 @@ public class JSONHTTPResponse extends AbstractHTTPResponse {
} }
// execute query // execute query
List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> result = final List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> result =
queryManager.getEvents(queries); queryManager.getEvents(queries);
// write the response back to the client using java 8 streams // write the response back to the client using java 8 streams
streamWriter.respond(result, out, this); streamWriter.respond(result, out, this);
@ -71,10 +71,10 @@ public class JSONHTTPResponse extends AbstractHTTPResponse {
} }
public static boolean validateQueries(DAQQueries queries) { public static boolean validateQueries(final DAQQueries queries) {
boolean hasMapping = false; boolean hasMapping = false;
for (DAQQueryElement query : queries) { for (final DAQQueryElement query : queries) {
if (query.getMapping() != null) { if (query.getMapping() != null) {
hasMapping = true; hasMapping = true;

View File

@ -8,12 +8,14 @@ import java.util.Set;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Stream; import java.util.stream.Stream;
import javax.annotation.Resource;
import javax.servlet.ServletResponse; import javax.servlet.ServletResponse;
import org.apache.commons.lang3.tuple.Triple; import org.apache.commons.lang3.tuple.Triple;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import com.fasterxml.jackson.core.JsonEncoding; import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonFactory;
@ -23,6 +25,8 @@ import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter; import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider; import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import ch.psi.daq.domain.backend.Backend;
import ch.psi.daq.domain.config.DomainConfig;
import ch.psi.daq.domain.json.ChannelName; import ch.psi.daq.domain.json.ChannelName;
import ch.psi.daq.domain.query.DAQQueryElement; import ch.psi.daq.domain.query.DAQQueryElement;
import ch.psi.daq.domain.query.backend.BackendQuery; import ch.psi.daq.domain.query.backend.BackendQuery;
@ -30,44 +34,52 @@ import ch.psi.daq.domain.query.operation.Aggregation;
import ch.psi.daq.domain.query.operation.Extrema; import ch.psi.daq.domain.query.operation.Extrema;
import ch.psi.daq.domain.query.operation.QueryField; import ch.psi.daq.domain.query.operation.QueryField;
import ch.psi.daq.domain.query.response.Response; import ch.psi.daq.domain.query.response.Response;
import ch.psi.daq.queryrest.config.QueryRestConfig;
import ch.psi.daq.queryrest.response.ResponseStreamWriter; import ch.psi.daq.queryrest.response.ResponseStreamWriter;
/** /**
* Takes a Java 8 stream and writes it to the output stream provided by the {@link ServletResponse} * Takes a Java 8 stream and writes it to the output stream provided by the {@link ServletResponse}
* of the current request. * of the current request.
*/ */
public class JSONResponseStreamWriter implements ResponseStreamWriter { public class JSONResponseStreamWriter implements ResponseStreamWriter, ApplicationContextAware {
public static final String DATA_RESP_FIELD = "data"; public static final String DATA_RESP_FIELD = "data";
private static final Logger LOGGER = LoggerFactory.getLogger(JSONResponseStreamWriter.class); private static final Logger LOGGER = LoggerFactory.getLogger(JSONResponseStreamWriter.class);
@Resource
private JsonFactory jsonFactory;
@Resource
private ObjectMapper mapper; private ObjectMapper mapper;
private JsonFactory factory;
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
final Backend backend = context.getBean(DomainConfig.BEAN_NAME_BACKEND_DEFAULT, Backend.class);
context = backend.getApplicationContext();
mapper = context.getBean(DomainConfig.BEAN_NAME_OBJECT_MAPPER, ObjectMapper.class);
factory = context.getBean(QueryRestConfig.BEAN_NAME_JSON_FACTORY, JsonFactory.class);
}
@Override @Override
public void respond(final List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> results, public void respond(final List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> results,
final OutputStream out, final Response response) throws Exception { final OutputStream out, final Response response) throws Exception {
respond(jsonFactory, mapper, results, out, response); respond(factory, mapper, results, out, response);
} }
public static Set<String> getFields(DAQQueryElement query, boolean removeIdentifiers) { public static Set<String> getFields(final DAQQueryElement query, final boolean removeIdentifiers) {
Set<QueryField> queryFields = query.getFields(); final Set<QueryField> queryFields = query.getFields();
List<Aggregation> aggregations = query.getAggregation() != null ? query.getAggregation().getAggregations() : null; final List<Aggregation> aggregations =
List<Extrema> extrema = query.getAggregation() != null ? query.getAggregation().getExtrema() : null; query.getAggregation() != null ? query.getAggregation().getAggregations() : null;
final List<Extrema> extrema = query.getAggregation() != null ? query.getAggregation().getExtrema() : null;
Set<String> includedFields = final Set<String> includedFields =
new LinkedHashSet<String>(queryFields.size() + (aggregations != null ? aggregations.size() : 0) new LinkedHashSet<String>(queryFields.size() + (aggregations != null ? aggregations.size() : 0)
+ (extrema != null ? extrema.size() : 0)); + (extrema != null ? extrema.size() : 0));
for (QueryField field : queryFields) { for (final QueryField field : queryFields) {
includedFields.add(field.name()); includedFields.add(field.name());
} }
if (aggregations != null) { if (aggregations != null) {
for (Aggregation aggregation : aggregations) { for (final Aggregation aggregation : aggregations) {
includedFields.add(aggregation.name()); includedFields.add(aggregation.name());
} }
} }
@ -85,11 +97,12 @@ public class JSONResponseStreamWriter implements ResponseStreamWriter {
return includedFields; return includedFields;
} }
public static void respond(final JsonFactory factory, final ObjectMapper mapper, final List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> results, public static void respond(final JsonFactory factory, final ObjectMapper mapper,
final List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> results,
final OutputStream out, final Response response) throws Exception { final OutputStream out, final Response response) throws Exception {
AtomicReference<Exception> exception = new AtomicReference<>(); final AtomicReference<Exception> exception = new AtomicReference<>();
JsonGenerator generator = factory.createGenerator(out, JsonEncoding.UTF8); final JsonGenerator generator = factory.createGenerator(out, JsonEncoding.UTF8);
try { try {
if (results.size() > 1) { if (results.size() > 1) {
@ -98,9 +111,9 @@ public class JSONResponseStreamWriter implements ResponseStreamWriter {
results results
.forEach(entryy -> { .forEach(entryy -> {
DAQQueryElement daqQuery = entryy.getKey(); final DAQQueryElement daqQuery = entryy.getKey();
Set<String> includedFields = getFields(daqQuery, true); final Set<String> includedFields = getFields(daqQuery, true);
ObjectWriter writer = configureWriter(includedFields, mapper); final ObjectWriter writer = configureWriter(includedFields, mapper);
try { try {
generator.writeStartArray(); generator.writeStartArray();
@ -121,9 +134,9 @@ public class JSONResponseStreamWriter implements ResponseStreamWriter {
LOGGER.error("Could not write channel name of channel '{}'", triple.getMiddle(), LOGGER.error("Could not write channel name of channel '{}'", triple.getMiddle(),
e); e);
exception.compareAndSet(null, e); exception.compareAndSet(null, e);
} finally{ } finally {
if(triple.getRight() instanceof Stream){ if (triple.getRight() instanceof Stream) {
((Stream<?>)(triple.getRight())).close(); ((Stream<?>) (triple.getRight())).close();
} }
} }
}); });
@ -132,7 +145,7 @@ public class JSONResponseStreamWriter implements ResponseStreamWriter {
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Exception while writing json for '{}'", daqQuery.getChannels(), e); LOGGER.error("Exception while writing json for '{}'", daqQuery.getChannels(), e);
exception.compareAndSet(null, e); exception.compareAndSet(null, e);
} }
}); });
} finally { } finally {
if (results.size() > 1) { if (results.size() > 1) {
@ -157,11 +170,11 @@ public class JSONResponseStreamWriter implements ResponseStreamWriter {
* @param mapper The ObjectMapper * @param mapper The ObjectMapper
* @return the configured writer that includes the specified fields * @return the configured writer that includes the specified fields
*/ */
public static ObjectWriter configureWriter(Set<String> includedFields, ObjectMapper mapper) { public static ObjectWriter configureWriter(final Set<String> includedFields, final ObjectMapper mapper) {
SimpleFilterProvider propertyFilter = new SimpleFilterProvider(); final SimpleFilterProvider propertyFilter = new SimpleFilterProvider();
propertyFilter.addFilter("namedPropertyFilter", SimpleBeanPropertyFilter.filterOutAllExcept(includedFields)); propertyFilter.addFilter("namedPropertyFilter", SimpleBeanPropertyFilter.filterOutAllExcept(includedFields));
// only write the properties not excluded in the filter // only write the properties not excluded in the filter
ObjectWriter writer = mapper.writer(propertyFilter); final ObjectWriter writer = mapper.writer(propertyFilter);
return writer; return writer;
} }
} }

View File

@ -15,14 +15,14 @@ import java.util.function.ToLongFunction;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.ServletResponse; import javax.servlet.ServletResponse;
import org.apache.commons.lang3.tuple.Triple; import org.apache.commons.lang3.tuple.Triple;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import com.fasterxml.jackson.core.JsonEncoding; import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonFactory;
@ -36,14 +36,16 @@ import ch.psi.daq.common.stream.match.Padder;
import ch.psi.daq.common.stream.match.StreamMatcher; import ch.psi.daq.common.stream.match.StreamMatcher;
import ch.psi.daq.common.time.TimeUtils; import ch.psi.daq.common.time.TimeUtils;
import ch.psi.daq.domain.DataEvent; import ch.psi.daq.domain.DataEvent;
import ch.psi.daq.domain.backend.Backend;
import ch.psi.daq.domain.config.DomainConfig;
import ch.psi.daq.domain.json.ChannelName; import ch.psi.daq.domain.json.ChannelName;
import ch.psi.daq.domain.query.DAQQueryElement; import ch.psi.daq.domain.query.DAQQueryElement;
import ch.psi.daq.domain.query.backend.BackendQuery; import ch.psi.daq.domain.query.backend.BackendQuery;
import ch.psi.daq.domain.query.bin.BinningStrategy; import ch.psi.daq.domain.query.bin.BinningStrategy;
import ch.psi.daq.domain.query.bin.strategy.BinningStrategyPerBinPulse; import ch.psi.daq.domain.query.bin.strategy.BinningStrategyPerBinPulse;
import ch.psi.daq.domain.query.bin.strategy.BinningStrategyPerBinTime; import ch.psi.daq.domain.query.bin.strategy.BinningStrategyPerBinTime;
import ch.psi.daq.domain.query.mapping.Mapping;
import ch.psi.daq.domain.query.mapping.IncompleteStrategy; import ch.psi.daq.domain.query.mapping.IncompleteStrategy;
import ch.psi.daq.domain.query.mapping.Mapping;
import ch.psi.daq.domain.query.operation.Aggregation; import ch.psi.daq.domain.query.operation.Aggregation;
import ch.psi.daq.domain.query.operation.QueryField; import ch.psi.daq.domain.query.operation.QueryField;
import ch.psi.daq.domain.query.response.Response; import ch.psi.daq.domain.query.response.Response;
@ -56,7 +58,7 @@ import ch.psi.daq.queryrest.response.ResponseStreamWriter;
* Takes a Java 8 stream and writes it to the output stream provided by the {@link ServletResponse} * Takes a Java 8 stream and writes it to the output stream provided by the {@link ServletResponse}
* of the current request. * of the current request.
*/ */
public class JSONTableResponseStreamWriter implements ResponseStreamWriter { public class JSONTableResponseStreamWriter implements ResponseStreamWriter, ApplicationContextAware {
private static final Logger LOGGER = LoggerFactory.getLogger(JSONTableResponseStreamWriter.class); private static final Logger LOGGER = LoggerFactory.getLogger(JSONTableResponseStreamWriter.class);
@ -69,24 +71,24 @@ public class JSONTableResponseStreamWriter implements ResponseStreamWriter {
private static final ToLongFunction<DataEvent> MATCHER_PROVIDER = (event) -> event.getGlobalMillis() private static final ToLongFunction<DataEvent> MATCHER_PROVIDER = (event) -> event.getGlobalMillis()
/ MILLIS_PER_PULSE; / MILLIS_PER_PULSE;
@Resource
private ApplicationContext context;
@Resource
private JsonFactory jsonFactory;
@Resource
private ObjectMapper mapper; private ObjectMapper mapper;
private JsonFactory factory;
@Resource(name = QueryRestConfig.BEAN_NAME_DEFAULT_RESPONSE_AGGREGATIONS)
private Set<Aggregation> defaultResponseAggregations;
// In case ArchiverAppliance had several events within the 10ms mapping interval, return these // In case ArchiverAppliance had several events within the 10ms mapping interval, return these
// aggregations // aggregations
private Set<String> defaultResponseAggregationsStr; private Set<String> defaultResponseAggregationsStr;
@PostConstruct @SuppressWarnings("unchecked")
public void afterPropertiesSet() { @Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
final Backend backend = context.getBean(DomainConfig.BEAN_NAME_BACKEND_DEFAULT, Backend.class);
context = backend.getApplicationContext();
mapper = context.getBean(DomainConfig.BEAN_NAME_OBJECT_MAPPER, ObjectMapper.class);
factory = context.getBean(QueryRestConfig.BEAN_NAME_JSON_FACTORY, JsonFactory.class);
final Set<Aggregation> defaultResponseAggregations =
context.getBean(QueryRestConfig.BEAN_NAME_DEFAULT_RESPONSE_AGGREGATIONS, Set.class);;
defaultResponseAggregationsStr = defaultResponseAggregationsStr =
defaultResponseAggregations.stream().map(Aggregation::name) defaultResponseAggregations.stream().map(Aggregation::name)
.collect(Collectors.toCollection(LinkedHashSet::new)); .collect(Collectors.toCollection(LinkedHashSet::new));
@ -95,16 +97,17 @@ public class JSONTableResponseStreamWriter implements ResponseStreamWriter {
@Override @Override
public void respond(final List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> results, public void respond(final List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> results,
final OutputStream out, final Response response) throws Exception { final OutputStream out, final Response response) throws Exception {
respond(context, jsonFactory, mapper, defaultResponseAggregationsStr, results, out, response); respond(factory, mapper, defaultResponseAggregationsStr, results, out, response);
} }
public static void respond(final ApplicationContext context, final JsonFactory factory, @SuppressWarnings("unchecked")
public static void respond(final JsonFactory factory,
final ObjectMapper mapper, final Set<String> defaultResponseAggregationsStr, final ObjectMapper mapper, final Set<String> defaultResponseAggregationsStr,
final List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> results, final List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> results,
final OutputStream out, final Response response) throws Exception { final OutputStream out, final Response response) throws Exception {
AtomicReference<Exception> exception = new AtomicReference<>(); final AtomicReference<Exception> exception = new AtomicReference<>();
JsonGenerator generator = factory.createGenerator(out, JsonEncoding.UTF8); final JsonGenerator generator = factory.createGenerator(out, JsonEncoding.UTF8);
try { try {
if (results.size() > 1) { if (results.size() > 1) {
@ -113,8 +116,8 @@ public class JSONTableResponseStreamWriter implements ResponseStreamWriter {
results results
.forEach(entryy -> { .forEach(entryy -> {
DAQQueryElement daqQuery = entryy.getKey(); final DAQQueryElement daqQuery = entryy.getKey();
Set<String> includedFields = JSONResponseStreamWriter.getFields(daqQuery, false); final Set<String> includedFields = JSONResponseStreamWriter.getFields(daqQuery, false);
/* make sure identifiers are available */ /* make sure identifiers are available */
includedFields.add(QueryField.channel.name()); includedFields.add(QueryField.channel.name());
includedFields.add(QueryField.backend.name()); includedFields.add(QueryField.backend.name());
@ -123,7 +126,7 @@ public class JSONTableResponseStreamWriter implements ResponseStreamWriter {
includedFields.addAll(defaultResponseAggregationsStr); includedFields.addAll(defaultResponseAggregationsStr);
} }
ObjectWriter writer = JSONResponseStreamWriter.configureWriter(includedFields, mapper); final ObjectWriter writer = JSONResponseStreamWriter.configureWriter(includedFields, mapper);
/* get DataEvent stream of sub-queries for later match */ /* get DataEvent stream of sub-queries for later match */
final Map<ChannelName, Stream<DataEvent>> streams = final Map<ChannelName, Stream<DataEvent>> streams =
@ -147,12 +150,12 @@ public class JSONTableResponseStreamWriter implements ResponseStreamWriter {
} }
}); });
BackendQuery backendQuery = backendQueryRef.get(); final BackendQuery backendQuery = backendQueryRef.get();
RequestRange requestRange = backendQuery.getRequest().getRequestRange(); final RequestRange requestRange = backendQuery.getRequest().getRequestRange();
BinningStrategy binningStrategy = backendQuery.getBinningStrategy(); BinningStrategy binningStrategy = backendQuery.getBinningStrategy();
Mapping mapping = daqQuery.getMappingOrDefault(DEFAULT_MAPPING); final Mapping mapping = daqQuery.getMappingOrDefault(DEFAULT_MAPPING);
Padder<ChannelName, DataEvent> padder = mapping.getIncomplete().getPadder(context, backendQuery); final Padder<ChannelName, DataEvent> padder = mapping.getIncomplete().getPadder(backendQuery);
ToLongFunction<DataEvent> matchProvider = binningStrategy; ToLongFunction<DataEvent> matchProvider = binningStrategy;
if (binningStrategy == null) { if (binningStrategy == null) {
@ -162,7 +165,7 @@ public class JSONTableResponseStreamWriter implements ResponseStreamWriter {
} else if (requestRange.isTimeRangeDefined()) { } else if (requestRange.isTimeRangeDefined()) {
binningStrategy = new BinningStrategyPerBinTime(MILLIS_PER_PULSE); binningStrategy = new BinningStrategyPerBinTime(MILLIS_PER_PULSE);
} else { } else {
String message = "Either time or pulseId range must be defined by the query!"; final String message = "Either time or pulseId range must be defined by the query!";
LOGGER.error(message); LOGGER.error(message);
throw new IllegalStateException(message); throw new IllegalStateException(message);
} }
@ -170,7 +173,7 @@ public class JSONTableResponseStreamWriter implements ResponseStreamWriter {
binningStrategy.setRequestRange(requestRange); binningStrategy.setRequestRange(requestRange);
/* online matching of the stream's content */ /* online matching of the stream's content */
StreamMatcher<ChannelName, DataEvent, List<DataEvent>> streamMatcher = final StreamMatcher<ChannelName, DataEvent, List<DataEvent>> streamMatcher =
new StreamMatcher<>( new StreamMatcher<>(
KEY_PROVIDER, KEY_PROVIDER,
matchProvider, matchProvider,
@ -179,7 +182,7 @@ public class JSONTableResponseStreamWriter implements ResponseStreamWriter {
new BinnedValueCombiner(binningStrategy), new BinnedValueCombiner(binningStrategy),
padder, padder,
streams.values()); streams.values());
Iterator<List<DataEvent>> streamsMatchIter = streamMatcher.iterator(); final Iterator<List<DataEvent>> streamsMatchIter = streamMatcher.iterator();
try { try {
generator.writeStartObject(); generator.writeStartObject();
@ -195,7 +198,8 @@ public class JSONTableResponseStreamWriter implements ResponseStreamWriter {
streamMatcher.close(); streamMatcher.close();
} catch (Throwable t) { } catch (Throwable t) {
LOGGER.error( LOGGER.error(
"Something went wrong while closing stream matcher for JSON table response writer.", t); "Something went wrong while closing stream matcher for JSON table response writer.",
t);
} }
} }
} }
@ -217,8 +221,8 @@ public class JSONTableResponseStreamWriter implements ResponseStreamWriter {
} }
} }
private static boolean containsAggregation(Set<String> includedFields) { private static boolean containsAggregation(final Set<String> includedFields) {
for (Aggregation aggregation : Aggregation.values()) { for (final Aggregation aggregation : Aggregation.values()) {
if (includedFields.contains(aggregation.name())) { if (includedFields.contains(aggregation.name())) {
return true; return true;
} }

View File

@ -34,22 +34,22 @@ public class MsgPackHTTPResponse extends AbstractHTTPResponse {
super(ResponseFormat.MSGP); super(ResponseFormat.MSGP);
} }
public MsgPackHTTPResponse(Compression compression) { public MsgPackHTTPResponse(final Compression compression) {
this(); this();
setCompression(compression); setCompression(compression);
} }
@Override @Override
public void respond(ApplicationContext context, DAQQueries queries, HttpServletResponse response) throws Exception { public void respond(final ApplicationContext context, final DAQQueries queries, final HttpServletResponse response) throws Exception {
OutputStream out = handleCompressionAndResponseHeaders(response, CONTENT_TYPE); final OutputStream out = handleCompressionAndResponseHeaders(response, CONTENT_TYPE);
boolean hasMapping = JSONHTTPResponse.validateQueries(queries); final boolean hasMapping = JSONHTTPResponse.validateQueries(queries);
try { try {
LOGGER.debug("Executing query '{}'", queries); LOGGER.debug("Executing query '{}'", queries);
QueryManager queryManager = context.getBean(QueryManager.class); final QueryManager queryManager = context.getBean(QueryManager.class);
ResponseStreamWriter streamWriter; final ResponseStreamWriter streamWriter;
if (hasMapping) { if (hasMapping) {
streamWriter = context.getBean(MsgPackTableResponseStreamWriter.class); streamWriter = context.getBean(MsgPackTableResponseStreamWriter.class);
} else { } else {
@ -57,7 +57,7 @@ public class MsgPackHTTPResponse extends AbstractHTTPResponse {
} }
// execute query // execute query
List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> result = final List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> result =
queryManager.getEvents(queries); queryManager.getEvents(queries);
// write the response back to the client using java 8 streams // write the response back to the client using java 8 streams
streamWriter.respond(result, out, this); streamWriter.respond(result, out, this);

View File

@ -5,18 +5,23 @@ import java.util.List;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.stream.Stream; import java.util.stream.Stream;
import javax.annotation.Resource;
import javax.servlet.ServletResponse; import javax.servlet.ServletResponse;
import org.apache.commons.lang3.tuple.Triple; import org.apache.commons.lang3.tuple.Triple;
import org.msgpack.jackson.dataformat.MessagePackFactory; import org.msgpack.jackson.dataformat.MessagePackFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import ch.psi.daq.domain.backend.Backend;
import ch.psi.daq.domain.config.DomainConfig;
import ch.psi.daq.domain.json.ChannelName; import ch.psi.daq.domain.json.ChannelName;
import ch.psi.daq.domain.query.DAQQueryElement; import ch.psi.daq.domain.query.DAQQueryElement;
import ch.psi.daq.domain.query.backend.BackendQuery; import ch.psi.daq.domain.query.backend.BackendQuery;
import ch.psi.daq.domain.query.response.Response; import ch.psi.daq.domain.query.response.Response;
import ch.psi.daq.queryrest.config.QueryRestConfig;
import ch.psi.daq.queryrest.response.ResponseStreamWriter; import ch.psi.daq.queryrest.response.ResponseStreamWriter;
import ch.psi.daq.queryrest.response.json.JSONResponseStreamWriter; import ch.psi.daq.queryrest.response.json.JSONResponseStreamWriter;
@ -24,17 +29,22 @@ import ch.psi.daq.queryrest.response.json.JSONResponseStreamWriter;
* Takes a Java 8 stream and writes it to the output stream provided by the {@link ServletResponse} * Takes a Java 8 stream and writes it to the output stream provided by the {@link ServletResponse}
* of the current request. * of the current request.
*/ */
public class MsgPackResponseStreamWriter implements ResponseStreamWriter { public class MsgPackResponseStreamWriter implements ResponseStreamWriter, ApplicationContextAware {
@Resource
private MessagePackFactory msgPackFactory;
@Resource
private ObjectMapper mapper; private ObjectMapper mapper;
private MessagePackFactory factory;
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
final Backend backend = context.getBean(DomainConfig.BEAN_NAME_BACKEND_DEFAULT, Backend.class);
context = backend.getApplicationContext();
mapper = context.getBean(DomainConfig.BEAN_NAME_OBJECT_MAPPER, ObjectMapper.class);
factory = context.getBean(QueryRestConfig.BEAN_NAME_MSG_PACK_FACTORY, MessagePackFactory.class);
}
@Override @Override
public void respond(final List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> results, public void respond(final List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> results,
final OutputStream out, final Response response) throws Exception { final OutputStream out, final Response response) throws Exception {
JSONResponseStreamWriter.respond(msgPackFactory, mapper, results, out, response); JSONResponseStreamWriter.respond(factory, mapper, results, out, response);
} }
} }

View File

@ -8,16 +8,18 @@ import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.ServletResponse; import javax.servlet.ServletResponse;
import org.apache.commons.lang3.tuple.Triple; import org.apache.commons.lang3.tuple.Triple;
import org.msgpack.jackson.dataformat.MessagePackFactory; import org.msgpack.jackson.dataformat.MessagePackFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import ch.psi.daq.domain.backend.Backend;
import ch.psi.daq.domain.config.DomainConfig;
import ch.psi.daq.domain.json.ChannelName; import ch.psi.daq.domain.json.ChannelName;
import ch.psi.daq.domain.query.DAQQueryElement; import ch.psi.daq.domain.query.DAQQueryElement;
import ch.psi.daq.domain.query.backend.BackendQuery; import ch.psi.daq.domain.query.backend.BackendQuery;
@ -31,24 +33,24 @@ import ch.psi.daq.queryrest.response.json.JSONTableResponseStreamWriter;
* Takes a Java 8 stream and writes it to the output stream provided by the {@link ServletResponse} * Takes a Java 8 stream and writes it to the output stream provided by the {@link ServletResponse}
* of the current request. * of the current request.
*/ */
public class MsgPackTableResponseStreamWriter implements ResponseStreamWriter { public class MsgPackTableResponseStreamWriter implements ResponseStreamWriter, ApplicationContextAware {
@Resource
private ApplicationContext context;
@Resource
private MessagePackFactory msgPackFactory;
@Resource
private ObjectMapper mapper; private ObjectMapper mapper;
private MessagePackFactory factory;
@Resource(name = QueryRestConfig.BEAN_NAME_DEFAULT_RESPONSE_AGGREGATIONS) // In case ArchiverAppliance had several events within the 10ms mapping interval, return these
private Set<Aggregation> defaultResponseAggregations; // aggregations
private Set<String> defaultResponseAggregationsStr; private Set<String> defaultResponseAggregationsStr;
@PostConstruct @SuppressWarnings("unchecked")
public void afterPropertiesSet() { @Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
final Backend backend = context.getBean(DomainConfig.BEAN_NAME_BACKEND_DEFAULT, Backend.class);
context = backend.getApplicationContext();
mapper = context.getBean(DomainConfig.BEAN_NAME_OBJECT_MAPPER, ObjectMapper.class);
factory = context.getBean(QueryRestConfig.BEAN_NAME_MSG_PACK_FACTORY, MessagePackFactory.class);
final Set<Aggregation> defaultResponseAggregations =
context.getBean(QueryRestConfig.BEAN_NAME_DEFAULT_RESPONSE_AGGREGATIONS, Set.class);;
defaultResponseAggregationsStr = defaultResponseAggregationsStr =
defaultResponseAggregations.stream().map(Aggregation::name) defaultResponseAggregations.stream().map(Aggregation::name)
.collect(Collectors.toCollection(LinkedHashSet::new)); .collect(Collectors.toCollection(LinkedHashSet::new));
@ -56,7 +58,7 @@ public class MsgPackTableResponseStreamWriter implements ResponseStreamWriter {
@Override @Override
public void respond(final List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> results, public void respond(final List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> results,
final OutputStream out, final Response response) throws Exception { final OutputStream out, final Response response) throws Exception {
JSONTableResponseStreamWriter.respond(context, msgPackFactory, mapper, defaultResponseAggregationsStr, results, out, response); JSONTableResponseStreamWriter.respond(factory, mapper, defaultResponseAggregationsStr, results, out, response);
} }
} }

View File

@ -34,22 +34,22 @@ public class SmileHTTPResponse extends AbstractHTTPResponse {
super(ResponseFormat.SMILE); super(ResponseFormat.SMILE);
} }
public SmileHTTPResponse(Compression compression) { public SmileHTTPResponse(final Compression compression) {
this(); this();
setCompression(compression); setCompression(compression);
} }
@Override @Override
public void respond(ApplicationContext context, DAQQueries queries, HttpServletResponse response) throws Exception { public void respond(final ApplicationContext context, final DAQQueries queries, final HttpServletResponse response) throws Exception {
OutputStream out = handleCompressionAndResponseHeaders(response, CONTENT_TYPE); final OutputStream out = handleCompressionAndResponseHeaders(response, CONTENT_TYPE);
boolean hasMapping = JSONHTTPResponse.validateQueries(queries); final boolean hasMapping = JSONHTTPResponse.validateQueries(queries);
try { try {
LOGGER.debug("Executing query '{}'", queries); LOGGER.debug("Executing query '{}'", queries);
QueryManager queryManager = context.getBean(QueryManager.class); final QueryManager queryManager = context.getBean(QueryManager.class);
ResponseStreamWriter streamWriter; final ResponseStreamWriter streamWriter;
if (hasMapping) { if (hasMapping) {
streamWriter = context.getBean(SmileTableResponseStreamWriter.class); streamWriter = context.getBean(SmileTableResponseStreamWriter.class);
} else { } else {
@ -57,7 +57,7 @@ public class SmileHTTPResponse extends AbstractHTTPResponse {
} }
// execute query // execute query
List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> result = final List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> result =
queryManager.getEvents(queries); queryManager.getEvents(queries);
// write the response back to the client using java 8 streams // write the response back to the client using java 8 streams
streamWriter.respond(result, out, this); streamWriter.respond(result, out, this);

View File

@ -5,18 +5,23 @@ import java.util.List;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.stream.Stream; import java.util.stream.Stream;
import javax.annotation.Resource;
import javax.servlet.ServletResponse; import javax.servlet.ServletResponse;
import org.apache.commons.lang3.tuple.Triple; import org.apache.commons.lang3.tuple.Triple;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.smile.SmileFactory; import com.fasterxml.jackson.dataformat.smile.SmileFactory;
import ch.psi.daq.domain.backend.Backend;
import ch.psi.daq.domain.config.DomainConfig;
import ch.psi.daq.domain.json.ChannelName; import ch.psi.daq.domain.json.ChannelName;
import ch.psi.daq.domain.query.DAQQueryElement; import ch.psi.daq.domain.query.DAQQueryElement;
import ch.psi.daq.domain.query.backend.BackendQuery; import ch.psi.daq.domain.query.backend.BackendQuery;
import ch.psi.daq.domain.query.response.Response; import ch.psi.daq.domain.query.response.Response;
import ch.psi.daq.queryrest.config.QueryRestConfig;
import ch.psi.daq.queryrest.response.ResponseStreamWriter; import ch.psi.daq.queryrest.response.ResponseStreamWriter;
import ch.psi.daq.queryrest.response.json.JSONResponseStreamWriter; import ch.psi.daq.queryrest.response.json.JSONResponseStreamWriter;
@ -24,17 +29,23 @@ import ch.psi.daq.queryrest.response.json.JSONResponseStreamWriter;
* Takes a Java 8 stream and writes it to the output stream provided by the {@link ServletResponse} * Takes a Java 8 stream and writes it to the output stream provided by the {@link ServletResponse}
* of the current request. * of the current request.
*/ */
public class SmileResponseStreamWriter implements ResponseStreamWriter { public class SmileResponseStreamWriter implements ResponseStreamWriter, ApplicationContextAware {
@Resource
private SmileFactory smileFactory;
@Resource
private ObjectMapper mapper; private ObjectMapper mapper;
private SmileFactory factory;
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
final Backend backend = context.getBean(DomainConfig.BEAN_NAME_BACKEND_DEFAULT, Backend.class);
context = backend.getApplicationContext();
mapper = context.getBean(DomainConfig.BEAN_NAME_OBJECT_MAPPER, ObjectMapper.class);
factory = context.getBean(QueryRestConfig.BEAN_NAME_SMILE_FACTORY, SmileFactory.class);
}
@Override @Override
public void respond(final List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> results, public void respond(final List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> results,
final OutputStream out, final Response response) throws Exception { final OutputStream out, final Response response) throws Exception {
JSONResponseStreamWriter.respond(smileFactory, mapper, results, out, response); JSONResponseStreamWriter.respond(factory, mapper, results, out, response);
} }
} }

View File

@ -8,16 +8,18 @@ import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.ServletResponse; import javax.servlet.ServletResponse;
import org.apache.commons.lang3.tuple.Triple; import org.apache.commons.lang3.tuple.Triple;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.smile.SmileFactory; import com.fasterxml.jackson.dataformat.smile.SmileFactory;
import ch.psi.daq.domain.backend.Backend;
import ch.psi.daq.domain.config.DomainConfig;
import ch.psi.daq.domain.json.ChannelName; import ch.psi.daq.domain.json.ChannelName;
import ch.psi.daq.domain.query.DAQQueryElement; import ch.psi.daq.domain.query.DAQQueryElement;
import ch.psi.daq.domain.query.backend.BackendQuery; import ch.psi.daq.domain.query.backend.BackendQuery;
@ -31,24 +33,25 @@ import ch.psi.daq.queryrest.response.json.JSONTableResponseStreamWriter;
* Takes a Java 8 stream and writes it to the output stream provided by the {@link ServletResponse} * Takes a Java 8 stream and writes it to the output stream provided by the {@link ServletResponse}
* of the current request. * of the current request.
*/ */
public class SmileTableResponseStreamWriter implements ResponseStreamWriter { public class SmileTableResponseStreamWriter implements ResponseStreamWriter, ApplicationContextAware {
@Resource
private ApplicationContext context;
@Resource
private SmileFactory smileFactory;
@Resource
private ObjectMapper mapper; private ObjectMapper mapper;
private SmileFactory factory;
@Resource(name = QueryRestConfig.BEAN_NAME_DEFAULT_RESPONSE_AGGREGATIONS) // In case ArchiverAppliance had several events within the 10ms mapping interval, return these
private Set<Aggregation> defaultResponseAggregations; // aggregations
private Set<String> defaultResponseAggregationsStr; private Set<String> defaultResponseAggregationsStr;
@PostConstruct @SuppressWarnings("unchecked")
public void afterPropertiesSet() { @Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
final Backend backend = context.getBean(DomainConfig.BEAN_NAME_BACKEND_DEFAULT, Backend.class);
context = backend.getApplicationContext();
mapper = context.getBean(DomainConfig.BEAN_NAME_OBJECT_MAPPER, ObjectMapper.class);
factory = context.getBean(QueryRestConfig.BEAN_NAME_SMILE_FACTORY, SmileFactory.class);
final Set<Aggregation> defaultResponseAggregations =
context.getBean(QueryRestConfig.BEAN_NAME_DEFAULT_RESPONSE_AGGREGATIONS, Set.class);;
defaultResponseAggregationsStr = defaultResponseAggregationsStr =
defaultResponseAggregations.stream().map(Aggregation::name) defaultResponseAggregations.stream().map(Aggregation::name)
.collect(Collectors.toCollection(LinkedHashSet::new)); .collect(Collectors.toCollection(LinkedHashSet::new));
@ -56,7 +59,7 @@ public class SmileTableResponseStreamWriter implements ResponseStreamWriter {
@Override @Override
public void respond(final List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> results, public void respond(final List<Entry<DAQQueryElement, Stream<Triple<BackendQuery, ChannelName, ?>>>> results,
final OutputStream out, final Response response) throws Exception { final OutputStream out, final Response response) throws Exception {
JSONTableResponseStreamWriter.respond(context, smileFactory, mapper, defaultResponseAggregationsStr, results, out, response); JSONTableResponseStreamWriter.respond(factory, mapper, defaultResponseAggregationsStr, results, out, response);
} }
} }

View File

@ -5,7 +5,7 @@ import javax.annotation.Resource;
import org.junit.Before; import org.junit.Before;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode; import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.TestExecutionListeners;
@ -19,17 +19,13 @@ import org.springframework.web.context.WebApplicationContext;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import ch.psi.daq.queryrest.QueryRestApplication; import ch.psi.daq.queryrest.QueryRestApplication;
import ch.psi.daq.queryrest.config.QueryRestConfig;
import ch.psi.daq.test.cassandra.CassandraDaqUnitDependencyInjectionTestExecutionListener;
import ch.psi.daq.test.queryrest.config.DaqWebMvcConfig; import ch.psi.daq.test.queryrest.config.DaqWebMvcConfig;
@TestExecutionListeners({ @TestExecutionListeners({
CassandraDaqUnitDependencyInjectionTestExecutionListener.class,
DependencyInjectionTestExecutionListener.class}) DependencyInjectionTestExecutionListener.class})
@SpringApplicationConfiguration(classes = { @SpringBootTest(classes = {
QueryRestApplication.class, QueryRestApplication.class,
QueryRestConfig.class,
DaqWebMvcConfig.class DaqWebMvcConfig.class
}) })
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
@ -53,5 +49,4 @@ public abstract class AbstractDaqRestTest {
// Setup Spring test in webapp-mode (same config as spring-boot) // Setup Spring test in webapp-mode (same config as spring-boot)
this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
} }
} }

View File

@ -0,0 +1,191 @@
package ch.psi.daq.test.queryrest.backend;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.util.HashSet;
import java.util.Set;
import javax.annotation.Resource;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import ch.psi.daq.common.serialization.SerializationHelper;
import ch.psi.daq.domain.backend.Backend;
import ch.psi.daq.domain.config.DomainConfig;
import ch.psi.daq.domain.query.channels.ChannelNameCache;
import ch.psi.daq.filestorage.config.FileStorageConfig;
import ch.psi.daq.query.config.QueryConfig;
import ch.psi.daq.test.queryrest.AbstractDaqRestTest;
public class BackendTest extends AbstractDaqRestTest {
@Resource
private ApplicationContext context;
@Before
public void setUp() throws Exception {}
@After
public void tearDown() {}
@Test
public void testSerialization_01() throws Exception {
boolean backendsInit = false;
for (final Backend backend : Backend.getBackends()) {
backendsInit = true;
final Backend copy = SerializationHelper.copy(backend);
assertSame(backend, copy);
}
assertTrue(backendsInit);
}
@Test
public void testOverload_01() throws Exception {
final String propertyContactPoints = "cassandra.contactpoints";
String contactPoints;
int timeout;
boolean backendsInit = false;
Backend backendContext;
String userHome = System.getProperty("user.home");
String storageRootDir;
boolean compaction;
for (final Backend backend : Backend.getBackends()) {
backendsInit = true;
final ApplicationContext context = backend.getApplicationContext();
// from domain.properties
timeout = context.getBean(DomainConfig.BEAN_NAME_UPDATE_TIMEOUT, Integer.class);
assertEquals(30, timeout);
// from queryrest-test.properties
timeout = context.getBean(DomainConfig.BEAN_NAME_QUERY_TIMEOUT, Integer.class);
assertEquals(11, timeout);
if (backend.getId() == 255) {
// from test-overload.properties
timeout = context.getBean(DomainConfig.BEAN_NAME_SHUTDOWN_TIMEOUT, Integer.class);
assertEquals(32, timeout);
// from test-overload.properties
backendContext = context.getBean(DomainConfig.BEAN_NAME_BACKEND_OF_CONTEXT, Backend.class);
assertEquals("queryrest-1", backendContext.getName());
assertSame(backend, backendContext);
timeout = context.getBean(DomainConfig.BEAN_NAME_DELETE_TIMEOUT, Integer.class);
assertEquals(42, timeout);
storageRootDir =
context.getBean(FileStorageConfig.BEAN_NAME_ROOT_DIR, String.class);
assertEquals(userHome + "/daq/queryrest1", storageRootDir);
// from filestorage.properties
compaction =
context.getBean(FileStorageConfig.BEAN_NAME_FILESTORAGE_COMPACTION_TTL_UNCOMPACTED_DELETES,
Boolean.class);
assertEquals(false, compaction);
} else if (backend.getId() == 254) {
// from queryrest-test.properties
timeout = context.getBean(DomainConfig.BEAN_NAME_SHUTDOWN_TIMEOUT, Integer.class);
assertEquals(31, timeout);
// from test-overload2.properties
backendContext = context.getBean(DomainConfig.BEAN_NAME_BACKEND_OF_CONTEXT, Backend.class);
assertEquals("queryrest-2", backendContext.getName());
assertSame(backend, backendContext);
timeout = context.getBean(DomainConfig.BEAN_NAME_DELETE_TIMEOUT, Integer.class);
assertEquals(43, timeout);
contactPoints = context.getEnvironment().getProperty(propertyContactPoints, String.class);
assertEquals("localhost", contactPoints);
} else if (backend.getId() == 253) {
// from queryrest-test.properties
timeout = context.getBean(DomainConfig.BEAN_NAME_SHUTDOWN_TIMEOUT, Integer.class);
assertEquals(31, timeout);
// from test-overload3.properties
backendContext = context.getBean(DomainConfig.BEAN_NAME_BACKEND_OF_CONTEXT, Backend.class);
assertEquals("queryrest-3", backendContext.getName());
assertSame(backend, backendContext);
timeout = context.getBean(DomainConfig.BEAN_NAME_DELETE_TIMEOUT, Integer.class);
assertEquals(50, timeout);
}
}
assertTrue(backendsInit);
// from domain.properties
timeout = context.getBean(DomainConfig.BEAN_NAME_UPDATE_TIMEOUT, Integer.class);
assertEquals(30, timeout);
// from queryrest-test.properties
timeout = context.getBean(DomainConfig.BEAN_NAME_QUERY_TIMEOUT, Integer.class);
assertEquals(11, timeout);
// from queryrest-test.properties
timeout = context.getBean(DomainConfig.BEAN_NAME_SHUTDOWN_TIMEOUT, Integer.class);
assertEquals(31, timeout);
// does not see these beans
try {
storageRootDir = context.getBean(FileStorageConfig.BEAN_NAME_ROOT_DIR, String.class);
assertTrue(false);
} catch (NoSuchBeanDefinitionException e) {
assertTrue(true);
}
try {
compaction =
context.getBean(FileStorageConfig.BEAN_NAME_FILESTORAGE_COMPACTION_TTL_UNCOMPACTED_DELETES,
Boolean.class);
assertTrue(false);
} catch (NoSuchBeanDefinitionException e) {
assertTrue(true);
}
assertNull(context.getEnvironment().getProperty(propertyContactPoints, String.class));
}
@Test
public void testInstance_01() throws Exception {
final Set<ApplicationContext> parentContexts = new HashSet<>();
final Set<ApplicationContext> backendContexts = new HashSet<>();
for (final Backend backend : Backend.getBackends()) {
parentContexts.add(backend.getParentApplicationContext());
backendContexts.add(backend.getApplicationContext());
}
assertEquals(1, parentContexts.size());
assertEquals(Backend.getBackends().size(), backendContexts.size());
final ApplicationContext parentContext = parentContexts.iterator().next();
assertSame(context, parentContext);
for (final ApplicationContext context : backendContexts) {
assertNotSame(context, parentContext);
assertSame(parentContext, context.getParent());
}
}
@Test
public void testSingleInstance_01() throws Exception {
final Set<ChannelNameCache> caches = new HashSet<>();
for (final Backend backend : Backend.getBackends()) {
ChannelNameCache cache =
backend.getApplicationContext().getBean(QueryConfig.BEAN_NAME_CHANNEL_NAME_CACHE,
ChannelNameCache.class);
caches.add(cache);
}
assertEquals(1, caches.size());
assertSame(
context.getBean(QueryConfig.BEAN_NAME_CHANNEL_NAME_CACHE, ChannelNameCache.class),
caches.iterator().next());
}
}

View File

@ -1,8 +1,12 @@
package ch.psi.daq.test.queryrest.config; package ch.psi.daq.test.queryrest.config;
import java.util.List;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.annotation.Resource; import javax.annotation.Resource;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -10,26 +14,23 @@ import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources; import org.springframework.context.annotation.PropertySources;
import org.springframework.context.annotation.Scope;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
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.WebMvcConfigurerAdapter;
import ch.psi.daq.archiverappliance.config.ArchiverApplianceConfig;
import ch.psi.daq.cassandra.config.CassandraConfig;
import ch.psi.daq.cassandra.reader.CassandraReader;
import ch.psi.daq.domain.backend.Backend; import ch.psi.daq.domain.backend.Backend;
import ch.psi.daq.domain.backend.BackendAccess; import ch.psi.daq.domain.backend.BackendType;
import ch.psi.daq.domain.config.DomainConfig; import ch.psi.daq.domain.config.DomainConfig;
import ch.psi.daq.domain.events.ChannelEvent; import ch.psi.daq.domain.events.ChannelEvent;
import ch.psi.daq.domain.query.channels.reader.ChannelInfoReader;
import ch.psi.daq.domain.query.processor.QueryProcessor; import ch.psi.daq.domain.query.processor.QueryProcessor;
import ch.psi.daq.domain.reader.DataReader;
import ch.psi.daq.domain.reader.StreamEventReader; import ch.psi.daq.domain.reader.StreamEventReader;
import ch.psi.daq.filestorage.config.FileStorageConfig; import ch.psi.daq.domain.test.reader.TestReader;
import ch.psi.daq.query.config.QueryConfig; import ch.psi.daq.query.config.QueryConfig;
import ch.psi.daq.query.processor.QueryProcessorLocal; import ch.psi.daq.query.processor.QueryProcessorLocal;
import ch.psi.daq.test.query.config.LocalQueryTestConfig; import ch.psi.daq.queryrest.config.QueryRestConfig;
import ch.psi.daq.test.queryrest.query.DummyArchiverApplianceReader;
import ch.psi.daq.test.queryrest.query.DummyCassandraReader;
import ch.psi.daq.test.queryrest.query.DummyFilestorageReader;
@Configuration @Configuration
@ComponentScan @ComponentScan
@ -37,50 +38,64 @@ import ch.psi.daq.test.queryrest.query.DummyFilestorageReader;
@PropertySources(value = { @PropertySources(value = {
@PropertySource(value = {"classpath:queryrest-test.properties"}) @PropertySource(value = {"classpath:queryrest-test.properties"})
}) })
public class DaqWebMvcConfig extends WebMvcConfigurationSupport { public class DaqWebMvcConfig extends WebMvcConfigurerAdapter {
// ensure that properties in dispatcher.properties are loaded first and then overwritten by the // ensure that properties in dispatcher.properties are loaded first and then overwritten by the
// properties in dispatcher-test.properties // properties in dispatcher-test.properties
@Import(value = {LocalQueryTestConfig.class}) @Import(value = {QueryRestConfig.class})
static class InnerConfiguration { static class InnerConfiguration {
} }
// somehow needed to make sure @Import elements will get initialized and afterPropertiesSet will
// get called (ensuring BackendAcces gets initialized according hierarchy)
@Resource
private QueryRestConfig queryRestConfig;
@Resource(name = DomainConfig.BEAN_NAME_BACKEND_ACCESS) public static final String IS_INITIAL_CHANNELS = "initial.channels";
private BackendAccess backendAccess; public static final String BEAN_NAME_READER = "dummyReader";
@Resource
private ApplicationContext context;
@SuppressWarnings("unchecked")
@PostConstruct @PostConstruct
public void afterPropertiesSet() { public void afterPropertiesSet() {
backendAccess.addStreamEventReaderSupplier(Backend.SF_DATABUFFER, () -> cassandraReader()); // init BackendAccesses
backendAccess.addChannelInfoReaderSupplier(Backend.SF_DATABUFFER, () -> cassandraReader()); final List<Backend> backends = context.getBean(DomainConfig.BEAN_NAME_BACKENDS, List.class);
final boolean overload = true;
backendAccess.addStreamEventReaderSupplier(Backend.SF_IMAGEBUFFER, () -> filestorageReader()); for (final Backend backend : backends) {
backendAccess.addChannelInfoReaderSupplier(Backend.SF_IMAGEBUFFER, () -> filestorageReader()); final BackendType backendType = backend.getType();
backendAccess.addDataReaderSupplier(Backend.SF_ARCHIVERAPPLIANCE, () -> archiverApplianceReader()); // backendType.initBean(backend, BEAN_NAME_READER, DataReader.class, overload);
backendType.initBean(backend, BEAN_NAME_READER, StreamEventReader.class, overload, backend);
backendType.initBean(backend, BEAN_NAME_READER, ChannelInfoReader.class, overload, backend);
}
} }
// make sure we use a local QueryProcessor even for distributed calls -> no Hazelcast needs to be started @Bean(name = BEAN_NAME_READER)
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Lazy
public StreamEventReader<ChannelEvent> testReader(final Backend backend) {
return new TestReader(backend);
}
// make sure we use a local QueryProcessor even for distributed calls -> no Hazelcast needs to be
// started
@Bean(name = QueryConfig.BEAN_NAME_QUERY_PROCESSOR_DISTRIBUTED) @Bean(name = QueryConfig.BEAN_NAME_QUERY_PROCESSOR_DISTRIBUTED)
@Lazy @Lazy
public QueryProcessor distributedQueryProcessor() { public QueryProcessor distributedQueryProcessor() {
return new QueryProcessorLocal(); return new QueryProcessorLocal();
} }
@Bean(name = CassandraConfig.BEAN_NAME_CASSANDRA_READER) @Override
@Lazy public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
public CassandraReader cassandraReader() { /**
return new DummyCassandraReader(); * This is necessary so that the message conversion uses the configured object mapper.
} * Otherwise, a separate object mapper is instantiated for Springs message conversion.
*/
@Bean(name = FileStorageConfig.BEAN_NAME_FILESTORAGE_READER) final MappingJackson2HttpMessageConverter converter =
@Lazy context.getBean(DomainConfig.BEAN_NAME_MESSAGE_CONVERTER, MappingJackson2HttpMessageConverter.class);
public StreamEventReader<ChannelEvent> filestorageReader() { converters.add(converter);
return new DummyFilestorageReader(); super.configureMessageConverters(converters);
}
@Bean(name = ArchiverApplianceConfig.BEAN_NAME_ARCHIVER_APPLIANCE_READER)
@Lazy
public DataReader archiverApplianceReader() {
return new DummyArchiverApplianceReader();
} }
} }

View File

@ -7,10 +7,11 @@ import java.awt.Color;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
import javax.annotation.Resource;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
@ -54,22 +55,39 @@ import ch.psi.daq.test.queryrest.AbstractDaqRestTest;
/** /**
* Tests the {@link DaqController} implementation. * Tests the {@link DaqController} implementation.
*/ */
public class JsonQueryRestControllerTest extends AbstractDaqRestTest { public class JsonQueryRestControllerTest extends AbstractDaqRestTest implements ApplicationContextAware {
public static final String TEST_CHANNEL_01 = "testChannel1"; public static final String TEST_CHANNEL_01 = "testChannel1";
public static final String TEST_CHANNEL_02 = "testChannel2"; public static final String TEST_CHANNEL_02 = "testChannel2";
public static final String TEST_CHANNEL_WAVEFORM_01 = "testChannelWaveform1"; public static final String TEST_CHANNEL_WAVEFORM_01 = "testChannelWaveform1";
public static final String[] TEST_CHANNEL_NAMES = new String[] {TEST_CHANNEL_01, TEST_CHANNEL_02}; public static final String[] TEST_CHANNEL_NAMES = new String[] {TEST_CHANNEL_01, TEST_CHANNEL_02};
@Resource(name = DomainConfig.BEAN_NAME_BACKEND_DEFAULT)
private Backend backend; private Backend backend;
private Backend backend2;
private Backend backend3;
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
backend = context.getBean(DomainConfig.BEAN_NAME_BACKEND_DEFAULT, Backend.class);
context = backend.getApplicationContext();
for (final Backend baknd : Backend.getBackends()) {
if (baknd != backend) {
if (backend2 == null) {
backend2 = baknd;
} else if (backend3 == null) {
backend3 = baknd;
break;
}
}
}
}
@After @After
public void tearDown() throws Exception {} public void tearDown() throws Exception {}
@Test @Test
public void testChannelNameQuery() throws Exception { public void testChannelNameQuery() throws Exception {
this.mockMvc this.mockMvc
.perform( .perform(
MockMvcRequestBuilders MockMvcRequestBuilders
@ -79,17 +97,17 @@ public class JsonQueryRestControllerTest extends AbstractDaqRestTest {
.andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$").isArray()) .andExpect(MockMvcResultMatchers.jsonPath("$").isArray())
.andExpect(MockMvcResultMatchers.jsonPath("$[0]").exists()) .andExpect(MockMvcResultMatchers.jsonPath("$[0]").exists())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].backend").value(Backend.SF_DATABUFFER.getKey())) .andExpect(MockMvcResultMatchers.jsonPath("$[0].backend").value(backend.getName()))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channels").isArray()) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channels").isArray())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channels[0]").exists()) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channels[0]").exists())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channels[0]").value("BoolScalar")) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channels[0]").value("BoolScalar"))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channels[1]").exists()) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channels[1]").exists())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channels[1]").value("BoolWaveform")) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channels[1]").value("BoolWaveform"))
.andExpect(MockMvcResultMatchers.jsonPath("$[1]").exists()) .andExpect(MockMvcResultMatchers.jsonPath("$[1]").exists())
.andExpect(MockMvcResultMatchers.jsonPath("$[1].backend").value(Backend.SF_ARCHIVERAPPLIANCE.getKey())) .andExpect(MockMvcResultMatchers.jsonPath("$[1].backend").value(backend2.getName()))
.andExpect(MockMvcResultMatchers.jsonPath("$[1].channels").isArray()) .andExpect(MockMvcResultMatchers.jsonPath("$[1].channels").isArray())
.andExpect(MockMvcResultMatchers.jsonPath("$[2]").exists()) .andExpect(MockMvcResultMatchers.jsonPath("$[2]").exists())
.andExpect(MockMvcResultMatchers.jsonPath("$[2].backend").value(Backend.SF_IMAGEBUFFER.getKey())) .andExpect(MockMvcResultMatchers.jsonPath("$[2].backend").value(backend3.getName()))
.andExpect(MockMvcResultMatchers.jsonPath("$[2].channels").isArray()) .andExpect(MockMvcResultMatchers.jsonPath("$[2].channels").isArray())
.andExpect(MockMvcResultMatchers.jsonPath("$[2].channels[0]").exists()) .andExpect(MockMvcResultMatchers.jsonPath("$[2].channels[0]").exists())
.andExpect(MockMvcResultMatchers.jsonPath("$[2].channels[0]").value("BoolScalar")) .andExpect(MockMvcResultMatchers.jsonPath("$[2].channels[0]").value("BoolScalar"))
@ -108,7 +126,7 @@ public class JsonQueryRestControllerTest extends AbstractDaqRestTest {
.andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$").isArray()) .andExpect(MockMvcResultMatchers.jsonPath("$").isArray())
.andExpect(MockMvcResultMatchers.jsonPath("$[0]").exists()) .andExpect(MockMvcResultMatchers.jsonPath("$[0]").exists())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].backend").value(Backend.SF_DATABUFFER.getKey())) .andExpect(MockMvcResultMatchers.jsonPath("$[0].backend").value(backend.getName()))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channels").isArray()) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channels").isArray())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channels[0]").exists()) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channels[0]").exists())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channels[0]").value("Int32Scalar")) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channels[0]").value("Int32Scalar"))
@ -119,10 +137,10 @@ public class JsonQueryRestControllerTest extends AbstractDaqRestTest {
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channels[3]").exists()) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channels[3]").exists())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channels[3]").value("UInt32Waveform")) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channels[3]").value("UInt32Waveform"))
.andExpect(MockMvcResultMatchers.jsonPath("$[1]").exists()) .andExpect(MockMvcResultMatchers.jsonPath("$[1]").exists())
.andExpect(MockMvcResultMatchers.jsonPath("$[1].backend").value(Backend.SF_ARCHIVERAPPLIANCE.getKey())) .andExpect(MockMvcResultMatchers.jsonPath("$[1].backend").value(backend2.getName()))
.andExpect(MockMvcResultMatchers.jsonPath("$[1].channels").isArray()) .andExpect(MockMvcResultMatchers.jsonPath("$[1].channels").isArray())
.andExpect(MockMvcResultMatchers.jsonPath("$[2]").exists()) .andExpect(MockMvcResultMatchers.jsonPath("$[2]").exists())
.andExpect(MockMvcResultMatchers.jsonPath("$[2].backend").value(Backend.SF_IMAGEBUFFER.getKey())) .andExpect(MockMvcResultMatchers.jsonPath("$[2].backend").value(backend3.getName()))
.andExpect(MockMvcResultMatchers.jsonPath("$[2].channels").isArray()) .andExpect(MockMvcResultMatchers.jsonPath("$[2].channels").isArray())
.andExpect(MockMvcResultMatchers.jsonPath("$[2].channels[0]").exists()) .andExpect(MockMvcResultMatchers.jsonPath("$[2].channels[0]").exists())
.andExpect(MockMvcResultMatchers.jsonPath("$[2].channels[0]").value("Int32Scalar")) .andExpect(MockMvcResultMatchers.jsonPath("$[2].channels[0]").value("Int32Scalar"))
@ -137,7 +155,7 @@ public class JsonQueryRestControllerTest extends AbstractDaqRestTest {
@Test @Test
public void testChannelNameQueryBackendOrder() throws Exception { public void testChannelNameQueryBackendOrder() throws Exception {
ChannelsRequest request = new ChannelsRequest("int64", Ordering.desc, backend); ChannelsRequest request = new ChannelsRequest("int64", Ordering.desc, backend);
String content = mapper.writeValueAsString(request); String content = mapper.writeValueAsString(request);
System.out.println(content); System.out.println(content);
@ -150,7 +168,7 @@ public class JsonQueryRestControllerTest extends AbstractDaqRestTest {
.andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$").isArray()) .andExpect(MockMvcResultMatchers.jsonPath("$").isArray())
.andExpect(MockMvcResultMatchers.jsonPath("$[0]").exists()) .andExpect(MockMvcResultMatchers.jsonPath("$[0]").exists())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].backend").value(backend.getKey())) .andExpect(MockMvcResultMatchers.jsonPath("$[0].backend").value(backend.getName()))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channels").isArray()) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channels").isArray())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channels[0]").exists()) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channels[0]").exists())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channels[0]").value("UInt64Waveform")) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channels[0]").value("UInt64Waveform"))
@ -169,7 +187,7 @@ public class JsonQueryRestControllerTest extends AbstractDaqRestTest {
String content = mapper.writeValueAsString(request); String content = mapper.writeValueAsString(request);
System.out.println(content); System.out.println(content);
this.mockMvc this.mockMvc
.perform(MockMvcRequestBuilders .perform(MockMvcRequestBuilders
.post(DomainConfig.PATH_CHANNELS) .post(DomainConfig.PATH_CHANNELS)
@ -179,13 +197,15 @@ public class JsonQueryRestControllerTest extends AbstractDaqRestTest {
.andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$").isArray()) .andExpect(MockMvcResultMatchers.jsonPath("$").isArray())
.andExpect(MockMvcResultMatchers.jsonPath("$[0]").exists()) .andExpect(MockMvcResultMatchers.jsonPath("$[0]").exists())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].backend").value(Backend.SF_DATABUFFER.getKey())) .andExpect(MockMvcResultMatchers.jsonPath("$[0].backend").value(backend.getName()))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channels").isArray()) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channels").isArray())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channels[24]").exists()) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channels[23]").exists())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channels[25]").doesNotExist()) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channels[24]").doesNotExist())
.andExpect(MockMvcResultMatchers.jsonPath("$[1].backend").value(Backend.SF_ARCHIVERAPPLIANCE.getKey())) .andExpect(MockMvcResultMatchers.jsonPath("$[1].backend").value(backend2.getName()))
.andExpect(MockMvcResultMatchers.jsonPath("$[1].channels").isArray()) .andExpect(MockMvcResultMatchers.jsonPath("$[1].channels").isArray())
.andExpect(MockMvcResultMatchers.jsonPath("$[2].backend").value(Backend.SF_IMAGEBUFFER.getKey())) .andExpect(MockMvcResultMatchers.jsonPath("$[1].channels[0]").exists())
.andExpect(MockMvcResultMatchers.jsonPath("$[1].channels[1]").doesNotExist())
.andExpect(MockMvcResultMatchers.jsonPath("$[2].backend").value(backend3.getName()))
.andExpect(MockMvcResultMatchers.jsonPath("$[2].channels").isArray()) .andExpect(MockMvcResultMatchers.jsonPath("$[2].channels").isArray())
.andExpect(MockMvcResultMatchers.jsonPath("$[2].channels[23]").exists()) .andExpect(MockMvcResultMatchers.jsonPath("$[2].channels[23]").exists())
.andExpect(MockMvcResultMatchers.jsonPath("$[2].channels[24]").doesNotExist()); .andExpect(MockMvcResultMatchers.jsonPath("$[2].channels[24]").doesNotExist());
@ -205,13 +225,13 @@ public class JsonQueryRestControllerTest extends AbstractDaqRestTest {
.andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$").isArray()) .andExpect(MockMvcResultMatchers.jsonPath("$").isArray())
.andExpect(MockMvcResultMatchers.jsonPath("$[0]").exists()) .andExpect(MockMvcResultMatchers.jsonPath("$[0]").exists())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].backend").value(Backend.SF_DATABUFFER.getKey())) .andExpect(MockMvcResultMatchers.jsonPath("$[0].backend").value(backend.getName()))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channels").isArray()) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channels").isArray())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channels[26]").exists()) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channels[24]").exists())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channels[27]").doesNotExist()) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channels[25]").doesNotExist())
.andExpect(MockMvcResultMatchers.jsonPath("$[1].backend").value(Backend.SF_ARCHIVERAPPLIANCE.getKey())) .andExpect(MockMvcResultMatchers.jsonPath("$[1].backend").value(backend2.getName()))
.andExpect(MockMvcResultMatchers.jsonPath("$[1].channels").isArray()) .andExpect(MockMvcResultMatchers.jsonPath("$[1].channels").isArray())
.andExpect(MockMvcResultMatchers.jsonPath("$[2].backend").value(Backend.SF_IMAGEBUFFER.getKey())) .andExpect(MockMvcResultMatchers.jsonPath("$[2].backend").value(backend3.getName()))
.andExpect(MockMvcResultMatchers.jsonPath("$[2].channels").isArray()) .andExpect(MockMvcResultMatchers.jsonPath("$[2].channels").isArray())
.andExpect(MockMvcResultMatchers.jsonPath("$[2].channels[24]").exists()) .andExpect(MockMvcResultMatchers.jsonPath("$[2].channels[24]").exists())
.andExpect(MockMvcResultMatchers.jsonPath("$[2].channels[25]").doesNotExist()); .andExpect(MockMvcResultMatchers.jsonPath("$[2].channels[25]").doesNotExist());
@ -366,7 +386,7 @@ public class JsonQueryRestControllerTest extends AbstractDaqRestTest {
100, 100,
101), 101),
new ChannelName(TEST_CHANNEL_01, backend), new ChannelName(TEST_CHANNEL_01, backend),
new ChannelName(TEST_CHANNEL_02, Backend.SF_ARCHIVERAPPLIANCE)); new ChannelName(TEST_CHANNEL_02, backend2));
String content = mapper.writeValueAsString(request); String content = mapper.writeValueAsString(request);
System.out.println(content); System.out.println(content);
@ -569,7 +589,7 @@ public class JsonQueryRestControllerTest extends AbstractDaqRestTest {
.andExpect(MockMvcResultMatchers.jsonPath("$").isArray()) .andExpect(MockMvcResultMatchers.jsonPath("$").isArray())
.andExpect(MockMvcResultMatchers.jsonPath("$[0]").exists()) .andExpect(MockMvcResultMatchers.jsonPath("$[0]").exists())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channel.name").value(TEST_CHANNEL_01)) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channel.name").value(TEST_CHANNEL_01))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channel.backend").value(backend.getKey())) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channel.backend").value(backend.getName()))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].data").isArray()) .andExpect(MockMvcResultMatchers.jsonPath("$[0].data").isArray())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].data[0].pulseId").value(100)) .andExpect(MockMvcResultMatchers.jsonPath("$[0].data[0].pulseId").value(100))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].data[0].globalSeconds").value( .andExpect(MockMvcResultMatchers.jsonPath("$[0].data[0].globalSeconds").value(
@ -579,7 +599,7 @@ public class JsonQueryRestControllerTest extends AbstractDaqRestTest {
TestTimeUtils.getTimeStr(1, 10000000))) TestTimeUtils.getTimeStr(1, 10000000)))
.andExpect(MockMvcResultMatchers.jsonPath("$[1]").exists()) .andExpect(MockMvcResultMatchers.jsonPath("$[1]").exists())
.andExpect(MockMvcResultMatchers.jsonPath("$[1].channel.name").value(TEST_CHANNEL_02)) .andExpect(MockMvcResultMatchers.jsonPath("$[1].channel.name").value(TEST_CHANNEL_02))
.andExpect(MockMvcResultMatchers.jsonPath("$[1].channel.backend").value(backend.getKey())) .andExpect(MockMvcResultMatchers.jsonPath("$[1].channel.backend").value(backend.getName()))
.andExpect(MockMvcResultMatchers.jsonPath("$[1].data").isArray()) .andExpect(MockMvcResultMatchers.jsonPath("$[1].data").isArray())
.andExpect(MockMvcResultMatchers.jsonPath("$[1].data[0].pulseId").value(100)) .andExpect(MockMvcResultMatchers.jsonPath("$[1].data[0].pulseId").value(100))
.andExpect(MockMvcResultMatchers.jsonPath("$[1].data[0].globalSeconds").value( .andExpect(MockMvcResultMatchers.jsonPath("$[1].data[0].globalSeconds").value(
@ -659,7 +679,7 @@ public class JsonQueryRestControllerTest extends AbstractDaqRestTest {
.andExpect(MockMvcResultMatchers.jsonPath("$[0]").exists()) .andExpect(MockMvcResultMatchers.jsonPath("$[0]").exists())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channel").isMap()) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channel").isMap())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channel.name").value(TEST_CHANNEL_01)) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channel.name").value(TEST_CHANNEL_01))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channel.backend").value(backend.getKey())) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channel.backend").value(backend.getName()))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].data").isArray()) .andExpect(MockMvcResultMatchers.jsonPath("$[0].data").isArray())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].data[0].pulseId").value(100)) .andExpect(MockMvcResultMatchers.jsonPath("$[0].data[0].pulseId").value(100))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].data[0].globalSeconds").value( .andExpect(MockMvcResultMatchers.jsonPath("$[0].data[0].globalSeconds").value(
@ -710,7 +730,7 @@ public class JsonQueryRestControllerTest extends AbstractDaqRestTest {
.andExpect(MockMvcResultMatchers.jsonPath("$[0]").exists()) .andExpect(MockMvcResultMatchers.jsonPath("$[0]").exists())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channel").isMap()) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channel").isMap())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channel.name").value(TEST_CHANNEL_01)) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channel.name").value(TEST_CHANNEL_01))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channel.backend").value(backend.getKey())) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channel.backend").value(backend.getName()))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].data").isArray()) .andExpect(MockMvcResultMatchers.jsonPath("$[0].data").isArray())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].data[0].pulseId").value(100)) .andExpect(MockMvcResultMatchers.jsonPath("$[0].data[0].pulseId").value(100))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].data[0].globalSeconds").value( .andExpect(MockMvcResultMatchers.jsonPath("$[0].data[0].globalSeconds").value(
@ -777,7 +797,7 @@ public class JsonQueryRestControllerTest extends AbstractDaqRestTest {
.andExpect(MockMvcResultMatchers.jsonPath("$[0]").exists()) .andExpect(MockMvcResultMatchers.jsonPath("$[0]").exists())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channel").isMap()) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channel").isMap())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channel.name").value(TEST_CHANNEL_01)) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channel.name").value(TEST_CHANNEL_01))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channel.backend").value(backend.getKey())) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channel.backend").value(backend.getName()))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].data").isArray()) .andExpect(MockMvcResultMatchers.jsonPath("$[0].data").isArray())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].data[0].pulseId").value(1000)) .andExpect(MockMvcResultMatchers.jsonPath("$[0].data[0].pulseId").value(1000))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].data[0].globalSeconds").value( .andExpect(MockMvcResultMatchers.jsonPath("$[0].data[0].globalSeconds").value(
@ -892,7 +912,7 @@ public class JsonQueryRestControllerTest extends AbstractDaqRestTest {
.andExpect(MockMvcResultMatchers.jsonPath("$[0]").exists()) .andExpect(MockMvcResultMatchers.jsonPath("$[0]").exists())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channel").isMap()) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channel").isMap())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channel.name").value(TEST_CHANNEL_WAVEFORM_01)) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channel.name").value(TEST_CHANNEL_WAVEFORM_01))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channel.backend").value(backend.getKey())) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channel.backend").value(backend.getName()))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].data").isArray()) .andExpect(MockMvcResultMatchers.jsonPath("$[0].data").isArray())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].data[0].pulseId").value(100)) .andExpect(MockMvcResultMatchers.jsonPath("$[0].data[0].pulseId").value(100))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].data[0].globalSeconds").value( .andExpect(MockMvcResultMatchers.jsonPath("$[0].data[0].globalSeconds").value(
@ -952,7 +972,7 @@ public class JsonQueryRestControllerTest extends AbstractDaqRestTest {
.andExpect(MockMvcResultMatchers.jsonPath("$[0]").exists()) .andExpect(MockMvcResultMatchers.jsonPath("$[0]").exists())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channel").isMap()) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channel").isMap())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channel.name").value(TEST_CHANNEL_WAVEFORM_01)) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channel.name").value(TEST_CHANNEL_WAVEFORM_01))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].channel.backend").value(backend.getKey())) .andExpect(MockMvcResultMatchers.jsonPath("$[0].channel.backend").value(backend.getName()))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].data").isArray()) .andExpect(MockMvcResultMatchers.jsonPath("$[0].data").isArray())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].data[0].pulseId").value(100)) .andExpect(MockMvcResultMatchers.jsonPath("$[0].data[0].pulseId").value(100))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].data[0].globalSeconds").value( .andExpect(MockMvcResultMatchers.jsonPath("$[0].data[0].globalSeconds").value(

View File

@ -35,10 +35,10 @@ import ch.psi.daq.test.queryrest.AbstractDaqRestTest;
*/ */
public class QueryRestControllerChannelInfoTest extends AbstractDaqRestTest { public class QueryRestControllerChannelInfoTest extends AbstractDaqRestTest {
private ObjectMapper objectMapper = new ObjectMapper();
@Resource(name = DomainConfig.BEAN_NAME_BACKEND_DEFAULT) @Resource(name = DomainConfig.BEAN_NAME_BACKEND_DEFAULT)
private Backend backend; private Backend backend;
private ObjectMapper objectMapper = new ObjectMapper();
@After @After
public void tearDown() throws Exception {} public void tearDown() throws Exception {}
@ -49,7 +49,7 @@ public class QueryRestControllerChannelInfoTest extends AbstractDaqRestTest {
new RequestRangePulseId( new RequestRangePulseId(
100, 100,
101), 101),
backend.getKey() + "1", backend.getKey() + "2"); backend.getName() + "1", backend.getName() + "2");
String content = mapper.writeValueAsString(query); String content = mapper.writeValueAsString(query);
System.out.println(content); System.out.println(content);
@ -69,19 +69,19 @@ public class QueryRestControllerChannelInfoTest extends AbstractDaqRestTest {
List<? extends ChannelInfos> infosList = objectMapper.readValue(response, ChannelInfosList.class); List<? extends ChannelInfos> infosList = objectMapper.readValue(response, ChannelInfosList.class);
assertEquals(2, infosList.size()); assertEquals(2, infosList.size());
ChannelInfos cInfos = infosList.get(0); ChannelInfos cInfos = infosList.get(0);
assertEquals(backend.getKey() + "1", cInfos.getChannel().getName()); assertEquals(backend.getName() + "1", cInfos.getChannel().getName());
assertEquals(backend, cInfos.getChannel().getBackend()); assertEquals(backend, cInfos.getChannel().getBackend());
List<ChannelInfo> infos = cInfos.getChannelInfos().collect(Collectors.toList()); List<ChannelInfo> infos = cInfos.getChannelInfos().collect(Collectors.toList());
assertEquals(2, infos.size()); assertEquals(2, infos.size());
ChannelInfo info = infos.get(0); ChannelInfo info = infos.get(0);
assertEquals(backend.getKey() + "1", info.getChannel()); assertEquals(backend.getName() + "1", info.getChannel());
assertEquals(backend, info.getBackend()); assertEquals(backend, info.getBackend());
assertEquals(TimeUtils.getTimeFromMillis(query.getRange().getStartPulseId() * 10, 0), info.getGlobalTime()); assertEquals(TimeUtils.getTimeFromMillis(query.getRange().getStartPulseId() * 10, 0), info.getGlobalTime());
assertEquals(query.getRange().getStartPulseId(), info.getPulseId()); assertEquals(query.getRange().getStartPulseId(), info.getPulseId());
assertArrayEquals(new int[] {1}, info.getShape()); assertArrayEquals(new int[] {1}, info.getShape());
assertEquals(Type.Int32.getKey(), info.getType()); assertEquals(Type.Int32.getKey(), info.getType());
info = infos.get(1); info = infos.get(1);
assertEquals(backend.getKey() + "1", info.getChannel()); assertEquals(backend.getName() + "1", info.getChannel());
assertEquals(backend, info.getBackend()); assertEquals(backend, info.getBackend());
assertEquals(TimeUtils.getTimeFromMillis(query.getRange().getEndPulseId() * 10, 0), info.getGlobalTime()); assertEquals(TimeUtils.getTimeFromMillis(query.getRange().getEndPulseId() * 10, 0), info.getGlobalTime());
assertEquals(query.getRange().getEndPulseId(), info.getPulseId()); assertEquals(query.getRange().getEndPulseId(), info.getPulseId());
@ -89,19 +89,19 @@ public class QueryRestControllerChannelInfoTest extends AbstractDaqRestTest {
assertEquals(Type.Int32.getKey(), info.getType()); assertEquals(Type.Int32.getKey(), info.getType());
cInfos = infosList.get(1); cInfos = infosList.get(1);
assertEquals(backend.getKey() + "2", cInfos.getChannel().getName()); assertEquals(backend.getName() + "2", cInfos.getChannel().getName());
assertEquals(backend, cInfos.getChannel().getBackend()); assertEquals(backend, cInfos.getChannel().getBackend());
infos = cInfos.getChannelInfos().collect(Collectors.toList()); infos = cInfos.getChannelInfos().collect(Collectors.toList());
assertEquals(2, infos.size()); assertEquals(2, infos.size());
info = infos.get(0); info = infos.get(0);
assertEquals(backend.getKey() + "2", info.getChannel()); assertEquals(backend.getName() + "2", info.getChannel());
assertEquals(backend, info.getBackend()); assertEquals(backend, info.getBackend());
assertEquals(TimeUtils.getTimeFromMillis(query.getRange().getStartPulseId() * 10, 0), info.getGlobalTime()); assertEquals(TimeUtils.getTimeFromMillis(query.getRange().getStartPulseId() * 10, 0), info.getGlobalTime());
assertEquals(query.getRange().getStartPulseId(), info.getPulseId()); assertEquals(query.getRange().getStartPulseId(), info.getPulseId());
assertArrayEquals(new int[] {1}, info.getShape()); assertArrayEquals(new int[] {1}, info.getShape());
assertEquals(Type.Int32.getKey(), info.getType()); assertEquals(Type.Int32.getKey(), info.getType());
info = infos.get(1); info = infos.get(1);
assertEquals(backend.getKey() + "2", info.getChannel()); assertEquals(backend.getName() + "2", info.getChannel());
assertEquals(backend, info.getBackend()); assertEquals(backend, info.getBackend());
assertEquals(TimeUtils.getTimeFromMillis(query.getRange().getEndPulseId() * 10, 0), info.getGlobalTime()); assertEquals(TimeUtils.getTimeFromMillis(query.getRange().getEndPulseId() * 10, 0), info.getGlobalTime());
assertEquals(query.getRange().getEndPulseId(), info.getPulseId()); assertEquals(query.getRange().getEndPulseId(), info.getPulseId());
@ -115,7 +115,7 @@ public class QueryRestControllerChannelInfoTest extends AbstractDaqRestTest {
new RequestRangePulseId( new RequestRangePulseId(
100, 100,
101), 101),
backend.getKey() + "1", backend.getKey() + "2"); backend.getName() + "1", backend.getName() + "2");
query.setOrdering(Ordering.desc); query.setOrdering(Ordering.desc);
String content = mapper.writeValueAsString(query); String content = mapper.writeValueAsString(query);
@ -136,19 +136,19 @@ public class QueryRestControllerChannelInfoTest extends AbstractDaqRestTest {
List<? extends ChannelInfos> infosList = objectMapper.readValue(response, ChannelInfosList.class); List<? extends ChannelInfos> infosList = objectMapper.readValue(response, ChannelInfosList.class);
assertEquals(2, infosList.size()); assertEquals(2, infosList.size());
ChannelInfos cInfos = infosList.get(0); ChannelInfos cInfos = infosList.get(0);
assertEquals(backend.getKey() + "1", cInfos.getChannel().getName()); assertEquals(backend.getName() + "1", cInfos.getChannel().getName());
assertEquals(backend, cInfos.getChannel().getBackend()); assertEquals(backend, cInfos.getChannel().getBackend());
List<ChannelInfo> infos = cInfos.getChannelInfos().collect(Collectors.toList()); List<ChannelInfo> infos = cInfos.getChannelInfos().collect(Collectors.toList());
assertEquals(2, infos.size()); assertEquals(2, infos.size());
ChannelInfo info = infos.get(0); ChannelInfo info = infos.get(0);
assertEquals(backend.getKey() + "1", info.getChannel()); assertEquals(backend.getName() + "1", info.getChannel());
assertEquals(backend, info.getBackend()); assertEquals(backend, info.getBackend());
assertEquals(TimeUtils.getTimeFromMillis(query.getRange().getEndPulseId() * 10, 0), info.getGlobalTime()); assertEquals(TimeUtils.getTimeFromMillis(query.getRange().getEndPulseId() * 10, 0), info.getGlobalTime());
assertEquals(query.getRange().getEndPulseId(), info.getPulseId()); assertEquals(query.getRange().getEndPulseId(), info.getPulseId());
assertArrayEquals(new int[] {1}, info.getShape()); assertArrayEquals(new int[] {1}, info.getShape());
assertEquals(Type.Int32.getKey(), info.getType()); assertEquals(Type.Int32.getKey(), info.getType());
info = infos.get(1); info = infos.get(1);
assertEquals(backend.getKey() + "1", info.getChannel()); assertEquals(backend.getName() + "1", info.getChannel());
assertEquals(backend, info.getBackend()); assertEquals(backend, info.getBackend());
assertEquals(TimeUtils.getTimeFromMillis(query.getRange().getStartPulseId() * 10, 0), info.getGlobalTime()); assertEquals(TimeUtils.getTimeFromMillis(query.getRange().getStartPulseId() * 10, 0), info.getGlobalTime());
assertEquals(query.getRange().getStartPulseId(), info.getPulseId()); assertEquals(query.getRange().getStartPulseId(), info.getPulseId());
@ -156,19 +156,19 @@ public class QueryRestControllerChannelInfoTest extends AbstractDaqRestTest {
assertEquals(Type.Int32.getKey(), info.getType()); assertEquals(Type.Int32.getKey(), info.getType());
cInfos = infosList.get(1); cInfos = infosList.get(1);
assertEquals(backend.getKey() + "2", cInfos.getChannel().getName()); assertEquals(backend.getName() + "2", cInfos.getChannel().getName());
assertEquals(backend, cInfos.getChannel().getBackend()); assertEquals(backend, cInfos.getChannel().getBackend());
infos = cInfos.getChannelInfos().collect(Collectors.toList()); infos = cInfos.getChannelInfos().collect(Collectors.toList());
assertEquals(2, infos.size()); assertEquals(2, infos.size());
info = infos.get(0); info = infos.get(0);
assertEquals(backend.getKey() + "2", info.getChannel()); assertEquals(backend.getName() + "2", info.getChannel());
assertEquals(backend, info.getBackend()); assertEquals(backend, info.getBackend());
assertEquals(TimeUtils.getTimeFromMillis(query.getRange().getEndPulseId() * 10, 0), info.getGlobalTime()); assertEquals(TimeUtils.getTimeFromMillis(query.getRange().getEndPulseId() * 10, 0), info.getGlobalTime());
assertEquals(query.getRange().getEndPulseId(), info.getPulseId()); assertEquals(query.getRange().getEndPulseId(), info.getPulseId());
assertArrayEquals(new int[] {1}, info.getShape()); assertArrayEquals(new int[] {1}, info.getShape());
assertEquals(Type.Int32.getKey(), info.getType()); assertEquals(Type.Int32.getKey(), info.getType());
info = infos.get(1); info = infos.get(1);
assertEquals(backend.getKey() + "2", info.getChannel()); assertEquals(backend.getName() + "2", info.getChannel());
assertEquals(backend, info.getBackend()); assertEquals(backend, info.getBackend());
assertEquals(TimeUtils.getTimeFromMillis(query.getRange().getStartPulseId() * 10, 0), info.getGlobalTime()); assertEquals(TimeUtils.getTimeFromMillis(query.getRange().getStartPulseId() * 10, 0), info.getGlobalTime());
assertEquals(query.getRange().getStartPulseId(), info.getPulseId()); assertEquals(query.getRange().getStartPulseId(), info.getPulseId());

View File

@ -1,430 +0,0 @@
package ch.psi.daq.test.queryrest.query;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicLong;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;
import javax.annotation.Resource;
import org.apache.commons.lang3.ArrayUtils;
import com.google.common.collect.Lists;
import ch.psi.bsread.message.Type;
import ch.psi.daq.common.ordering.Ordering;
import ch.psi.daq.common.time.TimeUtils;
import ch.psi.daq.domain.DataEvent;
import ch.psi.daq.domain.FieldNames;
import ch.psi.daq.domain.backend.Backend;
import ch.psi.daq.domain.config.DomainConfig;
import ch.psi.daq.domain.events.ChannelConfiguration;
import ch.psi.daq.domain.events.ChannelEvent;
import ch.psi.daq.domain.events.MetaPulseId;
import ch.psi.daq.domain.events.impl.ChannelConfigurationImpl;
import ch.psi.daq.domain.events.impl.ChannelEventImpl;
import ch.psi.daq.domain.events.impl.MetaPulseIdImpl;
import ch.psi.daq.domain.json.channels.info.ChannelInfo;
import ch.psi.daq.domain.json.channels.info.ChannelInfoImpl;
import ch.psi.daq.domain.query.event.EventQuery;
import ch.psi.daq.domain.query.event.StreamEventQuery;
import ch.psi.daq.domain.query.range.PulseIdRangeQuery;
import ch.psi.daq.domain.query.range.TimeRangeQuery;
import ch.psi.daq.domain.query.transform.ValueTransformation;
import ch.psi.daq.domain.reader.MetaStreamEventQuery;
import ch.psi.daq.domain.reader.StreamEventReader;
import ch.psi.daq.domain.test.backend.TestBackendAccess;
import ch.psi.daq.domain.test.gen.TestDataGen;
import ch.psi.daq.domain.utils.PropertiesUtils;
import ch.psi.data.stream.converters.impl.UShortByteValueConverter;
public abstract class AbstractStreamEventReader implements StreamEventReader<ChannelEvent> {
private static final Random random = new Random(0);
private static final int KEYSPACE = 1;
private List<String> channels;
private AtomicLong channelNameCallCounter = new AtomicLong();
private TestDataGen dataGen;
private Backend backend;
private String testChannelName;
@Resource(name = DomainConfig.BEAN_NAME_TEST_BACKEND_ACCESS)
private TestBackendAccess testBackendAccess;
public AbstractStreamEventReader() {
this.channels = Lists.newArrayList(
"BoolScalar",
"BoolWaveform",
"Int8Scalar",
"Int8Waveform",
"UInt8Scalar",
"UInt8Waveform",
"Int16Scalar",
"Int16Waveform",
"UInt16Scalar",
"UInt16Waveform",
"Int32Scalar",
"Int32Waveform",
"UInt32Scalar",
"UInt32Waveform",
"Int64Scalar",
"Int64Waveform",
"UInt64Scalar",
"UInt64Waveform",
"Float32Scalar",
"Float32Waveform",
"Float64Scalar",
"Float64Waveform",
"StringScalar");
}
protected void init(Backend backend) {
this.backend = backend;
this.dataGen = testBackendAccess.getTestDataGen(backend);
this.testChannelName = backend.getKey() + "_TestChannel_";
}
@Override
public Backend getBackend() {
return backend;
}
/**
* @{inheritDoc
*/
@Override
public Stream<String> getChannelStream(String regex) {
channels.add(testChannelName + channelNameCallCounter.incrementAndGet());
Stream<String> channelStream = channels.stream();
if (regex != null) {
Pattern pattern = Pattern.compile(regex.toLowerCase());
channelStream = channelStream.filter(channel -> pattern.matcher(channel.toLowerCase()).find());
}
return channelStream;
}
// @Override
// public Stream<ChannelEvent> getEventStream(PulseIdRangeQuery query) {
// return getDummyEventStream(query.getChannel(), query.getStartPulseId(), query.getEndPulseId(),
// query.getEventColumns())
// .filter(query.getFilterOrDefault(EventQuery.NO_OP_FILTER));
// }
@Override
public Stream<ChannelEvent> getEventStream(TimeRangeQuery query) {
return getDummyEventStream(query.getChannel(), getBackend(), query.getStartMillis() / 10,
query.getEndMillis() / 10)
.filter(query.getFilterOrDefault(EventQuery.NO_OP_FILTER))
// misuse value modification
.peek(query.getValueTransformationOrDefault(ValueTransformation.NO_OP));
}
public Stream<ChannelEvent> getEventStream(EventQuery eventQuery, Stream<? extends StreamEventQuery> queryProviders) {
Stream<ChannelEvent> result = queryProviders.map(ceq -> {
if (ceq instanceof MetaStreamEventQuery) {
return getEvent((MetaStreamEventQuery<ChannelEvent>) ceq);
} else {
throw new UnsupportedOperationException("This is not yet implemented!");
}
});
return result;
}
public static Stream<ChannelEvent> getDummyEventStream(String channelParam, Backend backend, long startIndex,
long endIndex,
String... columns) {
String channelLower = channelParam.toLowerCase();
String channel =
(columns == null || columns.length == 0 || ArrayUtils.contains(columns, FieldNames.FIELD_CHANNEL)) ? channelParam
: null;
LongStream millisRangeStream = null;
if (channelParam.contains("[") && channelParam.contains("]")) {
millisRangeStream =
Arrays.stream(
channelParam.substring(
channelParam.indexOf("[") + 1,
channelParam.indexOf("]"))
.split(",")
)
.mapToLong(str -> Long.parseLong(str.trim()) * 10);
} else if (channelParam.contains("{") && channelParam.contains("}")) {
millisRangeStream =
Arrays.stream(
channelParam.substring(
channelParam.indexOf("{") + 1,
channelParam.indexOf("}"))
.split(",")
)
.mapToLong(str -> Long.parseLong(str));
} else {
millisRangeStream = LongStream.rangeClosed(startIndex * 10, endIndex * 10)
.filter(val -> val % 10 == 0);
}
Stream<ChannelEvent> eventStream =
millisRangeStream.mapToObj(
millis -> {
long i = millis / 10;
BigDecimal iocTime =
(columns == null || columns.length == 0 || ArrayUtils.contains(columns,
FieldNames.FIELD_IOC_TIME)) ? TimeUtils.getTimeFromMillis(millis, 0)
: PropertiesUtils.DEFAULT_VALUE_DECIMAL;
BigDecimal globalTime =
(columns == null || columns.length == 0 || ArrayUtils.contains(columns,
FieldNames.FIELD_GLOBAL_TIME)) ? TimeUtils.getTimeFromMillis(millis, 0)
: PropertiesUtils.DEFAULT_VALUE_DECIMAL;
long pulseId =
(columns == null || columns.length == 0 || ArrayUtils.contains(columns,
FieldNames.FIELD_PULSE_ID)) ? i : PropertiesUtils.DEFAULT_VALUE_BIGINT_PRIMITIVE;
if (channelLower.contains("waveform")) {
long[] value = random.longs(8).toArray();
value[0] = i;
value[1] = i;
return new ChannelEventImpl(
channel,
backend,
iocTime,
pulseId,
globalTime,
KEYSPACE,
value
);
} else if (channelLower.contains("image")) {
int x = 80;
int y = 40;
int[] shape = new int[] {x, y};
short[] value = new short[x * y];
IntStream.range(0, value.length)
.forEach(index -> value[index] = UShortByteValueConverter.convertVal(random.nextInt()));
value[0] = UShortByteValueConverter.convertVal((int) i);
value[1] = UShortByteValueConverter.convertVal((int) i);
return new ChannelEventImpl(
channel,
backend,
iocTime,
pulseId,
globalTime,
KEYSPACE,
value,
shape
);
} else {
return new ChannelEventImpl(
channel,
backend,
iocTime,
pulseId,
globalTime,
KEYSPACE,
i
);
}
});
return eventStream;
}
private List<? extends DataEvent> getDummyEvents(String channel, long startIndex, long endIndex, String... columns) {
return getDummyEventStream(channel, getBackend(), startIndex, endIndex, columns).collect(Collectors.toList());
}
/**
* @{inheritDoc
*/
@Override
public List<String> getChannels() {
return Lists.newArrayList(channels);
}
/**
* @{inheritDoc
*/
@Override
public List<String> getChannels(String regex) {
return Lists.newArrayList(channels).stream().filter(s -> {
return s.contains(regex);
}).collect(Collectors.toList());
}
/**
* @{inheritDoc
*/
@Override
public ChannelEvent getEvent(MetaStreamEventQuery<ChannelEvent> queryInfo, String... columns) {
if (queryInfo.getPulseId() > 0) {
return (ChannelEvent) getDummyEvents(queryInfo.getChannel(), queryInfo.getPulseId(), queryInfo.getPulseId(),
columns)
.get(0);
}
return (ChannelEvent) getDummyEvents(queryInfo.getChannel(), queryInfo.getGlobalMillis() / 10,
queryInfo.getGlobalMillis() / 10).get(0);
}
/**
* @{inheritDoc
*/
@Override
public CompletableFuture<ChannelEvent> getEventAsync(MetaStreamEventQuery<ChannelEvent> queryInfo, String... columns) {
// implement when needed
throw new UnsupportedOperationException();
}
// /**
// * @{inheritDoc
// */
// @Override
// public Stream<? extends StreamEventQuery> getStreamEventQueryStream(PulseIdRangeQuery query) {
//
// return dataGen.generateMetaPulseId(
// query.getStartPulseId(),
// (query.getEndPulseId() - query.getStartPulseId() + 1),
// i -> i * 10,
// i -> 0,
// i -> i,
// query.getChannel())
// .stream()
// .map(metaPulse -> {
// metaPulse.setKeyspace(KEYSPACE);
// return metaPulse;
// });
// }
public Stream<? extends StreamEventQuery> getStreamEventQueryStream(TimeRangeQuery query) {
return dataGen.generateMetaTime(
KEYSPACE,
3600,
query.getStartMillis() / 10,
((query.getEndMillis() - query.getStartMillis()) / 10 + 1),
i -> i * 10,
i -> 0,
i -> i,
getBackend(),
query.getChannel()).stream();
}
// /**
// * @{inheritDoc
// */
// @Override
// public Stream<MetaPulseId> getMetaStream(PulseIdRangeQuery query) {
//
// return getStreamEventQueryStream(query).map(r -> {
// return (MetaPulseId) r;
// });
//
// }
/**
* @{inheritDoc
*/
@Override
public Stream<? extends MetaStreamEventQuery<ChannelEvent>> getMetaStream(TimeRangeQuery query) {
return getStreamEventQueryStream(query).map(r -> {
return (MetaStreamEventQuery<ChannelEvent>) r;
});
}
@Override
public Stream<ChannelEvent> getEventStream(Stream<? extends MetaStreamEventQuery<ChannelEvent>> queryInfos,
String... columns) {
return getEventStream(null, queryInfos);
}
@Override
public Stream<ChannelConfiguration> getChannelConfiguration(TimeRangeQuery query) {
List<ChannelConfiguration> configs = new ArrayList<>();
BigDecimal time = query.getStartTime();
configs.add(
new ChannelConfigurationImpl(
query.getChannel(),
time,
TimeUtils.getMillis(time) / 10,
0,
Type.Int32.getKey(),
new int[] {1},
false,
ChannelConfiguration.DEFAULT_LOCAL_WRITE,
ChannelConfiguration.DEFAULT_BIN_SIZE_IN_MILLIS,
ChannelConfiguration.SPLIT_COUNT,
ChannelConfiguration.DEFAULT_SOURCE,
ChannelConfiguration.DEFAULT_MODULO,
ChannelConfiguration.DEFAULT_OFFSET,
Backend.SF_DATABUFFER));
if (query.getEndMillis() > query.getStartMillis()) {
time = query.getEndTime();
configs.add(
new ChannelConfigurationImpl(
query.getChannel(),
time,
TimeUtils.getMillis(time) / 10,
1,
Type.Int32.getKey(),
new int[] {1},
false,
ChannelConfiguration.DEFAULT_LOCAL_WRITE,
ChannelConfiguration.DEFAULT_BIN_SIZE_IN_MILLIS,
ChannelConfiguration.SPLIT_COUNT,
ChannelConfiguration.DEFAULT_SOURCE,
ChannelConfiguration.DEFAULT_MODULO,
ChannelConfiguration.DEFAULT_OFFSET,
Backend.SF_DATABUFFER));
}
if (Ordering.desc.equals(query.getOrdering())) {
Collections.reverse(configs);
}
return configs.stream();
}
@Override
public TimeRangeQuery getTimeRangeQuery(PulseIdRangeQuery query) {
return new TimeRangeQuery(
TimeUtils.getTimeFromMillis(query.getStartPulseId() * 10, 0),
TimeUtils.getTimeFromMillis(query.getEndPulseId() * 10, 0),
query);
}
@Override
public Stream<? extends ChannelInfo> getChannelInfoStream(TimeRangeQuery query) {
return getChannelConfiguration(query)
.map(channelConfiguration -> new ChannelInfoImpl(channelConfiguration));
}
@Override
public void truncateCache() {}
@Override
public CompletableFuture<MetaPulseId> getStartMetaPulseIdAsync(PulseIdRangeQuery query) {
return CompletableFuture.completedFuture(new MetaPulseIdImpl(query.getChannel(), getBackend(), query
.getStartPulseId(),
TimeUtils.getTimeFromMillis(query.getStartPulseId() * 10, 0)));
}
@Override
public CompletableFuture<MetaPulseId> getEndMetaPulseIdAsync(PulseIdRangeQuery query) {
return CompletableFuture.completedFuture(new MetaPulseIdImpl(query.getChannel(), getBackend(), query
.getEndPulseId(),
TimeUtils.getTimeFromMillis(query.getEndPulseId() * 10, 0)));
}
}

View File

@ -1,74 +0,0 @@
package ch.psi.daq.test.queryrest.query;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import com.google.common.collect.Lists;
import ch.psi.daq.common.time.TimeUtils;
import ch.psi.daq.domain.StreamEvent;
import ch.psi.daq.domain.backend.Backend;
import ch.psi.daq.domain.query.event.EventQuery;
import ch.psi.daq.domain.query.range.PulseIdRangeQuery;
import ch.psi.daq.domain.query.range.TimeRangeQuery;
import ch.psi.daq.domain.query.transform.ValueTransformation;
import ch.psi.daq.domain.reader.DataReader;
public class DummyArchiverApplianceReader implements DataReader {
public static final String TEST_CHANNEL = Backend.SF_ARCHIVERAPPLIANCE.getKey() + "_TestChannel_";
public static final String TEST_CHANNEL_1 = Backend.SF_ARCHIVERAPPLIANCE.getKey() + "_Channel_1";
public static final String TEST_CHANNEL_2 = Backend.SF_ARCHIVERAPPLIANCE.getKey() + "_Channel_2";
private List<String> channels = Lists.newArrayList(TEST_CHANNEL_1, TEST_CHANNEL_2);
private AtomicLong channelNameCallCounter = new AtomicLong();
@Override
public Backend getBackend() {
return Backend.SF_ARCHIVERAPPLIANCE;
}
@Override
public Stream<String> getChannelStream(String regex) {
channels.add(TEST_CHANNEL + channelNameCallCounter.incrementAndGet());
Stream<String> channelStream = channels.stream();
if (regex != null) {
Pattern pattern = Pattern.compile(regex);
channelStream = channelStream.filter(channel -> pattern.matcher(channel).find());
}
return channelStream;
}
// @Override
// public Stream<? extends StreamEvent> getEventStream(PulseIdRangeQuery query) {
// return DummyCassandraReader.getDummyEventStream(query.getChannel(), query.getStartPulseId(),
// query.getEndPulseId(),
// query.getEventColumns())
// .filter(query.getFilterOrDefault(EventQuery.NO_OP_FILTER));
// }
@Override
public Stream<? extends StreamEvent> getEventStream(TimeRangeQuery query) {
return DummyCassandraReader.getDummyEventStream(query.getChannel(), getBackend(), query.getStartMillis() / 10,
query.getEndMillis() / 10)
.filter(query.getFilterOrDefault(EventQuery.NO_OP_FILTER))
// misuse value modification
.peek(query.getValueTransformationOrDefault(ValueTransformation.NO_OP));
}
@Override
public TimeRangeQuery getTimeRangeQuery(PulseIdRangeQuery query) {
return new TimeRangeQuery(
TimeUtils.getTimeFromMillis(query.getStartPulseId() * 10, 0),
TimeUtils.getTimeFromMillis(query.getEndPulseId() * 10, 0),
query);
}
@Override
public void truncateCache() {}
}

View File

@ -1,76 +0,0 @@
package ch.psi.daq.test.queryrest.query;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import javax.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.psi.daq.cassandra.reader.CassandraReader;
import ch.psi.daq.common.time.TimeUtils;
import ch.psi.daq.domain.backend.Backend;
import ch.psi.daq.domain.events.ChannelConfiguration;
import ch.psi.daq.domain.events.MetaPulseId;
import ch.psi.daq.domain.events.impl.MetaPulseIdImpl;
import ch.psi.daq.domain.query.range.PulseIdRangeQuery;
import ch.psi.daq.domain.query.range.TimeRangeQuery;
public class DummyCassandraReader extends AbstractStreamEventReader implements CassandraReader {
private static final Logger LOGGER = LoggerFactory.getLogger(DummyCassandraReader.class);
public DummyCassandraReader() {
}
@PostConstruct
public void afterPropertiesSet() {
init(Backend.SF_DATABUFFER);
}
@Override
public ChannelConfiguration getChannelConfigurationBefore(TimeRangeQuery query) {
try {
return getChannelConfigurationBeforeAsync(query)
.get(30, TimeUnit.SECONDS);
} catch (Throwable t) {
LOGGER.error("Could not read ChannelConfiguration from DB.", t);
return null;
}
}
@Override
public CompletableFuture<ChannelConfiguration> getChannelConfigurationBeforeAsync(TimeRangeQuery query) {
// implement when needed
throw new UnsupportedOperationException();
}
@Override
public ChannelConfiguration getChannelConfigurationAfter(TimeRangeQuery query) {
try {
return getChannelConfigurationAfterAsync(query)
.get(30, TimeUnit.SECONDS);
} catch (Throwable t) {
LOGGER.error("Could not read ChannelConfiguration from DB.", t);
return null;
}
}
@Override
public CompletableFuture<ChannelConfiguration> getChannelConfigurationAfterAsync(TimeRangeQuery query) {
// implement when needed
throw new UnsupportedOperationException();
}
@Override
public CompletableFuture<MetaPulseId> getStartMetaPulseIdAsync(PulseIdRangeQuery query) {
return CompletableFuture.completedFuture(new MetaPulseIdImpl(query.getChannel(), getBackend(), query.getStartPulseId(),
TimeUtils.getTimeFromMillis(query.getStartPulseId() * 10, 0)));
}
@Override
public CompletableFuture<MetaPulseId> getEndMetaPulseIdAsync(PulseIdRangeQuery query) {
return CompletableFuture.completedFuture(new MetaPulseIdImpl(query.getChannel(), getBackend(), query.getEndPulseId(),
TimeUtils.getTimeFromMillis(query.getEndPulseId() * 10, 0)));
}
}

View File

@ -1,16 +0,0 @@
package ch.psi.daq.test.queryrest.query;
import javax.annotation.PostConstruct;
import ch.psi.daq.domain.backend.Backend;
public class DummyFilestorageReader extends AbstractStreamEventReader {
public DummyFilestorageReader() {
}
@PostConstruct
public void afterPropertiesSet() {
init(Backend.SF_IMAGEBUFFER);
}
}

View File

@ -7,6 +7,7 @@ import static org.junit.Assert.assertTrue;
import java.io.IOException; import java.io.IOException;
import javax.annotation.PostConstruct;
import javax.annotation.Resource; import javax.annotation.Resource;
import org.junit.Test; import org.junit.Test;
@ -15,6 +16,8 @@ import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import ch.psi.daq.domain.backend.Backend;
import ch.psi.daq.domain.config.DomainConfig;
import ch.psi.daq.domain.query.DAQQuery; import ch.psi.daq.domain.query.DAQQuery;
import ch.psi.daq.domain.query.operation.Compression; import ch.psi.daq.domain.query.operation.Compression;
import ch.psi.daq.domain.query.response.Response; import ch.psi.daq.domain.query.response.Response;
@ -25,9 +28,15 @@ import ch.psi.daq.test.queryrest.AbstractDaqRestTest;
public class ResponseQueryTest extends AbstractDaqRestTest{ public class ResponseQueryTest extends AbstractDaqRestTest{
@Resource @Resource(name = DomainConfig.BEAN_NAME_BACKEND_DEFAULT)
private Backend backend;
private ObjectMapper mapper; private ObjectMapper mapper;
@PostConstruct
public void afterPropertiesSet() {
mapper = backend.getApplicationContext().getBean(DomainConfig.BEAN_NAME_OBJECT_MAPPER, ObjectMapper.class);
}
@Test @Test
public void test_JSON_01() throws JsonParseException, JsonMappingException, IOException { public void test_JSON_01() throws JsonParseException, JsonMappingException, IOException {
Response respose = new CSVHTTPResponse(); Response respose = new CSVHTTPResponse();

View File

@ -1,9 +1,19 @@
backend.default=sf-imagebuffer backend.types=ch.psi.daq.filestorage.backend.FilestorageBackendType,ch.psi.daq.cassandra.backend.CassandraBackendType,ch.psi.daq.archiverappliance.backend.ArchiverApplianceBackendType
backend.default=queryrest-1
backends.active=["queryrest-1","queryrest-2","queryrest-3"]
backends=[{"id":255,"name":"queryrest-1","dbKey":255,"type":{"name":"filestorage"},"properties":["classpath:test-overload.properties"],"packages":["ch.psi.daq.filestorage.config"]},{"id":254,"name":"queryrest-2","dbKey":254,"type":{"name":"cassandra"},"properties":["classpath:test-overload2.properties"],"packages":["ch.psi.daq.cassandra.config"]},{"id":253,"name":"queryrest-3","dbKey":253,"type":{"name":"archiverappliance"},"properties":["classpath:test-overload3.properties"],"packages":["ch.psi.daq.archiverappliance.config"]}]
query.hazelcast.node=true
# the base for the keyspaces
domain.keyspace.base=daq_query_test
channelname.cache.reload.period=-1
# defines if the MainHeader should be checked and dropped (false -> check but not dropped)
dispatcher.validate.mainheader=false
dispatcher.validate.mainheader.log=false
query.min.time=1970-01-01T00:00:00.000000000+00:00 query.min.time=1970-01-01T00:00:00.000000000+00:00
# enable/disable validation
filestorage.pulseidtime.store.validate.range=false # overload test
filestorage.pulseidtime.cache.commit.period=0 timeout.delete=41
timeout.query=11
timeout.shutdown=31
filestorage.root.dir=${user.home}/daq/query_test

View File

@ -0,0 +1,19 @@
backend.init=lazy
filestorage.root.dir=${user.home}/daq/queryrest1
filestorage.hazelcast.node=true
filestorage.netty.node.writer=true
filestorage.netty.node.query=true
# enables/disables ttl based compaction
filestorage.compaction.ttl.enable=false
# enable/disable validation
filestorage.pulseidtime.store.validate.range=false
filestorage.pulseidtime.cache.commit.period=0
# overload test
timeout.delete=42
timeout.shutdown=32
test.initial.channels=true

View File

@ -0,0 +1,11 @@
backend.init=lazy
cassandra.keyspace.config=[{"number":0,"replication":1,"note":"meta"},{"number":1,"replication":1,"note":"config_backup"},{"number":2,"replication":1,"note":"default_scalar"},{"number":3,"replication":1,"note":"default_waveform"},{"number":4,"replication":1,"note":"default_image"}]
# run a local cluster for the tests
cassandra.local.cluster=true
# overload test
timeout.delete=43
test.initial.channels=false

View File

@ -0,0 +1,6 @@
backend.init=lazy
# overload test
timeout.delete=50
test.initial.channels=true