Skip to content

Commit

Permalink
Fix quality flaws
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Brandhof committed Jun 16, 2016
1 parent 7eddfaa commit 2cb89af
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 92 deletions.
Expand Up @@ -20,6 +20,21 @@

package org.sonar.server.ce.ws;

import static java.lang.String.format;
import static java.util.Collections.singletonList;
import static org.apache.commons.lang.StringUtils.defaultString;
import static org.sonar.api.utils.DateUtils.parseDateQuietly;
import static org.sonar.api.utils.DateUtils.parseDateTimeQuietly;
import static org.sonar.server.ws.WsUtils.checkRequest;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT_ID;
import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT_QUERY;
import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_MAX_EXECUTED_AT;
import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_MIN_SUBMITTED_AT;
import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_ONLY_CURRENTS;
import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_STATUS;
import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_TYPE;

import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -56,21 +71,6 @@
import org.sonarqube.ws.WsCe.ActivityResponse;
import org.sonarqube.ws.client.ce.ActivityWsRequest;

import static java.lang.String.format;
import static java.util.Collections.singletonList;
import static org.apache.commons.lang.StringUtils.defaultString;
import static org.sonar.api.utils.DateUtils.parseDateQuietly;
import static org.sonar.api.utils.DateUtils.parseDateTimeQuietly;
import static org.sonar.server.ws.WsUtils.checkRequest;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT_ID;
import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT_QUERY;
import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_MAX_EXECUTED_AT;
import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_MIN_SUBMITTED_AT;
import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_ONLY_CURRENTS;
import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_STATUS;
import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_TYPE;

