feat: use run id and job id in page view

This commit is contained in:
Jason Song 2022-10-14 18:30:00 +08:00
parent d320eb66f4
commit 1cba52376a
5 changed files with 126 additions and 96 deletions

View File

@ -69,6 +69,40 @@ func (task *Task) LoadAttributes(ctx context.Context) error {
return nil return nil
} }
// FullSteps returns steps with "Set up job" and "Complete job"
func (task *Task) FullSteps() []*TaskStep {
var firstStep, lastStep *TaskStep
if l := len(task.Steps); l > 0 {
firstStep = task.Steps[0]
lastStep = task.Steps[l-1]
}
headStep := &TaskStep{
Name: "Set up job",
LogIndex: 0,
LogLength: -1, // no limit
Started: task.Started,
}
if firstStep != nil {
headStep.LogLength = firstStep.LogIndex
headStep.Stopped = firstStep.Started
}
tailStep := &TaskStep{
Name: "Complete job",
Stopped: task.Stopped,
}
if lastStep != nil {
tailStep.LogIndex = lastStep.LogIndex + lastStep.LogLength
tailStep.LogLength = -1 // no limit
tailStep.Started = lastStep.Stopped
}
steps := make([]*TaskStep, 0, len(task.Steps)+2)
steps = append(steps, headStep)
steps = append(steps, task.Steps...)
steps = append(steps, tailStep)
return steps
}
// ErrTaskNotExist represents an error for bot task not exist // ErrTaskNotExist represents an error for bot task not exist
type ErrTaskNotExist struct { type ErrTaskNotExist struct {
ID int64 ID int64

View File

@ -1,16 +1,19 @@
package dev package dev
import ( import (
"fmt"
"net/http"
"code.gitea.io/gitea/core" "code.gitea.io/gitea/core"
bots_model "code.gitea.io/gitea/models/bots" bots_model "code.gitea.io/gitea/models/bots"
"code.gitea.io/gitea/modules/web"
"net/http"
"strconv"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/web"
) )
func BuildView(ctx *context.Context) { func BuildView(ctx *context.Context) {
ctx.Data["RunID"] = ctx.Params("runid")
ctx.Data["JobID"] = ctx.Params("jobid")
ctx.HTML(http.StatusOK, "dev/buildview") ctx.HTML(http.StatusOK, "dev/buildview")
} }
@ -70,38 +73,34 @@ type BuildViewStepLogLine struct {
func BuildViewPost(ctx *context.Context) { func BuildViewPost(ctx *context.Context) {
req := web.GetForm(ctx).(*BuildViewRequest) req := web.GetForm(ctx).(*BuildViewRequest)
currentJobID, _ := strconv.ParseInt(ctx.Req.URL.Query().Get("job_id"), 10, 64) runID := ctx.ParamsInt64("runid")
jobID := ctx.ParamsInt64("jobid")
job, err := bots_model.GetRunJobByID(ctx, currentJobID) run, err := bots_model.GetRunByID(ctx, runID)
if err != nil { if err != nil {
if _, ok := err.(bots_model.ErrRunJobNotExist); ok { if _, ok := err.(bots_model.ErrRunNotExist); ok {
ctx.Error(http.StatusNotFound, err.Error()) ctx.Error(http.StatusNotFound, err.Error())
return return
} }
ctx.Error(http.StatusInternalServerError, err.Error()) ctx.Error(http.StatusInternalServerError, err.Error())
return return
} }
if err := job.LoadAttributes(ctx); err != nil {
ctx.Error(http.StatusInternalServerError, err.Error())
return
}
run := job.Run
jobs, err := bots_model.GetRunJobsByRunID(ctx, run.ID) jobs, err := bots_model.GetRunJobsByRunID(ctx, run.ID)
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, err.Error()) ctx.Error(http.StatusInternalServerError, err.Error())
return return
} }
var task *bots_model.Task
if job.TaskID > 0 { var job *bots_model.RunJob
task, err = bots_model.GetTaskByID(ctx, job.TaskID) if jobID != 0 {
if err != nil { for _, v := range jobs {
ctx.Error(http.StatusInternalServerError, err.Error()) if v.ID == jobID {
return job = v
break
}
} }
task.Job = job if job == nil {
if err := task.LoadAttributes(ctx); err != nil { ctx.Error(http.StatusNotFound, fmt.Sprintf("run %v has no job %v", runID, jobID))
ctx.Error(http.StatusInternalServerError, err.Error())
return return
} }
} }
@ -125,75 +124,64 @@ func BuildViewPost(ctx *context.Context) {
}, },
} }
resp.StateData.CurrentJobInfo.Title = job.Name if job != nil {
resp.LogsData.StreamingLogs = make([]BuildViewStepLog, 0, len(req.StepLogCursors)) var task *bots_model.Task
if job.TaskID == 0 { if job.TaskID > 0 {
resp.StateData.CurrentJobInfo.Detail = "wait to be pick up by a runner" task, err = bots_model.GetTaskByID(ctx, job.TaskID)
} else { if err != nil {
resp.StateData.CurrentJobInfo.Detail = "TODO: more detail info" // TODO: more detail info ctx.Error(http.StatusInternalServerError, err.Error())
return
var firstStep, lastStep *bots_model.TaskStep }
if l := len(task.Steps); l > 0 { task.Job = job
firstStep = task.Steps[0] if err := task.LoadAttributes(ctx); err != nil {
lastStep = task.Steps[l-1] ctx.Error(http.StatusInternalServerError, err.Error())
} return
headStep := &bots_model.TaskStep{
Name: "Set up job",
LogIndex: 0,
LogLength: -1, // no limit
Started: task.Started,
}
if firstStep != nil {
headStep.LogLength = firstStep.LogIndex
headStep.Stopped = firstStep.Started
}
tailStep := &bots_model.TaskStep{
Name: "Complete job",
Stopped: task.Stopped,
}
if lastStep != nil {
tailStep.LogIndex = lastStep.LogIndex + lastStep.LogLength
tailStep.LogLength = -1 // no limit
tailStep.Started = lastStep.Stopped
}
steps := make([]*bots_model.TaskStep, 0, len(task.Steps)+2)
steps = append(steps, headStep)
steps = append(steps, task.Steps...)
steps = append(steps, tailStep)
resp.StateData.CurrentJobSteps = make([]BuildViewJobStep, len(steps))
for i, v := range steps {
resp.StateData.CurrentJobSteps[i] = BuildViewJobStep{
Summary: v.Name,
Duration: float64(v.Stopped - v.Started),
Status: core.StatusRunning, // TODO: add status to step,
} }
} }
for _, cursor := range req.StepLogCursors { resp.StateData.CurrentJobInfo.Title = job.Name
if cursor.Expanded { resp.LogsData.StreamingLogs = make([]BuildViewStepLog, 0, len(req.StepLogCursors))
step := steps[cursor.StepIndex] if job.TaskID == 0 {
var logRows []*bots_model.TaskLog resp.StateData.CurrentJobInfo.Detail = "wait to be pick up by a runner"
if cursor.Cursor < step.LogLength || step.LogLength < 0 { } else {
logRows, err = bots_model.GetTaskLogs(task.ID, step.LogIndex+cursor.Cursor, step.LogLength-cursor.Cursor) resp.StateData.CurrentJobInfo.Detail = "TODO: more detail info" // TODO: more detail info
if err != nil {
ctx.Error(http.StatusInternalServerError, err.Error()) steps := task.FullSteps()
return
} resp.StateData.CurrentJobSteps = make([]BuildViewJobStep, len(steps))
for i, v := range steps {
resp.StateData.CurrentJobSteps[i] = BuildViewJobStep{
Summary: v.Name,
Duration: float64(v.Stopped - v.Started),
Status: core.StatusRunning, // TODO: add status to step,
} }
logLines := make([]BuildViewStepLogLine, len(logRows)) }
for i, row := range logRows {
logLines[i] = BuildViewStepLogLine{ for _, cursor := range req.StepLogCursors {
Ln: i, if cursor.Expanded {
M: row.Content, step := steps[cursor.StepIndex]
T: float64(row.Timestamp), var logRows []*bots_model.TaskLog
if cursor.Cursor < step.LogLength || step.LogLength < 0 {
logRows, err = bots_model.GetTaskLogs(task.ID, step.LogIndex+cursor.Cursor, step.LogLength-cursor.Cursor)
if err != nil {
ctx.Error(http.StatusInternalServerError, err.Error())
return
}
} }
logLines := make([]BuildViewStepLogLine, len(logRows))
for i, row := range logRows {
logLines[i] = BuildViewStepLogLine{
Ln: i,
M: row.Content,
T: float64(row.Timestamp),
}
}
resp.LogsData.StreamingLogs = append(resp.LogsData.StreamingLogs, BuildViewStepLog{
StepIndex: cursor.StepIndex,
Cursor: cursor.Cursor + int64(len(logLines)),
Lines: logLines,
})
} }
resp.LogsData.StreamingLogs = append(resp.LogsData.StreamingLogs, BuildViewStepLog{
StepIndex: cursor.StepIndex,
Cursor: cursor.Cursor + int64(len(logLines)),
Lines: logLines,
})
} }
} }
} }

