-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc.go
More file actions
184 lines (164 loc) · 4.64 KB
/
Copy pathfunc.go
File metadata and controls
184 lines (164 loc) · 4.64 KB
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
package main
import (
"bufio"
"crypto/md5"
"encoding/hex"
"github.com/mediocregopher/radix.v2/pool"
"github.com/mgutz/str"
"io"
"net/url"
"os"
"strconv"
"strings"
"time"
)
// HBase 劣势:列簇需要声明清楚
func dataStorage(storageChannel chan storageBlock, redisPool *pool.Pool) {
for block := range storageChannel {
prefix := block.counterType + "_"
// 逐层添加,加洋葱皮的过程
// 维度: 天-小时-分钟
// 层级: 定级-大分类-小分类-终极页面
// 存储模型: Redis SortedSet
setKeys := []string{
prefix + "day_" + getTime(block.unode.unTime, "day"),
prefix + "hour_" + getTime(block.unode.unTime, "hour"),
prefix + "min_" + getTime(block.unode.unTime, "min"),
prefix + block.unode.unType + "_day_" + getTime(block.unode.unTime, "day"),
prefix + block.unode.unType + "_hour_" + getTime(block.unode.unTime, "hour"),
prefix + block.unode.unType + "_min_" + getTime(block.unode.unTime, "min"),
}
rowId := block.unode.unRid
for _, key := range setKeys {
ret, err := redisPool.Cmd(block.storageModel, key, 1, rowId).Int()
if ret <= 0 || err != nil {
log.Errorln("DataStorage redis storage error.", block.storageModel, key, rowId)
}
}
}
}
func pvCounter(pvChannel chan urlData, storageChannel chan storageBlock) {
for data := range pvChannel {
sItem := storageBlock{"pv", "ZINCRBY", data.unode}
storageChannel <- sItem
}
}
func uvCounter(uvChannel chan urlData, storageChannel chan storageBlock, redisPool *pool.Pool) {
for data := range uvChannel {
//HyperLoglog redis
hyperLogLogKey := "uv_hpll_" + getTime(data.data.time, "day")
ret, err := redisPool.Cmd("PFADD", hyperLogLogKey, data.uid, "EX", 86400).Int()
if err != nil {
log.Warningln("UvCounter check redis hyperloglog failed, ", err)
}
if ret != 1 {
continue
}
sItem := storageBlock{"uv", "ZINCRBY", data.unode}
storageChannel <- sItem
}
}
func logConsumer(logChannel chan string, pvChannel, uvChannel chan urlData) error {
for logStr := range logChannel {
// 切割日志字符串,扣出打点上报的数据
data := cutLogFetchData(logStr)
// uid
// 说明: 课程中模拟生成uid, md5(refer+ua)
hasher := md5.New()
hasher.Write([]byte(data.refer + data.ua))
uid := hex.EncodeToString(hasher.Sum(nil))
// 很多解析的工作都可以放到这里完成
// ...
// ...
uData := urlData{uid, data, formatUrl(data.url, data.time)}
pvChannel <- uData
uvChannel <- uData
}
return nil
}
func cutLogFetchData(logStr string) digData {
logStr = strings.TrimSpace(logStr)
pos1 := str.IndexOf(logStr, HANDLE_DIG, 0)
if pos1 == -1 {
return digData{}
}
pos1 += len(HANDLE_DIG)
pos2 := str.IndexOf(logStr, " HTTP/", pos1)
d := str.Substr(logStr, pos1, pos2-pos1)
urlInfo, err := url.Parse("http://localhost/?" + d)
if err != nil {
return digData{}
}
data := urlInfo.Query()
return digData{
data.Get("time"),
data.Get("refer"),
data.Get("url"),
data.Get("ua"),
}
}
func formatUrl(url, t string) urlNode {
// 一定从量大的着手, 详情页>列表页≥首页
pos1 := str.IndexOf(url, HANDLE_MOVIE, 0)
if pos1 != -1 {
pos1 += len(HANDLE_MOVIE)
pos2 := str.IndexOf(url, HANDLE_HTML, 0)
idStr := str.Substr(url, pos1, pos2-pos1)
id, _ := strconv.Atoi(idStr)
return urlNode{"movie", id, url, t}
} else {
pos1 = str.IndexOf(url, HANDLE_LIST, 0)
if pos1 != -1 {
pos1 += len(HANDLE_LIST)
pos2 := str.IndexOf(url, HANDLE_HTML, 0)
idStr := str.Substr(url, pos1, pos2-pos1)
id, _ := strconv.Atoi(idStr)
return urlNode{"list", id, url, t}
} else {
return urlNode{"home", 1, url, t}
} // 如果页面url有很多种,就不断在这里扩展
}
}
func readFileLinebyLine(params cmdParams, logChannel chan string) error {
fd, err := os.Open(params.logFilePath)
if err != nil {
log.Warningf("ReadFileLinebyLine can't open file:%s", params.logFilePath)
return err
}
defer fd.Close()
count := 0
bufferRead := bufio.NewReader(fd)
for {
line, err := bufferRead.ReadString('\n')
logChannel <- line
count++
if count%(1000*params.routineNum) == 0 {
log.Infof("ReadFileLinebyLine line: %d", count)
}
if err != nil {
if err == io.EOF {
time.Sleep(3 * time.Second)
log.Infof("ReadFileLinebyLine wait, raedline:%d", count)
} else {
log.Warningf("ReadFileLinebyLine read log error")
}
}
}
return nil
}
func getTime(logTime, timeType string) string {
var item string
switch timeType {
case "day":
item = "2006-01-02"
break
case "hour":
item = "2006-01-02 15"
break
case "min":
item = "2006-01-02 15:04"
break
}
t, _ := time.Parse(item, time.Now().Format(item))
return strconv.FormatInt(t.Unix(), 10)
}