-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: start/stop measure from dev UI
- Loading branch information
Showing
6 changed files
with
158 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 35 additions & 6 deletions
41
runtime/src/main/java/net/laprun/sustainability/power/quarkus/runtime/Metadata.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,49 @@ | ||
package net.laprun.sustainability.power.quarkus.runtime; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import java.net.URI; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
|
||
import net.laprun.sustainability.power.SensorMetadata; | ||
|
||
import java.net.URI; | ||
import java.util.Optional; | ||
public class Metadata<T> { | ||
private final URI powerServerURI; | ||
private final String status; | ||
private final List<T> local; | ||
private final List<T> remote; | ||
private final Function<SensorMetadata.ComponentMetadata, T> converter; | ||
private final String documentation; | ||
|
||
public record Metadata(URI powerServerURI, Optional<SensorMetadata> remote, Optional<SensorMetadata> local, String status) { | ||
public Metadata(URI powerServerURI, String documentation, List<SensorMetadata.ComponentMetadata> remote, List<SensorMetadata.ComponentMetadata> local, String status, Function<SensorMetadata.ComponentMetadata, T> converter) { | ||
this.powerServerURI = powerServerURI; | ||
this.converter = converter; | ||
this.remote = converted(remote); | ||
this.local = converted(local); | ||
this.status = status; | ||
this.documentation = documentation; | ||
} | ||
|
||
private List<T> converted(List<SensorMetadata.ComponentMetadata> metadata) { | ||
return metadata.stream() | ||
.sorted(Comparator.comparing(SensorMetadata.ComponentMetadata::index)) | ||
.map(converter) | ||
.toList(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Connected to " + powerServerURI + " (status: " + status | ||
+ ")\n====\nLocal metadata (including synthetic components, if any):\n" | ||
+ Optional.ofNullable(local).map(Object::toString).orElse("No ongoing measure") | ||
+ (local.isEmpty() ? "No ongoing measure" : local) | ||
+ "\n====\nSensor metadata:\n" + remote; | ||
} | ||
|
||
public List<T> local() { | ||
return local; | ||
} | ||
|
||
public List<T> remote() { | ||
return remote; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 33 additions & 9 deletions
42
...ime/src/main/java/net/laprun/sustainability/power/quarkus/runtime/devui/PowerService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,50 @@ | ||
package net.laprun.sustainability.power.quarkus.runtime.devui; | ||
|
||
import java.util.List; | ||
import java.util.function.Function; | ||
|
||
import jakarta.annotation.PostConstruct; | ||
import jakarta.inject.Inject; | ||
|
||
import net.laprun.sustainability.power.SensorMetadata; | ||
import net.laprun.sustainability.power.quarkus.runtime.PowerMeasurer; | ||
|
||
public class PowerService { | ||
public static final Function<SensorMetadata.ComponentMetadata, ComponentMetadata> converter = cm -> new ComponentMetadata(cm.name(), cm.index(), cm.description(), cm.unitAsSymbol()); | ||
@Inject | ||
PowerMeasurer measurer; | ||
private String status; | ||
|
||
@PostConstruct | ||
public void init() { | ||
measurer.withCompletedHandler((stoppedMeasure) -> status = stoppedMeasure.toString()); | ||
} | ||
|
||
public boolean isRunning() { | ||
return measurer.isRunning(); | ||
} | ||
|
||
public String status() { | ||
return measurer.measureMetadata().status(); | ||
return measurer.sampler().status(); | ||
} | ||
|
||
public List<ComponentMetadata> remoteMetadata() { | ||
return measurer.measureMetadata(converter).remote(); | ||
} | ||
|
||
public String remoteMetadata() { | ||
return measurer.measureMetadata().remote() | ||
.map(Object::toString) | ||
.orElse("Could not get remote metadata"); | ||
public List<ComponentMetadata> localMetadata() { | ||
return measurer.measureMetadata(converter).local(); | ||
} | ||
|
||
public String localMetadata() { | ||
return measurer.measureMetadata().local() | ||
.map(Object::toString) | ||
.orElse("No ongoing measure"); | ||
public record ComponentMetadata(String name, int index, String description, String unit) {} | ||
|
||
public String startOrStop(boolean start) { | ||
if(start) { | ||
measurer.start(0, 500); | ||
return "Started"; | ||
} else { | ||
measurer.stop(); | ||
return "Stopped: " + status; | ||
} | ||
} | ||
} |