Skip to content

Commit

Permalink
Add logs and session info downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
rtm516 committed Jun 29, 2024
1 parent d616d68 commit 8c6a19e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

@RestController()
Expand Down Expand Up @@ -138,4 +142,33 @@ public String logs(HttpServletResponse response, @PathVariable ObjectId botId) {
response.setStatus(200);
return botManager.logs(botId);
}

@GetMapping("/{botId:[a-z0-9]+}/session")
public String session(HttpServletResponse response, @PathVariable ObjectId botId) {
if (!botManager.bots().containsKey(botId)) {
response.setStatus(404);
return "";
}

BotContainer botContainer = botManager.bots().get(botId);

botContainer.dumpSession();

Path sessionJson = Paths.get(botContainer.cacheFolder(), "currentSessionResponse.json");
if (!Files.exists(sessionJson)) {
response.setStatus(404);
return "";
}

String data;
try {
data = Files.readString(sessionJson);
} catch (IOException e) {
response.setStatus(500);
return "";
}

response.setStatus(200);
return data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ public void restart() {
start();
}

public void dumpSession() {
if (sessionManager != null) {
sessionManager.dumpSession();
}
}

public String cacheFolder() {
String cache = "./cache/" + bot._id();

Expand Down
29 changes: 29 additions & 0 deletions bootstrap/manager/src/ui/src/pages/BotDetails.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,28 @@ function BotDetails () {
})
}

const saveLogs = () => {
const blob = new Blob([logs], { type: 'text/plain' })
const url = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = 'latest.log'
a.click()
URL.revokeObjectURL(url)
}

const downloadSessionData = () => {
fetch('/api/bots/' + botId + '/session').then((res) => res.json()).then((data) => {
const blob = new Blob([JSON.stringify(data)], { type: 'application/json' })
const url = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = 'currentSessionResponse.json'
a.click()
URL.revokeObjectURL(url)
})
}

return (
<>
<ConfirmModal
Expand Down Expand Up @@ -213,6 +235,13 @@ function BotDetails () {
{logs}
</div>
</div>
<div className='bg-white p-6 rounded shadow-lg max-w-6xl w-full'>
<h3 className='text-3xl text-center pb-4'>Downloads</h3>
<div className='grid md:grid-cols-2 grid-cols-1 gap-2'>
<Button color='green' onClick={() => saveLogs()}>Download logs</Button>
<Button color='green' onClick={() => downloadSessionData()}>Download current session data</Button>
</div>
</div>
<div className='bg-white p-6 rounded shadow-lg max-w-6xl w-full'>
<h3 className='text-3xl text-center pb-4'>Settings</h3>
<form className='flex flex-col gap-4' onSubmit={handleSubmit}>
Expand Down

0 comments on commit 8c6a19e

Please sign in to comment.