-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
186 lines (169 loc) · 3.74 KB
/
service.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package walker
import (
"fmt"
"sort"
"strings"
"time"
"github.com/foomo/walker/config"
"github.com/foomo/walker/vo"
)
type Service struct {
Walker *Walker
// targetURL string
}
func NewService(
conf *config.Config,
linkListFilter LinkListFilterFunc,
scrapeFunc ScrapeFunc,
validationFunc ValidationFunc,
scrapeResultModifierFunc ScrapeResultModifierFunc,
) (s *Service, chanLoopComplete chan vo.Status, err error) {
w := NewWalker()
chanLoopComplete, errWalk := w.Walk(conf, linkListFilter, scrapeFunc, validationFunc, scrapeResultModifierFunc)
if errWalk != nil {
return nil, nil, errWalk
}
s = &Service{
Walker: w,
// targetURL: conf.Target,
}
return
}
func filter(resultMap map[string]vo.ScrapeResult, filterChain filterChain) {
for targetURL, scrapeResult := range resultMap {
for _, filterFunc := range filterChain {
if !filterFunc(scrapeResult) {
delete(resultMap, targetURL)
break
}
}
}
}
type filterFunc func(result vo.ScrapeResult) bool
type filterChain []filterFunc
type Filters struct {
Prefix string
Status []int
Errors []string
MinDur time.Duration
MaxDur time.Duration
}
type StatusStats struct {
Code int
Count int
}
type FilterOptions struct {
Status []StatusStats
MinDur time.Duration
MaxDur time.Duration
}
func getFilterChain(filters Filters) filterChain {
chain := filterChain{}
if filters.Prefix != "" {
chain = append(chain, func(result vo.ScrapeResult) bool {
return strings.HasPrefix(result.TargetURL, filters.Prefix)
})
}
if len(filters.Status) > 0 {
chain = append(chain, func(result vo.ScrapeResult) bool {
for _, status := range filters.Status {
if result.Code == status {
return true
}
}
return false
})
}
if filters.MaxDur > 0 {
chain = append(chain, func(result vo.ScrapeResult) bool {
return result.Duration < filters.MaxDur
})
}
if filters.MinDur > 0 {
chain = append(chain, func(result vo.ScrapeResult) bool {
return result.Duration > filters.MinDur
})
}
return chain
}
func getFilterOptions(resultMap map[string]vo.ScrapeResult) FilterOptions {
statusMap := map[int]int{}
minDur := time.Duration(1000000000000000000)
maxDur := time.Duration(0)
for _, result := range resultMap {
statusMap[result.Code]++
if result.Duration > maxDur {
maxDur = result.Duration
}
if result.Duration < minDur {
minDur = result.Duration
}
}
statusStats := []StatusStats{}
for code, count := range statusMap {
statusStats = append(statusStats, StatusStats{
Code: code,
Count: count,
})
}
return FilterOptions{
Status: statusStats,
MinDur: minDur,
MaxDur: maxDur,
}
}
func (s *Service) GetResults(
filters Filters,
page int,
pageSize int,
) (filterOptions FilterOptions, results []vo.ScrapeResult, numPages int) {
resultMap := s.Walker.GetStatus().Results
filterOptions = getFilterOptions(resultMap)
filter(resultMap, getFilterChain(filters))
i := 0
results = make([]vo.ScrapeResult, len(resultMap))
urls := make([]string, len(resultMap))
for url := range resultMap {
urls[i] = url
i++
}
sort.Strings(urls)
for index, url := range urls {
results[index] = resultMap[url]
}
// paging is missing
numPages = len(results) / pageSize
min := 0
max := len(results)
start := page * pageSize
end := start + pageSize
if start < min {
start = min
}
if end > max {
end = max
}
fmt.Println("slice", start, end)
if end > start {
results = results[start:end]
}
return
}
func (s *Service) GetStatus() vo.ServiceStatus {
walkerStatus := s.Walker.GetStatus()
open := 0
pending := 0
for _, active := range walkerStatus.Jobs {
if active {
pending++
} else {
open++
}
}
return vo.ServiceStatus{
// TargetURL: s.targetURL,
Done: len(walkerStatus.Results),
Open: open,
Pending: pending,
}
}