public class ActivityAction implements CeWsAction {
private static final int OFFSET = 0;
private static final int MAX_PAGE_SIZE = 1000;
Expand Down Expand Up @@ -242,12 +242,12 @@ private List<String> loadComponentUuids(DbSession dbSession, ActivityWsRequest r
return null;
}

private Iterable<WsCe.Task> loadQueuedTasks(DbSession dbSession, ActivityWsRequest request, CeTaskQuery query) {
private List<WsCe.Task> loadQueuedTasks(DbSession dbSession, ActivityWsRequest request, CeTaskQuery query) {
List<CeQueueDto> dtos = dbClient.ceQueueDao().selectByQueryInDescOrder(dbSession, query, request.getPageSize());
return formatter.formatQueue(dbSession, dtos);
}

private Iterable<WsCe.Task> loadPastTasks(DbSession dbSession, ActivityWsRequest request, CeTaskQuery query) {
private List<WsCe.Task> loadPastTasks(DbSession dbSession, ActivityWsRequest request, CeTaskQuery query) {
List<CeActivityDto> dtos = dbClient.ceActivityDao().selectByQuery(dbSession, query, OFFSET, request.getPageSize());
return formatter.formatActivity(dbSession, dtos);
}
Expand Down
Expand Up @@ -19,32 +19,29 @@
*/
package org.sonar.server.ce.ws;

import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.sonar.api.utils.DateUtils;
import org.sonar.api.utils.System2;
import org.sonar.ce.log.CeLogging;
import org.sonar.ce.log.LogFileRef;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.ce.CeActivityDto;
import org.sonar.db.ce.CeQueueDto;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentDtoFunctions;
import org.sonar.ce.log.CeLogging;
import org.sonar.ce.log.LogFileRef;
import org.sonarqube.ws.WsCe;

import static com.google.common.base.Predicates.notNull;
import static com.google.common.collect.FluentIterable.from;

/**
* Converts {@link CeActivityDto} and {@link CeQueueDto} to the protobuf objects
* used to write WS responses (see ws-ce.proto in module sonar-ws)
Expand All @@ -61,10 +58,9 @@ public TaskFormatter(DbClient dbClient, CeLogging ceLogging, System2 system2) {
this.system2 = system2;
}

public Iterable<WsCe.Task> formatQueue(DbSession dbSession, List<CeQueueDto> dtos) {
public List<WsCe.Task> formatQueue(DbSession dbSession, List<CeQueueDto> dtos) {
ComponentDtoCache cache = new ComponentDtoCache(dbSession, ceQueueDtoToComponentUuids(dtos));
return from(dtos)
.transform(new CeQueueDtoToTask(cache));
return dtos.stream().map(input -> formatQueue(input, cache)).collect(Collectors.toList());
}

public WsCe.Task formatQueue(DbSession dbSession, CeQueueDto dto) {
Expand Down Expand Up @@ -100,9 +96,9 @@ public WsCe.Task formatActivity(DbSession dbSession, CeActivityDto dto) {
return formatActivity(dto, new ComponentDtoCache(dbSession, dto.getComponentUuid()));
}

public Iterable<WsCe.Task> formatActivity(DbSession dbSession, List<CeActivityDto> dtos) {
public List<WsCe.Task> formatActivity(DbSession dbSession, List<CeActivityDto> dtos) {
ComponentDtoCache cache = new ComponentDtoCache(dbSession, ceActivityDtoToComponentUuids(dtos));
return from(dtos).transform(new CeActivityDtoToTask(cache));
return dtos.stream().map(input -> formatActivity(input, cache)).collect(Collectors.toList());
}

private WsCe.Task formatActivity(CeActivityDto dto, ComponentDtoCache componentDtoCache) {
Expand Down Expand Up @@ -142,50 +138,30 @@ private static void buildComponent(WsCe.Task.Builder builder, @Nullable Componen
}
}

private static Set<String> ceQueueDtoToComponentUuids(Iterable<CeQueueDto> dtos) {
return from(dtos)
.transform(CeQueueDtoToComponentUuid.INSTANCE)
.filter(notNull())
.toSet();
}

private static Set<String> ceActivityDtoToComponentUuids(Iterable<CeActivityDto> dtos) {
return from(dtos)
.transform(CeActivityDtoToComponentUuid.INSTANCE)
.filter(notNull())
.toSet();
}

private enum CeQueueDtoToComponentUuid implements Function<CeQueueDto, String> {
INSTANCE;

@Override
@Nullable
public String apply(@Nonnull CeQueueDto input) {
return input.getComponentUuid();
}
private static Set<String> ceQueueDtoToComponentUuids(List<CeQueueDto> dtos) {
return dtos.stream().map(CeQueueDto::getComponentUuid)
.filter(Objects::nonNull)
.collect(Collectors.toSet());
}

private enum CeActivityDtoToComponentUuid implements Function<CeActivityDto, String> {
INSTANCE;

@Override
@Nullable
public String apply(@Nonnull CeActivityDto input) {
return input.getComponentUuid();
}
private static Set<String> ceActivityDtoToComponentUuids(List<CeActivityDto> dtos) {
return dtos.stream().map(CeActivityDto::getComponentUuid)
.filter(Objects::nonNull)
.collect(Collectors.toSet());
}

private class ComponentDtoCache {
private final Map<String, ComponentDto> componentsByUuid;

public ComponentDtoCache(DbSession dbSession, Set<String> uuids) {
this.componentsByUuid = from(dbClient.componentDao().selectByUuids(dbSession, uuids)).uniqueIndex(ComponentDtoFunctions.toUuid());
ComponentDtoCache(DbSession dbSession, Set<String> uuids) {
this.componentsByUuid = dbClient.componentDao().selectByUuids(dbSession, uuids)
.stream()
.collect(Collectors.toMap(ComponentDto::uuid, Function.identity()));
}

public ComponentDtoCache(DbSession dbSession, String uuid) {
ComponentDtoCache(DbSession dbSession, String uuid) {
Optional<ComponentDto> componentDto = dbClient.componentDao().selectByUuid(dbSession, uuid);
this.componentsByUuid = componentDto.isPresent() ? ImmutableMap.of(uuid, componentDto.get()) : Collections.<String, ComponentDto>emptyMap();
this.componentsByUuid = componentDto.isPresent() ? ImmutableMap.of(uuid, componentDto.get()) : Collections.emptyMap();
}

@CheckForNull
Expand All @@ -201,39 +177,12 @@ ComponentDto get(@Nullable String uuid) {
* now - startedAt
*/
@CheckForNull
Long computeExecutionTimeMs(CeQueueDto dto) {
private Long computeExecutionTimeMs(CeQueueDto dto) {
Long startedAt = dto.getStartedAt();
if (startedAt == null) {
return null;
}
return system2.now() - startedAt;
}

private final class CeActivityDtoToTask implements Function<CeActivityDto, WsCe.Task> {
private final ComponentDtoCache cache;

public CeActivityDtoToTask(ComponentDtoCache cache) {
this.cache = cache;
}

@Override
@Nonnull
public WsCe.Task apply(@Nonnull CeActivityDto input) {
return formatActivity(input, cache);
}
}

private final class CeQueueDtoToTask implements Function<CeQueueDto, WsCe.Task> {
private final ComponentDtoCache cache;

public CeQueueDtoToTask(ComponentDtoCache cache) {
this.cache = cache;
}

@Override
@Nonnull
public WsCe.Task apply(@Nonnull CeQueueDto input) {
return formatQueue(input, cache);
}
}
}

0 comments on commit 2cb89af

Please sign in to comment.