Isolate display from collection of data
[pstop.git] / state / state.go
1 // lib - library routines for pstop.
2 //
3 // this file contains the library routines related to the stored state in pstop.
4 package state
5
6 import (
7         "database/sql"
8         "fmt"
9         "time"
10
11         "github.com/sjmudd/pstop/lib"
12         fsbi "github.com/sjmudd/pstop/performance_schema/file_summary_by_instance"
13         "github.com/sjmudd/pstop/performance_schema/ps_table"
14         tiwsbt "github.com/sjmudd/pstop/performance_schema/table_io_waits_summary_by_table"
15         tlwsbt "github.com/sjmudd/pstop/performance_schema/table_lock_waits_summary_by_table"
16         "github.com/sjmudd/pstop/screen"
17         "github.com/sjmudd/pstop/version"
18 )
19
20 // what information to show
21 type Show int
22
23 const (
24         showLatency = iota
25         showOps     = iota
26         showIO      = iota
27         showLocks   = iota
28 )
29
30 type State struct {
31         datadir             string
32         dbh                 *sql.DB
33         help                bool
34         hostname            string
35         fsbi                ps_table.Tabler // ufsbi.File_summary_by_instance
36         tiwsbt              tiwsbt.Table_io_waits_summary_by_table
37         tlwsbt              ps_table.Tabler // tlwsbt.Table_lock_waits_summary_by_table
38         screen              screen.TermboxScreen
39         show                Show
40         mysql_version       string
41         want_relative_stats bool
42 }
43
44 func (state *State) Setup(dbh *sql.DB) {
45         state.dbh = dbh
46
47         state.screen.Initialise()
48
49         _, variables := lib.SelectAllGlobalVariablesByVariableName(state.dbh)
50         // setup to their initial types/values
51         state.fsbi = fsbi.NewFileSummaryByInstance(variables)
52         state.tlwsbt = new(tlwsbt.Table_lock_waits_summary_by_table)
53
54         state.want_relative_stats = true // we show info from the point we start collecting data
55         state.fsbi.SetWantRelativeStats(state.want_relative_stats)
56         state.fsbi.SetNow()
57         state.tlwsbt.SetWantRelativeStats(state.want_relative_stats)
58         state.tlwsbt.SetNow()
59         state.tiwsbt.SetWantRelativeStats(state.want_relative_stats)
60         state.tiwsbt.SetNow()
61
62         state.ResetDBStatistics()
63
64         state.SetHelp(false)
65         state.show = showLatency
66         state.tiwsbt.SetWantsLatency(true)
67
68         _, hostname := lib.SelectGlobalVariableByVariableName(state.dbh, "HOSTNAME")
69         _, mysql_version := lib.SelectGlobalVariableByVariableName(state.dbh, "VERSION")
70         _, datadir := lib.SelectGlobalVariableByVariableName(state.dbh, "DATADIR")
71         state.SetHostname(hostname)
72         state.SetMySQLVersion(mysql_version)
73         state.SetDatadir(datadir)
74 }
75
76 // do a fresh collection of data and then update the initial values based on that.
77 func (state *State) ResetDBStatistics() {
78         state.Collect()
79         state.UpdateInitialValues()
80 }
81
82 func (state *State) UpdateInitialValues() {
83         state.fsbi.UpdateInitialValues()
84         state.tlwsbt.UpdateInitialValues()
85         state.tiwsbt.UpdateInitialValues()
86 }
87
88 func (state *State) Collect() {
89         state.fsbi.Collect(state.dbh)
90         state.tlwsbt.Collect(state.dbh)
91         state.tiwsbt.Collect(state.dbh)
92 }
93
94 func (state State) MySQLVersion() string {
95         return state.mysql_version
96 }
97
98 func (state State) Datadir() string {
99         return state.datadir
100 }
101
102 func (state *State) SetHelp(newHelp bool) {
103         state.help = newHelp
104
105         state.screen.Clear()
106         state.screen.Flush()
107 }
108
109 func (state *State) SetDatadir(datadir string) {
110         state.datadir = datadir
111 }
112
113 func (state *State) SetMySQLVersion(mysql_version string) {
114         state.mysql_version = mysql_version
115 }
116
117 func (state *State) SetHostname(hostname string) {
118         state.hostname = hostname
119 }
120
121 func (state State) Help() bool {
122         return state.help
123 }
124
125 // display the output according to the mode we are in
126 func (state *State) Display() {
127         if state.help {
128                 state.screen.DisplayHelp()
129         } else {
130                 state.displayHeading()
131                 switch state.show {
132                 case showLatency, showOps:
133                         state.displayOpsOrLatency()
134                 case showIO:
135                         state.displayIO()
136                 case showLocks:
137                         state.displayLocks()
138                 }
139         }
140 }
141
142 // change to the next display mode
143 func (state *State) DisplayNext() {
144         if state.show == showLocks {
145                 state.show = showLatency
146         } else {
147                 state.show++
148         }
149         // this needs to be done more cleanly
150         if state.show == showLatency {
151                 state.tiwsbt.SetWantsLatency(true)
152         }
153         if state.show == showOps {
154                 state.tiwsbt.SetWantsLatency(false)
155         }
156         state.screen.Clear()
157         state.screen.Flush()
158 }
159
160 func (state State) displayHeading() {
161         state.displayLine0()
162         state.displayLine1()
163 }
164
165 func (state State) displayLine0() {
166         _, uptime := lib.SelectGlobalStatusByVariableName(state.dbh, "UPTIME")
167         top_line := lib.MyName() + " " + version.Version() + " - " + now_hhmmss() + " " + state.hostname + " / " + state.mysql_version + ", up " + fmt.Sprintf("%-16s", lib.Uptime(uptime))
168         if state.want_relative_stats {
169                 now := time.Now()
170
171                 var initial time.Time
172
173                 switch state.show {
174                 case showLatency, showOps:
175                         initial = state.tiwsbt.Last()
176                 case showIO:
177                         initial = state.fsbi.Last()
178                 case showLocks:
179                         initial = state.tlwsbt.Last()
180                 default:
181                         initial = time.Now() // THIS IS WRONG !!!
182                 }
183
184                 d := now.Sub(initial)
185
186                 top_line = top_line + " [REL] " + fmt.Sprintf("%.0f seconds", d.Seconds())
187         } else {
188                 top_line = top_line + " [ABS]             "
189         }
190         state.screen.PrintAt(0, 0, top_line)
191 }
192
193 func (state State) displayLine1() {
194         switch state.show {
195         case showLatency, showOps:
196                 state.screen.PrintAt(0, 1, state.tiwsbt.Description())
197         case showIO:
198                 state.screen.PrintAt(0, 1, state.fsbi.Description())
199         case showLocks:
200                 state.screen.PrintAt(0, 1, state.tlwsbt.Description())
201         default:
202                 state.screen.PrintAt(0, 1, "UNKNOWN")
203         }
204 }
205
206 func (state *State) displayOpsOrLatency() {
207         state.screen.PrintAt(0, 2, state.tiwsbt.Headings())
208
209         max_rows := state.screen.Height() - 3
210         row_content := state.tiwsbt.RowContent(max_rows)
211
212         // print out rows
213         for k := range row_content {
214                 y := 3 + k
215                 state.screen.PrintAt(0, y, row_content[k])
216         }
217         // print out empty rows
218         for k := len(row_content); k < (state.screen.Height() - 3); k++ {
219                 y := 3 + k
220                 if y < state.screen.Height()-1 {
221                         state.screen.PrintAt(0, y, state.tiwsbt.EmptyRowContent())
222                 }
223         }
224
225         // print out the totals at the bottom
226         state.screen.PrintAt(0, state.screen.Height()-1, state.tiwsbt.TotalRowContent())
227 }
228
229 // show actual I/O latency values
230 func (state State) displayIO() {
231         state.screen.PrintAt(0, 2, state.fsbi.Headings())
232
233         // print out the data
234         max_rows := state.screen.Height() - 3
235         row_content := state.fsbi.RowContent(max_rows)
236
237         // print out rows
238         for k := range row_content {
239                 y := 3 + k
240                 state.screen.PrintAt(0, y, row_content[k])
241         }
242         // print out empty rows
243         for k := len(row_content); k < (state.screen.Height() - 3); k++ {
244                 y := 3 + k
245                 if y < state.screen.Height()-1 {
246                         state.screen.PrintAt(0, y, state.fsbi.EmptyRowContent())
247                 }
248         }
249
250         // print out the totals at the bottom
251         state.screen.PrintAt(0, state.screen.Height()-1, state.fsbi.TotalRowContent())
252 }
253
254 func (state *State) displayLocks() {
255         state.screen.PrintAt(0, 2, state.tlwsbt.Headings())
256
257         // print out the data
258         max_rows := state.screen.Height() - 3
259         row_content := state.tlwsbt.RowContent(max_rows)
260
261         // print out rows
262         for k := range row_content {
263                 y := 3 + k
264                 state.screen.PrintAt(0, y, row_content[k])
265         }
266         // print out empty rows
267         for k := len(row_content); k < (state.screen.Height() - 3); k++ {
268                 y := 3 + k
269                 if y < state.screen.Height()-1 {
270                         state.screen.PrintAt(0, y, state.tlwsbt.EmptyRowContent())
271                 }
272         }
273
274         // print out the totals at the bottom
275         state.screen.PrintAt(0, state.screen.Height()-1, state.tlwsbt.TotalRowContent())
276 }
277
278 // do we want to show all p_s data?
279 func (state State) WantRelativeStats() bool {
280         return state.want_relative_stats
281 }
282
283 // set if we want data from when we started/reset stats.
284 func (state *State) SetWantRelativeStats(want_relative_stats bool) {
285         state.want_relative_stats = want_relative_stats
286
287         state.fsbi.SetWantRelativeStats(want_relative_stats)
288         state.tlwsbt.SetWantRelativeStats(state.want_relative_stats)
289         state.tiwsbt.SetWantRelativeStats(state.want_relative_stats)
290
291         state.Display()
292 }
293
294 // if there's a better way of doing this do it better ...
295 func now_hhmmss() string {
296         t := time.Now()
297         return fmt.Sprintf("%2d:%02d:%02d", t.Hour(), t.Minute(), t.Second())
298 }
299
300 // record the latest screen size
301 func (state *State) ScreenSetSize(width, height int) {
302         state.screen.SetSize(width, height)
303 }
304
305 // clean up screen and disconnect database
306 func (state *State) Cleanup() {
307         state.screen.Close()
308         if state.dbh != nil {
309                 _ = state.dbh.Close()
310         }
311 }