/
statistic.go
116 lines (101 loc) · 3.36 KB
/
statistic.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
package service
import (
"fmt"
"path/filepath"
"sort"
"strings"
commConsts "github.com/easysoft/zentaoatf/internal/pkg/consts"
analysisHelper "github.com/easysoft/zentaoatf/internal/pkg/helper/analysis"
serverDomain "github.com/easysoft/zentaoatf/internal/server/modules/v1/domain"
"github.com/easysoft/zentaoatf/internal/server/modules/v1/model"
"github.com/easysoft/zentaoatf/internal/server/modules/v1/repo"
"github.com/jinzhu/copier"
)
type StatisticService struct {
StatisticRepo *repo.StatisticRepo `inject:""`
WorkspaceService *WorkspaceService `inject:""`
}
func NewStatisticService() *StatisticService {
return &StatisticService{}
}
func (s *StatisticService) Get(id uint) (statistics model.Statistic, err error) {
return s.StatisticRepo.Get(id)
}
func (s *StatisticService) GetByPath(path string) (statistics model.Statistic, err error) {
return s.StatisticRepo.GetByPath(path)
}
func (s *StatisticService) Create(statistics model.Statistic) (id uint, isDuplicate bool, err error) {
id, isDuplicate, err = s.StatisticRepo.Create(&statistics)
return
}
func (s *StatisticService) Update(statistics model.Statistic) (isDuplicate bool, err error) {
isDuplicate, err = s.StatisticRepo.Update(statistics)
if isDuplicate || err != nil {
return
}
return
}
func (s *StatisticService) Delete(id uint) error {
return s.StatisticRepo.Delete(id)
}
func (s *StatisticService) UpdateStatistic(logPath string) (scriptPaths []string, err error) {
pth := filepath.Join(logPath, commConsts.ResultJson)
report, err := analysisHelper.ReadReportByPath(pth)
scriptPaths = []string{}
for _, statistic := range report.FuncResult {
scriptPaths = append(scriptPaths, statistic.Path)
exist, _ := s.GetByPath(statistic.Path)
succ, fail := exist.Succ, exist.Fail
if statistic.Status == "pass" {
succ++
} else {
fail++
}
if exist.ID == 0 {
_, _, err = s.StatisticRepo.Create(&model.Statistic{
Path: statistic.Path,
Total: succ + fail,
Succ: succ,
Fail: fail,
FailLogs: pth,
})
return
}
err = s.StatisticRepo.UpdateStatistic(exist.Path, succ+fail, succ, fail, fmt.Sprintf("%s,%s", exist.FailLogs, pth))
if err != nil {
return
}
}
return
}
func (s *StatisticService) GetFailureLogs(scriptPath string) (reports []serverDomain.TestReportSummary, err error) {
statistic, err := s.GetByPath(scriptPath)
if err != nil {
return
}
logPath := strings.Split(statistic.FailLogs, ",")
for _, path := range logPath {
report, _ := analysisHelper.ReadReportByPath(path)
workspace, _ := s.WorkspaceService.GetByPath(report.WorkspacePath)
summary := serverDomain.TestReportSummary{
WorkspaceId: int(workspace.ID),
}
copier.Copy(&summary, report)
seq := strings.Replace(path, filepath.Join(workspace.Path, commConsts.LogDirName), "", -1)
seq = strings.Replace(seq, commConsts.ResultJson, "", -1)
seq = strings.Trim(seq, string(filepath.Separator))
seq = analysisHelper.EncodeSeq(seq)
summary.No = fmt.Sprintf("%d-%s", workspace.ID, seq)
summary.Seq = seq
summary.WorkspaceId = int(workspace.ID)
summary.WorkspaceName = workspace.Name
if report.Total == 1 && len(report.FuncResult) > 0 {
_, summary.TestScriptName = filepath.Split(report.FuncResult[0].Path)
}
reports = append(reports, summary)
}
sort.Slice(reports, func(i, j int) bool {
return reports[i].StartTime > reports[j].StartTime
})
return
}