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