Sensibly format rows with table name on the right.
[pstop.git] / p_s / events_stages_summary_global_by_event_name / private.go
1 package events_stages_summary_global_by_event_name
2
3 import (
4         "database/sql"
5         "fmt"
6         "log"
7         "sort"
8         "strings"
9
10         "github.com/sjmudd/pstop/lib"
11 )
12
13 /**************************************************************************
14
15 root@localhost [performance_schema]> show create table  events_stages_summary_global_by_event_name\G
16 *************************** 1. row ***************************
17        Table: events_stages_summary_global_by_event_name
18 Create Table: CREATE TABLE `events_stages_summary_global_by_event_name` (
19   `EVENT_NAME` varchar(128) NOT NULL,
20   `COUNT_STAR` bigint(20) unsigned NOT NULL,
21   `SUM_TIMER_WAIT` bigint(20) unsigned NOT NULL,
22   `MIN_TIMER_WAIT` bigint(20) unsigned NOT NULL, // not used
23   `AVG_TIMER_WAIT` bigint(20) unsigned NOT NULL, // not used
24   `MAX_TIMER_WAIT` bigint(20) unsigned NOT NULL  // not used
25 ) ENGINE=PERFORMANCE_SCHEMA DEFAULT CHARSET=utf8
26 1 row in set (0.00 sec)
27
28 **************************************************************************/
29
30 // one row of data
31 type table_row struct {
32         EVENT_NAME     string
33         COUNT_STAR     uint64
34         SUM_TIMER_WAIT uint64
35 }
36
37 // a table of rows
38 type table_rows []table_row
39
40 // select the rows into table
41 func select_rows(dbh *sql.DB) table_rows {
42         var t table_rows
43
44         lib.Logger.Println("events_stages_summary_global_by_event_name.select_rows()")
45         sql := "SELECT EVENT_NAME, COUNT_STAR, SUM_TIMER_WAIT FROM events_stages_summary_global_by_event_name WHERE SUM_TIMER_WAIT > 0"
46
47         rows, err := dbh.Query(sql)
48         if err != nil {
49                 log.Fatal(err)
50         }
51         defer rows.Close()
52
53         for rows.Next() {
54                 var r table_row
55                 if err := rows.Scan(
56                         &r.EVENT_NAME,
57                         &r.COUNT_STAR,
58                         &r.SUM_TIMER_WAIT); err != nil {
59                         log.Fatal(err)
60                 }
61                 t = append(t, r)
62         }
63         if err := rows.Err(); err != nil {
64                 log.Fatal(err)
65         }
66         lib.Logger.Println("recovered", len(t), "row(s):")
67         lib.Logger.Println(t)
68
69         return t
70 }
71
72 // if the data in t2 is "newer", "has more values" than t then it needs refreshing.
73 // check this by comparing totals.
74 func (t table_rows) needs_refresh(t2 table_rows) bool {
75         my_totals := t.totals()
76         t2_totals := t2.totals()
77
78         return my_totals.SUM_TIMER_WAIT > t2_totals.SUM_TIMER_WAIT
79 }
80
81 // generate the totals of a table
82 func (t table_rows) totals() table_row {
83         var totals table_row
84         totals.EVENT_NAME = "Totals"
85
86         for i := range t {
87                 totals.add(t[i])
88         }
89
90         return totals
91 }
92
93 // return the stage name, removing any leading stage/sql/
94 func (r *table_row) name() string {
95         if len(r.EVENT_NAME) > 10 && r.EVENT_NAME[0:10] == "stage/sql/" {
96                 return r.EVENT_NAME[10:]
97         } else {
98                 return r.EVENT_NAME
99         }
100 }
101
102 // add the values of one row to another one
103 func (this *table_row) add(other table_row) {
104         this.SUM_TIMER_WAIT += other.SUM_TIMER_WAIT
105         this.COUNT_STAR += other.COUNT_STAR
106 }
107
108 // subtract the countable values in one row from another
109 func (this *table_row) subtract(other table_row) {
110         // check for issues here (we have a bug) and log it
111         // - this situation should not happen so there's a logic bug somewhere else
112         if this.SUM_TIMER_WAIT >= other.SUM_TIMER_WAIT {
113                 this.SUM_TIMER_WAIT -= other.SUM_TIMER_WAIT
114                 this.COUNT_STAR -= other.COUNT_STAR
115         } else {
116                 lib.Logger.Println("WARNING: table_row.subtract() - subtraction problem! (not subtracting)")
117                 lib.Logger.Println("this=", this)
118                 lib.Logger.Println("other=", other)
119         }
120 }
121
122 func (t table_rows) Len() int      { return len(t) }
123 func (t table_rows) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
124
125 // sort by value (descending) but also by "name" (ascending) if the values are the same
126 func (t table_rows) Less(i, j int) bool {
127         return (t[i].SUM_TIMER_WAIT > t[j].SUM_TIMER_WAIT) ||
128                 ((t[i].SUM_TIMER_WAIT == t[j].SUM_TIMER_WAIT) && (t[i].EVENT_NAME < t[j].EVENT_NAME))
129 }
130
131 func (t table_rows) Sort() {
132         sort.Sort(t)
133 }
134
135 // remove the initial values from those rows where there's a match
136 // - if we find a row we can't match ignore it
137 func (this *table_rows) subtract(initial table_rows) {
138         initial_by_name := make(map[string]int)
139
140         // iterate over rows by name
141         for i := range initial {
142                 initial_by_name[initial[i].name()] = i
143         }
144
145         for i := range *this {
146                 this_name := (*this)[i].name()
147                 if _, ok := initial_by_name[this_name]; ok {
148                         initial_index := initial_by_name[this_name]
149                         (*this)[i].subtract(initial[initial_index])
150                 }
151         }
152 }
153
154 // stage headings
155 func (r *table_row) headings() string {
156         return fmt.Sprintf("%10s %6s %8s|%s", "Latency", "%", "Counter", "Stage Name")
157 }
158
159 // generate a printable result
160 func (r *table_row) row_content(totals table_row) string {
161         name := r.name()
162         if r.COUNT_STAR == 0 && name != "Totals" {
163                 name = ""
164         }
165
166         return fmt.Sprintf("%10s %6s %8s|%s",
167                 lib.FormatTime(r.SUM_TIMER_WAIT),
168                 lib.FormatPct(lib.MyDivide(r.SUM_TIMER_WAIT, totals.SUM_TIMER_WAIT)),
169                 lib.FormatAmount(r.COUNT_STAR),
170                 name)
171 }
172
173 // describe a whole row
174 func (r table_row) String() string {
175         return fmt.Sprintf("%10s %10s %s",
176                 lib.FormatTime(r.SUM_TIMER_WAIT),
177                 lib.FormatAmount(r.COUNT_STAR),
178                 r.name())
179 }
180
181 // describe a whole table
182 func (t table_rows) String() string {
183         s := make([]string, len(t))
184
185         for i := range t {
186                 s = append(s, t[i].String())
187         }
188
189         return strings.Join(s, "\n")
190 }