mirror of
https://github.com/go-gitea/gitea.git
synced 2025-07-14 17:35:07 +02:00
feat: calculate duration
This commit is contained in:
parent
c510fdbcf1
commit
a4b2cf2426
@ -82,15 +82,8 @@ func (run *ActionRun) LoadAttributes(ctx context.Context) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (run *ActionRun) TakeTime() time.Duration {
|
func (run *ActionRun) Duration() time.Duration {
|
||||||
if run.Started == 0 {
|
return calculateDuration(run.Started, run.Stopped, run.Status)
|
||||||
return 0
|
|
||||||
}
|
|
||||||
started := run.Started.AsTime()
|
|
||||||
if run.Status.IsDone() {
|
|
||||||
return run.Stopped.AsTime().Sub(started)
|
|
||||||
}
|
|
||||||
return time.Since(started).Truncate(time.Second)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (run *ActionRun) GetPushEventPayload() (*api.PushPayload, error) {
|
func (run *ActionRun) GetPushEventPayload() (*api.PushPayload, error) {
|
||||||
|
@ -43,16 +43,8 @@ func init() {
|
|||||||
db.RegisterModel(new(ActionRunJob))
|
db.RegisterModel(new(ActionRunJob))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (job *ActionRunJob) TakeTime() time.Duration {
|
func (job *ActionRunJob) Duration() time.Duration {
|
||||||
if job.Started == 0 {
|
return calculateDuration(job.Started, job.Stopped, job.Status)
|
||||||
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 *ActionRunJob) LoadRun(ctx context.Context) error {
|
func (job *ActionRunJob) LoadRun(ctx context.Context) error {
|
||||||
|
@ -75,15 +75,8 @@ func init() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (task *ActionTask) TakeTime() time.Duration {
|
func (task *ActionTask) Duration() time.Duration {
|
||||||
if task.Started == 0 {
|
return calculateDuration(task.Started, task.Stopped, task.Status)
|
||||||
return 0
|
|
||||||
}
|
|
||||||
started := task.Started.AsTime()
|
|
||||||
if task.Status.IsDone() {
|
|
||||||
return task.Stopped.AsTime().Sub(started)
|
|
||||||
}
|
|
||||||
return time.Since(started).Truncate(time.Second)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (task *ActionTask) IsStopped() bool {
|
func (task *ActionTask) IsStopped() bool {
|
||||||
|
@ -27,15 +27,8 @@ type ActionTaskStep struct {
|
|||||||
Updated timeutil.TimeStamp `xorm:"updated"`
|
Updated timeutil.TimeStamp `xorm:"updated"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (step *ActionTaskStep) TakeTime() time.Duration {
|
func (step *ActionTaskStep) Duration() time.Duration {
|
||||||
if step.Started == 0 {
|
return calculateDuration(step.Started, step.Stopped, step.Status)
|
||||||
return 0
|
|
||||||
}
|
|
||||||
started := step.Started.AsTime()
|
|
||||||
if step.Status.IsDone() {
|
|
||||||
return step.Stopped.AsTime().Sub(started)
|
|
||||||
}
|
|
||||||
return time.Since(started).Truncate(time.Second)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -10,8 +10,10 @@ 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/modules/timeutil"
|
||||||
"code.gitea.io/gitea/modules/util"
|
"code.gitea.io/gitea/modules/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -67,3 +69,16 @@ func (indexes *LogIndexes) ToDB() ([]byte, error) {
|
|||||||
}
|
}
|
||||||
return buf[:i], nil
|
return buf[:i], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var timeSince = time.Since
|
||||||
|
|
||||||
|
func calculateDuration(started, stopped timeutil.TimeStamp, status Status) time.Duration {
|
||||||
|
if started == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
s := started.AsTime()
|
||||||
|
if status.IsDone() {
|
||||||
|
return stopped.AsTime().Sub(s)
|
||||||
|
}
|
||||||
|
return timeSince(s).Truncate(time.Second)
|
||||||
|
}
|
||||||
|
@ -6,6 +6,9 @@ package actions
|
|||||||
import (
|
import (
|
||||||
"math"
|
"math"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"code.gitea.io/gitea/modules/timeutil"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
@ -31,3 +34,57 @@ func TestLogIndexes_ToDB(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_calculateDuration(t *testing.T) {
|
||||||
|
oldTimeSince := timeSince
|
||||||
|
defer func() {
|
||||||
|
timeSince = oldTimeSince
|
||||||
|
}()
|
||||||
|
|
||||||
|
timeSince = func(t time.Time) time.Duration {
|
||||||
|
return timeutil.TimeStamp(1000).AsTime().Sub(t)
|
||||||
|
}
|
||||||
|
type args struct {
|
||||||
|
started timeutil.TimeStamp
|
||||||
|
stopped timeutil.TimeStamp
|
||||||
|
status Status
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want time.Duration
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "unknown",
|
||||||
|
args: args{
|
||||||
|
started: 0,
|
||||||
|
stopped: 0,
|
||||||
|
status: StatusUnknown,
|
||||||
|
},
|
||||||
|
want: 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "running",
|
||||||
|
args: args{
|
||||||
|
started: 500,
|
||||||
|
stopped: 0,
|
||||||
|
status: StatusRunning,
|
||||||
|
},
|
||||||
|
want: 500 * time.Second,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "done",
|
||||||
|
args: args{
|
||||||
|
started: 500,
|
||||||
|
stopped: 600,
|
||||||
|
status: StatusSuccess,
|
||||||
|
},
|
||||||
|
want: 100 * time.Second,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
assert.Equalf(t, tt.want, calculateDuration(tt.args.started, tt.args.stopped, tt.args.status), "calculateDuration(%v, %v, %v)", tt.args.started, tt.args.stopped, tt.args.status)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -156,7 +156,7 @@ func ViewPost(ctx *context_module.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.TakeTime() / time.Second),
|
Duration: float64(v.Duration() / time.Second),
|
||||||
Status: v.Status.String(),
|
Status: v.Status.String(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="issue-item-right">
|
<div class="issue-item-right">
|
||||||
<div>{{TimeSinceUnix .Updated $.locale}}</div>
|
<div>{{TimeSinceUnix .Updated $.locale}}</div>
|
||||||
<div>{{.TakeTime}}</div>
|
<div>{{.Duration}}</div>
|
||||||
</div>
|
</div>
|
||||||
</li>
|
</li>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user