JOJ3/internal/parser/healthcheck/parser.go
张泊明518370910136 4c15ac48d9
Some checks failed
build / build (push) Successful in 2m16s
build / trigger-build-image (push) Successful in 13s
submodules sync / sync (push) Has been cancelled
feat(healthcheck): support score
2025-05-29 00:06:06 -04:00

65 lines
1.6 KiB
Go

package healthcheck
import (
"encoding/json"
"fmt"
"log/slog"
"github.com/joint-online-judge/JOJ3/internal/stage"
"github.com/joint-online-judge/JOJ3/pkg/healthcheck"
)
func (*Healthcheck) parse(executorResult stage.ExecutorResult, conf Conf) (stage.ParserResult, bool) {
stdout := executorResult.Files[conf.Stdout]
stderr := executorResult.Files[conf.Stderr]
slog.Debug("healthcheck files", "stdout", stdout, "stderr", stderr)
if executorResult.Status != stage.StatusAccepted {
return stage.ParserResult{
Score: 0,
Comment: fmt.Sprintf(
"Unexpected executor status: `%s`\n`stderr`:\n```%s\n```\n",
executorResult.Status, stderr,
),
}, true
}
var res healthcheck.Result
err := json.Unmarshal([]byte(stdout), &res)
if err != nil {
return stage.ParserResult{
Score: 0,
Comment: fmt.Sprintf(
"Failed to parse result: `%s`\n`stderr`:\n```%s\n```\n",
err, stderr,
),
}, true
}
slog.Debug("healthcheck result", "res", res)
score := conf.Score
if res.Failed {
score = 0
}
comment := res.Msg
forceQuit := res.Failed
return stage.ParserResult{
Score: score,
Comment: comment,
}, forceQuit
}
func (p *Healthcheck) Run(results []stage.ExecutorResult, confAny any) (
[]stage.ParserResult, bool, error,
) {
conf, err := stage.DecodeConf[Conf](confAny)
if err != nil {
return nil, true, err
}
res := make([]stage.ParserResult, 0, len(results))
forceQuit := false
for _, result := range results {
parserResult, forceQuitResult := p.parse(result, *conf)
res = append(res, parserResult)
forceQuit = forceQuit || forceQuitResult
}
return res, forceQuit, nil
}