Skip to content

Commit ce6d7b1

Browse files
committed
feat: ComponentMeasure -> Analyzer for data analysis instead of measure
1 parent a463646 commit ce6d7b1

File tree

8 files changed

+52
-56
lines changed

8 files changed

+52
-56
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package net.laprun.sustainability.power.measure;
2+
3+
public interface Analyzer {
4+
void recordComponentValue(double value, long timestamp);
5+
}

measure/src/main/java/net/laprun/sustainability/power/measure/ComponentMeasure.java

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,16 @@
22

33
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
44

5-
public class DescriptiveStatisticsComponentMeasure implements ComponentMeasure {
5+
@SuppressWarnings("unused")
6+
public class DescriptiveStatisticsAnalyzer implements Analyzer {
67
private final DescriptiveStatistics statistics;
78

8-
public DescriptiveStatisticsComponentMeasure() {
9+
public DescriptiveStatisticsAnalyzer() {
910
statistics = new DescriptiveStatistics();
1011
}
1112

1213
@Override
13-
public void recordComponentValue(double value) {
14+
public void recordComponentValue(double value, long timestamp) {
1415
statistics.addValue(value);
1516
}
16-
17-
@Override
18-
public double[] getComponentValues() {
19-
return statistics.getValues();
20-
}
2117
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package net.laprun.sustainability.power.measure;
2+
3+
import org.HdrHistogram.IntCountsHistogram;
4+
5+
@SuppressWarnings("unused")
6+
public class HdrHistogramAnalyzer implements Analyzer {
7+
private static final int HIGHEST_TRACKABLE_VALUE = 1_000_000;
8+
private static final int NUMBER_OF_SIGNIFICANT_VALUE_DIGITS = 4;
9+
private static final int CONVERSION_FACTOR = 1000;
10+
private final IntCountsHistogram histogram;
11+
12+
public HdrHistogramAnalyzer() {
13+
histogram = new IntCountsHistogram(HIGHEST_TRACKABLE_VALUE, NUMBER_OF_SIGNIFICANT_VALUE_DIGITS);
14+
}
15+
16+
@Override
17+
public void recordComponentValue(double value, long timestamp) {
18+
histogram.recordValue((long) (CONVERSION_FACTOR * value));
19+
}
20+
}

measure/src/main/java/net/laprun/sustainability/power/measure/HdrHistogramComponentMeasure.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

measure/src/main/java/net/laprun/sustainability/power/measure/OngoingPowerMeasure.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.time.Duration;
44
import java.util.BitSet;
5+
import java.util.Objects;
56

67
import org.apache.commons.math3.util.FastMath;
78

@@ -21,8 +22,9 @@ public class OngoingPowerMeasure implements PowerMeasure {
2122
private int samples;
2223
private final double[][] measures;
2324
private long[] timestamps;
25+
private final Analyzer[] analyzers;
2426

25-
public OngoingPowerMeasure(SensorMetadata sensorMetadata) {
27+
public OngoingPowerMeasure(SensorMetadata sensorMetadata, Analyzer... analyzers) {
2628
this.sensorMetadata = sensorMetadata;
2729
startedAt = System.currentTimeMillis();
2830

@@ -36,6 +38,7 @@ public OngoingPowerMeasure(SensorMetadata sensorMetadata) {
3638
// we don't need to record the total component as a non-zero component since it's almost never zero and we compute the std dev separately
3739
nonZeroComponents = new BitSet(numComponents);
3840
totalComponents = sensorMetadata.totalComponents();
41+
this.analyzers = Objects.requireNonNullElseGet(analyzers, () -> new Analyzer[0]);
3942
}
4043

4144
@Override
@@ -89,8 +92,12 @@ private void recordComponentValue(int component, double value) {
8992
System.arraycopy(timestamps, 0, newTimestamps, 0, currentSize);
9093
timestamps = newTimestamps;
9194
}
92-
timestamps[component] = System.currentTimeMillis();
95+
final var timestamp = System.currentTimeMillis();
96+
timestamps[component] = timestamp;
9397
measures[component][samples - 1] = value;
98+
for (var analyzer : analyzers) {
99+
analyzer.recordComponentValue(value, timestamp);
100+
}
94101
}
95102

96103
@Override
@@ -148,4 +155,9 @@ public double[] getMeasuresFor(int component) {
148155
System.arraycopy(measures[component], 0, dest, 0, samples);
149156
return dest;
150157
}
158+
159+
@Override
160+
public Analyzer[] analyzers() {
161+
return analyzers;
162+
}
151163
}

measure/src/main/java/net/laprun/sustainability/power/measure/PowerMeasure.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ default double average() {
6363

6464
double[] getMeasuresFor(int component);
6565

66+
Analyzer[] analyzers();
67+
6668
/**
6769
* Records the standard deviations for the aggregated energy comsumption value (as returned by {@link #total()}) and
6870
* per component

measure/src/main/java/net/laprun/sustainability/power/measure/StoppedPowerMeasure.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class StoppedPowerMeasure implements PowerMeasure {
1515
private final double[] averages;
1616
private final StdDev standardDeviations;
1717
private final double[][] measures;
18+
private final Analyzer[] analyzers;
1819

1920
public StoppedPowerMeasure(PowerMeasure powerMeasure) {
2021
this.sensorMetadata = powerMeasure.metadata();
@@ -30,6 +31,7 @@ public StoppedPowerMeasure(PowerMeasure powerMeasure) {
3031
for (int i = 0; i < cardinality; i++) {
3132
measures[i] = powerMeasure.getMeasuresFor(i);
3233
}
34+
analyzers = powerMeasure.analyzers();
3335
}
3436

3537
@Override
@@ -76,4 +78,9 @@ public StdDev standardDeviations() {
7678
public double[] getMeasuresFor(int component) {
7779
return measures[component];
7880
}
81+
82+
@Override
83+
public Analyzer[] analyzers() {
84+
return analyzers;
85+
}
7986
}

0 commit comments

Comments
 (0)