Skip to content

Commit

Permalink
Optimized imports and reformatted code (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusoe authored Dec 4, 2019
1 parent 6b996d0 commit acbe33d
Showing 29 changed files with 246 additions and 250 deletions.
21 changes: 12 additions & 9 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ plugins {
apply plugin: 'io.spring.dependency-management'

group 'de.novatec'
version '1.0-SNAPSHOT'
version '0.2-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
@@ -17,22 +17,25 @@ test {
}

dependencies {
implementation(
"org.springframework.boot:spring-boot-starter",
"org.hibernate:hibernate-validator:6.1.0.Final"
annotationProcessor(
"org.projectlombok:lombok:1.18.4"
)

compileOnly "org.projectlombok:lombok:1.18.4"
annotationProcessor "org.projectlombok:lombok:1.18.4"
implementation group: 'org.apache.commons', name: 'commons-math3', version: '3.0'
compileOnly(
"org.projectlombok:lombok:1.18.4"
)

implementation group: 'org.influxdb', name: 'influxdb-java', version: '2.15'
implementation(
"org.springframework.boot:spring-boot-starter",
"org.hibernate:hibernate-validator:6.1.0.Final",
"org.influxdb:influxdb-java:2.15",
"org.apache.commons:commons-math3:3.0"
)

testImplementation(
'org.springframework.boot:spring-boot-starter-test:2.1.6.RELEASE',
'org.junit.jupiter:junit-jupiter-api:5.3.1',
'org.mockito:mockito-junit-jupiter:2.23.0'

)

}
6 changes: 0 additions & 6 deletions src/main/java/de/novatec/baselining/Application.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
package de.novatec.baselining;

import de.novatec.baselining.config.BaselineServiceSettings;
import de.novatec.baselining.datasources.GaugeDataSource;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
@Slf4j
public class Application {
20 changes: 10 additions & 10 deletions src/main/java/de/novatec/baselining/BaseliningService.java
Original file line number Diff line number Diff line change
@@ -38,10 +38,10 @@ void start() {
baselines.addAll(buildRateBaselines());
baselines.addAll(buildCounterBaselines());
baselines.addAll(buildCounterRatioBaselines());
baselines.forEach(blg -> lastUpdatedTimestamp.put(blg,start));
baselines.forEach(blg -> lastUpdatedTimestamp.put(blg, start));

new Thread(() -> {
while(true) {
while (true) {
updateAll();
try {
Thread.sleep(5000);
@@ -53,18 +53,18 @@ void start() {
}

private void updateAll() {
for(BaselineGenerator generator : baselines) {
for (BaselineGenerator generator : baselines) {
long now = System.currentTimeMillis() - config.getUpdateDelay().toMillis() - generator.getMinimumDelayMillis();
long updateTimestamp = lastUpdatedTimestamp.get(generator);
try {
while(generator.getIntervalIndex(now) != generator.getIntervalIndex(updateTimestamp)) {
while (generator.getIntervalIndex(now) != generator.getIntervalIndex(updateTimestamp)) {
long updateTo = Math.min(now, updateTimestamp + generator.getMaxUpdateIntervalSizeMillis());
generator.updateBaselines(updateTimestamp, updateTo);
lastUpdatedTimestamp.put(generator, updateTo);
updateTimestamp = updateTo;
}
} catch(Throwable t) {
log.error("An error occurred updating the baseline",t);
} catch (Throwable t) {
log.error("An error occurred updating the baseline", t);
}
}
}
@@ -82,7 +82,7 @@ private List<BaselineGenerator> buildGaugeBaselines() {
private List<BaselineGenerator> buildCounterBaselines() {
return config.getCounters().stream()
.map(definition -> {
CounterDataSource src = new CounterDataSource(influx,definition);
CounterDataSource src = new CounterDataSource(influx, definition);
return buildBaselineGenerator(definition, src);
})
.collect(Collectors.toList());
@@ -91,7 +91,7 @@ private List<BaselineGenerator> buildCounterBaselines() {
private List<BaselineGenerator> buildCounterRatioBaselines() {
return config.getCounterRatios().stream()
.map(definition -> {
CounterRatioDataSource src = new CounterRatioDataSource(influx,definition);
CounterRatioDataSource src = new CounterRatioDataSource(influx, definition);
return buildBaselineGenerator(definition, src);
})
.collect(Collectors.toList());
@@ -100,13 +100,13 @@ private List<BaselineGenerator> buildCounterRatioBaselines() {
private List<BaselineGenerator> buildRateBaselines() {
return config.getRates().stream()
.map(definition -> {
RateBaselineSource src = new RateBaselineSource(influx,definition);
RateBaselineSource src = new RateBaselineSource(influx, definition);
return buildBaselineGenerator(definition, src);
})
.collect(Collectors.toList());
}

private BaselineGenerator buildBaselineGenerator(AbstractBaselineDefinition definition, BaselineDataSource source) {
return new BaselineGenerator(influx,source,definition);
return new BaselineGenerator(influx, source, definition);
}
}
36 changes: 18 additions & 18 deletions src/main/java/de/novatec/baselining/InfluxAccess.java
Original file line number Diff line number Diff line change
@@ -29,15 +29,15 @@ public class InfluxAccess {
private InfluxDB influx;

void connect() {
if(influx== null) {
if (influx == null) {
OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient().newBuilder()
.connectTimeout(config.getConnectTimeout().getSeconds(), TimeUnit.SECONDS)
.readTimeout(config.getReadTimeout().getSeconds(), TimeUnit.SECONDS)
.writeTimeout(config.getWriteTimeout().getSeconds(), TimeUnit.SECONDS);

boolean userEmpty = StringUtils.isEmpty(config.getUser());
boolean passwordEmpty = StringUtils.isEmpty(config.getPassword());
if(userEmpty && passwordEmpty) {
if (userEmpty && passwordEmpty) {
influx = InfluxDBFactory.connect(config.getUrl().toString(), okHttpClientBuilder);
} else {
influx = InfluxDBFactory.connect(config.getUrl().toString(), config.getUser(), config.getPassword(), okHttpClientBuilder);
@@ -56,17 +56,17 @@ public QueryResult query(String selectFrom, long startMillis, long endMillis) {
return query(selectFrom, null, "*", startMillis, endMillis);
}

public QueryResult query(String selectFrom,String filter, String groupBy, long startMillis, long endMillis) {
public QueryResult query(String selectFrom, String filter, String groupBy, long startMillis, long endMillis) {
StringBuilder query = new StringBuilder(selectFrom);
query.append(" WHERE ").append(buildTimeFilter(startMillis, endMillis));
if(!StringUtils.isEmpty(filter)) {
if (!StringUtils.isEmpty(filter)) {
query.append(" AND ").append(filter);
}
query.append(" GROUP BY ").append(groupBy);
try {
connect();
return influx.query(new Query(query.toString()), TimeUnit.MILLISECONDS);
} catch(Throwable t) {
} catch (Throwable t) {
try {
influx.close();
} catch (Exception e) {
@@ -83,22 +83,22 @@ public Map<TagValues, List<DataPoint>> querySingleField(String selectFrom, long
}

public Map<TagValues, List<DataPoint>> queryAggregate(String selectFrom, long startMillis, long endMillis, long intervalMillis) {
String groupBy = "*, time("+intervalMillis+"ms) fill(none)";
QueryResult queryResult = query(selectFrom,null,groupBy, startMillis, endMillis);
String groupBy = "*, time(" + intervalMillis + "ms) fill(none)";
QueryResult queryResult = query(selectFrom, null, groupBy, startMillis, endMillis);
return extractSeriesResults(queryResult);
}

private Map<TagValues, List<DataPoint>> extractSeriesResults(QueryResult queryResult) {
if(queryResult.getError() != null) {
if (queryResult.getError() != null) {
throw new RuntimeException("Influx Returned Error: " + queryResult.getError());
}
if(queryResult.getResults() != null) {
if (queryResult.getResults() != null) {
return queryResult.getResults().stream()
.filter(Objects::nonNull)
.map(result -> result.getSeries())
.filter(Objects::nonNull)
.flatMap(List::stream)
.filter(series -> series.getValues()!= null && !series.getValues().isEmpty())
.filter(series -> series.getValues() != null && !series.getValues().isEmpty())
.collect(Collectors.toMap(
series -> TagValues.from(series.getTags()),
series -> seriesToPoints(series)
@@ -109,17 +109,17 @@ private Map<TagValues, List<DataPoint>> extractSeriesResults(QueryResult queryRe
}

private List<DataPoint> seriesToPoints(QueryResult.Series series) {
if(series.getColumns().size() != 2) {
if (series.getColumns().size() != 2) {
throw new IllegalArgumentException("Query returned more than one non-time field: " + series.getColumns());
}
int timeIndex = series.getColumns().indexOf("time");
int fieldIndex = timeIndex == 0 ? 1 : 0;
return series.getValues().stream()
.filter(vals -> vals.get(fieldIndex) != null)
.map(values -> DataPoint.builder()
.time(((Number)values.get(timeIndex)).longValue())
.value(((Number)values.get(fieldIndex)).doubleValue())
.build())
.time(((Number) values.get(timeIndex)).longValue())
.value(((Number) values.get(fieldIndex)).doubleValue())
.build())
.collect(Collectors.toList());
}

@@ -132,13 +132,13 @@ public void writePoints(String database, String retention, String measurement, M
.addField("value", pt.getValue())
.build())
.collect(Collectors.toList());
if(!converted.isEmpty()) {
writePoints(database,retention,tags.getTags(),converted);
if (!converted.isEmpty()) {
writePoints(database, retention, tags.getTags(), converted);
}
});
}

public void writePoints(String database,String retention, Map<String,String> tags, Collection<Point> points) {
public void writePoints(String database, String retention, Map<String, String> tags, Collection<Point> points) {
BatchPoints.Builder builder = BatchPoints.database(database)
.retentionPolicy(retention)
.precision(TimeUnit.MILLISECONDS);
@@ -152,7 +152,7 @@ public void writePoints(String database,String retention, Map<String,String> tag
try {
connect();
influx.write(builder.build());
} catch(Throwable t) {
} catch (Throwable t) {
try {
influx.close();
} catch (Exception e) {
18 changes: 9 additions & 9 deletions src/main/java/de/novatec/baselining/InfluxUtils.java
Original file line number Diff line number Diff line change
@@ -26,38 +26,38 @@ public static String getMeasurement(String dbRpMeasurement) {

private static String[] splitToSegments(String dbRpMeasurement) {
String[] segments = dbRpMeasurement.split("\\.");
if(segments.length != 3) {
throw new IllegalArgumentException("'"+dbRpMeasurement+"' is not int the form <database>.<retention>.<measurement>");
if (segments.length != 3) {
throw new IllegalArgumentException("'" + dbRpMeasurement + "' is not int the form <database>.<retention>.<measurement>");
}
return segments;
}

private static String unquote(String segment) {
return segment
.replace("\"","")
.replace("'","");
.replace("\"", "")
.replace("'", "");
}

public static String prettyPrintDuration(long millis) {
long timeLeft = millis;
StringBuilder result = new StringBuilder();
if(timeLeft / MILLIS_PER_DAY > 0) {
if (timeLeft / MILLIS_PER_DAY > 0) {
result.append(timeLeft / MILLIS_PER_DAY).append("d");
timeLeft = timeLeft % MILLIS_PER_DAY;
}
if(timeLeft / MILLIS_PER_HOUR > 0) {
if (timeLeft / MILLIS_PER_HOUR > 0) {
result.append(timeLeft / MILLIS_PER_HOUR).append("h");
timeLeft = timeLeft % MILLIS_PER_HOUR;
}
if(timeLeft / MILLIS_PER_MINUTE > 0) {
if (timeLeft / MILLIS_PER_MINUTE > 0) {
result.append(timeLeft / MILLIS_PER_MINUTE).append("m");
timeLeft = timeLeft % MILLIS_PER_MINUTE;
}
if(timeLeft / MILLIS_PER_SECOND > 0) {
if (timeLeft / MILLIS_PER_SECOND > 0) {
result.append(timeLeft / MILLIS_PER_SECOND).append("s");
timeLeft = timeLeft % MILLIS_PER_SECOND;
}
if(timeLeft > 0 ||result.length() == 0) {
if (timeLeft > 0 || result.length() == 0) {
result.append(timeLeft).append("ms");
}
return result.toString();
Loading

0 comments on commit acbe33d

Please sign in to comment.