databoard-transform/main.go
2020-12-09 10:51:01 +08:00

80 lines
1.7 KiB
Go

package main
import (
"context"
"encoding/json"
"flag"
"log"
"time"
_ "github.com/go-sql-driver/mysql"
"git.nonolive.co/eson.hsm/databoard-collect/database"
"github.com/go-xorm/xorm"
"github.com/google/uuid"
"go.mongodb.org/mongo-driver/bson"
)
type CountLiveAnchors struct {
UID string `xorm:"uid"`
CreateAt time.Time `xorm:"create_at"`
IsCounted int `xorm:"is_counted"`
CountMap string `xorm:"count_map"`
}
func getArgsStartTime() time.Time {
flag.Parse()
date := flag.Arg(0)
if date == "" {
// log.Println(flag.)
panic("please input date eg. '2020-11-30 16:29:17'")
}
start, err := time.ParseInLocation("2006-01-02 15:04:03", date, time.Local)
if err != nil {
panic(err)
}
return start
}
func main() {
mdb := database.NewStatisticsDB("mongodb://sg-board1.livenono.com:27018")
engine, err := xorm.NewEngine("mysql", "root:Nono-databoard@tcp(127.0.0.1:3306)/databoard?parseTime=true&loc=Local&charset=utf8&collation=utf8_unicode_ci")
if err != nil {
panic(err)
}
cla := engine.Table("count_live_anchors")
start := getArgsStartTime()
// start, err := time.ParseInLocation("2006-01-02 15:04:03", "2020-11-30 16:29:17", time.Local)
if err != nil {
panic(err)
}
cur, err := mdb.C.CountLiveAnchors.Find(context.TODO(), bson.M{"create_at": bson.M{"$gte": start}})
if err != nil {
panic(err)
}
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 = cla.Insert(c)
if err != nil {
log.Println(err)
}
}
}