blob: 43970bec95291eba01787882b359c468968608cf [file] [log] [blame]
margarethad3c0fc92017-10-25 15:03:32 +02001package de.ids_mannheim.korap.web.controller;
margaretha54134902017-09-27 18:43:11 +02002
3import java.util.Locale;
4
5import javax.ws.rs.GET;
6import javax.ws.rs.Path;
7import javax.ws.rs.Produces;
8import javax.ws.rs.QueryParam;
9import javax.ws.rs.core.Context;
10import javax.ws.rs.core.MediaType;
11import javax.ws.rs.core.Response;
12import javax.ws.rs.core.SecurityContext;
13
margaretha49cb6882018-07-04 04:19:54 +020014import org.apache.logging.log4j.LogManager;
15import org.apache.logging.log4j.Logger;
margaretha54134902017-09-27 18:43:11 +020016import org.springframework.beans.factory.annotation.Autowired;
17import org.springframework.stereotype.Controller;
18
19import com.sun.jersey.spi.container.ResourceFilters;
20
21import de.ids_mannheim.korap.exceptions.KustvaktException;
22import de.ids_mannheim.korap.exceptions.StatusCodes;
23import de.ids_mannheim.korap.utils.KoralCollectionQueryBuilder;
margaretha56e8e552017-12-05 16:31:21 +010024import de.ids_mannheim.korap.web.CoreResponseHandler;
margaretha54134902017-09-27 18:43:11 +020025import de.ids_mannheim.korap.web.SearchKrill;
margarethaee0cbfe2018-08-28 17:47:14 +020026import de.ids_mannheim.korap.web.APIVersionFilter;
margaretha54134902017-09-27 18:43:11 +020027import de.ids_mannheim.korap.web.filter.PiwikFilter;
margaretha54134902017-09-27 18:43:11 +020028
29/**
margarethad3c0fc92017-10-25 15:03:32 +020030 * Web services related to statistics
margaretha54134902017-09-27 18:43:11 +020031 *
32 * @author hanl
33 * @author margaretha
34 *
margaretha894a7d72017-11-08 19:24:20 +010035 * @date 08/11/2017
margaretha54134902017-09-27 18:43:11 +020036 *
37 */
38@Controller
margarethaee0cbfe2018-08-28 17:47:14 +020039@Path("{version}/statistics/")
40@ResourceFilters({APIVersionFilter.class, PiwikFilter.class })
margaretha54134902017-09-27 18:43:11 +020041@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
margarethad3c0fc92017-10-25 15:03:32 +020042public class StatisticController {
margaretha54134902017-09-27 18:43:11 +020043
44
45 private static Logger jlog =
margaretha49cb6882018-07-04 04:19:54 +020046 LogManager.getLogger(StatisticController.class);
margaretha894a7d72017-11-08 19:24:20 +010047 @Autowired
margarethada3c7852018-06-14 20:35:11 +020048 private CoreResponseHandler kustvaktResponseHandler;
margaretha54134902017-09-27 18:43:11 +020049 @Autowired
50 private SearchKrill searchKrill;
51
52
53 /**
54 * Returns statistics of the virtual corpus defined by the given
margarethae8ab51d2018-01-16 19:27:40 +010055 * corpusQuery parameter.
margaretha54134902017-09-27 18:43:11 +020056 *
57 * @param context
58 * SecurityContext
59 * @param locale
60 * Locale
margarethae8ab51d2018-01-16 19:27:40 +010061 * @param corpusQuery
margaretha54134902017-09-27 18:43:11 +020062 * a collection query specifying a virtual corpus
63 * @return statistics of the virtual corpus defined by the given
margarethae8ab51d2018-01-16 19:27:40 +010064 * corpusQuery parameter.
margaretha54134902017-09-27 18:43:11 +020065 */
66 @GET
67 public Response getStatistics (@Context SecurityContext context,
68 @Context Locale locale,
margarethae8ab51d2018-01-16 19:27:40 +010069 @QueryParam("corpusQuery") String corpusQuery) {
margaretha54134902017-09-27 18:43:11 +020070
margaretha54134902017-09-27 18:43:11 +020071 KoralCollectionQueryBuilder builder = new KoralCollectionQueryBuilder();
margaretha54134902017-09-27 18:43:11 +020072
Akron6091cf32018-07-03 20:45:49 +020073 String stats;
74 if (corpusQuery != null && !corpusQuery.isEmpty()) {
75 builder.with(corpusQuery);
76 String json = null;
77 try {
78 json = builder.toJSON();
79 }
80 catch (KustvaktException e) {
81 throw kustvaktResponseHandler.throwit(e);
82 }
83 stats = searchKrill.getStatistics(json);
84 }
85 else {
86 stats = searchKrill.getStatistics(null);
87 };
88
margaretha54134902017-09-27 18:43:11 +020089 if (stats.contains("-1"))
margarethada3c7852018-06-14 20:35:11 +020090 throw kustvaktResponseHandler.throwit(StatusCodes.NO_RESULT_FOUND);
margaretha54134902017-09-27 18:43:11 +020091 jlog.debug("Stats: " + stats);
92 return Response.ok(stats).build();
93 }
94}