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