databoard-transform/collect.go

59 lines
1.2 KiB
Go

package main
import (
"context"
"encoding/json"
"log"
"time"
"git.nonolive.co/eson.hsm/databoard-collect/database"
"github.com/google/uuid"
"go.mongodb.org/mongo-driver/bson"
)
// collectCopyCountLiveAnchors 从mongodb里复制需要增量的值
func collectCopyCountLiveAnchors(cxt *WorkerContext) {
defer cxt.Done()
for !ps.IsClose() {
liveanchor := &CountLiveAnchors{}
if ok, err := db.T.CountLiveAnchors.OrderBy("create_at desc").Limit(1).Get(liveanchor); ok {
last := liveanchor.CreateAt
log.Println("last: ", last)
cur, err := mdb.C.CountLiveAnchors.Find(context.TODO(), bson.M{"create_at": bson.M{"$gt": last}})
if err != nil {
log.Println(err)
ps.Wait(time.Second * 5)
continue
}
for cur.Next(context.TODO()) {
la := &database.LiveAnchorsCountPoint{}
err = cur.Decode(la)
if err != nil {
panic(err)
}
c := &CountLiveAnchors{}
c.UID = uuid.New().String()
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 {
log.Println(err)
}
}
} else {
log.Println(err)
ps.Wait(time.Second * 5)
}
}
}