Skip to content

Commit

Permalink
fix(stats): Remove synthetic stages from Stages list
Browse files Browse the repository at this point in the history
  • Loading branch information
Travis Tomsu committed Sep 12, 2019
1 parent 239e691 commit 500d45b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -109,8 +110,14 @@ public void processEvent(Event event) {
Execution.Trigger.Type.valueOf(
trigger.getOrDefault("type", "UNKNOWN").toString().toUpperCase());

List<Map> stages = (List<Map>) execution.getOrDefault("stages", new ArrayList<>());
List<Stage> protoStages = stages.stream().map(this::toStage).collect(Collectors.toList());
List<Map<String, Object>> stages =
(List<Map<String, Object>>) execution.getOrDefault("stages", new ArrayList<>());
List<Stage> protoStages =
stages.stream()
.map(this::toStage)
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());

Execution.Builder executionBuilder =
Execution.newBuilder()
Expand Down Expand Up @@ -147,7 +154,15 @@ public void processEvent(Event event) {
}
}

private Stage toStage(Map stage) {
@SuppressWarnings("unchecked")
private Optional<Stage> toStage(Map<String, Object> stage) {
// Only interested in user-configured stages.
if (stage.get("syntheticStageOwner") != null
&& !stage.get("syntheticStageOwner").toString().isEmpty()) {
log.debug("Discarding synthetic stage");
return Optional.empty();
}

Status status =
Status.valueOf(stage.getOrDefault("status", "UNKNOWN").toString().toUpperCase());
Stage.Builder stageBuilder =
Expand All @@ -164,7 +179,7 @@ private Stage toStage(Map stage) {
// TODO(ttomsu): Figure out how to detect Kubernetes "flavor" - i.e. GKE, EKS, vanilla, etc.
}

return stageBuilder.build();
return Optional.of(stageBuilder.build());
}

private String hash(String clearText) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ class TelemetryEventListenerSpec extends Specification {
stages : [
[
type : "deploy",
status: "SUCCEEDED",
syntheticStageOwner: null,
context: [
"cloudProvider": "nine"
]
],
[
type: "removed",
syntheticStageOwner: "somethingNonNull",
status: "SUCCEEDED"
],
[
Expand Down Expand Up @@ -151,6 +160,7 @@ class TelemetryEventListenerSpec extends Specification {
Stage stage1 = stages.get(0)
assert stage1.type == "deploy"
assert stage1.status == Status.SUCCEEDED
assert stage1.cloudProvider.id == "nine"

Stage stage2 = stages.get(1)
assert stage2.type == "wait"
Expand Down

0 comments on commit 500d45b

Please sign in to comment.