-
Notifications
You must be signed in to change notification settings - Fork 66
/
stat.go
135 lines (107 loc) · 3.5 KB
/
stat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package agentExec
import (
"encoding/json"
agentDomain "github.com/aaronchen2k/deeptest/internal/agent/exec/domain"
"github.com/aaronchen2k/deeptest/internal/pkg/consts"
"github.com/aaronchen2k/deeptest/internal/pkg/domain"
"regexp"
"strings"
)
func ResetStat(execUuid string) {
SetInterfaceStat(execUuid, &agentDomain.InterfaceStat{})
}
func CountInterfaceStat(execUuid string, result *agentDomain.ScenarioExecResult) agentDomain.InterfaceStat {
stat := GetInterfaceStat(execUuid)
stat.InterfaceCount += 1
stat.InterfaceDurationTotal += result.Cost
stat.InterfaceDurationAverage = stat.InterfaceDurationTotal / stat.InterfaceCount
result.ResultStatus = consts.Pass
for _, item := range result.PostConditions {
if item.Type != consts.ConditionTypeCheckpoint {
continue
}
var checkpointBase domain.CheckpointBase
json.Unmarshal(item.Raw, &checkpointBase)
if checkpointBase.Disabled {
continue
}
if checkpointBase.ResultStatus == consts.Pass {
stat.CheckpointPass += 1
} else if checkpointBase.ResultStatus == consts.Fail {
stat.CheckpointFail += 1
result.ResultStatus = consts.Fail
}
/*
if item.Type != consts.ConditionTypeResponseDefine {
var responseDefineBase domain.ResponseDefineBase
json.Unmarshal(item.Raw, &responseDefineBase)
if responseDefineBase.Disabled {
continue
}
}
*/
}
if result.ResultStatus == consts.Pass {
stat.InterfacePass += 1
} else if result.ResultStatus == consts.Fail {
stat.InterfaceFail += 1
}
SetInterfaceStat(execUuid, stat)
return *stat
}
func CountScriptAssertionStat(execUuid string, output string, result *agentDomain.ScenarioExecResult) agentDomain.InterfaceStat {
stat := GetInterfaceStat(execUuid)
arr := []string{}
json.Unmarshal([]byte(output), &arr)
for _, item := range arr {
status, _, _ := ParseChaiAssertion(item)
if status == "pass" {
result.Stat.CheckpointPass += 1
stat.CheckpointPass += 1
} else if status == "fail" {
result.Stat.CheckpointFail += 1
stat.CheckpointFail += 1
}
}
SetInterfaceStat(execUuid, stat)
return *stat
}
func CountSkip(execUuid string, executedProcessorIds map[uint]bool, skippedChildren []*Processor) agentDomain.InterfaceStat {
countedProcessorIds := map[uint]bool{}
countSkipInterface(execUuid, executedProcessorIds, skippedChildren, &countedProcessorIds)
return *GetInterfaceStat(execUuid)
}
func countSkipInterface(execUuid string, executedProcessorIds map[uint]bool, skippedChildren []*Processor, countedProcessorIds *map[uint]bool) agentDomain.InterfaceStat {
stat := GetInterfaceStat(execUuid)
for _, child := range skippedChildren {
if child.Disable {
continue
}
_, executed := executedProcessorIds[child.ID]
_, counted := (*countedProcessorIds)[child.ID]
if child.EntityType == consts.ProcessorInterfaceDefault && !executed && !counted {
stat.InterfaceSkip += 1
(*countedProcessorIds)[child.ID] = true
}
if len(child.Children) > 0 {
countSkipInterface(execUuid, map[uint]bool{}, child.Children, countedProcessorIds)
}
}
return *stat
}
func ParseChaiAssertion(output string) (status, name, checkpoint string) {
// Assertion Pass [Assertion 1].
// Assertion Failed [Assertion 1] AssertionError: check status code: expected 200 to equal 2001.
regx := regexp.MustCompile(`Assertion (Failed|Pass) \[(.+)\](.*)\.`)
arr := regx.FindAllStringSubmatch(output, -1)
if len(arr) == 0 {
return
}
status = strings.ToLower(arr[0][1])
if status != "pass" {
status = "fail"
}
name = arr[0][2]
checkpoint = arr[0][3]
return
}