Skip to content

Commit

Permalink
s/scala/java+groovy (#18)
Browse files Browse the repository at this point in the history
Assemble the set of atlas results into map, grouped by id.
  • Loading branch information
Matt Duftler committed May 17, 2017
1 parent 60e1bf8 commit 6ed889d
Show file tree
Hide file tree
Showing 8 changed files with 347 additions and 177 deletions.
25 changes: 1 addition & 24 deletions kayenta-atlas/kayenta-atlas.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
apply plugin: 'scala'

dependencies {
compile project(":kayenta-core")
compile spinnaker.dependency('bootWeb')
Expand All @@ -8,26 +6,5 @@ dependencies {

compile "org.apache.commons:commons-io:1.3.2"

compile 'org.scala-lang:scala-library-all:2.11.8'

testCompile 'org.scalatest:scalatest_2.11:latest.release'
testCompile 'org.pegdown:pegdown:latest.release'
}

test << {
ant.taskdef(
name: 'scalatest',
classname: 'org.scalatest.tools.ScalaTestAntTask',
classpath: classpath.asPath)

ant.scalatest(
runpath: testClassesDir,
fork: 'false',
haltonfailure: 'false',
suffixes: 'Suite') {
wildcard(package: 'com.netflix')
reporter(type: 'html', directory: "${buildDir}/reports/tests")
reporter(type: 'junitxml', directory: "${buildDir}/test-results/junit")
reporter(type: 'stderr', config: "WFD")
}
testCompile spinnaker.dependency('groovy')
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Builder
@Slf4j
Expand Down Expand Up @@ -69,7 +67,7 @@ public List<MetricSet> queryMetrics(String accountName,
AtlasRemoteService atlasRemoteService = credentials.getAtlasRemoteService();
AtlasResults atlasResults = atlasRemoteService.fetch("name,randomValue,:eq,:sum,(,name,),:by", "std.json");
Instant responseStartTimeInstant = Instant.ofEpochMilli(atlasResults.getStart());
List<List<Double>> timeSeriesList = atlasResults.getValues();
List<Double> timeSeriesList = atlasResults.getData().getValues();

if (timeSeriesList == null) {
timeSeriesList = new ArrayList<>();
Expand All @@ -78,11 +76,6 @@ public List<MetricSet> queryMetrics(String accountName,
// TODO: Get sample Atlas response with more than one set of results.
// Deferring this for now since we're going to move to the /fetch endpoint once that's available in oss Atlas.
// We are currently developing against canned output retrieved via OSS Atlas's /graph endpoint.
List<Double> pointValues =
timeSeriesList
.stream()
.map(timeSeries -> timeSeries.get(0))
.collect(Collectors.toList());

// TODO: Get the metric set name from the request/canary-config.
MetricSet.MetricSetBuilder metricSetBuilder =
Expand All @@ -91,7 +84,7 @@ public List<MetricSet> queryMetrics(String accountName,
.startTimeMillis(atlasResults.getStart())
.startTimeIso(responseStartTimeInstant.toString())
.stepMillis(atlasResults.getStep())
.values(pointValues);
.values(timeSeriesList);

// TODO: These have to come from the Atlas response. Just not sure from where exactly yet.
Map<String, String> tags = ImmutableMap.of("not-sure", "about-tags");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,56 @@

package com.netflix.kayenta.atlas.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.List;
import javax.validation.constraints.NotNull;
import java.util.Map;

@Data
@Builder
@ToString
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
public class AtlasResults {

@NotNull
@Getter
private String type;

@NotNull
@Getter
private String id;

@NotNull
@Getter
private String query;

@NotNull
@Getter
private String label;

@NotNull
@Getter
private long start;

@NotNull
@Getter
private long step;

private List<String> legend;
@NotNull
@Getter
private long end;

@JsonProperty("metrics")
private List<Map<String, String>> metricsDescriptors;
@NotNull
@Getter
private Map<String, String> tags;

private List<List<Double>> values;
@NotNull
@Getter
private TimeseriesData data;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright 2017 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.kayenta.atlas.model;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;

import static java.util.stream.Collectors.toList;

public class AtlasResultsHelper {

private static AtlasResults mergeByTime(List<AtlasResults> atlasResultsList) {
if (atlasResultsList == null || atlasResultsList.isEmpty()) {
return null;
}

atlasResultsList.sort(Comparator.comparingLong(AtlasResults::getStart));

AtlasResults firstAtlasResults = atlasResultsList.get(0);
AtlasResults lastAtlasResults = atlasResultsList.get(atlasResultsList.size() - 1);
AtlasResults.AtlasResultsBuilder atlasResultsBuilder =
AtlasResults
.builder()
.type(firstAtlasResults.getType())
.id(firstAtlasResults.getId())
.query(firstAtlasResults.getQuery())
.label(firstAtlasResults.getLabel())
.start(firstAtlasResults.getStart())
.step(firstAtlasResults.getStep())
.end(lastAtlasResults.getEnd())
.tags(firstAtlasResults.getTags());
List<Double> values = new ArrayList<>();
Long lastTimestamp = null;

for (AtlasResults atlasResults : atlasResultsList) {
if (lastTimestamp != null) {
long nextTimestamp = atlasResults.getStart();
long offset = (nextTimestamp - lastTimestamp) / atlasResults.getStep();
List<Double> padding =
DoubleStream
.generate(() -> Double.NaN)
.limit(offset)
.boxed()
.collect(toList());

values.addAll(padding);
}

values.addAll(atlasResults.getData().getValues());
lastTimestamp = atlasResults.getEnd();
}

return atlasResultsBuilder.data(TimeseriesData.builder().values(values).type(firstAtlasResults.getData().getType()).build()).build();
}

static Map<String, AtlasResults> merge(List<AtlasResults> atlasResultsList) {
return atlasResultsList
.stream()
.collect(Collectors.groupingBy(AtlasResults::getId))
.entrySet()
.stream()
.collect(Collectors.toMap(e -> e.getKey(), e -> mergeByTime(e.getValue())));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2017 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.kayenta.atlas.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

import javax.validation.constraints.NotNull;
import java.util.List;

@Builder
@ToString
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
public class TimeseriesData {

@NotNull
@Getter
private String type;

@NotNull
@Getter
private List<Double> values;
}

This file was deleted.

Loading

0 comments on commit 6ed889d

Please sign in to comment.