change some

This commit is contained in:
2020-08-24 00:23:43 +08:00
parent 7d54a42da9
commit 3a987464e6
11 changed files with 344 additions and 233 deletions

View File

@@ -4,13 +4,14 @@ import (
"encoding/json"
"fmt"
"log"
"sync"
"time"
"github.com/474420502/focus/tree/heap"
"github.com/gin-gonic/gin"
)
var tagCounter = make(map[string]*tagcounter)
var tagCounter = &sync.Map{} // make(map[string]*tagcounter)
type taginfo struct {
Name string
@@ -28,12 +29,26 @@ func init() {
}
func AllTag(cxt *gin.Context) {
countTagInfo(cxt, func(cxt *gin.Context, cw *tagcounter) {
cxt.JSON(200, cw.CountWord)
})
}
func CountTag(cxt *gin.Context) {
countTagInfo(cxt, func(cxt *gin.Context, cw *tagcounter) {
cxt.JSON(200, cw.PQueue)
})
}
func countTagInfo(cxt *gin.Context, ret func(cxt *gin.Context, cw *tagcounter)) {
platform := cxt.Query("platform")
var cw *tagcounter
if cw, ok := tagCounter[platform]; ok {
if icw, ok := tagCounter.Load(platform); ok {
cw = icw.(*tagcounter)
if time.Now().Sub(cw.LastTime).Minutes() <= 10 {
cxt.JSON(200, cw.PQueue)
ret(cxt, cw)
return
}
}
@@ -49,7 +64,6 @@ func CountTag(cxt *gin.Context) {
cw.CountWord = make(map[string]*taginfo)
cw.Name = platform
cw.LastTime = time.Now()
tagCounter[platform] = cw
for rows.Next() {
var stag string
@@ -87,6 +101,11 @@ func CountTag(cxt *gin.Context) {
i++
}
if heap.Size() == 0 {
cxt.JSON(200, nil)
return
}
// heap.Put(other)
cw.PQueue = heap.Values()
other.Value = cw.PQueue[len(cw.PQueue)-1].(*taginfo).Value - 1
@@ -95,6 +114,8 @@ func CountTag(cxt *gin.Context) {
}
cw.PQueue = append(cw.PQueue, other)
cxt.JSON(200, cw.PQueue)
// cxt.JSON(200, cw.PQueue)
ret(cxt, cw)
cw.LastTime = time.Now()
tagCounter.Store(platform, cw)
}

View File

@@ -4,6 +4,7 @@ go 1.15
require (
github.com/474420502/focus v0.12.0
github.com/Pallinder/go-randomdata v1.1.0
github.com/gin-gonic/gin v1.6.3
github.com/go-sql-driver/mysql v1.5.0
github.com/jinzhu/gorm v1.9.16 // indirect

View File

@@ -1,5 +1,6 @@
github.com/474420502/focus v0.12.0 h1:+icbmj7IEOefvTegHt5EpcHt6WFbe2miIrceUJx2Evo=
github.com/474420502/focus v0.12.0/go.mod h1:d0PMjtMxFz1a9HIhwyFPkWa+JF+0LgOrEUfd8iZka6s=
github.com/Pallinder/go-randomdata v1.1.0 h1:gUubB1IEUliFmzjqjhf+bgkg1o6uoFIkRsP3VrhEcx8=
github.com/Pallinder/go-randomdata v1.1.0/go.mod h1:yHmJgulpD2Nfrm0cR9tI/+oAgRqCQQixsA8HyRZfV9Y=
github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=

View File

@@ -52,7 +52,7 @@ func Cors() gin.HandlerFunc {
// 允许跨域设置 可以返回其他子段
c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers,Cache-Control,Content-Language,Content-Type,Expires,Last-Modified,Pragma,FooBar") // 跨域关键设置 让浏览器可以解析
c.Header("Access-Control-Max-Age", "172800") // 缓存请求信息 单位为秒
c.Header("Access-Control-Allow-Credentials", "false") // 跨域请求是否需要带cookie信息 默认设置为true
c.Header("Access-Control-Allow-Credentials", "false") // 跨域请求是否需要带cookie信息 默认设置为true
c.Set("content-type", "application/json") // 设置返回格式是json
}
@@ -185,5 +185,6 @@ func main() {
engine.GET("openrec/query", OpenrecQuery)
engine.GET("twitch/query", TwitchQuery)
engine.GET("tag/count", CountTag)
engine.GET("tag/allcount", CountTag)
engine.Run(":5500")
}

View File

@@ -1,13 +1,44 @@
package main
import (
"encoding/json"
"log"
"regexp"
"testing"
"github.com/Pallinder/go-randomdata"
)
func TestMain(t *testing.T) {
main()
}
func TestCountTag(t *testing.T) {
func estCountTag(t *testing.T) {
var a = `男人 女人 儿童 老人 母亲 父亲 爷爷 奶奶 老师 美女 帅哥 性格 善良 性格 品质 聪明 女儿 儿子 军人 坏蛋 心情 笑 哭 高兴
害怕 愤怒 激动 紧张 忧虑 着急 满足 眼睛 鼻子 嘴巴 头发 耳朵 牙齿 美 丑 眉毛 脸 手 脚 腰 腿
胖 瘦 矮 高 清明节 劳动节 端午节 七夕节 中秋节 重阳节 元宵节 服饰 鼠 牛 虎 兔 龙 蛇 马 羊
猴 狗 猪 鸡 颜色 聚会 时间短 时间 爱情 脚步声 水声 花 教堂 速度快 速度慢 桃花 幼苗 紫色 白色
黑色 红色 女性身材 男性身材 沙沙尘尘 猪甘蠢 猪 无脑 大唔透 玩世不恭 眼大无神 大细超 白鸽眼 咸湿 浪漫 靓仔 靓
女 猛男 咸猪手 西施 好色 色狼 色魔 畜牲 食碗面反碗底 抵死 恶死 曾眉凸眼 眉耒眼去 温柔体贴假细心 放荡 淫贱 荒唐 离谱 头尖额窄无厘贵格 肚满肠肥 两面三刀 反革命 红卫兵
`
tags := regexp.MustCompile("[^ \n\t]+").FindAllString(a, -1)
for {
var stags []string
for i := 0; i < randomdata.Number(1, 5); i++ {
tag := randomdata.StringSample(tags...)
stags = append(stags, tag)
}
data, _ := json.Marshal(stags)
sql := "update streamer set tags = ?, operator = 20 where operator != 20 limit 1"
_, err := StoreStreamer.Exec(sql, data)
if err != nil {
log.Println(err)
break
}
}
}