Skip to content

Commit

Permalink
Fix quality flaws
Browse files Browse the repository at this point in the history
  • Loading branch information
julienlancelot committed Sep 7, 2016
1 parent f7c2985 commit de20887
Show file tree
Hide file tree
Showing 17 changed files with 185 additions and 194 deletions.
Expand Up @@ -22,9 +22,6 @@
import java.io.IOException; import java.io.IOException;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.RequestHandler;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService; import org.sonar.api.server.ws.WebService;


public class BatchWs implements WebService { public class BatchWs implements WebService {
Expand Down Expand Up @@ -59,15 +56,12 @@ private void defineIndexAction(NewController controller) {
.setInternal(true) .setInternal(true)
.setSince("4.4") .setSince("4.4")
.setDescription("List the JAR files to be downloaded by source analyzer") .setDescription("List the JAR files to be downloaded by source analyzer")
.setHandler(new RequestHandler() { .setHandler((request, response) -> {
@Override try {
public void handle(Request request, Response response) { response.stream().setMediaType("text/plain");
try { IOUtils.write(batchIndex.getIndex(), response.stream().output());
response.stream().setMediaType("text/plain"); } catch (IOException e) {
IOUtils.write(batchIndex.getIndex(), response.stream().output()); throw new IllegalStateException(e);
} catch (IOException e) {
throw new IllegalStateException(e);
}
} }
}) })
.setResponseExample(getClass().getResource("batch-index-example.txt")); .setResponseExample(getClass().getResource("batch-index-example.txt"));
Expand All @@ -79,16 +73,13 @@ private void defineFileAction(NewController controller) {
.setSince("4.4") .setSince("4.4")
.setDescription("Download a JAR file required by source analyzer") .setDescription("Download a JAR file required by source analyzer")
.setResponseExample(getClass().getResource("batch-file-example.txt")) .setResponseExample(getClass().getResource("batch-file-example.txt"))
.setHandler(new RequestHandler() { .setHandler((request, response) -> {
@Override String filename = request.mandatoryParam("name");
public void handle(Request request, Response response) { try {
String filename = request.mandatoryParam("name"); response.stream().setMediaType("application/java-archive");
try { FileUtils.copyFile(batchIndex.getFile(filename), response.stream().output());
response.stream().setMediaType("application/java-archive"); } catch (IOException e) {
FileUtils.copyFile(batchIndex.getFile(filename), response.stream().output()); throw new IllegalStateException(e);
} catch (IOException e) {
throw new IllegalStateException(e);
}
} }
}); });
action action
Expand Down
Expand Up @@ -54,20 +54,20 @@ private static ComponentProcessor getComponentProcessor(String sort) {
throw new IllegalArgumentException("Cannot sort on field : " + sort); throw new IllegalArgumentException("Cannot sort on field : " + sort);
} }


abstract static class ComponentProcessor { interface ComponentProcessor {
abstract Function sortFieldFunction(); Function sortFieldFunction();


abstract Ordering sortFieldOrdering(boolean ascending); Ordering sortFieldOrdering(boolean ascending);


final List<? extends Component> sort(Collection<? extends Component> components, boolean ascending) { default List<? extends Component> sort(Collection<? extends Component> components, boolean ascending) {
Ordering<Component> ordering = sortFieldOrdering(ascending).onResultOf(sortFieldFunction()); Ordering<Component> ordering = sortFieldOrdering(ascending).onResultOf(sortFieldFunction());
return ordering.immutableSortedCopy(components); return ordering.immutableSortedCopy(components);
} }
} }


abstract static class TextSort extends ComponentProcessor { abstract static class TextSort implements ComponentProcessor {
@Override @Override
Function sortFieldFunction() { public Function sortFieldFunction() {
return new Function<Component, String>() { return new Function<Component, String>() {
@Override @Override
public String apply(Component component) { public String apply(Component component) {
Expand All @@ -79,7 +79,7 @@ public String apply(Component component) {
abstract String sortField(Component component); abstract String sortField(Component component);


@Override @Override
Ordering sortFieldOrdering(boolean ascending) { public Ordering sortFieldOrdering(boolean ascending) {
Ordering<String> ordering = Ordering.from(String.CASE_INSENSITIVE_ORDER).nullsLast(); Ordering<String> ordering = Ordering.from(String.CASE_INSENSITIVE_ORDER).nullsLast();
if (!ascending) { if (!ascending) {
ordering = ordering.reverse(); ordering = ordering.reverse();
Expand Down
Expand Up @@ -62,11 +62,11 @@ private static Collection<Component> search(ComponentQuery query, List<? extends
return newArrayList(Iterables.filter(allComponents, new MatchQuery(query))); return newArrayList(Iterables.filter(allComponents, new MatchQuery(query)));
} }


abstract static class Filter { interface Filter {


abstract String field(Component component); String field(Component component);


final boolean accept(Component component, Collection<String> collections) { default boolean accept(Component component, Collection<String> collections) {
if (!collections.isEmpty()) { if (!collections.isEmpty()) {
for (String item : collections) { for (String item : collections) {
if (field(component).toLowerCase().contains(item.toLowerCase())) { if (field(component).toLowerCase().contains(item.toLowerCase())) {
Expand All @@ -79,16 +79,16 @@ final boolean accept(Component component, Collection<String> collections) {
} }
} }


static class NameFilter extends Filter { static class NameFilter implements Filter {
@Override @Override
String field(Component component) { public String field(Component component) {
return component.name(); return component.name();
} }
} }


static class KeyFilter extends Filter { static class KeyFilter implements Filter {
@Override @Override
String field(Component component) { public String field(Component component) {
return component.key(); return component.key();
} }
} }
Expand Down
Expand Up @@ -27,6 +27,8 @@
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import javax.annotation.CheckForNull; import javax.annotation.CheckForNull;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
Expand All @@ -49,8 +51,9 @@
import org.sonar.server.computation.task.step.ComputationStep; import org.sonar.server.computation.task.step.ComputationStep;


import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.FluentIterable.from;
import static java.lang.String.format; import static java.lang.String.format;
import static java.util.function.Function.identity;
import static org.sonar.core.util.stream.Collectors.uniqueIndex;
import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.DIRECTORY; import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.DIRECTORY;
import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.SUBVIEW; import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.SUBVIEW;
import static org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER; import static org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER;
Expand All @@ -69,15 +72,6 @@ public class ComputeMeasureVariationsStep implements ComputationStep {
private final MetricRepository metricRepository; private final MetricRepository metricRepository;
private final MeasureRepository measureRepository; private final MeasureRepository measureRepository;


private final Function<PastMeasureDto, MeasureKey> pastMeasureToMeasureKey = new Function<PastMeasureDto, MeasureKey>() {
@Nullable
@Override
public MeasureKey apply(@Nonnull PastMeasureDto input) {
Metric metric = metricRepository.getById((long)input.getMetricId());
return new MeasureKey(metric.getKey(), null);
}
};

public ComputeMeasureVariationsStep(DbClient dbClient, TreeRootHolder treeRootHolder, PeriodsHolder periodsHolder, MetricRepository metricRepository, public ComputeMeasureVariationsStep(DbClient dbClient, TreeRootHolder treeRootHolder, PeriodsHolder periodsHolder, MetricRepository metricRepository,
MeasureRepository measureRepository) { MeasureRepository measureRepository) {
this.dbClient = dbClient; this.dbClient = dbClient;
Expand All @@ -91,7 +85,7 @@ public ComputeMeasureVariationsStep(DbClient dbClient, TreeRootHolder treeRootHo
public void execute() { public void execute() {
DbSession dbSession = dbClient.openSession(false); DbSession dbSession = dbClient.openSession(false);
try { try {
List<Metric> metrics = from(metricRepository.getAll()).filter(NumericMetric.INSTANCE).toList(); List<Metric> metrics = StreamSupport.stream(metricRepository.getAll().spliterator(), false).filter(NumericMetric.INSTANCE::apply).collect(Collectors.toList());
new DepthTraversalTypeAwareCrawler(new VariationMeasuresVisitor(dbSession, metrics)) new DepthTraversalTypeAwareCrawler(new VariationMeasuresVisitor(dbSession, metrics))
.visit(treeRootHolder.getRoot()); .visit(treeRootHolder.getRoot());
} finally { } finally {
Expand All @@ -109,7 +103,7 @@ private class VariationMeasuresVisitor extends TypeAwareVisitorAdapter {
// measures on files are currently purged, so past measures are not available on files // measures on files are currently purged, so past measures are not available on files
super(CrawlerDepthLimit.reportMaxDepth(DIRECTORY).withViewsMaxDepth(SUBVIEW), PRE_ORDER); super(CrawlerDepthLimit.reportMaxDepth(DIRECTORY).withViewsMaxDepth(SUBVIEW), PRE_ORDER);
this.session = session; this.session = session;
this.metricIds = from(metrics).transform(MetricDtoToMetricId.INSTANCE).toSet(); this.metricIds = metrics.stream().map(MetricDtoToMetricId.INSTANCE::apply).collect(Collectors.toSet());
this.metrics = metrics; this.metrics = metrics;
} }


Expand All @@ -130,7 +124,9 @@ private MeasuresWithVariationRepository computeMeasuresWithVariations(Component
} }


private void setVariationMeasures(Component component, List<PastMeasureDto> pastMeasures, int period, MeasuresWithVariationRepository measuresWithVariationRepository) { private void setVariationMeasures(Component component, List<PastMeasureDto> pastMeasures, int period, MeasuresWithVariationRepository measuresWithVariationRepository) {
Map<MeasureKey, PastMeasureDto> pastMeasuresByMeasureKey = from(pastMeasures).uniqueIndex(pastMeasureToMeasureKey); Map<MeasureKey, PastMeasureDto> pastMeasuresByMeasureKey = pastMeasures
.stream()
.collect(uniqueIndex(m -> new MeasureKey(metricRepository.getById((long) m.getMetricId()).getKey(), null), identity()));
for (Metric metric : metrics) { for (Metric metric : metrics) {
Optional<Measure> measure = measureRepository.getRawMeasure(component, metric); Optional<Measure> measure = measureRepository.getRawMeasure(component, metric);
if (measure.isPresent() && !measure.get().hasVariations()) { if (measure.isPresent() && !measure.get().hasVariations()) {
Expand Down
Expand Up @@ -19,15 +19,16 @@
*/ */
package org.sonar.server.computation.task.projectanalysis.step; package org.sonar.server.computation.task.projectanalysis.step;


import com.google.common.base.Function; import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.sonar.api.utils.System2; import org.sonar.api.utils.System2;
import org.sonar.db.DbClient; import org.sonar.db.DbClient;
import org.sonar.db.DbSession; import org.sonar.db.DbSession;
import org.sonar.db.MyBatis; import org.sonar.db.MyBatis;
import org.sonar.db.event.EventDto; import org.sonar.db.event.EventDto;
import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder; import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
import org.sonar.server.computation.task.projectanalysis.component.Component; import org.sonar.server.computation.task.projectanalysis.component.Component;
import org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor;
import org.sonar.server.computation.task.projectanalysis.component.CrawlerDepthLimit; import org.sonar.server.computation.task.projectanalysis.component.CrawlerDepthLimit;
import org.sonar.server.computation.task.projectanalysis.component.DepthTraversalTypeAwareCrawler; import org.sonar.server.computation.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolder; import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolder;
Expand All @@ -36,8 +37,6 @@
import org.sonar.server.computation.task.projectanalysis.event.EventRepository; import org.sonar.server.computation.task.projectanalysis.event.EventRepository;
import org.sonar.server.computation.task.step.ComputationStep; import org.sonar.server.computation.task.step.ComputationStep;


import static com.google.common.collect.Iterables.transform;

public class PersistEventsStep implements ComputationStep { public class PersistEventsStep implements ComputationStep {


private final DbClient dbClient; private final DbClient dbClient;
Expand Down Expand Up @@ -68,55 +67,6 @@ public void execute() {
} }
} }


private void processEvents(DbSession session, Component component, Long analysisDate) {
Function<Event, EventDto> eventToEventDto = event -> newBaseEvent(component, analysisDate)
.setName(event.getName())
.setCategory(convertCategory(event.getCategory()))
.setDescription(event.getDescription())
.setData(event.getData());
// FIXME bulk insert
for (EventDto batchEventDto : transform(eventRepository.getEvents(component), eventToEventDto)) {
dbClient.eventDao().insert(session, batchEventDto);
}
}

private void saveVersionEvent(DbSession session, Component component, Long analysisDate) {
String version = component.getReportAttributes().getVersion();
if (version != null) {
deletePreviousEventsHavingSameVersion(session, version, component);
dbClient.eventDao().insert(session, newBaseEvent(component, analysisDate)
.setName(version)
.setCategory(EventDto.CATEGORY_VERSION));
}
}

private void deletePreviousEventsHavingSameVersion(DbSession session, String version, Component component) {
for (EventDto dto : dbClient.eventDao().selectByComponentUuid(session, component.getUuid())) {
if (dto.getCategory().equals(EventDto.CATEGORY_VERSION) && dto.getName().equals(version)) {
dbClient.eventDao().delete(session, dto.getId());
}
}
}

private EventDto newBaseEvent(Component component, Long analysisDate) {
return new EventDto()
.setAnalysisUuid(analysisMetadataHolder.getUuid())
.setComponentUuid(component.getUuid())
.setCreatedAt(system2.now())
.setDate(analysisDate);
}

private static String convertCategory(Event.Category category) {
switch (category) {
case ALERT:
return EventDto.CATEGORY_ALERT;
case PROFILE:
return EventDto.CATEGORY_PROFILE;
default:
throw new IllegalArgumentException(String.format("Unsupported category %s", category.name()));
}
}

@Override @Override
public String getDescription() { public String getDescription() {
return "Persist events"; return "Persist events";
Expand All @@ -126,8 +76,8 @@ private class PersistEventComponentVisitor extends TypeAwareVisitorAdapter {
private final DbSession session; private final DbSession session;
private final long analysisDate; private final long analysisDate;


public PersistEventComponentVisitor(DbSession session, long analysisDate) { PersistEventComponentVisitor(DbSession session, long analysisDate) {
super(CrawlerDepthLimit.PROJECT, ComponentVisitor.Order.PRE_ORDER); super(CrawlerDepthLimit.PROJECT, Order.PRE_ORDER);
this.session = session; this.session = session;
this.analysisDate = analysisDate; this.analysisDate = analysisDate;
} }
Expand All @@ -138,5 +88,54 @@ public void visitProject(Component project) {
saveVersionEvent(session, project, analysisDate); saveVersionEvent(session, project, analysisDate);
} }


private void processEvents(DbSession session, Component component, Long analysisDate) {
Function<Event, EventDto> eventToEventDto = event -> newBaseEvent(component, analysisDate)
.setName(event.getName())
.setCategory(convertCategory(event.getCategory()))
.setDescription(event.getDescription())
.setData(event.getData());
// FIXME bulk insert
for (EventDto batchEventDto : StreamSupport.stream(eventRepository.getEvents(component).spliterator(), false).map(eventToEventDto).collect(Collectors.toList())) {
dbClient.eventDao().insert(session, batchEventDto);
}
}

private void saveVersionEvent(DbSession session, Component component, Long analysisDate) {
String version = component.getReportAttributes().getVersion();
if (version != null) {
deletePreviousEventsHavingSameVersion(session, version, component);
dbClient.eventDao().insert(session, newBaseEvent(component, analysisDate)
.setName(version)
.setCategory(EventDto.CATEGORY_VERSION));
}
}

private void deletePreviousEventsHavingSameVersion(DbSession session, String version, Component component) {
for (EventDto dto : dbClient.eventDao().selectByComponentUuid(session, component.getUuid())) {
if (dto.getCategory().equals(EventDto.CATEGORY_VERSION) && dto.getName().equals(version)) {
dbClient.eventDao().delete(session, dto.getId());
}
}
}

private EventDto newBaseEvent(Component component, Long analysisDate) {
return new EventDto()
.setAnalysisUuid(analysisMetadataHolder.getUuid())
.setComponentUuid(component.getUuid())
.setCreatedAt(system2.now())
.setDate(analysisDate);
}

private String convertCategory(Event.Category category) {
switch (category) {
case ALERT:
return EventDto.CATEGORY_ALERT;
case PROFILE:
return EventDto.CATEGORY_PROFILE;
default:
throw new IllegalArgumentException(String.format("Unsupported category %s", category.name()));
}
}

} }
} }
Expand Up @@ -19,14 +19,12 @@
*/ */
package org.sonar.server.computation.task.projectanalysis.step; package org.sonar.server.computation.task.projectanalysis.step;


import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import javax.annotation.Nullable;
import org.sonar.api.i18n.I18n; import org.sonar.api.i18n.I18n;
import org.sonar.db.DbClient; import org.sonar.db.DbClient;
import org.sonar.db.DbSession; import org.sonar.db.DbSession;
Expand Down Expand Up @@ -120,22 +118,19 @@ private void mergeLinks(DbSession session, String componentUuid, List<ScannerRep
throw new IllegalArgumentException(String.format("Link of type '%s' has already been declared on component '%s'", type, componentUuid)); throw new IllegalArgumentException(String.format("Link of type '%s' has already been declared on component '%s'", type, componentUuid));
} }


ComponentLinkDto previousLink = Iterables.find(previousLinks, new Predicate<ComponentLinkDto>() { Optional<ComponentLinkDto> previousLink = previousLinks.stream()
@Override .filter(input -> input != null && input.getType().equals(convertType(link.getType())))
public boolean apply(@Nullable ComponentLinkDto input) { .findFirst();
return input != null && input.getType().equals(convertType(link.getType())); if (previousLink.isPresent()) {
} previousLink.get().setHref(link.getHref());
}, null); dbClient.componentLinkDao().update(session, previousLink.get());
if (previousLink == null) { } else {
dbClient.componentLinkDao().insert(session, dbClient.componentLinkDao().insert(session,
new ComponentLinkDto() new ComponentLinkDto()
.setComponentUuid(componentUuid) .setComponentUuid(componentUuid)
.setType(type) .setType(type)
.setName(i18n.message(Locale.ENGLISH, "project_links." + type, null)) .setName(i18n.message(Locale.ENGLISH, "project_links." + type, null))
.setHref(link.getHref())); .setHref(link.getHref()));
} else {
previousLink.setHref(link.getHref());
dbClient.componentLinkDao().update(session, previousLink);
} }
} }


Expand Down

0 comments on commit de20887

Please sign in to comment.