ATEST-249:

- provide a channel search method with with the user can define all or
part of the channel name
This commit is contained in:
Zellweger Christof Ralf
2015-10-20 11:49:12 +02:00
parent 9030776f61
commit c894bb5939
3 changed files with 137 additions and 121 deletions

View File

@ -17,6 +17,7 @@ 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;
import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
@ -25,6 +26,7 @@ import org.springframework.web.bind.annotation.RestController;
import ch.psi.daq.cassandra.request.validate.RequestProviderValidator; import ch.psi.daq.cassandra.request.validate.RequestProviderValidator;
import ch.psi.daq.cassandra.util.test.CassandraDataGen; import ch.psi.daq.cassandra.util.test.CassandraDataGen;
import ch.psi.daq.common.json.deserialize.AttributeBasedDeserializer;
import ch.psi.daq.domain.DataEvent; import ch.psi.daq.domain.DataEvent;
import ch.psi.daq.query.analyzer.QueryAnalyzer; import ch.psi.daq.query.analyzer.QueryAnalyzer;
import ch.psi.daq.query.model.Aggregation; import ch.psi.daq.query.model.Aggregation;
@ -67,7 +69,6 @@ public class QueryRestController {
@Resource(name = QueryRestConfig.BEAN_NAME_DEFAULT_RESPONSE_AGGREGATIONS) @Resource(name = QueryRestConfig.BEAN_NAME_DEFAULT_RESPONSE_AGGREGATIONS)
private Set<Aggregation> defaultResponseAggregations; private Set<Aggregation> defaultResponseAggregations;
@InitBinder @InitBinder
protected void initBinder(WebDataBinder binder) { protected void initBinder(WebDataBinder binder) {
if (binder.getTarget() != null) { if (binder.getTarget() != null) {
@ -80,10 +81,7 @@ public class QueryRestController {
} }
} }
@RequestMapping( @RequestMapping(value = CHANNELS, method = { RequestMethod.GET, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
value = CHANNELS,
method = {RequestMethod.GET, RequestMethod.POST},
produces = {MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody Collection<String> getChannels(@RequestBody(required = false) ChannelsRequest request) public @ResponseBody Collection<String> getChannels(@RequestBody(required = false) ChannelsRequest request)
throws Throwable { throws Throwable {
// in case not specified use default (e.g. GET) // in case not specified use default (e.g. GET)
@ -92,25 +90,57 @@ public class QueryRestController {
} }
try { try {
return getQueryProcessor(request.getDbMode()).getChannels(request.getRegex()); // sorted collection
Collection<String> allChannels = getQueryProcessor(request.getDbMode()).getChannels(request.getRegex());
return allChannels;
} catch (Throwable t) { } catch (Throwable t) {
LOGGER.error("Failed to query channel names.", t); LOGGER.error("Failed to query channel names.", t);
throw t; throw t;
} }
} }
@RequestMapping( /**
value = QUERY, * Query specific channel names, and return only those.
method = RequestMethod.POST, *
consumes = {MediaType.APPLICATION_JSON_VALUE}) * @param channelName
* part of (or full) channel name
* @return Collection of channel names matching the specified input channel
* name
*/
@RequestMapping(value = CHANNELS + "/{channelName}", method = { RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody Collection<String> getChannels(@PathVariable(value = "channelName") String channelName) {
ChannelsRequest request = new ChannelsRequest(channelName);
Collection<String> specificChannels = getQueryProcessor(request.getDbMode()).getChannels(request.getRegex());
return specificChannels;
}
/**
* Catch-all query method for getting data from the backend.
* <p>
* The {@link AbstractQuery} object will be a concrete subclass based on the
* combination of fields defined in the user's query. The
* {@link AttributeBasedDeserializer} decides which class to deserialize the
* information into and has been configured (see
* QueryRestConfig#afterPropertiesSet) accordingly.
*
* @param query
* concrete implementation of {@link AbstractQuery}
* @param res
* the {@link HttpServletResponse} instance associated with this
* request
* @throws IOException
* thrown if writing to the output stream fails
*/
@RequestMapping(value = QUERY, method = RequestMethod.POST, consumes = { MediaType.APPLICATION_JSON_VALUE })
public void executeQuery(@RequestBody @Valid AbstractQuery query, HttpServletResponse res) throws IOException { public void executeQuery(@RequestBody @Valid AbstractQuery query, HttpServletResponse res) throws IOException {
try { try {
LOGGER.debug("Executing query '{}'", query.getClass().getSimpleName()); LOGGER.debug("Executing query '{}'", query.toString());
// write the response back to the client using java 8 streams // write the response back to the client using java 8 streams
responseStreamWriter.respond(executeQuery(query), query, res); responseStreamWriter.respond(executeQuery(query), query, res);
} catch (Throwable t) { } catch (IOException t) {
LOGGER.error("Failed execute query '{}'.", query, t); LOGGER.error("Failed to execute query '{}'.", query, t);
throw t; throw t;
} }
} }
@ -119,8 +149,8 @@ public class QueryRestController {
QueryAnalyzer queryAnalizer = queryAnalizerFactory.apply(query); QueryAnalyzer queryAnalizer = queryAnalizerFactory.apply(query);
// all the magic happens here // all the magic happens here
Stream<Entry<String, Stream<? extends DataEvent>>> channelToDataEvents = Stream<Entry<String, Stream<? extends DataEvent>>> channelToDataEvents = getQueryProcessor(query.getDbMode())
getQueryProcessor(query.getDbMode()).process(queryAnalizer); .process(queryAnalizer);
// do post-process // do post-process
Stream<Entry<String, ?>> channelToData = queryAnalizer.postProcess(channelToDataEvents); Stream<Entry<String, ?>> channelToData = queryAnalizer.postProcess(channelToDataEvents);
@ -140,7 +170,8 @@ public class QueryRestController {
} }
// ========================================================================================== // ==========================================================================================
// TODO: This is simply for initial / rudimentary testing - remove once further evolved // TODO: This is simply for initial / rudimentary testing - remove once
// further evolved
@Resource @Resource
private CassandraDataGen cassandraDataGen; private CassandraDataGen cassandraDataGen;
@ -148,40 +179,15 @@ public class QueryRestController {
@RequestMapping(value = "/write") @RequestMapping(value = "/write")
public void writeDummyEntry() { public void writeDummyEntry() {
long nrOfElements = 4; long nrOfElements = 4;
cassandraDataGen.writeData(3, 0, 4, cassandraDataGen.writeData(3, 0, 4, i -> i, i -> 0, i -> i, i -> i, i -> 0, i -> new long[] { i, i, i, i },
i -> i,
i -> 0,
i -> i,
i -> i,
i -> 0,
i -> new long[] {i, i, i, i},
"TRFCA-channel1"); "TRFCA-channel1");
cassandraDataGen.writeData(3, 0, 4, cassandraDataGen.writeData(3, 0, 4, i -> i, i -> 0, i -> i, i -> i, i -> 0, i -> new long[] { nrOfElements - i,
i -> i, nrOfElements - i, nrOfElements - i, nrOfElements - i }, "TRFCA-channel2");
i -> 0,
i -> i,
i -> i,
i -> 0,
i -> new long[] {nrOfElements - i, nrOfElements - i, nrOfElements - i, nrOfElements - i},
"TRFCA-channel2");
cassandraDataGen.writeData(3, 0, 4, cassandraDataGen.writeData(3, 0, 4, i -> i, i -> 0, i -> i, i -> i, i -> 0, i -> i, "TRFCB-channel3");
i -> i,
i -> 0,
i -> i,
i -> i,
i -> 0,
i -> i,
"TRFCB-channel3");
cassandraDataGen.writeData(3, 0, 4, cassandraDataGen.writeData(3, 0, 4, i -> i, i -> 0, i -> i, i -> i, i -> 0, i -> nrOfElements - i,
i -> i,
i -> 0,
i -> i,
i -> i,
i -> 0,
i -> nrOfElements - i,
"TRFCB-channel4"); "TRFCB-channel4");
} }
} }

View File

@ -14,6 +14,10 @@ public class ChannelsRequest {
public ChannelsRequest() {} public ChannelsRequest() {}
public ChannelsRequest(String regex) {
this(DBMode.databuffer, regex);
}
public ChannelsRequest(DBMode dbMode, String regex) { public ChannelsRequest(DBMode dbMode, String regex) {
this.regex = regex; this.regex = regex;
this.dbMode = dbMode; this.dbMode = dbMode;

View File

@ -74,3 +74,9 @@ curl -v -X POST -H 'Content-Type: application/json' -d '{"queryType":"timerange"
curl -v -X POST -H 'Content-Type: application/json' -d '{"queryType":"pulserange","ordering":"ASC","channels":["dummy-test"],"fields":["channel","pulseId","globalMillis","globalNanos","dbValueBytes"],"binningStrategy":"bincount","binDuration":100,"aggregateChannels":"false","aggregationType":"index","aggregations":[{"fieldRef":"e_val","type":"max","resultFieldName":"maximum"},{"fieldRef":"e_val","type":"min","resultFieldName":"minimum"}], "queryRange":{"startPulseId":100,"endPulseId":100}}' http://localhost:8080/pulserange curl -v -X POST -H 'Content-Type: application/json' -d '{"queryType":"pulserange","ordering":"ASC","channels":["dummy-test"],"fields":["channel","pulseId","globalMillis","globalNanos","dbValueBytes"],"binningStrategy":"bincount","binDuration":100,"aggregateChannels":"false","aggregationType":"index","aggregations":[{"fieldRef":"e_val","type":"max","resultFieldName":"maximum"},{"fieldRef":"e_val","type":"min","resultFieldName":"minimum"}], "queryRange":{"startPulseId":100,"endPulseId":100}}' http://localhost:8080/pulserange
===============================================================================================================================================
curl -H "Content-Type: application/json" -X POST -d '{"channels":["BooleanScalar"],"startPulseId":144404537,"endPulseId":144404547}' http://sf-nube-14.psi.ch:8080/query