🌠 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.
func init() {
// TODO: args
confPath := flag.String("conf", "conf/wide.json", "path of wide.json")
confIP := flag.String("ip", "", "ip to visit")
confPort := flag.String("port", "", "port to visit")
confServer := flag.String("server", "", "this will overwrite Wide.Server 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")
flag.Set("alsologtostderr", "true")
flag.Set("stderrthreshold", "INFO")
flag.Set("v", "3")
flag.Parse()
i18n.Load()
@ -62,6 +63,10 @@ func init() {
conf.FixedTimeSave()
session.FixedTimeRelease()
if *confStat {
session.FixedTimeReport()
}
}
// indexHandler handles request of Wide index.
@ -288,7 +293,7 @@ func main() {
http.HandleFunc("/signup", handlerWrapper(session.SignUpUser))
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)
if err != nil {

View File

@ -26,6 +26,7 @@ import (
"encoding/json"
"net/http"
"os"
"strconv"
"sync"
"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.
//
// When a channel closed, releases all resources associated with it.