6
6
import org .springframework .lang .Nullable ;
7
7
8
8
import org .togetherjava .jshellapi .dto .*;
9
+ import org .togetherjava .jshellapi .dto .sessionstats .SessionStats ;
9
10
import org .togetherjava .jshellapi .exceptions .DockerException ;
10
11
11
12
import java .io .*;
@@ -21,6 +22,8 @@ public class JShellService implements Closeable {
21
22
private final String id ;
22
23
private final BufferedWriter writer ;
23
24
private final BufferedReader reader ;
25
+ private final Instant creationTime ;
26
+ private long totalEvalTime ;
24
27
25
28
private Instant lastTimeoutUpdate ;
26
29
private final long timeout ;
@@ -66,6 +69,7 @@ public JShellService(DockerService dockerService, JShellSessionService sessionSe
66
69
throw new DockerException ("Creation of the session failed." , e );
67
70
}
68
71
this .doingOperation = false ;
72
+ this .creationTime = Instant .now ();
69
73
}
70
74
71
75
public Optional <JShellResult > eval (String code ) throws DockerException {
@@ -80,6 +84,7 @@ public Optional<JShellResult> eval(String code) throws DockerException {
80
84
}
81
85
updateLastTimeout ();
82
86
sessionService .scheduleEvalTimeoutValidation (id , evalTimeout + evalTimeoutValidationLeeway );
87
+ Instant start = Instant .now ();
83
88
if (!code .endsWith ("\n " ))
84
89
code += '\n' ;
85
90
try {
@@ -98,6 +103,7 @@ public Optional<JShellResult> eval(String code) throws DockerException {
98
103
close ();
99
104
throw new DockerException (ex );
100
105
} finally {
106
+ totalEvalTime += Duration .between (start , Instant .now ()).getSeconds ();
101
107
stopOperation ();
102
108
}
103
109
}
@@ -204,6 +210,18 @@ public String id() {
204
210
return id ;
205
211
}
206
212
213
+ public SessionStats fetchSessionInfo () {
214
+ long timeSinceCreation = Duration .between (creationTime , Instant .now ()).getSeconds ();
215
+ long timeUntilExpiration =
216
+ Duration .between (Instant .now (), lastTimeoutUpdate .plusSeconds (timeout ))
217
+ .getSeconds ();
218
+ if (timeUntilExpiration < 0 ) {
219
+ timeUntilExpiration = 0 ;
220
+ }
221
+ return new SessionStats (
222
+ id , timeSinceCreation , timeUntilExpiration , totalEvalTime , doingOperation );
223
+ }
224
+
207
225
@ Override
208
226
public void close () {
209
227
LOGGER .debug ("Close called for session {}." , id );
0 commit comments