finish charts

This commit is contained in:
eson
2020-08-21 19:36:05 +08:00
parent c632837767
commit 7d54a42da9
8 changed files with 249 additions and 175 deletions

View File

@@ -6,15 +6,22 @@ import (
"log"
"time"
"github.com/474420502/focus/tree/heap"
"github.com/gin-gonic/gin"
)
var tagCounter = make(map[string]*tagcounter)
type taginfo struct {
Name string
Value int
}
type tagcounter struct {
Name string
LastTime time.Time
CountWord map[string]int
CountWord map[string]*taginfo
PQueue []interface{}
}
func init() {
@@ -26,7 +33,7 @@ func CountTag(cxt *gin.Context) {
var cw *tagcounter
if cw, ok := tagCounter[platform]; ok {
if time.Now().Sub(cw.LastTime).Minutes() <= 10 {
cxt.JSON(200, cw.CountWord)
cxt.JSON(200, cw.PQueue)
return
}
}
@@ -39,7 +46,7 @@ func CountTag(cxt *gin.Context) {
}
cw = &tagcounter{}
cw.CountWord = make(map[string]int)
cw.CountWord = make(map[string]*taginfo)
cw.Name = platform
cw.LastTime = time.Now()
tagCounter[platform] = cw
@@ -54,12 +61,40 @@ func CountTag(cxt *gin.Context) {
json.Unmarshal([]byte(stag), &tag)
for _, t := range tag {
if _, ok := cw.CountWord[t]; ok {
cw.CountWord[t]++
cw.CountWord[t].Value++
} else {
cw.CountWord[t] = 1
cw.CountWord[t] = &taginfo{Name: t, Value: 1}
}
}
}
cxt.JSON(200, cw.CountWord)
heap := heap.New(func(a, b interface{}) int {
if a.(*taginfo).Value >= b.(*taginfo).Value {
return 1
}
return -1
})
var other = &taginfo{Name: "Other...", Value: 0}
var i = 0
for _, v := range cw.CountWord {
if i <= 99 {
heap.Put(v)
} else {
break
}
i++
}
// heap.Put(other)
cw.PQueue = heap.Values()
other.Value = cw.PQueue[len(cw.PQueue)-1].(*taginfo).Value - 1
if other.Value == 0 {
other.Value = 1
}
cw.PQueue = append(cw.PQueue, other)
cxt.JSON(200, cw.PQueue)
cw.LastTime = time.Now()
}

View File

@@ -3,6 +3,7 @@ module initmate_server
go 1.15
require (
github.com/474420502/focus v0.12.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,3 +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/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=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=