This repository has been archived by the owner on May 11, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.go
244 lines (216 loc) · 5.34 KB
/
report.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package availability
import (
"net/url"
"sync"
"github.com/kamilsk/check/errors"
)
// NewReport returns configured report builder.
func NewReport(options ...func(*Report)) *Report {
r := &Report{}
for _, f := range options {
f(r)
}
return r
}
// CrawlerForSites sets the website crawler to a report builder.
func CrawlerForSites(crawler Crawler) func(*Report) {
return func(r *Report) {
r.crawler = crawler
}
}
// Report represents a report builder.
type Report struct {
crawler Crawler
sites []*Site
ready chan Site
}
// For prepares report builder for passed websites' URLs.
func (r *Report) For(rawURLs []string) *Report {
r.sites = make([]*Site, 0, len(rawURLs))
for _, rawURL := range rawURLs {
r.sites = append(r.sites, NewSite(rawURL))
}
return r
}
// Fill starts to fetch sites and prepared them for reading.
func (r *Report) Fill() *Report {
r.ready = make(chan Site, len(r.sites))
for _, site := range r.sites {
site.Error = site.Fetch(r.crawler)
{
copied := *site
pages := make([]*Page, 0, len(site.Pages))
for _, page := range site.Pages {
page := *page
pages = append(pages, &page)
links := make([]Link, len(page.Links))
copy(links, page.Links)
page.Links = links
}
copied.Pages = pages
r.ready <- copied
}
}
close(r.ready)
return r
}
// Sites returns a channel what will be closed when the report is available.
func (r *Report) Sites() <-chan Site {
return r.ready
}
// NewSite returns new instance of the website.
func NewSite(rawURL string) *Site {
u, err := url.Parse(rawURL)
return &Site{
url: u,
Name: hostOrRawURL(u, rawURL),
Error: errors.Wrapf(err, "parse rawURL %q for report", rawURL),
}
}
// Site contains a meta information about a website.
type Site struct {
url *url.URL
Name string
Error error
Pages []*Page
Problems []ProblemEvent
}
// Fetch runs the website crawler and starts listen its events to build a website tree.
func (s *Site) Fetch(crawler Crawler) error {
if s.Error != nil {
return s.Error
}
if crawler == nil {
s.Error = errors.Simple("crawler is not provided")
return s.Error
}
var unexpected error
wg, events := &sync.WaitGroup{}, make(chan event, 512)
wg.Add(1)
go func() {
defer wg.Done()
defer errors.Recover(&unexpected)
s.listen(events)
}()
s.Error = crawler.Visit(s.url.String(), events)
wg.Wait()
if unexpected != nil {
panic(unexpected)
}
return s.Error
}
func (s *Site) listen(events <-chan event) {
links := make(map[string]*Link)
pages := make(map[string]*Page)
linkToPage := make([][2]string, 0, 512)
for event := range events {
switch e := event.(type) {
case ErrorEvent:
if _, exists := links[e.Location]; !exists {
links[e.Location] = &Link{
StatusCode: e.StatusCode,
Location: e.Location,
Redirect: e.Redirect,
Error: e.Error,
}
}
case ResponseEvent:
if _, exists := links[e.Location]; !exists {
links[e.Location] = &Link{
StatusCode: e.StatusCode,
Location: e.Location,
}
}
case WalkEvent:
if _, exists := pages[e.Page]; !exists {
pages[e.Page] = &Page{Links: make([]Link, 0, 8)}
}
linkToPage = append(linkToPage, [2]string{e.Href, e.Page})
case ProblemEvent:
s.Problems = append(s.Problems, e)
default:
panic(errors.Errorf("panic: unexpected event type %T", e))
}
}
barrier := make(map[*Page]map[*Link]struct{})
s.Pages = make([]*Page, 0, len(pages))
for location, page := range pages {
page.Link = links[location]
s.Pages = append(s.Pages, page)
barrier[page] = make(map[*Link]struct{})
}
for _, linkAndPage := range linkToPage {
linkLocation, pageLocation := linkAndPage[0], linkAndPage[1]
link := links[linkLocation]
page := pages[pageLocation]
if _, exists := barrier[page][link]; !exists {
barrier[page][link] = struct{}{}
{
link := *link
link.Page = page
link.Internal = hasSameHost(page.Link.Location, link.Location)
page.Links = append(page.Links, link)
}
}
}
}
// Page contains meta information about a website page.
type Page struct {
*Link
Links []Link
}
// Link contains meta information about a web link.
type Link struct {
Page *Page
Internal bool
StatusCode int
Location string
Redirect string
Error error
}
func hostOrRawURL(u *url.URL, raw string) string {
if u == nil {
return raw
}
return u.Host
}
func hasSameHost(link1, link2 string) bool {
u1, _ := url.Parse(link1)
u2, _ := url.Parse(link2)
return u1 != nil && u2 != nil && u1.Host == u2.Host
}
type event interface {
family()
}
// NewReadableEventBus returns read/write-channel of events.
func NewReadableEventBus(size int) chan event {
return make(chan event, size)
}
// EventBus is a write-only channel to communicate between a website crawler and a report builder.
type EventBus chan<- event
// ErrorEvent contains a response' status code, its URL and an encountered error.
type ErrorEvent struct {
event
StatusCode int
Location string
Redirect string
Error error
}
// ResponseEvent contains a response' status code and its URL.
type ResponseEvent struct {
event
StatusCode int
Location string
}
// WalkEvent contains information about a page and a link located on it.
type WalkEvent struct {
event
Page string
Href string
}
// ProblemEvent contains information about unexpected error.
type ProblemEvent struct {
event
Message string
Context interface{}
}