feat: include complete stage name in command

This commit is contained in:
wancat committed 2023-10-29 17:41:38 +08:00
1 parent 8f22babe7d
commit aec0cf84ad
6 files changed
+27 -7

No files matched your search

+14 -3
View File
@@ -51,7 +51,16 @@ export async function sessionList() {
},
])
return sessions.map(
({ _id, user, quest, status, createdAt, finishedAt, commands }) => {
({
_id,
user,
quest,
status,
createdAt,
finishedAt,
commands,
lastActivityAt,
}) => {
return {
_id,
user: user.username,
@@ -59,7 +68,8 @@ export async function sessionList() {
status,
createdAt: createdAt?.toLocaleString(),
finishedAt: finishedAt?.toLocaleString(),
usedTime: finishedAt ? formatTime(finishedAt - createdAt) : '',
lastActivityAt: lastActivityAt?.toLocaleString(),
usedTime: formatTime(lastActivityAt - createdAt),
commandCount: commands?.length,
}
}
@@ -69,9 +79,10 @@ export async function sessionList() {
export async function sessionDetail(id) {
const session = await Session.findById(id).populate('user').exec()
const commands = (await Command.find({ session: id })).map(
({ command, pwd, output, error, createdAt }) => ({
({ command, pwd, output, error, createdAt, stage }) => ({
command: command?.slice(0, 20),
pwd,
stage,
output: output?.slice(0, 50),
error: error?.slice(0, 50),
createdAt: createdAt?.toLocaleString(),
@@ -40,6 +40,7 @@
<th>PWD</th>
<th>Output</th>
<th>Error</th>
<th>Stage</th>
<th>Created At</th>
</tr>
{{#each commands}}
@@ -48,6 +49,7 @@
<td>{{pwd}}</td>
<td>{{output}}</td>
<td>{{error}}</td>
<td>{{stage}}</td>
<td>{{createdAt}}</td>
</tr>
{{/each}}
+4
View File
@@ -25,6 +25,10 @@ const Command = model(
type: String,
required: false,
},
stage: {
type: String,
required: false,
},
},
{ timestamps: true }
)
+1 -1
View File
@@ -82,7 +82,7 @@ stages:
pwd:
- '^/home/commander$'
command:
- cat forgotten-scroll.txt
- cat forgotten_scroll.txt
responses:
- type: dialogue
speaker: Unix 精靈
@@ -41,6 +41,11 @@ export async function newCommand(req, res) {
const response = await commandHandler.run()
if (response) {
c.stage = response.stage
await c.save()
}
res.status(201).json(response)
await session.save()
}
+1 -3
View File
@@ -113,16 +113,14 @@ export default class CommandHandler extends SessionHandler {
stages.map((stage) => this.isMatch(stage.condition))
)
console.log('matches', matches)
const stage = stages.find((_, i) => matches[i])
console.log('stage', stage)
if (!stage) {
return {}
}
const response = this.execute(stage)
return {
stage: stage.id,
end: this.session.status === 'finished',
...response,
}