-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
382 lines (354 loc) · 10.1 KB
/
main.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package main
import (
"flag"
"fmt"
"math"
"strings"
"sync"
"time"
"github.com/admpub/confl"
"github.com/admpub/log"
"github.com/webx-top/db"
"github.com/webx-top/db/lib/factory"
"github.com/webx-top/db/mongo"
"github.com/webx-top/db/mysql"
"gopkg.in/mgo.v2/bson"
)
type CateItem struct {
ID string `bson:"id" db:"id"`
Name string `bson:"name" db:"name"`
}
type CateModel struct {
First *CateItem `bson:"first" db:"first"`
Second *CateItem `bson:"second" db:"second"`
Third *CateItem `bson:"third" db:"third"`
}
type ContentModel struct {
ID interface{} `bson:"id" db:"id"`
Title string `bson:"title" db:"title"`
Cate *CateModel `bson:"cate" db:"cate"`
}
type EventModel struct {
ID bson.ObjectId `bson:"_id" db:"_id"`
Event string `bson:"event" db:"event"`
Timestamp uint `bson:"timestamp" db:"timestamp"`
Content *ContentModel `bson:"content" db:"content"`
Udid string `bson:"udid" db:"udid"`
Platform string `bson:"platform" db:"platform"`
OS string `bson:"os" db:"os"`
OsType string `bson:"osType" db:"osType"`
Version string `bson:"version" db:"version"`
BundleId string `bson:"bundleId" db:"bundleId"`
IP string `bson:"ip" db:"ip"`
Account struct {
ID string `bson:"accountId" db:"accountId"`
} `bson:"account" db:"account"`
}
var config = struct {
ConfigFile *string
Operation *string
}{}
func main() {
config.ConfigFile = flag.String(`c`, `dbconfig.yml`, `database setting`)
config.Operation = flag.String(`t`, `insertBrandId`, `operation type: removeDuplicates / updateEvent / updateOsType)`)
flag.Parse()
log.Sync()
log.DefaultLog.AddSpace = true
log.SetFatalAction(log.ActionExit)
dbConfig := struct {
Mongo mongo.ConnectionURL
MySQL mysql.ConnectionURL
}{}
_, err := confl.DecodeFile(*config.ConfigFile, &dbConfig)
if err != nil {
log.Fatal(err)
}
//factory.SetDebug(true) //调试时可以打开Debug模式来查看sql语句
mongo.ConnTimeout = time.Second * 30
dbMongo, err := mongo.Open(dbConfig.Mongo)
if err != nil {
log.Fatal(err)
}
dbMySQL, err := mysql.Open(dbConfig.MySQL)
if err != nil {
log.Fatal(err)
}
cluster := factory.NewCluster().AddW(dbMongo)
factory.AddCluster(cluster) //第一次添加。索引编号为0
clusterMySQL := factory.NewCluster().AddW(dbMySQL)
factory.AddCluster(clusterMySQL) //第二次添加。索引编号为1,以此类推。
defer factory.CloseAll()
detail := map[string]string{}
detail["appid"] = "11244bf15870d8567b41d99b908544ed"
wg := &sync.WaitGroup{}
if _, ok := detail["appid"]; ok {
wg.Add(1)
checkAppID(detail, wg)
} else {
//使用Link(1)来选择索引编号为1的数据库连接(默认使用编号为0的连接)
result := factory.NewParam().Setter().Link(1).C(`libuser_detail`).Result()
total, err := factory.NewParam().Setter().Link(1).C(`libuser_detail`).Count()
if err != nil {
log.Fatal(err)
}
wg.Add(int(total))
for result.Next(&detail) {
switch *config.Operation { //修改event值infoXXX为downloadXXX
case `updateEvent`:
go checkEvent(detail, wg)
default:
go checkAppID(detail, wg)
}
}
result.Close()
}
wg.Wait()
log.Info(`任务完成`)
}
type Executor struct {
Cond db.Cond
Func func(EventModel, map[string]string) error
}
var executors = map[string]*Executor{
"updateOsType": &Executor{ //更新osType
Cond: db.Cond{
"udid": "00old00analysis00",
"osType": "windows",
},
Func: updateOsType,
},
"removeDuplicates": &Executor{ //删除重复数据
Cond: db.Cond{"udid": "00old00analysis00"},
Func: removeDuplicates,
},
"insertBrandId": &Executor{
Cond: db.Cond{
"timestamp >=": strToTime(`2016-11-01 00:00:00`).Unix(),
"content.bid $exists": false,
"content.cate $exists": false,
"event IN": []string{
"downloadMag",
"infoMag",
},
},
Func: insertBrandId,
},
}
func strToTime(value string) time.Time {
t, _ := time.Parse(`2006-01-02 15:04:05`, value)
return t
}
func checkAppID(detail map[string]string, wg *sync.WaitGroup) {
defer wg.Done()
if len(detail["appid"]) == 0 {
return
}
log.Info(`AppID`, detail["appid"])
mdt := new([]EventModel)
cond := db.Cond{}
executor, ok := executors[*config.Operation]
if !ok {
return
}
for k, v := range executor.Cond {
cond[k] = v
}
size := 1000
page := 1
log.Info(`开始查询第1页`)
//这里没有使用Link()函数,默认选择索引编号为0的数据库连接
cnt, err := factory.NewParam().Setter().C(`event` + detail["appid"]).Args(cond).Page(page).Size(size).Recv(mdt).List()
if err != nil {
if err == db.ErrNoMoreRows || factory.IsTimeoutError(err) {
log.Error(err)
return
}
log.Fatal(err)
}
tot := cnt()
pages := int(math.Ceil(float64(tot) / float64(size)))
for ; page <= pages; page++ {
if page > 1 {
log.Infof(`开始查询第%d页,共%d页`, page, pages)
_, err = factory.NewParam().Setter().C(`event` + detail["appid"]).Args(cond).Page(page).Size(size).Recv(mdt).List()
if err != nil {
if err == db.ErrNoMoreRows || factory.IsTimeoutError(err) {
log.Error(err)
break
}
log.Fatal(err)
}
}
for _, row := range *mdt {
err := executor.Func(row, detail)
if err != nil {
if err == db.ErrNoMoreRows || factory.IsTimeoutError(err) {
log.Error(err)
break
}
log.Fatal(err)
}
}
}
}
//删除重复数据
func removeDuplicates(row EventModel, detail map[string]string) error {
n, err := factory.NewParam().Setter().C(`event` + detail["appid"]).Args(db.Cond{
"_id <>": row.ID,
"timestamp": row.Timestamp,
"account.accountId": row.Account.ID,
}).Count()
if err == nil && n > 0 {
log.Infof(`Found %d duplicate(s) => %s`, n, row.ID)
err = factory.NewParam().Setter().C(`event` + detail["appid"]).Args(db.Cond{"_id": row.ID}).Delete()
if err == nil {
log.Info(`Remove success.`)
}
}
return err
}
//更新osType
func updateOsType(row EventModel, detail map[string]string) error {
var osType, bundleId string
switch row.Platform {
case `pc`, `pc_down`:
osType = `Windows`
bundleId = `com.dooland.pc`
case `ipad`:
osType = `iOS`
bundleId = `com.dooland.padforiosfromweb.reader`
case `iphone`:
osType = `iOS`
bundleId = `com.dooland.mobileforiosfromweb.reader`
case `android`:
osType = `Android`
bundleId = `com.dooland.padforandroidfromweb.reader`
case `androidmobile`:
osType = `Android`
bundleId = `com.dooland.mobileforandroidfromweb.reader`
case `waparticle`:
osType = `Wap`
bundleId = `com.dooland.wapforweb.reader`
case `article`:
osType = `Windows`
bundleId = `com.dooland.pc`
case `dudubao`:
osType = `Dudubao`
bundleId = `com.dooland.dudubao`
case `dudubao_down`:
osType = `Dudubao`
bundleId = `com.dooland.dudubao`
default:
return nil
}
log.Infof(`Update [%s] %s => %s, %s => %s`, row.ID, row.OsType, osType, row.BundleId, bundleId)
err := factory.NewParam().Setter().C(`event` + detail["appid"]).Args(db.Cond{"_id": row.ID}).Send(map[string]string{
"osType": osType,
"bundleId": bundleId,
}).Update()
return err
}
//修改infoXXX为downloadXXX
func updateEvent(row EventModel, detail map[string]string) error {
if strings.HasPrefix(row.Event, `info`) == false {
return nil
}
event := `download` + strings.TrimPrefix(row.Event, `info`)
log.Infof(`Update [%s] %s => %s, %s => %s`, row.ID, row.Event, event)
err := factory.NewParam().Setter().C(`event` + detail["appid"]).Args(db.Cond{"_id": row.ID}).Send(map[string]string{
"event": event,
}).Update()
return err
}
//修改infoXXX为downloadXXX
func checkEvent(detail map[string]string, wg *sync.WaitGroup) {
defer wg.Done()
if len(detail["appid"]) == 0 {
return
}
log.Info(`AppID`, detail["appid"])
size := 1000
page := 1
r := []map[string]string{}
cnt, err := factory.NewParam().Setter().Link(1).C(`user_down_mag`).Args(db.Cond{"lib_id": detail["id"]}).Recv(&r).Page(page).Size(size).List()
if err != nil {
if err == db.ErrNoMoreRows || factory.IsTimeoutError(err) {
log.Error(err)
return
}
log.Fatal(err)
}
tot := cnt()
pages := int(math.Ceil(float64(tot) / float64(size)))
for ; page <= pages; page++ {
if page > 1 {
_, err = factory.NewParam().Setter().Link(1).C(`user_down_mag`).Args(db.Cond{"lib_id": detail["id"]}).Recv(&r).Page(page).Size(size).List()
if err != nil {
if err == db.ErrNoMoreRows || factory.IsTimeoutError(err) {
log.Error(err)
break
}
log.Fatal(err)
}
}
for _, row := range r {
t, err := time.Parse(`2006-01-02 15:04:05`, row["add_time"])
if err != nil {
log.Error(err)
continue
}
mdt := new(EventModel)
cond := db.Cond{
"udid": "00old00analysis00",
"event IN": []string{"infoMag", "infoBook"},
"account.accountId": row["user_id"],
"timestamp": t.Unix(),
}
err = factory.NewParam().Setter().C(`event` + detail["appid"]).Args(cond).Page(page).Size(size).Recv(mdt).One()
if err != nil {
if err == db.ErrNoMoreRows || factory.IsTimeoutError(err) {
log.Error(err)
continue
}
log.Fatal(err)
}
err = updateEvent(*mdt, detail)
if err != nil {
if err == db.ErrNoMoreRows || factory.IsTimeoutError(err) {
log.Error(err)
continue
}
log.Fatal(err)
}
}
}
}
//插入品牌ID
func insertBrandId(row EventModel, detail map[string]string) error {
if row.Content.ID == nil {
log.Warn(`content.id is empty: `, row.ID)
return nil
}
id := fmt.Sprint(row.Content.ID)
var brandId string
recv := map[string]string{}
err := factory.NewParam().Setter().Link(1).C(`dudubao.mag_list`).Args(db.Cond{"id": id}).Recv(&recv).One()
if err != nil {
if err == db.ErrNoMoreRows {
err = factory.NewParam().Setter().Link(1).C(`dudubao_bak.mag_list_bak`).Args(db.Cond{"id": id}).Recv(&recv).One()
}
}
if err != nil {
return err
}
log.Infof(`Update [%s] %s => %s`, row.ID, id, recv["sort_id"])
brandId = recv["sort_id"]
if len(brandId) == 0 || brandId == `0` {
log.Warn(` -> Skiped.`)
return nil
}
err = factory.NewParam().Setter().C(`event` + detail["appid"]).Args(db.Cond{"_id": row.ID}).Send(map[string]string{
"content.bid": brandId,
}).Update()
return err
}