databoard-transform/main.go

82 lines
1.8 KiB
Go
Raw Normal View History

2020-11-30 07:43:16 +00:00
package main
import (
"context"
"encoding/json"
2020-12-09 02:47:19 +00:00
"flag"
2020-12-09 02:41:55 +00:00
"log"
2020-11-30 07:43:16 +00:00
"time"
2020-11-30 08:10:53 +00:00
_ "github.com/go-sql-driver/mysql"
2020-11-30 07:43:16 +00:00
"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"`
}
2020-12-09 02:47:19 +00:00
func getArgsStartTime() time.Time {
2020-12-09 02:51:01 +00:00
flag.Parse()
date := flag.Arg(0)
2020-12-09 02:49:20 +00:00
if date == "" {
2020-12-09 02:54:13 +00:00
log.Println("args", flag.Args())
2020-12-09 02:47:19 +00:00
panic("please input date eg. '2020-11-30 16:29:17'")
}
2020-12-09 02:54:13 +00:00
start, err := time.ParseInLocation("2006-01-02 15:04:05", date, time.Local)
2020-12-09 02:47:19 +00:00
if err != nil {
panic(err)
}
2020-12-09 02:54:13 +00:00
2020-12-09 02:47:19 +00:00
return start
}
2020-11-30 07:43:16 +00:00
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")
2020-12-09 02:47:19 +00:00
start := getArgsStartTime()
// start, err := time.ParseInLocation("2006-01-02 15:04:03", "2020-11-30 16:29:17", time.Local)
2020-12-09 02:41:55 +00:00
if err != nil {
panic(err)
}
cur, err := mdb.C.CountLiveAnchors.Find(context.TODO(), bson.M{"create_at": bson.M{"$gte": start}})
2020-11-30 07:43:16 +00:00
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 {
2020-12-09 02:41:55 +00:00
log.Println(err)
2020-11-30 07:43:16 +00:00
}
}
}