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" "encoding/json"
"net/http" "net/http"
"os" "os"
"sort"
"strconv" "strconv"
"sync" "sync"
"time" "time"
@ -126,23 +127,23 @@ func (u *userReport) report() string {
// FixedTimeReport reports the Wide sessions status periodically (10 minutes). // FixedTimeReport reports the Wide sessions status periodically (10 minutes).
func FixedTimeReport() { func FixedTimeReport() {
go func() { go func() {
for _ = range time.Tick(10 * time.Minute) { for _ = range time.Tick(10 * time.Second) {
users := map[string]*userReport{} // <username, *userReport> users := userReports{}
processSum := 0 processSum := 0
for _, s := range WideSessions { for _, s := range WideSessions {
processCnt := len(s.Processes) processCnt := len(s.Processes)
processSum += processCnt processSum += processCnt
if report, exists := users[s.Username]; exists { if report, exists := contains(users, s.Username); exists {
if s.Updated.After(report.updated) { if s.Updated.After(report.updated) {
users[s.Username].updated = s.Updated report.updated = s.Updated
} }
report.sessionCnt++ report.sessionCnt++
report.processCnt += processCnt report.processCnt += processCnt
} else { } 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 [" + buf.WriteString("\n [" + strconv.Itoa(len(users)) + "] users, [" + strconv.Itoa(processSum) + "] running processes and [" +
strconv.Itoa(len(WideSessions)) + "] sessions currently\n") strconv.Itoa(len(WideSessions)) + "] sessions currently\n")
sort.Sort(users)
for _, t := range users { for _, t := range users {
buf.WriteString(" " + t.report() + "\n") 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. // 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.