🌠 online stat.

This commit is contained in:
Liang Ding 2014-11-21 11:19:57 +08:00
parent 10d8ee13ab
commit 02a6004188
2 changed files with 48 additions and 2 deletions

View File

@ -39,17 +39,18 @@ import (
// The only one init function in Wide. // The only one init function in Wide.
func init() { func init() {
// TODO: args
confPath := flag.String("conf", "conf/wide.json", "path of wide.json") confPath := flag.String("conf", "conf/wide.json", "path of wide.json")
confIP := flag.String("ip", "", "ip to visit") confIP := flag.String("ip", "", "ip to visit")
confPort := flag.String("port", "", "port to visit") confPort := flag.String("port", "", "port to visit")
confServer := flag.String("server", "", "this will overwrite Wide.Server if specified") confServer := flag.String("server", "", "this will overwrite Wide.Server if specified")
confChannel := flag.String("channel", "", "this will overwrite Wide.XXXChannel if specified") confChannel := flag.String("channel", "", "this will overwrite Wide.XXXChannel if specified")
confStat := flag.Bool("stat", false, "whether report statistics periodically")
confDocker := flag.Bool("docker", false, "whether run in a docker container") confDocker := flag.Bool("docker", false, "whether run in a docker container")
flag.Set("alsologtostderr", "true") flag.Set("alsologtostderr", "true")
flag.Set("stderrthreshold", "INFO") flag.Set("stderrthreshold", "INFO")
flag.Set("v", "3") flag.Set("v", "3")
flag.Parse() flag.Parse()
i18n.Load() i18n.Load()
@ -62,6 +63,10 @@ func init() {
conf.FixedTimeSave() conf.FixedTimeSave()
session.FixedTimeRelease() session.FixedTimeRelease()
if *confStat {
session.FixedTimeReport()
}
} }
// indexHandler handles request of Wide index. // indexHandler handles request of Wide index.
@ -288,7 +293,7 @@ func main() {
http.HandleFunc("/signup", handlerWrapper(session.SignUpUser)) http.HandleFunc("/signup", handlerWrapper(session.SignUpUser))
http.HandleFunc("/preference", handlerWrapper(session.PreferenceHandler)) http.HandleFunc("/preference", handlerWrapper(session.PreferenceHandler))
glog.V(0).Infof("Wide is running [%s]", conf.Wide.Server) glog.Infof("Wide is running [%s]", conf.Wide.Server)
err := http.ListenAndServe(conf.Wide.Server, nil) err := http.ListenAndServe(conf.Wide.Server, nil)
if err != nil { if err != nil {

View File

@ -26,6 +26,7 @@ import (
"encoding/json" "encoding/json"
"net/http" "net/http"
"os" "os"
"strconv"
"sync" "sync"
"time" "time"
@ -102,6 +103,46 @@ func FixedTimeRelease() {
}() }()
} }
// Online user statistic report.
type userReport struct {
username string
sessionCnt int
updated time.Time
}
// report returns a online user statistics in pretty format.
func (u *userReport) report() string {
return "[" + u.username + "] has [" + strconv.Itoa(u.sessionCnt) + "] wide sessions, latest activity time [" +
u.updated.Format("2006-01-02 15:04:05") + "]"
}
// FixedTimeReport reports the Wide sessions status periodically (10 minutes).
func FixedTimeReport() {
go func() {
for _ = range time.Tick(10 * time.Minute) {
users := map[string]*userReport{} // <username, *userReport>
for _, s := range WideSessions {
if report, exists := users[s.Username]; exists {
if s.Updated.After(report.updated) {
users[s.Username].updated = s.Updated
}
report.sessionCnt++
} else {
users[s.Username] = &userReport{username: s.Username, sessionCnt: 1, updated: s.Updated}
}
}
glog.Infof("[%d] users are online and [%d] wide sessions currently", len(users), len(WideSessions))
for _, t := range users {
glog.Infof(t.report())
}
}
}()
}
// WSHandler handles request of creating session channel. // WSHandler handles request of creating session channel.
// //
// When a channel closed, releases all resources associated with it. // When a channel closed, releases all resources associated with it.