Update to show idle times and adjust formatting
[pstop.git] / i_s / pl_by_user.go
index f5bd83f..78ddc2a 100644 (file)
@@ -24,6 +24,7 @@ CREATE TEMPORARY TABLE `PROCESSLIST` (
 type pl_by_user_row struct {
        username    string
        runtime     uint64
+       sleeptime   uint64
        connections uint64
        active      uint64
        hosts       uint64
@@ -37,13 +38,13 @@ type pl_by_user_row struct {
 type pl_by_user_rows []pl_by_user_row
 
 /*
-username      |Run Time   %age|Conn Actv|Hosts DBs|Select Insert Update Delete  Other|
-xxxxxxxxxxxxxx|hh:mm:ss 100.0%|9999 9999|9999  999|100.0% 100.0% 100.0% 100.0% 100.0%|
+username      |Run Time   %age|Sleep   |Conn Actv|Hosts DBs|Sel Ins Upd Del Oth|
+xxxxxxxxxxxxxx|hh:mm:ss 100.0%|hh:mm:ss|9999 9999|9999  999|999 999 999 999 999|
 */
 
 func (r *pl_by_user_row) headings() string {
-       return fmt.Sprintf("%-14s|%10s %6s|%4s %4s|%5s %3s|%6s %6s %6s %6s %6s|",
-               "User", "Run Time", "%", "Conn", "Actv", "Hosts", "DBs", "Select", "Insert", "Update", "Delete", "Other")
+       return fmt.Sprintf("%-14s|%8s %6s|%8s %6s|%4s %4s|%5s %3s|%3s %3s %3s %3s %3s|",
+               "User", "Run Time", "%", "Sleep", "%", "Conn", "Actv", "Hosts", "DBs", "Sel", "Ins", "Upd", "Del", "Oth")
 }
 
 // generate a printable result
@@ -56,19 +57,21 @@ func (r *pl_by_user_row) row_content(totals pl_by_user_row) string {
        } else {
                u = r.username
        }
-       return fmt.Sprintf("%-14s|%10s %6s|%4s %4s|%5s %3s|%6s %6s %6s %6s %6s|",
+       return fmt.Sprintf("%-14s|%8s %6s|%8s %6s|%4s %4s|%5s %3s|%3s %3s %3s %3s %3s|",
                u,
-               lib.FormatTime(r.runtime),
+               lib.FormatSeconds(r.runtime),
                lib.FormatPct(lib.MyDivide(r.runtime, totals.runtime)),
-               lib.FormatAmount(r.connections),
-               lib.FormatAmount(r.active),
-               lib.FormatAmount(r.hosts),
-               lib.FormatAmount(r.dbs),
-               lib.FormatAmount(r.selects),
-               lib.FormatAmount(r.inserts),
-               lib.FormatAmount(r.updates),
-               lib.FormatAmount(r.deletes),
-               lib.FormatAmount(r.other))
+               lib.FormatSeconds(r.sleeptime),
+               lib.FormatPct(lib.MyDivide(r.sleeptime, totals.sleeptime)),
+               lib.FormatCounter(int(r.connections), 4),
+               lib.FormatCounter(int(r.active), 4),
+               lib.FormatCounter(int(r.hosts), 5),
+               lib.FormatCounter(int(r.dbs), 3),
+               lib.FormatCounter(int(r.selects), 3),
+               lib.FormatCounter(int(r.inserts), 3),
+               lib.FormatCounter(int(r.updates), 3),
+               lib.FormatCounter(int(r.deletes), 3),
+               lib.FormatCounter(int(r.other), 3))
 }
 
 // generate a row of totals from a table
@@ -78,6 +81,7 @@ func (t pl_by_user_rows) totals() pl_by_user_row {
 
        for i := range t {
                totals.runtime += t[i].runtime
+               totals.sleeptime += t[i].sleeptime
                totals.connections += t[i].connections
                totals.active += t[i].active
                //      totals.hosts += t[i].hosts      This needs to be done differently to get the total number of distinct hosts
@@ -99,7 +103,12 @@ func (t pl_by_user_rows) Headings() string {
 
 // describe a whole row
 func (r pl_by_user_row) String() string {
-       return fmt.Sprintf("%v %v %v %v %v %v %v %v %v", r.username, r.runtime, r.connections, r.active, r.hosts, r.dbs, r.selects, r.inserts, r.updates, r.deletes, r.other)
+       return fmt.Sprintf("%v %v %v %v %v %v %v %v %v %v", r.username, r.runtime, r.connections, r.sleeptime, r.active, r.hosts, r.dbs, r.selects, r.inserts, r.updates, r.deletes, r.other)
+}
+
+// total time is runtime + sleeptime
+func (r pl_by_user_row) total_time() uint64 {
+       return r.runtime + r.sleeptime
 }
 
 // describe a whole table
@@ -117,9 +126,9 @@ type ByRunTime pl_by_user_rows
 func (t ByRunTime) Len() int      { return len(t) }
 func (t ByRunTime) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
 func (t ByRunTime) Less(i, j int) bool {
-       return (t[i].runtime > t[j].runtime) ||
-               ((t[i].runtime == t[j].runtime) && (t[i].connections > t[j].connections)) ||
-               ((t[i].runtime == t[j].runtime) && (t[i].connections == t[j].connections) && (t[i].username < t[j].username))
+       return (t[i].total_time() > t[j].total_time()) ||
+               ((t[i].total_time() == t[j].total_time()) && (t[i].connections > t[j].connections)) ||
+               ((t[i].total_time() == t[j].total_time()) && (t[i].connections == t[j].connections) && (t[i].username < t[j].username))
 }
 
 func (t pl_by_user_rows) Sort() {