加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
monitor.go 3.69 KB
一键复制 编辑 原始数据 按行查看 历史
liutianpeng 提交于 2017-01-19 10:58 . ..
package main
import (
"encoding/json"
"net/http"
//"github.com/golang/protobuf/proto"
//"errors"
//"fmt"
"github.com/playnb/mustang/log"
"github.com/playnb/mustang/nosql"
"strconv"
)
type JsonOnlyCount struct {
Count uint32 `json:"Count"`
}
func GetOnlyCountByKey(key string) []byte {
count, err := nosql.Redis.Get(key).Int64()
if err != nil {
log.Error("Ket:%s %s", key, err.Error())
count = 0
}
//log.Trace("%s key-> %d", key, count)
co := &JsonOnlyCount{}
co.Count = uint32(count)
bstr, err := json.Marshal(co)
return (bstr)
}
func SCardOnlyCountByKey(key string) []byte {
count, err := nosql.Redis.SCard(key).Result()
if err != nil {
log.Error("Key:%s %s", key, err.Error())
log.Error(err.Error())
count = 0
}
co := &JsonOnlyCount{}
co.Count = uint32(count)
bstr, err := json.Marshal(co)
return (bstr)
}
func handleCurrentOnline(w http.ResponseWriter, r *http.Request) {
w.Write(GetOnlyCountByKey("ST:CURRENT_ONLINE"))
}
func handleTotalUsers(w http.ResponseWriter, r *http.Request) {
w.Write(SCardOnlyCountByKey("ST:TOTAL_USERS"))
}
func handleTotalView(w http.ResponseWriter, r *http.Request) {
w.Write(SCardOnlyCountByKey("ST:TOTAL_VIEW"))
}
func handlePlayGame(w http.ResponseWriter, r *http.Request) {
w.Write(GetOnlyCountByKey("ST:PLAY_GAME"))
}
func handleViewPage(w http.ResponseWriter, r *http.Request) {
w.Write(GetOnlyCountByKey("ST:VIEW_PAGE"))
}
func handleStartGame(w http.ResponseWriter, r *http.Request) {
w.Write(GetOnlyCountByKey("ST:START_GAME"))
}
func handleTaunterOther(w http.ResponseWriter, r *http.Request) {
w.Write(GetOnlyCountByKey("ST:TAUNTER_OTHER"))
}
func handleShareFirends(w http.ResponseWriter, r *http.Request) {
w.Write(GetOnlyCountByKey("ST:ShareFirends"))
}
func handleShareTimeline(w http.ResponseWriter, r *http.Request) {
w.Write(GetOnlyCountByKey("ST:ShareTimeline"))
}
func handleShareFirendUsers(w http.ResponseWriter, r *http.Request) {
w.Write(SCardOnlyCountByKey("ST:ShareFirendUsers"))
}
func handleShareTimelineUser(w http.ResponseWriter, r *http.Request) {
w.Write(SCardOnlyCountByKey("ST:ShareTimelineUser"))
}
func handleAvgCostTime(w http.ResponseWriter, r *http.Request) {
w.Write(GetOnlyCountByKey("ST:AVG:TIMECOST"))
}
func handleMaxTimeCost(w http.ResponseWriter, r *http.Request) {
w.Write(GetOnlyCountByKey("ST:MaxTimeCost"))
}
func handleBestScore(w http.ResponseWriter, r *http.Request) {
score := make(map[string]int64)
jsonStr := "{"
seperate := ""
for i := 1000; i > 0; i-- {
key := "ST:BEST_SCORE_" + strconv.FormatInt(int64(i), 10)
s := nosql.Redis.SCard(key).Val()
if s > 0 {
score[key] = s
jsonStr = jsonStr + seperate + `"` + key + `":` + strconv.Itoa(int(s))
seperate = `,`
}
}
jsonStr = jsonStr + "}"
/*
b, err := json.Marshal(score)
if err!=nil{
log.Error(err.Error())
}
log.Debug(jsonStr)
*/
w.Write([]byte(jsonStr))
}
func registMonitor(mux *http.ServeMux) {
mux.HandleFunc("/monitor/CurrentOnline", handleCurrentOnline)
mux.HandleFunc("/monitor/TotalUsers", handleTotalUsers)
mux.HandleFunc("/monitor/TotalView", handleTotalView)
mux.HandleFunc("/monitor/PlayGame", handlePlayGame)
mux.HandleFunc("/monitor/ViewPage", handleViewPage)
mux.HandleFunc("/monitor/StartGame", handleStartGame)
mux.HandleFunc("/monitor/BestScore", handleBestScore)
mux.HandleFunc("/monitor/TaunterOther", handleTaunterOther)
mux.HandleFunc("/monitor/ShareFirends", handleShareFirends)
mux.HandleFunc("/monitor/ShareTimeline", handleShareTimeline)
mux.HandleFunc("/monitor/ShareFirendUsers", handleShareFirendUsers)
mux.HandleFunc("/monitor/ShareTimelineUser", handleShareTimelineUser)
mux.HandleFunc("/monitor/AvgCostTime", handleAvgCostTime)
mux.HandleFunc("/monitor/MaxTimeCost", handleMaxTimeCost)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化