databoard-transform/collect.go

87 lines
1.8 KiB
Go
Raw Normal View History

package main
import (
"context"
"encoding/json"
"log"
"time"
2020-12-11 06:40:31 +00:00
mongodb "git.nonolive.co/eson.hsm/databoard-collect/database"
myrocks "git.nonolive.co/eson.hsm/databoard-database-myrocks"
2020-12-10 09:50:42 +00:00
"github.com/go-sql-driver/mysql"
"go.mongodb.org/mongo-driver/bson"
2020-12-09 10:11:52 +00:00
"go.mongodb.org/mongo-driver/mongo"
)
// collectCopyCountLiveAnchors 从mongodb里复制需要增量的值
func collectCopyCountLiveAnchors(cxt *WorkerContext) {
2020-12-09 10:18:09 +00:00
var err error
2020-12-11 06:40:31 +00:00
var lastuid string
2020-12-10 11:00:35 +00:00
for !ps.IsClose() {
2020-12-11 06:40:31 +00:00
var isNoData bool
db.Do(func(T *myrocks.TableManager) {
liveanchor := &CountLiveAnchors{}
2020-12-09 10:11:52 +00:00
2020-12-11 06:40:31 +00:00
if err = T.CountLiveAnchors.Order("create_at desc").Limit(1).Find(liveanchor).Error; err == nil {
var cur *mongo.Cursor
var last time.Time
2020-12-09 10:18:09 +00:00
last = liveanchor.CreateAt
2020-12-11 06:40:31 +00:00
if lastuid == liveanchor.UID {
isNoData = true
return
}
2020-12-10 09:50:42 +00:00
log.Println("last: ", last, liveanchor.UID)
2020-12-11 06:40:31 +00:00
2020-12-09 10:11:52 +00:00
cur, err = mdb.C.CountLiveAnchors.Find(context.TODO(), bson.M{"create_at": bson.M{"$gt": last}})
2020-12-11 06:40:31 +00:00
if err != nil {
log.Println(err)
ps.Wait(time.Second * 5)
return
}
2020-12-09 10:18:09 +00:00
2020-12-11 06:40:31 +00:00
for cur.Next(context.TODO()) && !ps.IsClose() {
2020-12-11 06:40:31 +00:00
la := &mongodb.LiveAnchorsCountPointObjectID{}
err = cur.Decode(la)
if err != nil {
panic(err)
}
uid := la.ObjectID.Hex()
2020-12-10 09:50:42 +00:00
2020-12-11 06:40:31 +00:00
c := &CountLiveAnchors{}
2020-12-10 11:00:35 +00:00
2020-12-11 06:40:31 +00:00
c.UID = uid
c.IsCounted = 0
c.CreateAt = la.CreateAt
2020-12-11 06:40:31 +00:00
data, err := json.Marshal(la.LiveAnchorDict)
if err != nil {
panic(err)
}
c.CountMap = string(data)
2020-12-11 06:40:31 +00:00
err = T.CountLiveAnchors.FirstOrCreate(c).Error
if err != nil {
switch err.(*mysql.MySQLError).Number {
case 1062: // duplicate
isNoData = true
default:
log.Println(err.(*mysql.MySQLError).Number, err)
}
} else {
lastuid = uid
2020-12-10 09:50:42 +00:00
}
}
}
2020-12-11 06:40:31 +00:00
})
2020-12-10 09:50:42 +00:00
2020-12-11 06:40:31 +00:00
if isNoData {
ps.Wait(time.Second * 2)
}
}
}