Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
mwangggg committed Aug 24, 2023
1 parent 1164873 commit ec7d88d
Showing 1 changed file with 56 additions and 3 deletions.
59 changes: 56 additions & 3 deletions src/main/java/io/cryostat/agent/remote/RecordingsContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import javax.inject.Inject;

import org.openjdk.jmc.common.unit.IConstrainedMap;
import org.openjdk.jmc.common.unit.ITypedQuantity;
import org.openjdk.jmc.common.unit.QuantityConversionException;
import org.openjdk.jmc.common.unit.UnitLookup;
import org.openjdk.jmc.flightrecorder.configuration.events.EventOptionID;
Expand All @@ -50,6 +51,7 @@
import io.cryostat.core.templates.Template;
import io.cryostat.core.templates.TemplateType;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.net.httpserver.HttpExchange;
import io.smallrye.config.SmallRyeConfig;
Expand Down Expand Up @@ -110,8 +112,19 @@ public void handle(HttpExchange exchange) throws IOException {
break;
case "PATCH":
id = extractId(exchange);
if (id >= 0) {
handleStop(exchange, id);
String request = requestStopOrUpdate(exchange);
if (request == "STOPPED") {
if (id >= 0) {
handleStop(exchange, id);
} else {
exchange.sendResponseHeaders(HttpStatus.SC_BAD_REQUEST, BODY_LENGTH_NONE);
}
} else if (request == "UPDATE") {
if (id >= 0) {
handleRecordingUpdate(exchange, id);
} else {
exchange.sendResponseHeaders(HttpStatus.SC_BAD_REQUEST, BODY_LENGTH_NONE);
}
} else {
exchange.sendResponseHeaders(HttpStatus.SC_BAD_REQUEST, BODY_LENGTH_NONE);
}
Expand All @@ -135,6 +148,14 @@ public void handle(HttpExchange exchange) throws IOException {
}
}

private static String requestStopOrUpdate(HttpExchange exchange) throws IOException {
try (InputStream body = exchange.getRequestBody()) {
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonMap = mapper.readTree(body);
return jsonMap.get("state").toString();
}
}

private static long extractId(HttpExchange exchange) throws IOException {
Matcher m = PATH_ID_PATTERN.matcher(exchange.getRequestURI().getPath());
if (!m.find()) {
Expand Down Expand Up @@ -231,6 +252,38 @@ private void handleStartRecordingOrSnapshot(HttpExchange exchange) throws IOExce
}
}

private void handleRecordingUpdate(HttpExchange exchange, long id) throws IOException {
try (InputStream body = exchange.getRequestBody()) {
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonMap = mapper.readTree(body);
invokeOnRecording(
exchange,
id,
r -> {
try {
if (jsonMap.has("name")) {
r.setName(jsonMap.get("name").toString());
}
if (jsonMap.has("toDisk")) {
r.setToDisk(jsonMap.get("toDisk").asBoolean());
}
// if (jsonMap.has("duration")) {
// IQuantity duration = UnitLookup.MILLISECOND.quantity(jsonMap.get("duration").asLong());
// r.setDuration(jsonMap.get("duration").asLong());
// }
if (jsonMap.has("maxSize")) {
r.setMaxSize(jsonMap.get("name").asLong());
}
// if (jsonMap.has("maxAge")) {
// r.setMaxAge(jsonMap.get("name").asLong());
// }
} catch (IllegalStateException e) {
sendHeader(exchange, HttpStatus.SC_CONFLICT);
}
});
}
}

private void handleStop(HttpExchange exchange, long id) throws IOException {
invokeOnRecording(
exchange,
Expand Down Expand Up @@ -378,7 +431,7 @@ boolean requestsBundledTemplate() {
boolean requestSnapshot() {
boolean snapshotName = name.equals("snapshot");
boolean snapshotTemplate =
StringUtils.isBlank(template) && StringUtils.isBlank(localTemplateName);
StringUtils.isBlank(template) && StringUtils.isBlank(localTemplateName);
boolean snapshotFeatures = duration == 0 && maxSize == 0 && maxAge == 0;
return snapshotName && snapshotTemplate && snapshotFeatures;
}
Expand Down

0 comments on commit ec7d88d

Please sign in to comment.