Skip to content

Commit

Permalink
feat: ComponentMeasure -> Analyzer for data analysis instead of measure
Browse files Browse the repository at this point in the history
  • Loading branch information
metacosm committed Nov 26, 2024
1 parent a463646 commit ce6d7b1
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package net.laprun.sustainability.power.measure;

public interface Analyzer {
void recordComponentValue(double value, long timestamp);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@

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

public class DescriptiveStatisticsComponentMeasure implements ComponentMeasure {
@SuppressWarnings("unused")
public class DescriptiveStatisticsAnalyzer implements Analyzer {
private final DescriptiveStatistics statistics;

public DescriptiveStatisticsComponentMeasure() {
public DescriptiveStatisticsAnalyzer() {
statistics = new DescriptiveStatistics();
}

@Override
public void recordComponentValue(double value) {
public void recordComponentValue(double value, long timestamp) {
statistics.addValue(value);
}

@Override
public double[] getComponentValues() {
return statistics.getValues();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package net.laprun.sustainability.power.measure;

import org.HdrHistogram.IntCountsHistogram;

@SuppressWarnings("unused")
public class HdrHistogramAnalyzer implements Analyzer {
private static final int HIGHEST_TRACKABLE_VALUE = 1_000_000;
private static final int NUMBER_OF_SIGNIFICANT_VALUE_DIGITS = 4;
private static final int CONVERSION_FACTOR = 1000;
private final IntCountsHistogram histogram;

public HdrHistogramAnalyzer() {
histogram = new IntCountsHistogram(HIGHEST_TRACKABLE_VALUE, NUMBER_OF_SIGNIFICANT_VALUE_DIGITS);
}

@Override
public void recordComponentValue(double value, long timestamp) {
histogram.recordValue((long) (CONVERSION_FACTOR * value));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.time.Duration;
import java.util.BitSet;
import java.util.Objects;

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

Expand All @@ -21,8 +22,9 @@ public class OngoingPowerMeasure implements PowerMeasure {
private int samples;
private final double[][] measures;
private long[] timestamps;
private final Analyzer[] analyzers;

public OngoingPowerMeasure(SensorMetadata sensorMetadata) {
public OngoingPowerMeasure(SensorMetadata sensorMetadata, Analyzer... analyzers) {
this.sensorMetadata = sensorMetadata;
startedAt = System.currentTimeMillis();

Expand All @@ -36,6 +38,7 @@ public OngoingPowerMeasure(SensorMetadata sensorMetadata) {
// 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
nonZeroComponents = new BitSet(numComponents);
totalComponents = sensorMetadata.totalComponents();
this.analyzers = Objects.requireNonNullElseGet(analyzers, () -> new Analyzer[0]);
}

@Override
Expand Down Expand Up @@ -89,8 +92,12 @@ private void recordComponentValue(int component, double value) {
System.arraycopy(timestamps, 0, newTimestamps, 0, currentSize);
timestamps = newTimestamps;
}
timestamps[component] = System.currentTimeMillis();
final var timestamp = System.currentTimeMillis();
timestamps[component] = timestamp;
measures[component][samples - 1] = value;
for (var analyzer : analyzers) {
analyzer.recordComponentValue(value, timestamp);
}
}

@Override
Expand Down Expand Up @@ -148,4 +155,9 @@ public double[] getMeasuresFor(int component) {
System.arraycopy(measures[component], 0, dest, 0, samples);
return dest;
}

@Override
public Analyzer[] analyzers() {
return analyzers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ default double average() {

double[] getMeasuresFor(int component);

Analyzer[] analyzers();

/**
* Records the standard deviations for the aggregated energy comsumption value (as returned by {@link #total()}) and
* per component
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class StoppedPowerMeasure implements PowerMeasure {
private final double[] averages;
private final StdDev standardDeviations;
private final double[][] measures;
private final Analyzer[] analyzers;

public StoppedPowerMeasure(PowerMeasure powerMeasure) {
this.sensorMetadata = powerMeasure.metadata();
Expand All @@ -30,6 +31,7 @@ public StoppedPowerMeasure(PowerMeasure powerMeasure) {
for (int i = 0; i < cardinality; i++) {
measures[i] = powerMeasure.getMeasuresFor(i);
}
analyzers = powerMeasure.analyzers();
}

@Override
Expand Down Expand Up @@ -76,4 +78,9 @@ public StdDev standardDeviations() {
public double[] getMeasuresFor(int component) {
return measures[component];
}

@Override
public Analyzer[] analyzers() {
return analyzers;
}
}

0 comments on commit ce6d7b1

Please sign in to comment.