5
5
import org .slf4j .LoggerFactory ;
6
6
import org .springframework .lang .Nullable ;
7
7
import org .togetherjava .jshellapi .dto .*;
8
+ import org .togetherjava .jshellapi .dto .sessionstats .SessionStats ;
8
9
import org .togetherjava .jshellapi .exceptions .DockerException ;
9
10
10
11
import java .io .*;
@@ -20,6 +21,8 @@ public class JShellService implements Closeable {
20
21
private final String id ;
21
22
private final BufferedWriter writer ;
22
23
private final BufferedReader reader ;
24
+ private final Instant creationTime ;
25
+ private long totalEvalTime ;
23
26
24
27
private Instant lastTimeoutUpdate ;
25
28
private final long timeout ;
@@ -82,6 +85,7 @@ public JShellService(
82
85
throw new DockerException ("Creation of the session failed." , e );
83
86
}
84
87
this .doingOperation = false ;
88
+ this .creationTime = Instant .now ();
85
89
}
86
90
87
91
public Optional <JShellResult > eval (String code ) throws DockerException {
@@ -96,6 +100,7 @@ public Optional<JShellResult> eval(String code) throws DockerException {
96
100
}
97
101
updateLastTimeout ();
98
102
sessionService .scheduleEvalTimeoutValidation (id , evalTimeout + evalTimeoutValidationLeeway );
103
+ Instant start = Instant .now ();
99
104
if (!code .endsWith ("\n " )) code += '\n' ;
100
105
try {
101
106
writer .write ("eval" );
@@ -113,6 +118,7 @@ public Optional<JShellResult> eval(String code) throws DockerException {
113
118
close ();
114
119
throw new DockerException (ex );
115
120
} finally {
121
+ totalEvalTime += Duration .between (start , Instant .now ()).getSeconds ();
116
122
stopOperation ();
117
123
}
118
124
}
@@ -231,6 +237,18 @@ public String id() {
231
237
return id ;
232
238
}
233
239
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
+
234
252
@ Override
235
253
public void close () {
236
254
try {
0 commit comments