feat: show step duration

This commit is contained in:
Jason Song 2022-11-01 18:14:20 +08:00
parent b4b22e78ad
commit b1da53286d
4 changed files with 51 additions and 2 deletions

View File

@ -7,6 +7,7 @@ package bots
import ( import (
"context" "context"
"fmt" "fmt"
"time"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/timeutil"
@ -43,6 +44,18 @@ func (RunJob) TableName() string {
return "bots_run_job" return "bots_run_job"
} }
func (job *RunJob) TakeTime() time.Duration {
if job.Started == 0 {
return 0
}
started := job.Started.AsTime()
if job.Status.IsDone() {
return job.Stopped.AsTime().Sub(started)
}
job.Stopped.AsTime().Sub(started)
return time.Since(started).Truncate(time.Second)
}
func (job *RunJob) LoadRun(ctx context.Context) error { func (job *RunJob) LoadRun(ctx context.Context) error {
if job.Run == nil { if job.Run == nil {
run, err := GetRunByID(ctx, job.RunID) run, err := GetRunByID(ctx, job.RunID)
@ -60,7 +73,7 @@ func (job *RunJob) LoadAttributes(ctx context.Context) error {
return nil return nil
} }
if err := job.LoadRun(ctx);err != nil { if err := job.LoadRun(ctx); err != nil {
return err return err
} }

View File

@ -11,6 +11,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"time"
auth_model "code.gitea.io/gitea/models/auth" auth_model "code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
@ -24,6 +25,7 @@ import (
gouuid "github.com/google/uuid" gouuid "github.com/google/uuid"
"github.com/nektos/act/pkg/jobparser" "github.com/nektos/act/pkg/jobparser"
"google.golang.org/protobuf/types/known/timestamppb"
) )
// Task represents a distribution of job // Task represents a distribution of job
@ -63,6 +65,18 @@ func (Task) TableName() string {
return "bots_task" return "bots_task"
} }
func (task *Task) TakeTime() time.Duration {
if task.Started == 0 {
return 0
}
started := task.Started.AsTime()
if task.Status.IsDone() {
return task.Stopped.AsTime().Sub(started)
}
task.Stopped.AsTime().Sub(started)
return time.Since(started).Truncate(time.Second)
}
func (task *Task) LoadJob(ctx context.Context) error { func (task *Task) LoadJob(ctx context.Context) error {
if task.Job == nil { if task.Job == nil {
job, err := GetRunJobByID(ctx, task.JobID) job, err := GetRunJobByID(ctx, task.JobID)
@ -376,6 +390,8 @@ func UpdateTaskByState(state *runnerv1.TaskState) (*Task, error) {
step.Result = v.Result step.Result = v.Result
step.LogIndex = v.LogIndex step.LogIndex = v.LogIndex
step.LogLength = v.LogLength step.LogLength = v.LogLength
step.Started = convertTimestamp(v.StartedAt)
step.Stopped = convertTimestamp(v.StoppedAt)
} }
if step.Result != runnerv1.Result_RESULT_UNSPECIFIED { if step.Result != runnerv1.Result_RESULT_UNSPECIFIED {
step.Status = Status(step.Result) step.Status = Status(step.Result)
@ -408,3 +424,10 @@ func isSubset(set, subset []string) bool {
} }
return true return true
} }
func convertTimestamp(timestamp *timestamppb.Timestamp) timeutil.TimeStamp {
if timestamp.GetSeconds() == 0 && timestamp.GetNanos() == 0 {
return timeutil.TimeStamp(0)
}
return timeutil.TimeStamp(timestamp.AsTime().Unix())
}

View File

@ -6,6 +6,7 @@ package bots
import ( import (
"context" "context"
"time"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/timeutil"
@ -32,6 +33,18 @@ func (TaskStep) TableName() string {
return "bots_task_step" return "bots_task_step"
} }
func (step *TaskStep) TakeTime() time.Duration {
if step.Started == 0 {
return 0
}
started := step.Started.AsTime()
if step.Status.IsDone() {
return step.Stopped.AsTime().Sub(started)
}
step.Stopped.AsTime().Sub(started)
return time.Since(started).Truncate(time.Second)
}
func init() { func init() {
db.RegisterModel(new(TaskStep)) db.RegisterModel(new(TaskStep))
} }

View File

@ -145,7 +145,7 @@ func ViewPost(ctx *context.Context) {
for i, v := range steps { for i, v := range steps {
resp.StateData.CurrentJobSteps[i] = ViewJobStep{ resp.StateData.CurrentJobSteps[i] = ViewJobStep{
Summary: v.Name, Summary: v.Name,
Duration: float64(v.Stopped - v.Started), Duration: float64(v.TakeTime() / time.Second),
Status: v.Status.String(), Status: v.Status.String(),
} }
} }