databoard-transform/collect.go

76 lines
1.6 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"
database "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-10 11:00:35 +00:00
for !ps.IsClose() {
db.Do(func(T *database.TableManager) {
2020-12-11 06:40:31 +00:00
liveanchor := &CountLiveAnchors{}
T.CountLiveAnchors.Order("create_at desc").Limit(1).Find(liveanchor)
if err = T.CountLiveAnchors.Error; err == nil {
2020-12-11 06:40:31 +00:00
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
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{}
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)
err = T.CountLiveAnchors.Create(c).Error
switch merr := err.(*mysql.MySQLError); merr.Number {
case 1062:
default:
log.Println(merr)
2020-12-10 09:50:42 +00:00
}
}
}
2020-12-11 06:40:31 +00:00
})
2020-12-10 09:50:42 +00:00
ps.Wait(time.Second * 5)
}
}