reporting

This commit is contained in:
Liang Ding 2014-12-23 16:03:09 +08:00
parent d94e6759bf
commit 991cd7f606
1 changed files with 24 additions and 5 deletions

View File

@ -27,6 +27,7 @@ import (
"encoding/json"
"net/http"
"os"
"sort"
"strconv"
"sync"
"time"
@ -126,23 +127,23 @@ func (u *userReport) report() string {
// 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 _ = range time.Tick(10 * time.Second) {
users := userReports{}
processSum := 0
for _, s := range WideSessions {
processCnt := len(s.Processes)
processSum += processCnt
if report, exists := users[s.Username]; exists {
if report, exists := contains(users, s.Username); exists {
if s.Updated.After(report.updated) {
users[s.Username].updated = s.Updated
report.updated = s.Updated
}
report.sessionCnt++
report.processCnt += processCnt
} else {
users[s.Username] = &userReport{username: s.Username, sessionCnt: 1, processCnt: processCnt, updated: s.Updated}
users = append(users, &userReport{username: s.Username, sessionCnt: 1, processCnt: processCnt, updated: s.Updated})
}
}
@ -150,6 +151,8 @@ func FixedTimeReport() {
buf.WriteString("\n [" + strconv.Itoa(len(users)) + "] users, [" + strconv.Itoa(processSum) + "] running processes and [" +
strconv.Itoa(len(WideSessions)) + "] sessions currently\n")
sort.Sort(users)
for _, t := range users {
buf.WriteString(" " + t.report() + "\n")
}
@ -159,6 +162,22 @@ func FixedTimeReport() {
}()
}
func contains(reports []*userReport, username string) (*userReport, bool) {
for _, ur := range reports {
if username == ur.username {
return ur, true
}
}
return nil, false
}
type userReports []*userReport
func (f userReports) Len() int { return len(f) }
func (f userReports) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
func (f userReports) Less(i, j int) bool { return f[i].processCnt > f[j].processCnt }
// WSHandler handles request of creating session channel.
//
// When a channel closed, releases all resources associated with it.