finish charts
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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=
|
||||
|
||||
Reference in New Issue
Block a user