Skip to content

Commit

Permalink
[Feature/SessionStats] Endpoint pour récupérer les stats des sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
Alathreon committed May 21, 2024
1 parent 545b1e9 commit b8d540e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.togetherjava.jshellapi.dto.sessionstats;

/**
* Represents the stats of a session.
*
* @param id the id of this session
* @param timeSinceCreation the time in seconds since the creation of this session
* @param timeUntilExpiration the time in seconds until the expiration of this session
* @param totalEvalTime the time spent evaluating code
* @param doingOperation if the session is currently evaluating some code
*/
public record SessionStats(
String id,
long timeSinceCreation,
long timeUntilExpiration,
long totalEvalTime,
boolean doingOperation) {}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.web.server.ResponseStatusException;
import org.togetherjava.jshellapi.dto.JShellResult;
import org.togetherjava.jshellapi.dto.JShellResultWithId;
import org.togetherjava.jshellapi.dto.sessionstats.SessionStats;
import org.togetherjava.jshellapi.exceptions.DockerException;
import org.togetherjava.jshellapi.service.JShellService;
import org.togetherjava.jshellapi.service.JShellSessionService;
Expand Down Expand Up @@ -89,6 +90,11 @@ public void delete(@PathVariable String id) throws DockerException {
service.deleteSession(id);
}

@GetMapping("sessions")
public List<SessionStats> sessions() {
return service.fetchStats();
}

@GetMapping("/startup_script/{id}")
public String startupScript(@PathVariable StartupScriptId id) {
return startupScriptsService.get(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.lang.Nullable;
import org.togetherjava.jshellapi.dto.*;
import org.togetherjava.jshellapi.dto.sessionstats.SessionStats;
import org.togetherjava.jshellapi.exceptions.DockerException;

import java.io.*;
Expand All @@ -20,6 +21,8 @@ public class JShellService implements Closeable {
private final String id;
private final BufferedWriter writer;
private final BufferedReader reader;
private final Instant creationTime;
private long totalEvalTime;

private Instant lastTimeoutUpdate;
private final long timeout;
Expand Down Expand Up @@ -82,6 +85,7 @@ public JShellService(
throw new DockerException("Creation of the session failed.", e);
}
this.doingOperation = false;
this.creationTime = Instant.now();
}

public Optional<JShellResult> eval(String code) throws DockerException {
Expand All @@ -96,6 +100,7 @@ public Optional<JShellResult> eval(String code) throws DockerException {
}
updateLastTimeout();
sessionService.scheduleEvalTimeoutValidation(id, evalTimeout + evalTimeoutValidationLeeway);
Instant start = Instant.now();
if (!code.endsWith("\n")) code += '\n';
try {
writer.write("eval");
Expand All @@ -113,6 +118,7 @@ public Optional<JShellResult> eval(String code) throws DockerException {
close();
throw new DockerException(ex);
} finally {
totalEvalTime += Duration.between(start, Instant.now()).getSeconds();
stopOperation();
}
}
Expand Down Expand Up @@ -231,6 +237,18 @@ public String id() {
return id;
}

public SessionStats fetchSessionInfo() {
long timeSinceCreation = Duration.between(creationTime, Instant.now()).getSeconds();
long timeUntilExpiration =
Duration.between(Instant.now(), lastTimeoutUpdate.plusSeconds(timeout))
.getSeconds();
if (timeUntilExpiration < 0) {
timeUntilExpiration = 0;
}
return new SessionStats(
id, timeSinceCreation, timeUntilExpiration, totalEvalTime, doingOperation);
}

@Override
public void close() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
import org.togetherjava.jshellapi.Config;
import org.togetherjava.jshellapi.dto.sessionstats.SessionStats;
import org.togetherjava.jshellapi.exceptions.DockerException;

import java.util.*;
Expand Down Expand Up @@ -139,6 +140,13 @@ public void scheduleEvalTimeoutValidation(String id, long timeSeconds) {
TimeUnit.SECONDS);
}

public List<SessionStats> fetchStats() {
return jshellSessions.values().stream()
.map(JShellService::fetchSessionInfo)
.sorted(Comparator.comparingLong(SessionStats::timeSinceCreation).reversed())
.toList();
}

@Autowired
public void setConfig(Config config) {
this.config = config;
Expand Down

0 comments on commit b8d540e

Please sign in to comment.