add charts

This commit is contained in:
eson
2020-08-20 19:29:22 +08:00
parent 17362cd7fe
commit c632837767
14 changed files with 517 additions and 194 deletions

65
goserver/count_tag.go Normal file
View File

@@ -0,0 +1,65 @@
package main
import (
"encoding/json"
"fmt"
"log"
"time"
"github.com/gin-gonic/gin"
)
var tagCounter = make(map[string]*tagcounter)
type tagcounter struct {
Name string
LastTime time.Time
CountWord map[string]int
}
func init() {
}
func CountTag(cxt *gin.Context) {
platform := cxt.Query("platform")
var cw *tagcounter
if cw, ok := tagCounter[platform]; ok {
if time.Now().Sub(cw.LastTime).Minutes() <= 10 {
cxt.JSON(200, cw.CountWord)
return
}
}
sql := fmt.Sprintf(SqlTag, platform)
rows, err := StoreStreamer.Query(sql)
if err != nil {
cxt.Error(err)
return
}
cw = &tagcounter{}
cw.CountWord = make(map[string]int)
cw.Name = platform
cw.LastTime = time.Now()
tagCounter[platform] = cw
for rows.Next() {
var stag string
err = rows.Scan(&stag)
if err != nil {
log.Println(err)
}
var tag []string
json.Unmarshal([]byte(stag), &tag)
for _, t := range tag {
if _, ok := cw.CountWord[t]; ok {
cw.CountWord[t]++
} else {
cw.CountWord[t] = 1
}
}
}
cxt.JSON(200, cw.CountWord)
cw.LastTime = time.Now()
}

View File