View File

@ -19,6 +19,7 @@ import (
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/metrics" "code.gitea.io/gitea/modules/metrics"
"code.gitea.io/gitea/modules/public" "code.gitea.io/gitea/modules/public"
_ "code.gitea.io/gitea/modules/session" // to registers all internal adapters
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/storage" "code.gitea.io/gitea/modules/storage"
"code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/structs"
@ -45,8 +46,6 @@ import (
"code.gitea.io/gitea/services/forms" "code.gitea.io/gitea/services/forms"
"code.gitea.io/gitea/services/lfs" "code.gitea.io/gitea/services/lfs"
_ "code.gitea.io/gitea/modules/session" // to registers all internal adapters
"gitea.com/go-chi/captcha" "gitea.com/go-chi/captcha"
"gitea.com/go-chi/session" "gitea.com/go-chi/session"
"github.com/NYTimes/gziphandler" "github.com/NYTimes/gziphandler"
@ -661,8 +660,12 @@ func RegisterRoutes(m *web.Route) {
if !setting.IsProd { if !setting.IsProd {
m.Any("/dev/termdemo", dev.TermDemo) m.Any("/dev/termdemo", dev.TermDemo)
m.Get("/dev/buildview", dev.BuildView) m.Combo("/dev/buildview/runs/{runid}").
m.Post("/dev/buildview", bindIgnErr(dev.BuildViewRequest{}), dev.BuildViewPost) Get(dev.BuildView).
Post(bindIgnErr(dev.BuildViewRequest{}), dev.BuildViewPost)
m.Combo("/dev/buildview/runs/{runid}/jobs/{jobid}").
Get(dev.BuildView).
Post(bindIgnErr(dev.BuildViewRequest{}), dev.BuildViewPost)
} }
reqRepoAdmin := context.RequireRepoAdmin() reqRepoAdmin := context.RequireRepoAdmin()

View File

@ -1,6 +1,6 @@
{{template "base/head" .}} {{template "base/head" .}}
<div id="repo-build-view" class="h-100"> <div id="repo-build-view" run-id="{{.RunID}}" job-id="{{.JobID}}" class="h-100">
</div> </div>

View File

@ -10,7 +10,7 @@
{{ jobGroup.summary }} {{ jobGroup.summary }}
</div> </div>
<div class="job-brief-list"> <div class="job-brief-list">
<a class="job-brief-item" v-for="job in jobGroup.jobs" :key="job.id"> <a class="job-brief-item" v-for="job in jobGroup.jobs" :key="job.id" v-bind:href="'/dev/buildview/runs/'+runId+'/jobs/'+job.id">
<SvgIcon name="octicon-check-circle-fill" class="green" v-if="job.status === 'success'"/> <SvgIcon name="octicon-check-circle-fill" class="green" v-if="job.status === 'success'"/>
<SvgIcon name="octicon-skip" class="ui text grey" v-else-if="job.status === 'skipped'"/> <SvgIcon name="octicon-skip" class="ui text grey" v-else-if="job.status === 'skipped'"/>
<SvgIcon name="octicon-clock" class="ui text yellow" v-else-if="job.status === 'waiting'"/> <SvgIcon name="octicon-clock" class="ui text yellow" v-else-if="job.status === 'waiting'"/>
@ -77,11 +77,13 @@ const sfc = {
components: { components: {
SvgIcon, SvgIcon,
}, },
props: {
runId: Number,
jobId: Number,
},
data() { data() {
return { return {
jobId: 120, // TODO: read job id
// internal state // internal state
loading: false, loading: false,
currentJobStepsStates: [], currentJobStepsStates: [],
@ -138,7 +140,7 @@ const sfc = {
toggleStepLogs(idx) { toggleStepLogs(idx) {
this.currentJobStepsStates[idx].expanded = !this.currentJobStepsStates[idx].expanded; this.currentJobStepsStates[idx].expanded = !this.currentJobStepsStates[idx].expanded;
if (this.currentJobStepsStates[idx].expanded) { if (this.currentJobStepsStates[idx].expanded) {
this.loadJobData(); // this.loadJobData(); // FIXME: cannot call loadJobData more than once
} }
}, },
@ -250,7 +252,7 @@ const sfc = {
}, },
async fetchJobData(reqData) { async fetchJobData(reqData) {
const resp = await fetch(`?job_id=${this.jobId}`, { const resp = await fetch(`/dev/buildview/runs/${this.runId}/jobs/${this.jobId}`, { // FIXME: hard code path
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(reqData), body: JSON.stringify(reqData),
@ -307,7 +309,10 @@ export function initRepositoryBuildView() {
const el = document.getElementById('repo-build-view'); const el = document.getElementById('repo-build-view');
if (!el) return; if (!el) return;
const view = createApp(sfc); const view = createApp(sfc, {
jobId: el.getAttribute("job-id"),
runId: el.getAttribute("run-id"),
});
view.mount(el); view.mount(el);
} }