forked from hyper0x/goc2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
summary.go
144 lines (136 loc) · 4.61 KB
/
summary.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
package scheduler
import (
"bytes"
"fmt"
base "webcrawler/base"
)
// 调度器摘要信息的接口类型。
type SchedSummary interface {
String() string // 获得摘要信息的一般表示。
Detail() string // 获取摘要信息的详细表示。
Same(other SchedSummary) bool // 判断是否与另一份摘要信息相同。
}
// 创建调度器摘要信息。
func NewSchedSummary(sched *myScheduler, prefix string) SchedSummary {
if sched == nil {
return nil
}
urlCount := len(sched.urlMap)
var urlDetail string
if urlCount > 0 {
var buffer bytes.Buffer
buffer.WriteByte('\n')
for k, _ := range sched.urlMap {
buffer.WriteString(prefix)
buffer.WriteString(prefix)
buffer.WriteString(k)
buffer.WriteByte('\n')
}
urlDetail = buffer.String()
} else {
urlDetail = "\n"
}
return &mySchedSummary{
prefix: prefix,
running: sched.running,
channelArgs: sched.channelArgs,
poolBaseArgs: sched.poolBaseArgs,
crawlDepth: sched.crawlDepth,
chanmanSummary: sched.chanman.Summary(),
reqCacheSummary: sched.reqCache.summary(),
dlPoolLen: sched.dlpool.Used(),
dlPoolCap: sched.dlpool.Total(),
analyzerPoolLen: sched.analyzerPool.Used(),
analyzerPoolCap: sched.analyzerPool.Total(),
itemPipelineSummary: sched.itemPipeline.Summary(),
urlCount: urlCount,
urlDetail: urlDetail,
stopSignSummary: sched.stopSign.Summary(),
}
}
// 调度器摘要信息的实现类型。
type mySchedSummary struct {
prefix string // 前缀。
running uint32 // 运行标记。
channelArgs base.ChannelArgs // 通道参数的容器。
poolBaseArgs base.PoolBaseArgs // 池基本参数的容器。
crawlDepth uint32 // 爬取的最大深度。
chanmanSummary string // 通道管理器的摘要信息。
reqCacheSummary string // 请求缓存的摘要信息。
dlPoolLen uint32 // 网页下载器池的长度。
dlPoolCap uint32 // 网页下载器池的容量。
analyzerPoolLen uint32 // 分析器池的长度。
analyzerPoolCap uint32 // 分析器池的容量。
itemPipelineSummary string // 条目处理管道的摘要信息。
urlCount int // 已请求的URL的计数。
urlDetail string // 已请求的URL的详细信息。
stopSignSummary string // 停止信号的摘要信息。
}
func (ss *mySchedSummary) String() string {
return ss.getSummary(false)
}
func (ss *mySchedSummary) Detail() string {
return ss.getSummary(true)
}
// 获取摘要信息。
func (ss *mySchedSummary) getSummary(detail bool) string {
prefix := ss.prefix
template := prefix + "Running: %v \n" +
prefix + "Channel args: %s \n" +
prefix + "Pool base args: %s \n" +
prefix + "Crawl depth: %d \n" +
prefix + "Channels manager: %s \n" +
prefix + "Request cache: %s\n" +
prefix + "Downloader pool: %d/%d\n" +
prefix + "Analyzer pool: %d/%d\n" +
prefix + "Item pipeline: %s\n" +
prefix + "Urls(%d): %s" +
prefix + "Stop sign: %s\n"
return fmt.Sprintf(template,
func() bool {
return ss.running == 1
}(),
ss.channelArgs.String(),
ss.poolBaseArgs.String(),
ss.crawlDepth,
ss.chanmanSummary,
ss.reqCacheSummary,
ss.dlPoolLen, ss.dlPoolCap,
ss.analyzerPoolLen, ss.analyzerPoolCap,
ss.itemPipelineSummary,
ss.urlCount,
func() string {
if detail {
return ss.urlDetail
} else {
return "<concealed>\n"
}
}(),
ss.stopSignSummary)
}
func (ss *mySchedSummary) Same(other SchedSummary) bool {
if other == nil {
return false
}
otherSs, ok := interface{}(other).(*mySchedSummary)
if !ok {
return false
}
if ss.running != otherSs.running ||
ss.crawlDepth != otherSs.crawlDepth ||
ss.dlPoolLen != otherSs.dlPoolLen ||
ss.dlPoolCap != otherSs.dlPoolCap ||
ss.analyzerPoolLen != otherSs.analyzerPoolLen ||
ss.analyzerPoolCap != otherSs.analyzerPoolCap ||
ss.urlCount != otherSs.urlCount ||
ss.stopSignSummary != otherSs.stopSignSummary ||
ss.reqCacheSummary != otherSs.reqCacheSummary ||
ss.poolBaseArgs.String() != otherSs.poolBaseArgs.String() ||
ss.channelArgs.String() != otherSs.channelArgs.String() ||
ss.itemPipelineSummary != otherSs.itemPipelineSummary ||
ss.chanmanSummary != otherSs.chanmanSummary {
return false
} else {
return true
}
}