You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SearchHistoryAction.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.measure.ws;
  21. import com.google.common.collect.Sets;
  22. import java.util.Date;
  23. import java.util.List;
  24. import java.util.Set;
  25. import java.util.function.Function;
  26. import java.util.stream.Stream;
  27. import javax.annotation.CheckForNull;
  28. import javax.annotation.Nullable;
  29. import org.sonar.api.server.ws.Request;
  30. import org.sonar.api.server.ws.Response;
  31. import org.sonar.api.server.ws.WebService;
  32. import org.sonar.api.server.ws.WebService.Param;
  33. import org.sonar.api.web.UserRole;
  34. import org.sonar.core.util.stream.MoreCollectors;
  35. import org.sonar.db.DbClient;
  36. import org.sonar.db.DbSession;
  37. import org.sonar.db.component.ComponentDto;
  38. import org.sonar.db.component.SnapshotDto;
  39. import org.sonar.db.component.SnapshotQuery;
  40. import org.sonar.db.component.SnapshotQuery.SORT_FIELD;
  41. import org.sonar.db.component.SnapshotQuery.SORT_ORDER;
  42. import org.sonar.db.measure.MeasureDto;
  43. import org.sonar.db.measure.PastMeasureQuery;
  44. import org.sonar.db.metric.MetricDto;
  45. import org.sonar.server.component.ComponentFinder;
  46. import org.sonar.server.user.UserSession;
  47. import org.sonar.server.ws.KeyExamples;
  48. import org.sonarqube.ws.Measures.SearchHistoryResponse;
  49. import static java.lang.String.format;
  50. import static org.sonar.api.utils.DateUtils.parseEndingDateOrDateTime;
  51. import static org.sonar.api.utils.DateUtils.parseStartingDateOrDateTime;
  52. import static org.sonar.core.util.Protobuf.setNullable;
  53. import static org.sonar.db.component.SnapshotDto.STATUS_PROCESSED;
  54. import static org.sonar.server.component.ws.MeasuresWsParameters.ACTION_SEARCH_HISTORY;
  55. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_BRANCH;
  56. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_COMPONENT;
  57. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_FROM;
  58. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_METRICS;
  59. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_PULL_REQUEST;
  60. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_TO;
  61. import static org.sonar.server.ws.KeyExamples.KEY_BRANCH_EXAMPLE_001;
  62. import static org.sonar.server.ws.KeyExamples.KEY_PULL_REQUEST_EXAMPLE_001;
  63. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  64. public class SearchHistoryAction implements MeasuresWsAction {
  65. private static final int MAX_PAGE_SIZE = 1_000;
  66. private static final int DEFAULT_PAGE_SIZE = 100;
  67. private final DbClient dbClient;
  68. private final ComponentFinder componentFinder;
  69. private final UserSession userSession;
  70. public SearchHistoryAction(DbClient dbClient, ComponentFinder componentFinder, UserSession userSession) {
  71. this.dbClient = dbClient;
  72. this.componentFinder = componentFinder;
  73. this.userSession = userSession;
  74. }
  75. @Override
  76. public void define(WebService.NewController context) {
  77. WebService.NewAction action = context.createAction(ACTION_SEARCH_HISTORY)
  78. .setDescription("Search measures history of a component.<br>" +
  79. "Measures are ordered chronologically.<br>" +
  80. "Pagination applies to the number of measures for each metric.<br>" +
  81. "Requires the following permission: 'Browse' on the specified component")
  82. .setResponseExample(getClass().getResource("search_history-example.json"))
  83. .setSince("6.3")
  84. .setHandler(this);
  85. action.createParam(PARAM_COMPONENT)
  86. .setDescription("Component key")
  87. .setRequired(true)
  88. .setExampleValue(KeyExamples.KEY_PROJECT_EXAMPLE_001);
  89. action.createParam(PARAM_BRANCH)
  90. .setDescription("Branch key")
  91. .setSince("6.6")
  92. .setInternal(true)
  93. .setExampleValue(KEY_BRANCH_EXAMPLE_001);
  94. action.createParam(PARAM_PULL_REQUEST)
  95. .setDescription("Pull request id")
  96. .setSince("7.1")
  97. .setInternal(true)
  98. .setExampleValue(KEY_PULL_REQUEST_EXAMPLE_001);
  99. action.createParam(PARAM_METRICS)
  100. .setDescription("Comma-separated list of metric keys")
  101. .setRequired(true)
  102. .setExampleValue("ncloc,coverage,new_violations");
  103. action.createParam(PARAM_FROM)
  104. .setDescription("Filter measures created after the given date (inclusive). <br>" +
  105. "Either a date (server timezone) or datetime can be provided")
  106. .setExampleValue("2017-10-19 or 2017-10-19T13:00:00+0200");
  107. action.createParam(PARAM_TO)
  108. .setDescription("Filter measures created before the given date (inclusive). <br>" +
  109. "Either a date (server timezone) or datetime can be provided")
  110. .setExampleValue("2017-10-19 or 2017-10-19T13:00:00+0200");
  111. action.addPagingParams(DEFAULT_PAGE_SIZE, MAX_PAGE_SIZE);
  112. }
  113. @Override
  114. public void handle(Request request, Response response) throws Exception {
  115. SearchHistoryResponse searchHistoryResponse = Stream.of(request)
  116. .map(SearchHistoryAction::toWsRequest)
  117. .map(search())
  118. .map(result -> new SearchHistoryResponseFactory(result).apply())
  119. .collect(MoreCollectors.toOneElement());
  120. writeProtobuf(searchHistoryResponse, request, response);
  121. }
  122. private static SearchHistoryRequest toWsRequest(Request request) {
  123. return SearchHistoryRequest.builder()
  124. .setComponent(request.mandatoryParam(PARAM_COMPONENT))
  125. .setBranch(request.param(PARAM_BRANCH))
  126. .setPullRequest(request.param(PARAM_PULL_REQUEST))
  127. .setMetrics(request.mandatoryParamAsStrings(PARAM_METRICS))
  128. .setFrom(request.param(PARAM_FROM))
  129. .setTo(request.param(PARAM_TO))
  130. .setPage(request.mandatoryParamAsInt(Param.PAGE))
  131. .setPageSize(request.mandatoryParamAsInt(Param.PAGE_SIZE))
  132. .build();
  133. }
  134. private Function<SearchHistoryRequest, SearchHistoryResult> search() {
  135. return request -> {
  136. try (DbSession dbSession = dbClient.openSession(false)) {
  137. ComponentDto component = searchComponent(request, dbSession);
  138. SearchHistoryResult result = new SearchHistoryResult(request.page, request.pageSize)
  139. .setComponent(component)
  140. .setAnalyses(searchAnalyses(dbSession, request, component))
  141. .setMetrics(searchMetrics(dbSession, request));
  142. return result.setMeasures(searchMeasures(dbSession, request, result));
  143. }
  144. };
  145. }
  146. private ComponentDto searchComponent(SearchHistoryRequest request, DbSession dbSession) {
  147. ComponentDto component = loadComponent(dbSession, request);
  148. userSession.checkComponentPermission(UserRole.USER, component);
  149. return component;
  150. }
  151. private List<MeasureDto> searchMeasures(DbSession dbSession, SearchHistoryRequest request, SearchHistoryResult result) {
  152. Date from = parseStartingDateOrDateTime(request.getFrom());
  153. Date to = parseEndingDateOrDateTime(request.getTo());
  154. PastMeasureQuery dbQuery = new PastMeasureQuery(
  155. result.getComponent().uuid(),
  156. result.getMetrics().stream().map(MetricDto::getId).collect(MoreCollectors.toList()),
  157. from == null ? null : from.getTime(),
  158. to == null ? null : (to.getTime() + 1_000L));
  159. return dbClient.measureDao().selectPastMeasures(dbSession, dbQuery);
  160. }
  161. private List<SnapshotDto> searchAnalyses(DbSession dbSession, SearchHistoryRequest request, ComponentDto component) {
  162. SnapshotQuery dbQuery = new SnapshotQuery()
  163. .setComponentUuid(component.projectUuid())
  164. .setStatus(STATUS_PROCESSED)
  165. .setSort(SORT_FIELD.BY_DATE, SORT_ORDER.ASC);
  166. setNullable(request.getFrom(), from -> dbQuery.setCreatedAfter(parseStartingDateOrDateTime(from).getTime()));
  167. setNullable(request.getTo(), to -> dbQuery.setCreatedBefore(parseEndingDateOrDateTime(to).getTime() + 1_000L));
  168. return dbClient.snapshotDao().selectAnalysesByQuery(dbSession, dbQuery);
  169. }
  170. private List<MetricDto> searchMetrics(DbSession dbSession, SearchHistoryRequest request) {
  171. List<MetricDto> metrics = dbClient.metricDao().selectByKeys(dbSession, request.getMetrics());
  172. if (request.getMetrics().size() > metrics.size()) {
  173. Set<String> requestedMetrics = request.getMetrics().stream().collect(MoreCollectors.toSet());
  174. Set<String> foundMetrics = metrics.stream().map(MetricDto::getKey).collect(MoreCollectors.toSet());
  175. Set<String> unfoundMetrics = Sets.difference(requestedMetrics, foundMetrics).immutableCopy();
  176. throw new IllegalArgumentException(format("Metrics %s are not found", String.join(", ", unfoundMetrics)));
  177. }
  178. return metrics;
  179. }
  180. private ComponentDto loadComponent(DbSession dbSession, SearchHistoryRequest request) {
  181. String componentKey = request.getComponent();
  182. String branch = request.getBranch();
  183. String pullRequest = request.getPullRequest();
  184. return componentFinder.getByKeyAndOptionalBranchOrPullRequest(dbSession, componentKey, branch, pullRequest);
  185. }
  186. static class SearchHistoryRequest {
  187. private final String component;
  188. private final String branch;
  189. private final String pullRequest;
  190. private final List<String> metrics;
  191. private final String from;
  192. private final String to;
  193. private final int page;
  194. private final int pageSize;
  195. public SearchHistoryRequest(Builder builder) {
  196. this.component = builder.component;
  197. this.branch = builder.branch;
  198. this.pullRequest = builder.pullRequest;
  199. this.metrics = builder.metrics;
  200. this.from = builder.from;
  201. this.to = builder.to;
  202. this.page = builder.page;
  203. this.pageSize = builder.pageSize;
  204. }
  205. public String getComponent() {
  206. return component;
  207. }
  208. @CheckForNull
  209. public String getBranch() {
  210. return branch;
  211. }
  212. @CheckForNull
  213. public String getPullRequest() {
  214. return pullRequest;
  215. }
  216. public List<String> getMetrics() {
  217. return metrics;
  218. }
  219. @CheckForNull
  220. public String getFrom() {
  221. return from;
  222. }
  223. @CheckForNull
  224. public String getTo() {
  225. return to;
  226. }
  227. public int getPage() {
  228. return page;
  229. }
  230. public int getPageSize() {
  231. return pageSize;
  232. }
  233. public static Builder builder() {
  234. return new Builder();
  235. }
  236. }
  237. static class Builder {
  238. private String component;
  239. private String branch;
  240. private String pullRequest;
  241. private List<String> metrics;
  242. private String from;
  243. private String to;
  244. private int page = 1;
  245. private int pageSize = DEFAULT_PAGE_SIZE;
  246. private Builder() {
  247. // enforce build factory method
  248. }
  249. public Builder setComponent(String component) {
  250. this.component = component;
  251. return this;
  252. }
  253. public Builder setBranch(@Nullable String branch) {
  254. this.branch = branch;
  255. return this;
  256. }
  257. public Builder setPullRequest(@Nullable String pullRequest) {
  258. this.pullRequest = pullRequest;
  259. return this;
  260. }
  261. public Builder setMetrics(List<String> metrics) {
  262. this.metrics = metrics;
  263. return this;
  264. }
  265. public Builder setFrom(@Nullable String from) {
  266. this.from = from;
  267. return this;
  268. }
  269. public Builder setTo(@Nullable String to) {
  270. this.to = to;
  271. return this;
  272. }
  273. public Builder setPage(int page) {
  274. this.page = page;
  275. return this;
  276. }
  277. public Builder setPageSize(int pageSize) {
  278. this.pageSize = pageSize;
  279. return this;
  280. }
  281. public SearchHistoryRequest build() {
  282. checkArgument(component != null && !component.isEmpty(), "Component key is required");
  283. checkArgument(metrics != null && !metrics.isEmpty(), "Metric keys are required");
  284. checkArgument(pageSize <= MAX_PAGE_SIZE, "Page size (%d) must be lower than or equal to %d", pageSize, MAX_PAGE_SIZE);
  285. return new SearchHistoryRequest(this);
  286. }
  287. private static void checkArgument(boolean condition, String message, Object... args) {
  288. if (!condition) {
  289. throw new IllegalArgumentException(format(message, args));
  290. }
  291. }
  292. }
  293. }