Skip to content

Commit b8d540e

Browse files
committed
[Feature/SessionStats] Endpoint pour récupérer les stats des sessions
1 parent 545b1e9 commit b8d540e

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.togetherjava.jshellapi.dto.sessionstats;
2+
3+
/**
4+
* Represents the stats of a session.
5+
*
6+
* @param id the id of this session
7+
* @param timeSinceCreation the time in seconds since the creation of this session
8+
* @param timeUntilExpiration the time in seconds until the expiration of this session
9+
* @param totalEvalTime the time spent evaluating code
10+
* @param doingOperation if the session is currently evaluating some code
11+
*/
12+
public record SessionStats(
13+
String id,
14+
long timeSinceCreation,
15+
long timeUntilExpiration,
16+
long totalEvalTime,
17+
boolean doingOperation) {}

JShellAPI/src/main/java/org/togetherjava/jshellapi/rest/JShellController.java

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.springframework.web.server.ResponseStatusException;
77
import org.togetherjava.jshellapi.dto.JShellResult;
88
import org.togetherjava.jshellapi.dto.JShellResultWithId;
9+
import org.togetherjava.jshellapi.dto.sessionstats.SessionStats;
910
import org.togetherjava.jshellapi.exceptions.DockerException;
1011
import org.togetherjava.jshellapi.service.JShellService;
1112
import org.togetherjava.jshellapi.service.JShellSessionService;
@@ -89,6 +90,11 @@ public void delete(@PathVariable String id) throws DockerException {
8990
service.deleteSession(id);
9091
}
9192

93+
@GetMapping("sessions")
94+
public List<SessionStats> sessions() {
95+
return service.fetchStats();
96+
}
97+
9298
@GetMapping("/startup_script/{id}")
9399
public String startupScript(@PathVariable StartupScriptId id) {
94100
return startupScriptsService.get(id);

JShellAPI/src/main/java/org/togetherjava/jshellapi/service/JShellService.java

+18
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.slf4j.LoggerFactory;
66
import org.springframework.lang.Nullable;
77
import org.togetherjava.jshellapi.dto.*;
8+
import org.togetherjava.jshellapi.dto.sessionstats.SessionStats;
89
import org.togetherjava.jshellapi.exceptions.DockerException;
910

1011
import java.io.*;
@@ -20,6 +21,8 @@ public class JShellService implements Closeable {
2021
private final String id;
2122
private final BufferedWriter writer;
2223
private final BufferedReader reader;
24+
private final Instant creationTime;
25+
private long totalEvalTime;
2326

2427
private Instant lastTimeoutUpdate;
2528
private final long timeout;
@@ -82,6 +85,7 @@ public JShellService(
8285
throw new DockerException("Creation of the session failed.", e);
8386
}
8487
this.doingOperation = false;
88+
this.creationTime = Instant.now();
8589
}
8690

8791
public Optional<JShellResult> eval(String code) throws DockerException {
@@ -96,6 +100,7 @@ public Optional<JShellResult> eval(String code) throws DockerException {
96100
}
97101
updateLastTimeout();
98102
sessionService.scheduleEvalTimeoutValidation(id, evalTimeout + evalTimeoutValidationLeeway);
103+
Instant start = Instant.now();
99104
if (!code.endsWith("\n")) code += '\n';
100105
try {
101106
writer.write("eval");
@@ -113,6 +118,7 @@ public Optional<JShellResult> eval(String code) throws DockerException {
113118
close();
114119
throw new DockerException(ex);
115120
} finally {
121+
totalEvalTime += Duration.between(start, Instant.now()).getSeconds();
116122
stopOperation();
117123
}
118124
}
@@ -231,6 +237,18 @@ public String id() {
231237
return id;
232238
}
233239

240+
public SessionStats fetchSessionInfo() {
241+
long timeSinceCreation = Duration.between(creationTime, Instant.now()).getSeconds();
242+
long timeUntilExpiration =
243+
Duration.between(Instant.now(), lastTimeoutUpdate.plusSeconds(timeout))
244+
.getSeconds();
245+
if (timeUntilExpiration < 0) {
246+
timeUntilExpiration = 0;
247+
}
248+
return new SessionStats(
249+
id, timeSinceCreation, timeUntilExpiration, totalEvalTime, doingOperation);
250+
}
251+
234252
@Override
235253
public void close() {
236254
try {

JShellAPI/src/main/java/org/togetherjava/jshellapi/service/JShellSessionService.java

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.springframework.stereotype.Service;
99
import org.springframework.web.server.ResponseStatusException;
1010
import org.togetherjava.jshellapi.Config;
11+
import org.togetherjava.jshellapi.dto.sessionstats.SessionStats;
1112
import org.togetherjava.jshellapi.exceptions.DockerException;
1213

1314
import java.util.*;
@@ -139,6 +140,13 @@ public void scheduleEvalTimeoutValidation(String id, long timeSeconds) {
139140
TimeUnit.SECONDS);
140141
}
141142

143+
public List<SessionStats> fetchStats() {
144+
return jshellSessions.values().stream()
145+
.map(JShellService::fetchSessionInfo)
146+
.sorted(Comparator.comparingLong(SessionStats::timeSinceCreation).reversed())
147+
.toList();
148+
}
149+
142150
@Autowired
143151
public void setConfig(Config config) {
144152
this.config = config;

0 commit comments

Comments
 (0)