From 529aa6a6ff0a0d4d59c3c27abfa9e2185df08f1c Mon Sep 17 00:00:00 2001 From: eson Date: Wed, 9 Dec 2020 17:43:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=80=E5=90=8E=E6=89=8B=E5=8A=A8=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- database.go | 40 ++++++++++++++++++++++++++++++++++++++++ main.go | 18 ++++-------------- 2 files changed, 44 insertions(+), 14 deletions(-) create mode 100644 database.go diff --git a/database.go b/database.go new file mode 100644 index 0000000..9c8afba --- /dev/null +++ b/database.go @@ -0,0 +1,40 @@ +package main + +import ( + "time" + + "github.com/go-xorm/xorm" +) + +// Database mysql数据结构 +type Database struct { + engine *xorm.Engine + T struct { + CountLiveAnchors *xorm.Session + } +} + +// 默认全局的数据库对象 +var db *Database + +func databaseInit() { + db := &Database{} + + // 初始化数据myrocks驱动链接 + 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) + } + db.engine = engine + + // 数据库表对象 + db.T.CountLiveAnchors = engine.Table("count_live_anchors") +} + +// CountLiveAnchors count_live_anchors +type CountLiveAnchors struct { + UID string `xorm:"uid"` + CreateAt time.Time `xorm:"create_at"` + IsCounted int `xorm:"is_counted"` + CountMap string `xorm:"count_map"` +} diff --git a/main.go b/main.go index bf85e01..9f0134f 100644 --- a/main.go +++ b/main.go @@ -10,16 +10,12 @@ import ( _ "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 init() { + databaseInit() } func getArgsStartTime() time.Time { @@ -41,13 +37,6 @@ func getArgsStartTime() time.Time { 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() log.Println("start from ", start) // start, err := time.ParseInLocation("2006-01-02 15:04:03", "2020-11-30 16:29:17", time.Local) @@ -65,6 +54,7 @@ func main() { panic(err) } c := &CountLiveAnchors{} + c.UID = uuid.New().String() c.IsCounted = 0 c.CreateAt = la.CreateAt @@ -74,7 +64,7 @@ func main() { panic(err) } c.CountMap = string(data) - _, err = cla.Insert(c) + _, err = db.T.CountLiveAnchors.Insert(c) if err != nil { log.Println(err) }