Skip to content

wip: record measures in local DB #213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion analysis/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>net.laprun.sustainability</groupId>
<artifactId>power-server-parent</artifactId>
<version>0.2.4-SNAPSHOT</version>
<version>0.3.0-SNAPSHOT</version>
</parent>

<artifactId>power-server-analysis</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion if-manifest-export/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>net.laprun.sustainability</groupId>
<artifactId>power-server-parent</artifactId>
<version>0.2.4-SNAPSHOT</version>
<version>0.3.0-SNAPSHOT</version>
</parent>

<artifactId>power-server-if-manifest-export</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion measure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>net.laprun.sustainability</groupId>
<artifactId>power-server-parent</artifactId>
<version>0.2.4-SNAPSHOT</version>
<version>0.3.0-SNAPSHOT</version>
</parent>

<artifactId>power-server-measure</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion metadata/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>net.laprun.sustainability</groupId>
<artifactId>power-server-parent</artifactId>
<version>0.2.4-SNAPSHOT</version>
<version>0.3.0-SNAPSHOT</version>
</parent>

<artifactId>power-server-metadata</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
* with the sensor.
*
* @param components an array recording the power consumption reported by each component of this sensor
* @param tick the ordinal tick associated with this measure
* @param startMs the start timestamp in milliseconds for this measure
* @param endMs the end timestamp in milliseconds for this measure
*/
public record SensorMeasure(double[] components, long tick, long timestamp, long duration) {
public record SensorMeasure(double[] components, long startMs, long endMs) {
/**
* Represents an invalid or somehow missed measure.
*/
public static final SensorMeasure missing = new SensorMeasure(new double[] { -1.0 }, -1, -1, -1);
public static final SensorMeasure missing = new SensorMeasure(new double[] { -1.0 }, -1, -1);
}
61 changes: 23 additions & 38 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
<?xml version="1.0"?>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.laprun.sustainability</groupId>
<artifactId>power-server-parent</artifactId>
<version>0.2.4-SNAPSHOT</version>
<version>0.3.0-SNAPSHOT</version>
<packaging>pom</packaging>

<name>power-server : parent</name>
<description>An application allowing to retrieve power consumption and associated metadata on a per-process basis,
via a RESTful endpoint
</description>
via a RESTful endpoint</description>
<url>https://github.com/metacosm/power-server</url>

<licenses>
Expand Down Expand Up @@ -184,6 +183,14 @@
<disable>true</disable>
</consoleOutputReporter>
<statelessTestsetInfoReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5StatelessTestsetInfoTreeReporter">
<printStacktraceOnError>true</printStacktraceOnError>
<printStacktraceOnFailure>true</printStacktraceOnFailure>
<printStdoutOnError>true</printStdoutOnError>
<printStdoutOnFailure>true</printStdoutOnFailure>
<printStdoutOnSuccess>false</printStdoutOnSuccess>
<printStderrOnError>true</printStderrOnError>
<printStderrOnFailure>true</printStderrOnFailure>
<printStderrOnSuccess>false</printStderrOnSuccess>
<theme>UNICODE</theme>
<printStacktraceOnError>true</printStacktraceOnError>
<printStacktraceOnFailure>true</printStacktraceOnFailure>
Expand Down Expand Up @@ -212,10 +219,8 @@
</goals>
<configuration>
<systemPropertyVariables>
<native.image.path>${project.build.directory}/${project.build.finalName}-runner
</native.image.path>
<java.util.logging.manager>org.jboss.logmanager.LogManager
</java.util.logging.manager>
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
Expand Down Expand Up @@ -326,12 +331,8 @@
</sdkman>
<artifacts>
<artifact>
<path>
{{artifactsDir}}/{{distributionName}}-{{projectVersion}}-linux-x86_64.tar.gz
</path>
<transform>
artifacts/{{distributionName}}-{{projectEffectiveVersion}}-linux-x86_64.tar.gz
</transform>
<path>{{artifactsDir}}/{{distributionName}}-{{projectVersion}}-linux-x86_64.tar.gz</path>
<transform>artifacts/{{distributionName}}-{{projectEffectiveVersion}}-linux-x86_64.tar.gz</transform>
<platform>linux-x86_64</platform>
</artifact>
<!--<artifact>
Expand All @@ -344,21 +345,13 @@
<platform>windows-x86_64</platform>
</artifact>-->
<artifact>
<path>
{{artifactsDir}}/{{distributionName}}-{{projectVersion}}-osx-x86_64.tar.gz
</path>
<transform>
artifacts/{{distributionName}}-{{projectEffectiveVersion}}-osx-x86_64.tar.gz
</transform>
<path>{{artifactsDir}}/{{distributionName}}-{{projectVersion}}-osx-x86_64.tar.gz</path>
<transform>artifacts/{{distributionName}}-{{projectEffectiveVersion}}-osx-x86_64.tar.gz</transform>
<platform>osx-x86_64</platform>
</artifact>
<artifact>
<path>
{{artifactsDir}}/{{distributionName}}-{{projectVersion}}-osx-aarch_64.tar.gz
</path>
<transform>
artifacts/{{distributionName}}-{{projectEffectiveVersion}}-osx-aarch_64.tar.gz
</transform>
<path>{{artifactsDir}}/{{distributionName}}-{{projectVersion}}-osx-aarch_64.tar.gz</path>
<transform>artifacts/{{distributionName}}-{{projectEffectiveVersion}}-osx-aarch_64.tar.gz</transform>
<platform>osx-aarch_64</platform>
</artifact>
</artifacts>
Expand Down Expand Up @@ -396,12 +389,8 @@
<archive>
<index>true</index>
<manifest>
<addDefaultSpecificationEntries>
true
</addDefaultSpecificationEntries>
<addDefaultImplementationEntries>
true
</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
<manifestEntries>
<Implementation-URL>${project.url}</Implementation-URL>
Expand Down Expand Up @@ -438,12 +427,8 @@
<archive>
<index>true</index>
<manifest>
<addDefaultSpecificationEntries>
true
</addDefaultSpecificationEntries>
<addDefaultImplementationEntries>
true
</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
<manifestEntries>
<Implementation-URL>${project.url}</Implementation-URL>
Expand Down
15 changes: 14 additions & 1 deletion server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>net.laprun.sustainability</groupId>
<artifactId>power-server-parent</artifactId>
<version>0.2.4-SNAPSHOT</version>
<version>0.3.0-SNAPSHOT</version>
</parent>
<artifactId>power-server</artifactId>
<name>power-server : server</name>
Expand Down Expand Up @@ -32,6 +32,19 @@
<artifactId>quarkus-rest-client</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-orm-panache</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-scheduler</artifactId>
</dependency>
<dependency>
<groupId>io.quarkiverse.jdbc</groupId>
<artifactId>quarkus-jdbc-sqlite4j</artifactId>
<version>0.0.5</version>
</dependency>
</dependencies>
<build>
<extensions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.infrastructure.Infrastructure;
import net.laprun.sustainability.power.persistence.Persistence;
import net.laprun.sustainability.power.sensors.Measures;
import net.laprun.sustainability.power.sensors.PowerSensor;
import net.laprun.sustainability.power.sensors.RegisteredPID;

@ApplicationScoped
public class PowerMeasurer {
Expand All @@ -24,7 +26,16 @@ public class PowerMeasurer {

private Multi<Measures> periodicSensorCheck;

public Multi<SensorMeasure> startTracking(String pid) throws Exception {
public Multi<SensorMeasure> stream(String pid) throws Exception {
final var registeredPID = track(pid);
return periodicSensorCheck.map(measures -> measures.getOrDefault(registeredPID));
}

public void startTrackingApp(String appName, String pid) throws Exception {
stream(pid).subscribe().with(m -> Persistence.save(m, appName));
}

private RegisteredPID track(String pid) throws Exception {
// first make sure that the process with that pid exists
final var parsedPID = validPIDOrFail(pid);

Expand All @@ -41,9 +52,8 @@ public Multi<SensorMeasure> startTracking(String pid) throws Exception {
final var registeredPID = sensor.register(parsedPID);
// todo: the timing of things could make it so that the pid has been removed before the map operation occurs so
// currently return -1 instead of null but this needs to be properly addressed
return periodicSensorCheck
.map(measures -> measures.getOrDefault(registeredPID))
.onCancellation().invoke(() -> sensor.unregister(registeredPID));
periodicSensorCheck = periodicSensorCheck.onCancellation().invoke(() -> sensor.unregister(registeredPID));
return registeredPID;
}

protected long validPIDOrFail(String pid) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package net.laprun.sustainability.power;

import java.time.Duration;
import java.util.List;

import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;
import jakarta.ws.rs.*;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.NotFoundException;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.core.MediaType;

import org.jboss.resteasy.reactive.RestStreamElementType;

import io.quarkus.logging.Log;
import io.quarkus.runtime.StartupEvent;
import io.smallrye.mutiny.Multi;
import net.laprun.sustainability.power.persistence.Measure;

@Path("/power")
public class PowerResource {
Expand All @@ -25,10 +31,20 @@ public void onStartup(@Observes StartupEvent event) {

@GET
@RestStreamElementType(MediaType.APPLICATION_JSON)
@Path("{pid}")
public Multi<SensorMeasure> powerFor(@PathParam("pid") String pid) throws Exception {
@Path("stream/{pid}")
public Multi<SensorMeasure> streamMeasuresFor(@PathParam("pid") String pid) throws Exception {
try {
return measurer.startTracking(pid);
return measurer.stream(pid);
} catch (IllegalArgumentException e) {
throw new NotFoundException("Unknown process: " + pid);
}
}

@POST
@Path("start/{appName}/{pid}")
public void startMeasure(@PathParam("appName") String appName, @PathParam("pid") String pid) throws Exception {
try {
measurer.startTrackingApp(appName, pid);
} catch (IllegalArgumentException e) {
throw new NotFoundException("Unknown process: " + pid);
}
Expand All @@ -45,4 +61,10 @@ public SensorMetadata metadata() {
public Duration samplingPeriod() {
return measurer.getSamplingPeriod();
}

@GET
@Path("measures/{appName}")
public List<SensorMeasure> measures(@PathParam("appName") String appName) throws Exception {
return Measure.forApplication(appName).stream().map(Measure::asSensorMeasure).toList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package net.laprun.sustainability.power.persistence;

import java.util.List;

import jakarta.persistence.Entity;

import io.quarkus.hibernate.orm.panache.PanacheEntity;
import net.laprun.sustainability.power.SensorMeasure;

@Entity
public class Measure extends PanacheEntity {
public String appName;
public long startTime;
public long endTime;
public double[] components;

public static List<Measure> forApplication(String appName) {
return find("appName", appName).list();
}

public static List<Measure> all() {
return Measure.findAll().list();
}

public SensorMeasure asSensorMeasure() {
return new SensorMeasure(components, startTime, endTime);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package net.laprun.sustainability.power.persistence;

import jakarta.transaction.Transactional;

import net.laprun.sustainability.power.SensorMeasure;

public enum Persistence {
;

@Transactional
public static Measure save(SensorMeasure measure, String appName) {
final var persisted = new Measure();
persisted.components = measure.components();
persisted.appName = appName;
persisted.startTime = measure.startMs();
persisted.endTime = measure.endMs();
persisted.persist();
return persisted;
}
}
Loading
Loading