147 lines
4.6 KiB
Java
147 lines
4.6 KiB
Java
package org.lucares.pdbui;
|
|
|
|
import java.io.File;
|
|
import java.nio.file.Path;
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
|
|
import org.lucares.ludb.Proposal;
|
|
import org.lucares.pdbui.domain.AutocompleteProposal;
|
|
import org.lucares.pdbui.domain.AutocompleteProposalByValue;
|
|
import org.lucares.pdbui.domain.AutocompleteResponse;
|
|
import org.lucares.pdbui.domain.PlotRequest;
|
|
import org.lucares.pdbui.domain.PlotResponse;
|
|
import org.lucares.performance.db.CollectionUtils;
|
|
import org.lucares.recommind.logs.InternalPlottingException;
|
|
import org.lucares.recommind.logs.Plotter;
|
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.stereotype.Controller;
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
@Controller
|
|
@EnableAutoConfiguration
|
|
public class PdbController implements HardcodedValues, CollectionUtils {
|
|
|
|
private final Plotter plotter;
|
|
private final PdbRepository db;
|
|
|
|
public PdbController(final PdbRepository db, final Plotter plotter) {
|
|
this.db = db;
|
|
this.plotter = plotter;
|
|
}
|
|
|
|
@RequestMapping(path = "/plots", //
|
|
method = RequestMethod.POST, //
|
|
consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, //
|
|
produces = MediaType.APPLICATION_JSON_UTF8_VALUE //
|
|
)
|
|
@ResponseBody
|
|
PlotResponse createPlot(@RequestBody final PlotRequest request) {
|
|
|
|
try {
|
|
final String query = request.getQuery();
|
|
final int height = request.getHeight();
|
|
final int width = request.getWidth();
|
|
|
|
System.out.println(query);
|
|
final File image = plotter.plot(query, height, width, request.getGroupBy());
|
|
|
|
final Path relativeImagePath = plotter.getOutputDir().relativize(image.toPath());
|
|
return new PlotResponse(WEB_IMAGE_OUTPUT_PATH + "/" + relativeImagePath.toString());
|
|
|
|
} catch (final InternalPlottingException e) {
|
|
throw new InternalServerError(e);
|
|
}
|
|
}
|
|
|
|
@RequestMapping(path = "/autocomplete", //
|
|
method = RequestMethod.GET, //
|
|
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, // APPLICATION_JSON_UTF8_VALUE
|
|
produces = MediaType.APPLICATION_JSON_UTF8_VALUE //
|
|
)
|
|
@ResponseBody
|
|
AutocompleteResponse autocomplete(@RequestParam(name = "query") final String query,
|
|
@RequestParam(name = "caretIndex") final int caretIndex) {
|
|
|
|
try {
|
|
final AutocompleteResponse result = new AutocompleteResponse();
|
|
final int zeroBasedCaretIndex = caretIndex - 1;
|
|
|
|
final List<Proposal> proposals = db.autocomplete(query, zeroBasedCaretIndex);
|
|
final List<Proposal> nonEmptyProposals = filter(proposals, p -> p.getResults() > 0);
|
|
|
|
final List<AutocompleteProposal> autocompleteProposals = toAutocompleteProposals(nonEmptyProposals);
|
|
Collections.sort(autocompleteProposals, new AutocompleteProposalByValue());
|
|
|
|
result.setProposals(autocompleteProposals);
|
|
return result;
|
|
|
|
} catch (final Exception e) {
|
|
e.printStackTrace();
|
|
throw new InternalServerError(e);
|
|
}
|
|
}
|
|
|
|
@RequestMapping(path = "/fields", //
|
|
method = RequestMethod.GET, //
|
|
consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, //
|
|
produces = MediaType.APPLICATION_JSON_UTF8_VALUE //
|
|
)
|
|
@ResponseBody
|
|
List<String> fields(@RequestParam(name = "query") final String query) {
|
|
|
|
try {
|
|
final List<String> fields = db.getDb().getFields(query);
|
|
|
|
return fields;
|
|
|
|
} catch (final Exception e) {
|
|
e.printStackTrace();
|
|
throw new InternalServerError(e);
|
|
}
|
|
}
|
|
|
|
@RequestMapping(path = "/fields/{fieldName}/values", //
|
|
method = RequestMethod.GET, //
|
|
consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, //
|
|
produces = MediaType.APPLICATION_JSON_UTF8_VALUE //
|
|
)
|
|
@ResponseBody
|
|
List<String> fields(@PathVariable(name = "fieldName") final String fieldName,
|
|
@RequestParam(name = "query") final String query) {
|
|
|
|
try {
|
|
final List<String> fields = db.getDb().getFieldsValues(query, fieldName);
|
|
|
|
return fields;
|
|
|
|
} catch (final Exception e) {
|
|
e.printStackTrace();
|
|
throw new InternalServerError(e);
|
|
}
|
|
}
|
|
|
|
private List<AutocompleteProposal> toAutocompleteProposals(final List<Proposal> proposals) {
|
|
|
|
final List<AutocompleteProposal> result = new ArrayList<>();
|
|
|
|
for (final Proposal proposal : proposals) {
|
|
final AutocompleteProposal e = new AutocompleteProposal();
|
|
e.setValue(proposal.getProposedTag());
|
|
e.setProposedQuery(proposal.getProposedQuery());
|
|
|
|
result.add(e);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
}
|