Skip to content

Commit

Permalink
[Feature/SessionStats] Endpoint to fetch session data
Browse files Browse the repository at this point in the history
  • Loading branch information
Alathreon committed Aug 1, 2024
1 parent daddfe9 commit b0b959a
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 @@ -7,6 +7,7 @@

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 @@ -71,6 +72,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 @@ -6,6 +6,7 @@
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 @@ -21,6 +22,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 @@ -66,6 +69,7 @@ public JShellService(DockerService dockerService, JShellSessionService sessionSe
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 @@ -80,6 +84,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 {
Expand All @@ -98,6 +103,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 @@ -204,6 +210,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() {
LOGGER.debug("Close called for session {}.", id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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 @@ -133,6 +134,13 @@ public void scheduleEvalTimeoutValidation(String id, long timeSeconds) {
}, 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 b0b959a

Please sign in to comment.