@@ -4,7 +4,6 @@ import (
"database/sql"
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"strings"
@@ -15,33 +14,6 @@ import (
)
var StoreStreamer *sql.DB
var SqlQuery string = `SELECT
ie.uid,
ie.platform,
ie.user_id,
ie.user_name ,
ie.live_url ,
ie.tags ,
cl.followers ,
cl.views ,
cl.gratuity ,
cl.live_title,
cl.live_start_time ,
cl.live_end_time ,
cl.update_time
From
(
SELECT
*
FROM
intimate_extractor.streamer
WHERE
platform = "twitcasting"
AND operator = 0
AND latest_log_uid is not NULL limit %s,%s) ie
JOIN intimate_extractor.collect_log cl
WHERE
ie.latest_log_uid = cl.log_uid; `
func init() {
db, err := sql.Open("mysql", InitConfig.Database.ExtractorURI)
@@ -51,121 +23,12 @@ func init() {
StoreStreamer = db
}
// ie.uid,
// ie.platform,
// ie.user_id,
// ie.user_name ,
// ie.live_url ,
// ie.tags ,
// cl.followers ,
// cl.views ,
// cl.gratuity ,
// cl.live_title,
// cl.live_start_time ,
// cl.live_end_time ,
// cl.update_time
type ObjectTwistcasting struct {
Uid int64
Platform string
UserId string
UserName string
LiveUrl string
Tags []string
Followers int64
Views int64
Gratuity int64
LiveTitle string
LiveStartTime *time.Time
LiveEndTime *time.Time
UpdateTime *time.Time
}
type Result struct {
Code int
Error string
Data interface{}
}
func TwitcastingQuery(cxt *gin.Context) {
var err error
page, err := strconv.Atoi(cxt.Query("page"))
if err != nil {
cxt.Error(err)
}
psize, err := strconv.Atoi(cxt.Query("psize"))
if err != nil {
cxt.Error(err)
}
if psize > 100 {
cxt.Error(fmt.Errorf("page size <= 100"))
}
start := (page - 1) * 200
// end := start + 200
ssql := fmt.Sprintf(SqlQuery, strconv.Itoa(start), strconv.Itoa(psize))
rows, err := StoreStreamer.Query(ssql)
if err != nil {
cxt.Error(err)
}
var ots []*ObjectTwistcasting
for rows.Next() {
ot := &ObjectTwistcasting{}
var lstm, letm, utm sql.NullTime
var tags string
err = rows.Scan(
&ot.Uid,
&ot.Platform,
&ot.UserId,
&ot.UserName,
&ot.LiveUrl,
&tags,
&ot.Followers,
&ot.Views,
&ot.Gratuity,
&ot.LiveTitle,
&lstm,
&letm,
&utm,
)
if err != nil {
cxt.Error(err)
return
}
if !lstm.Valid {
ot.LiveStartTime = nil
} else {
ot.LiveStartTime = &lstm.Time
}
if !letm.Valid {
ot.LiveEndTime = nil
} else {
ot.LiveEndTime = &letm.Time
}
if !utm.Valid {
ot.UpdateTime = nil
} else {
ot.UpdateTime = &utm.Time
}
if err = json.Unmarshal([]byte(tags), &ot.Tags); err != nil {
log.Println(tags)
}
ots = append(ots, ot)
}
r := &Result{Code: 200}
r.Data = ots
log.Println(len(ots))
if retdata, err := json.Marshal(r); err != nil {
cxt.Error(err)
} else {
cxt.JSON(r.Code, string(retdata))
}
}
func Cors() gin.HandlerFunc {
return func(c *gin.Context) {
method := c.Request.Method //请求方法
@@ -202,10 +65,125 @@ func Cors() gin.HandlerFunc {
}
}
type ObjectQuery struct {
Uid int64
Platform string
UserId string
UserName string
LiveUrl string
Tags []string
Followers int64
Views int64
Gratuity int64
LiveTitle string
LiveStartTime *time.Time
LiveEndTime *time.Time
UpdateTime *time.Time
}
func Query(cxt *gin.Context, platform string) {
var err error
page, err := strconv.Atoi(cxt.Query("page"))
if err != nil {
cxt.Error(err)
return
}
psize, err := strconv.Atoi(cxt.Query("psize"))
if err != nil {
cxt.Error(err)
return
}
if psize > 100 {
cxt.Error(fmt.Errorf("page size <= 100"))
return
}
start := (page - 1) * psize
// end := start + 200
ssql := fmt.Sprintf(SqlQuery, platform, strconv.Itoa(start), strconv.Itoa(psize))
rows, err := StoreStreamer.Query(ssql)
if err != nil {
cxt.Error(err)
return
}
var ots []*ObjectQuery
for rows.Next() {
ot := &ObjectQuery{}
var view, gratuity sql.NullInt64
var lstm, letm, utm sql.NullTime
var tags, livetitle sql.NullString
err = rows.Scan(
&ot.Uid,
&ot.Platform,
&ot.UserId,
&ot.UserName,
&ot.LiveUrl,
&tags,
&ot.Followers,
&view,
&gratuity,
&livetitle,
&lstm,
&letm,
&utm,
)
if err != nil {
cxt.Error(err)
return
}
if !lstm.Valid {
ot.LiveStartTime = nil
} else {
ot.LiveStartTime = &lstm.Time
}
if !letm.Valid {
ot.LiveEndTime = nil
} else {
ot.LiveEndTime = &letm.Time
}
if !utm.Valid {
ot.UpdateTime = nil
} else {
ot.UpdateTime = &utm.Time
}
if livetitle.Valid {
ot.LiveTitle = livetitle.String
}
if view.Valid {
ot.Views = view.Int64
}
if gratuity.Valid {
ot.Gratuity = gratuity.Int64
}
if err = json.Unmarshal([]byte(tags.String), &ot.Tags); err != nil {
// log.Println(tags)
}
ots = append(ots, ot)
}
r := &Result{Code: 200}
r.Data = ots
// log.Println(len(ots))
if retdata, err := json.Marshal(r); err != nil {
cxt.Error(err)
} else {
cxt.JSON(r.Code, string(retdata))
}
}
func main() {
engine := gin.New() //r := gin.Default() //使用默认中间件
engine.Use(gin.Logger())
engine.Use(Cors())
engine.GET("twitcasting/query", TwitcastingQuery)
engine.GET("openrec/query", OpenrecQuery)
engine.GET("twitch/query", TwitchQuery)
engine.GET("tag/count", CountTag)
engine.Run(":5500")
}

View File

@@ -1,7 +1,13 @@
package main
import "testing"
import (
"testing"
)
func TestMain(t *testing.T) {
main()
}
func TestCountTag(t *testing.T) {
}

7
goserver/openrec.go Normal file
View File

@@ -0,0 +1,7 @@
package main
import "github.com/gin-gonic/gin"
func OpenrecQuery(cxt *gin.Context) {
Query(cxt, "openrec")
}

9
goserver/twitcasting.go Normal file
View File

@@ -0,0 +1,9 @@
package main
import (
"github.com/gin-gonic/gin"
)
func TwitcastingQuery(cxt *gin.Context) {
Query(cxt, "twitcasting")
}

8
goserver/twitch.go Normal file
View File

@@ -0,0 +1,8 @@
package main
import "github.com/gin-gonic/gin"
// TwitchQuery
func TwitchQuery(cxt *gin.Context) {
Query(cxt, "twitch")
}

34
goserver/var_sql.go Normal file
View File

@@ -0,0 +1,34 @@
package main
// SqlTag 获取 tag
var SqlTag string = `SELECT tags FROM intimate_extractor.streamer
WHERE platform = "%s" AND tags IS NOT NULL`
//SqlQuery 获取 网站数据
var SqlQuery string = `SELECT
ie.uid,
ie.platform,
ie.user_id,
ie.user_name ,
ie.live_url ,
ie.tags ,
cl.followers ,
cl.views ,
cl.gratuity ,
cl.live_title,
cl.live_start_time ,
cl.live_end_time ,
cl.update_time
From
(
SELECT
*
FROM
intimate_extractor.streamer
WHERE
platform = "%s"
AND operator = 0
AND latest_log_uid is not NULL limit %s,%s) ie
JOIN intimate_extractor.collect_log cl
WHERE
ie.latest_log_uid = cl.log_uid; `