diff --git a/models/bots/run_job.go b/models/bots/run_job.go index 23f70d68b5..b78ae31e9c 100644 --- a/models/bots/run_job.go +++ b/models/bots/run_job.go @@ -7,6 +7,7 @@ package bots import ( "context" "fmt" + "time" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/timeutil" @@ -43,6 +44,18 @@ func (RunJob) TableName() string { 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 { if job.Run == nil { run, err := GetRunByID(ctx, job.RunID) @@ -60,7 +73,7 @@ func (job *RunJob) LoadAttributes(ctx context.Context) error { return nil } - if err := job.LoadRun(ctx);err != nil { + if err := job.LoadRun(ctx); err != nil { return err } diff --git a/models/bots/task.go b/models/bots/task.go index ae3be6da0e..1d511b1923 100644 --- a/models/bots/task.go +++ b/models/bots/task.go @@ -11,6 +11,7 @@ import ( "errors" "fmt" "io" + "time" auth_model "code.gitea.io/gitea/models/auth" "code.gitea.io/gitea/models/db" @@ -24,6 +25,7 @@ import ( gouuid "github.com/google/uuid" "github.com/nektos/act/pkg/jobparser" + "google.golang.org/protobuf/types/known/timestamppb" ) // Task represents a distribution of job @@ -63,6 +65,18 @@ func (Task) TableName() string { 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 { if task.Job == nil { job, err := GetRunJobByID(ctx, task.JobID) @@ -376,6 +390,8 @@ func UpdateTaskByState(state *runnerv1.TaskState) (*Task, error) { step.Result = v.Result step.LogIndex = v.LogIndex step.LogLength = v.LogLength + step.Started = convertTimestamp(v.StartedAt) + step.Stopped = convertTimestamp(v.StoppedAt) } if step.Result != runnerv1.Result_RESULT_UNSPECIFIED { step.Status = Status(step.Result) @@ -408,3 +424,10 @@ func isSubset(set, subset []string) bool { } 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()) +} diff --git a/models/bots/task_step.go b/models/bots/task_step.go index 1f529ae364..ba24fc34d7 100644 --- a/models/bots/task_step.go +++ b/models/bots/task_step.go @@ -6,6 +6,7 @@ package bots import ( "context" + "time" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/timeutil" @@ -32,6 +33,18 @@ func (TaskStep) TableName() string { 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() { db.RegisterModel(new(TaskStep)) } diff --git a/routers/web/repo/builds/view.go b/routers/web/repo/builds/view.go index f69c486914..df97588b6b 100644 --- a/routers/web/repo/builds/view.go +++ b/routers/web/repo/builds/view.go @@ -145,7 +145,7 @@ func ViewPost(ctx *context.Context) { for i, v := range steps { resp.StateData.CurrentJobSteps[i] = ViewJobStep{ Summary: v.Name, - Duration: float64(v.Stopped - v.Started), + Duration: float64(v.TakeTime() / time.Second), Status: v.Status.String(), } }