setup_instruments: use query with parameter
[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 // stage name limited to 40 characters
103 func (r *table_row) pretty_name() string {
104         s := r.name()
105         if len(s) > 40 {
106                 s = s[:39]
107         }
108         return s
109 }
110
111 // add the values of one row to another one
112 func (this *table_row) add(other table_row) {
113         this.SUM_TIMER_WAIT += other.SUM_TIMER_WAIT
114         this.COUNT_STAR += other.COUNT_STAR
115 }
116
117 // subtract the countable values in one row from another
118 func (this *table_row) subtract(other table_row) {
119         // check for issues here (we have a bug) and log it
120         // - this situation should not happen so there's a logic bug somewhere else
121         if this.SUM_TIMER_WAIT >= other.SUM_TIMER_WAIT {
122                 this.SUM_TIMER_WAIT -= other.SUM_TIMER_WAIT
123                 this.COUNT_STAR -= other.COUNT_STAR
124         } else {
125                 lib.Logger.Println("WARNING: table_row.subtract() - subtraction problem! (not subtracting)")
126                 lib.Logger.Println("this=", this)
127                 lib.Logger.Println("other=", other)
128         }
129 }
130
131 func (t table_rows) Len() int      { return len(t) }
132 func (t table_rows) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
133
134 // sort by value (descending) but also by "name" (ascending) if the values are the same
135 func (t table_rows) Less(i, j int) bool {
136         return (t[i].SUM_TIMER_WAIT > t[j].SUM_TIMER_WAIT) ||
137                 ((t[i].SUM_TIMER_WAIT == t[j].SUM_TIMER_WAIT) && (t[i].EVENT_NAME < t[j].EVENT_NAME))
138 }
139
140 func (t table_rows) Sort() {
141         sort.Sort(t)
142 }
143
144 // remove the initial values from those rows where there's a match
145 // - if we find a row we can't match ignore it
146 func (this *table_rows) subtract(initial table_rows) {
147         initial_by_name := make(map[string]int)
148
149         // iterate over rows by name
150         for i := range initial {
151                 initial_by_name[initial[i].name()] = i
152         }
153
154         for i := range *this {
155                 this_name := (*this)[i].name()
156                 if _, ok := initial_by_name[this_name]; ok {
157                         initial_index := initial_by_name[this_name]
158                         (*this)[i].subtract(initial[initial_index])
159                 }
160         }
161 }
162
163 // stage headings
164 func (r *table_row) headings() string {
165         return fmt.Sprintf("%-40s|%10s %6s %8s|", "Stage Name", "Latency", "%", "Counter")
166 }
167
168 // generate a printable result
169 func (r *table_row) row_content(totals table_row) string {
170         name := r.pretty_name()
171         if r.COUNT_STAR == 0 && name != "Totals" {
172                 name = ""
173         }
174
175         return fmt.Sprintf("%-40s|%10s %6s %8s|",
176                 name,
177                 lib.FormatTime(r.SUM_TIMER_WAIT),
178                 lib.FormatPct(lib.MyDivide(r.SUM_TIMER_WAIT, totals.SUM_TIMER_WAIT)),
179                 lib.FormatAmount(r.COUNT_STAR))
180 }
181
182 // describe a whole row
183 func (r table_row) String() string {
184         return fmt.Sprintf("%-40s %10s %10s",
185                 r.pretty_name(),
186                 lib.FormatTime(r.SUM_TIMER_WAIT),
187                 lib.FormatAmount(r.COUNT_STAR))
188 }
189
190 // describe a whole table
191 func (t table_rows) String() string {
192         s := make([]string, len(t))
193
194         for i := range t {
195                 s = append(s, t[i].String())
196         }
197
198         return strings.Join(s, "\n")
199 }