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