databoard-transform/collect.go

84 lines
1.8 KiB
Go
Raw Normal View History

package main
import (
"context"
"encoding/json"
"log"
"time"
"git.nonolive.co/eson.hsm/databoard-collect/database"
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
var ok bool
2020-12-10 11:00:35 +00:00
for !ps.IsClose() {
liveanchor := &CountLiveAnchors{}
2020-12-09 10:18:09 +00:00
ok, err = db.T.CountLiveAnchors.OrderBy("create_at desc").Limit(1).Get(liveanchor)
2020-12-09 10:11:52 +00:00
if ok || (ok == false && err == nil) {
var cur *mongo.Cursor
2020-12-09 10:18:09 +00:00
var last time.Time
2020-12-09 10:12:39 +00:00
if ok == false {
2020-12-09 10:18:09 +00:00
last, err = time.ParseInLocation("2006-01-02", "2020-05-30", time.Local)
if err != nil {
panic(err)
}
cur, err = mdb.C.CountLiveAnchors.Find(context.TODO(), bson.M{"create_at": bson.M{"$gt": last}})
2020-12-09 10:11:52 +00:00
} else {
2020-12-09 10:18:09 +00:00
last = liveanchor.CreateAt
2020-12-10 09:50:42 +00:00
log.Println("last: ", last, liveanchor.UID)
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-09 10:18:09 +00:00
if err != nil {
log.Println(err)
ps.Wait(time.Second * 5)
continue
}
2020-12-10 09:50:42 +00:00
var isNoData bool
2020-12-09 11:10:51 +00:00
for cur.Next(context.TODO()) && !ps.IsClose() {
2020-12-10 09:50:42 +00:00
2020-12-09 11:00:49 +00:00
la := &database.LiveAnchorsCountPointObjectID{}
err = cur.Decode(la)
if err != nil {
panic(err)
}
2020-12-10 11:00:35 +00:00
c := &CountLiveAnchors{}
2020-12-09 11:14:16 +00:00
c.UID = la.ObjectID.Hex()
c.IsCounted = 0
c.CreateAt = la.CreateAt
data, err := json.Marshal(la.LiveAnchorDict)
if err != nil {
panic(err)
}
c.CountMap = string(data)
_, err = db.T.CountLiveAnchors.Insert(c)
if err != nil {
2020-12-10 09:50:42 +00:00
switch err.(*mysql.MySQLError).Number {
case 1062: // duplicate
isNoData = true
default:
log.Println(err.(*mysql.MySQLError).Number, err)
}
}
}
2020-12-10 09:50:42 +00:00
if isNoData {
ps.Wait(time.Second * 2)
}
}
}
}