Skip to content

Commit

Permalink
feature: add costTime of autoTest plan list (#2040)
Browse files Browse the repository at this point in the history
* feature: add exec time of autoTest scene list

* fix: update pipeline task when fulfillParentSnippetTask

* fix: add comment
  • Loading branch information
littlejiancc authored Sep 24, 2021
1 parent 7040218 commit 74ae9d1
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/json"
"fmt"
"strconv"
"time"

"github.com/sirupsen/logrus"

Expand Down Expand Up @@ -214,6 +215,12 @@ func getProps() map[string]interface{} {
Width: 85,
Ellipsis: true,
},
{
Title: "执行时间",
DataIndex: "time",
Width: 85,
Ellipsis: true,
},
{
Title: "接口路径",
DataIndex: "path",
Expand Down Expand Up @@ -323,6 +330,7 @@ func (a *ExecuteTaskTable) setData(pipeline *apistructs.PipelineDetailDTO) error
"status": getStatus(task.Status),
"type": transformStepType(apistructs.StepAPIType(task.Type)),
"path": "",
"time": a.getCostTime(task),
}
lists = append(lists, list)
ret--
Expand Down Expand Up @@ -398,6 +406,7 @@ func (a *ExecuteTaskTable) setData(pipeline *apistructs.PipelineDetailDTO) error
"status": getStatus(task.Status),
"type": transformStepType(res.Type),
"path": path,
"time": a.getCostTime(task),
}

if task.SnippetPipelineID != nil && (res.Type == apistructs.StepTypeScene ||
Expand Down Expand Up @@ -432,6 +441,7 @@ func (a *ExecuteTaskTable) setData(pipeline *apistructs.PipelineDetailDTO) error
"status": getStatus(task.Status),
"type": transformStepType(apistructs.AutotestSceneSet),
"path": "",
"time": a.getCostTime(task),
}
if task.SnippetPipelineDetail != nil {
list["tasksNum"] = task.SnippetPipelineDetail.DirectSnippetTasksNum
Expand Down Expand Up @@ -464,6 +474,7 @@ func (a *ExecuteTaskTable) setData(pipeline *apistructs.PipelineDetailDTO) error
"status": getStatus(task.Status),
"type": transformStepType(apistructs.AutotestScene),
"path": "",
"time": a.getCostTime(task),
}
if task.SnippetPipelineDetail != nil {
list["tasksNum"] = task.SnippetPipelineDetail.DirectSnippetTasksNum
Expand All @@ -488,6 +499,15 @@ func (a *ExecuteTaskTable) setData(pipeline *apistructs.PipelineDetailDTO) error
return nil
}

// getCostTime the format of time is "00:00:00"
// id is not end status or err return "-"
func (a *ExecuteTaskTable) getCostTime(task apistructs.PipelineTaskDTO) string {
if !task.Status.IsEndStatus() {
return "-"
}
return time.Unix(task.CostTimeSec, 0).In(time.UTC).Format("15:04:05")
}

func (a *ExecuteTaskTable) marshal(c *apistructs.Component) error {
stateValue, err := json.Marshal(a.State)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/alecthomas/assert"

"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/bundle"
protocol "github.com/erda-project/erda/modules/openapi/component-protocol"
)

Expand All @@ -44,3 +45,49 @@ func Test_handlerListOperation(t *testing.T) {
}
assert.NoError(t, err)
}

func TestGetCostTime(t *testing.T) {
tt := []struct {
task apistructs.PipelineTaskDTO
want string
}{
{
apistructs.PipelineTaskDTO{
Status: apistructs.PipelineStatusRunning,
},
"-",
},
{
apistructs.PipelineTaskDTO{
Status: apistructs.PipelineStatusSuccess,
IsSnippet: false,
CostTimeSec: 59,
},
"00:00:59",
},
{
apistructs.PipelineTaskDTO{
Status: apistructs.PipelineStatusSuccess,
IsSnippet: false,
CostTimeSec: 3600,
},
"01:00:00",
},
{
apistructs.PipelineTaskDTO{
Status: apistructs.PipelineStatusSuccess,
IsSnippet: true,
CostTimeSec: 59*60 + 59,
},
"00:59:59",
},
}
r := ExecuteTaskTable{
CtxBdl: protocol.ContextBundle{
Bdl: bundle.New(),
},
}
for _, v := range tt {
assert.Equal(t, v.want, r.getCostTime(v.task))
}
}
22 changes: 22 additions & 0 deletions modules/pipeline/dbclient/op_pipeline_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,28 @@ func (client *Client) UpdatePipelineTaskStatus(id uint64, status apistructs.Pipe
return err
}

// UpdatePipelineTaskTime update the costTime,timeBegin and timeEnd of pipeline task
func (client *Client) UpdatePipelineTaskTime(p *spec.Pipeline, ops ...SessionOption) error {
session := client.NewSession(ops...)
defer session.Close()

if p.TimeBegin == nil || p.TimeBegin.IsZero() {
p.TimeBegin = p.TimeCreated
}
if p.TimeEnd == nil || p.TimeEnd.IsZero() {
p.TimeEnd = p.TimeUpdated
}
timeBegin, timeEnd := *p.TimeBegin, *p.TimeEnd

costTimeSec := p.CostTimeSec
if costTimeSec == -1 {
costTimeSec = int64(timeEnd.Sub(timeBegin).Seconds())
}
_, err := session.ID(p.ParentTaskID).Cols("cost_time_sec", "time_begin", "time_end").
Update(&spec.PipelineTask{CostTimeSec: costTimeSec, TimeBegin: timeBegin, TimeEnd: timeEnd})
return err
}

func (client *Client) UpdatePipelineTaskContext(id uint64, ctx spec.PipelineTaskContext, ops ...SessionOption) error {
session := client.NewSession(ops...)
defer session.Close()
Expand Down
4 changes: 4 additions & 0 deletions modules/pipeline/pipengine/reconciler/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ func (r *Reconciler) fulfillParentSnippetTask(p *spec.Pipeline) error {
if err := r.dbClient.UpdatePipelineTaskStatus(*p.ParentTaskID, calcStatus); err != nil {
return err
}
// update the costTime,timeBegin,timeEnd of pipeline task
if err := r.dbClient.UpdatePipelineTaskTime(p); err != nil {
return err
}
return nil
}

Expand Down

0 comments on commit 74ae9d1

Please sign in to comment.