Initial import of pstop v0.0.10
[pstop.git] / performance_schema / table_io_waits_summary_by_table / table_io_waits_summary_by_table.go
1 // performance_schema - library routines for pstop.
2 //
3 // This file contains the library routines for managing the
4 // table_io_waits_by_table table.
5 package table_io_waits_summary_by_table
6
7 import (
8         "database/sql"
9         "fmt"
10
11         "github.com/sjmudd/pstop/lib"
12         ps "github.com/sjmudd/pstop/performance_schema"
13 )
14
15 // a table of rows
16 type Table_io_waits_summary_by_table struct {
17         ps.RelativeStats
18         ps.InitialTime
19         want_latency bool
20         initial      table_io_waits_summary_by_table_rows // initial data for relative values
21         current      table_io_waits_summary_by_table_rows // last loaded values
22         results      table_io_waits_summary_by_table_rows // results (maybe with subtraction)
23         totals       table_io_waits_summary_by_table_row  // totals of results
24 }
25
26 func (t *Table_io_waits_summary_by_table) SetWantsLatency(want_latency bool) {
27         t.want_latency = want_latency
28 }
29
30 func (t Table_io_waits_summary_by_table) WantsLatency() bool {
31         return t.want_latency
32 }
33
34 // Collect() collects data from the db, updating initial
35 // values if needed, and then subtracting initial values if we want
36 // relative values, after which it stores totals.
37 func (t *Table_io_waits_summary_by_table) Collect(dbh *sql.DB) {
38         lib.Logger.Println("Table_io_waits_summary_by_table.Collect() BEGIN")
39         t.current = select_tiwsbt_rows(dbh)
40         lib.Logger.Println("- t.current set from", len(t.current), "collected row(s) from SELECT")
41
42         if len(t.initial) == 0 && len(t.current) > 0 {
43                 // lib.Logger.Println("- setting t.initial to initial value" )
44                 t.initial = make(table_io_waits_summary_by_table_rows, len(t.current))
45                 copy(t.initial, t.current)
46         }
47
48         // check for reload initial characteristics
49         if t.initial.needs_refresh(t.current) {
50                 // lib.Logger.Println( "- t.initial data needs refreshing!" )
51                 t.initial = make(table_io_waits_summary_by_table_rows, len(t.current))
52                 copy(t.initial, t.current)
53         }
54
55         t.make_results()
56
57         // lib.Logger.Println( "t.initial:", t.initial )
58         // lib.Logger.Println( "t.current:", t.current )
59         lib.Logger.Println("t.results:", t.results)
60         lib.Logger.Println("t.totals:", t.totals)
61         lib.Logger.Println("Table_io_waits_summary_by_table.Collect() END")
62 }
63
64 func (t *Table_io_waits_summary_by_table) make_results() {
65         // lib.Logger.Println( "- t.results set from t.current" )
66         t.results = make(table_io_waits_summary_by_table_rows, len(t.current))
67         copy(t.results, t.current)
68         if t.WantRelativeStats() {
69                 // lib.Logger.Println( "- subtracting t.initial from t.results as WantRelativeStats()" )
70                 t.results.subtract(t.initial)
71         }
72
73         // lib.Logger.Println( "- sorting t.results" )
74         t.results.Sort(t.want_latency)
75         // lib.Logger.Println( "- collecting t.totals from t.results" )
76         t.totals = t.results.totals()
77 }
78
79 // reset the statistics to current values
80 func (t *Table_io_waits_summary_by_table) UpdateInitialValues() {
81         // lib.Logger.Println( "Table_io_waits_summary_by_table.UpdateInitialValues() BEGIN" )
82
83         t.initial = make(table_io_waits_summary_by_table_rows, len(t.current))
84         copy(t.initial, t.current)
85
86         t.make_results()
87
88         // lib.Logger.Println( "Table_io_waits_summary_by_table.UpdateInitialValues() END" )
89 }
90
91 func (t *Table_io_waits_summary_by_table) Headings() string {
92         if t.want_latency {
93                 return t.latencyHeadings()
94         } else {
95                 return t.opsHeadings()
96         }
97 }
98
99 func (t Table_io_waits_summary_by_table) RowContent(max_rows int) []string {
100         if t.want_latency {
101                 return t.latencyRowContent(max_rows)
102         } else {
103                 return t.opsRowContent(max_rows)
104         }
105 }
106
107 func (t Table_io_waits_summary_by_table) EmptyRowContent() string {
108         if t.want_latency {
109                 return t.emptyLatencyRowContent()
110         } else {
111                 return t.emptyOpsRowContent()
112         }
113 }
114
115 func (t Table_io_waits_summary_by_table) TotalRowContent() string {
116         if t.want_latency {
117                 return t.totalLatencyRowContent()
118         } else {
119                 return t.totalOpsRowContent()
120         }
121 }
122
123 func (t Table_io_waits_summary_by_table) Description() string {
124         if t.want_latency {
125                 return t.latencyDescription()
126         } else {
127                 return t.opsDescription()
128         }
129 }
130
131 func (t *Table_io_waits_summary_by_table) latencyHeadings() string {
132         var r table_io_waits_summary_by_table_row
133
134         return r.latency_headings()
135 }
136
137 func (t *Table_io_waits_summary_by_table) opsHeadings() string {
138         var r table_io_waits_summary_by_table_row
139
140         return r.ops_headings()
141 }
142
143 func (t Table_io_waits_summary_by_table) opsRowContent(max_rows int) []string {
144         rows := make([]string, 0, max_rows)
145
146         for i := range t.results {
147                 if i < max_rows {
148                         rows = append(rows, t.results[i].ops_row_content(t.totals))
149                 }
150         }
151
152         return rows
153 }
154
155 func (t Table_io_waits_summary_by_table) latencyRowContent(max_rows int) []string {
156         rows := make([]string, 0, max_rows)
157
158         for i := range t.results {
159                 if i < max_rows {
160                         rows = append(rows, t.results[i].latency_row_content(t.totals))
161                 }
162         }
163
164         return rows
165 }
166
167 func (t Table_io_waits_summary_by_table) emptyOpsRowContent() string {
168         var r table_io_waits_summary_by_table_row
169
170         return r.ops_row_content(r)
171 }
172
173 func (t Table_io_waits_summary_by_table) emptyLatencyRowContent() string {
174         var r table_io_waits_summary_by_table_row
175
176         return r.latency_row_content(r)
177 }
178
179 func (t Table_io_waits_summary_by_table) totalOpsRowContent() string {
180         return t.totals.ops_row_content(t.totals)
181 }
182
183 func (t Table_io_waits_summary_by_table) totalLatencyRowContent() string {
184         return t.totals.latency_row_content(t.totals)
185 }
186
187 func (t Table_io_waits_summary_by_table) latencyDescription() string {
188         count := t.count_rows()
189         return fmt.Sprintf("Latency by Table Name (table_io_waits_summary_by_table) %d rows", count)
190 }
191
192 func (t Table_io_waits_summary_by_table) opsDescription() string {
193         count := t.count_rows()
194         return fmt.Sprintf("Operations by Table Name (table_io_waits_summary_by_table) %d rows", count)
195 }
196
197 func (t Table_io_waits_summary_by_table) count_rows() int {
198         var count int
199         for row := range t.results {
200                 if t.results[row].SUM_TIMER_WAIT > 0 {
201                         count++
202                 }
203         }
204         return count
205 }