88983311b10cea39a485e8a9b94764ecaa3cb7c5
[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         start := time.Now()
84         state.fsbi.UpdateInitialValues()
85         state.tlwsbt.UpdateInitialValues()
86         state.tiwsbt.UpdateInitialValues()
87         lib.Logger.Println("state.UpdateInitialValues() took", time.Duration(time.Since(start)).String())
88 }
89
90 func (state *State) Collect() {
91         start := time.Now()
92         state.fsbi.Collect(state.dbh)
93         state.tlwsbt.Collect(state.dbh)
94         state.tiwsbt.Collect(state.dbh)
95         lib.Logger.Println("state.Collect() took", time.Duration(time.Since(start)).String())
96 }
97
98 func (state State) MySQLVersion() string {
99         return state.mysql_version
100 }
101
102 func (state State) Datadir() string {
103         return state.datadir
104 }
105
106 func (state *State) SetHelp(newHelp bool) {
107         state.help = newHelp
108
109         state.screen.Clear()
110         state.screen.Flush()
111 }
112
113 func (state *State) SetDatadir(datadir string) {
114         state.datadir = datadir
115 }
116
117 func (state *State) SetMySQLVersion(mysql_version string) {
118         state.mysql_version = mysql_version
119 }
120
121 func (state *State) SetHostname(hostname string) {
122         state.hostname = hostname
123 }
124
125 func (state State) Help() bool {
126         return state.help
127 }
128
129 // display the output according to the mode we are in
130 func (state *State) Display() {
131         if state.help {
132                 state.screen.DisplayHelp()
133         } else {
134                 state.displayHeading()
135                 switch state.show {
136                 case showLatency, showOps:
137                         state.displayOpsOrLatency()
138                 case showIO:
139                         state.displayIO()
140                 case showLocks:
141                         state.displayLocks()
142                 }
143         }
144 }
145
146 // change to the next display mode
147 func (state *State) DisplayNext() {
148         if state.show == showLocks {
149                 state.show = showLatency
150         } else {
151                 state.show++
152         }
153         // this needs to be done more cleanly
154         if state.show == showLatency {
155                 state.tiwsbt.SetWantsLatency(true)
156         }
157         if state.show == showOps {
158                 state.tiwsbt.SetWantsLatency(false)
159         }
160         state.screen.Clear()
161         state.screen.Flush()
162 }
163
164 func (state State) displayHeading() {
165         state.displayLine0()
166         state.displayLine1()
167 }
168
169 func (state State) displayLine0() {
170         _, uptime := lib.SelectGlobalStatusByVariableName(state.dbh, "UPTIME")
171         top_line := lib.MyName() + " " + version.Version() + " - " + now_hhmmss() + " " + state.hostname + " / " + state.mysql_version + ", up " + fmt.Sprintf("%-16s", lib.Uptime(uptime))
172         if state.want_relative_stats {
173                 now := time.Now()
174
175                 var initial time.Time
176
177                 switch state.show {
178                 case showLatency, showOps:
179                         initial = state.tiwsbt.Last()
180                 case showIO:
181                         initial = state.fsbi.Last()
182                 case showLocks:
183                         initial = state.tlwsbt.Last()
184                 default:
185                         initial = time.Now() // THIS IS WRONG !!!
186                 }
187
188                 d := now.Sub(initial)
189
190                 top_line = top_line + " [REL] " + fmt.Sprintf("%.0f seconds", d.Seconds())
191         } else {
192                 top_line = top_line + " [ABS]             "
193         }
194         state.screen.PrintAt(0, 0, top_line)
195 }
196
197 func (state State) displayLine1() {
198         switch state.show {
199         case showLatency, showOps:
200                 state.screen.PrintAt(0, 1, state.tiwsbt.Description())
201         case showIO:
202                 state.screen.PrintAt(0, 1, state.fsbi.Description())
203         case showLocks:
204                 state.screen.PrintAt(0, 1, state.tlwsbt.Description())
205         default:
206                 state.screen.PrintAt(0, 1, "UNKNOWN")
207         }
208 }
209
210 func (state *State) displayOpsOrLatency() {
211         state.screen.PrintAt(0, 2, state.tiwsbt.Headings())
212
213         max_rows := state.screen.Height() - 3
214         row_content := state.tiwsbt.RowContent(max_rows)
215
216         // print out rows
217         for k := range row_content {
218                 y := 3 + k
219                 state.screen.PrintAt(0, y, row_content[k])
220         }
221         // print out empty rows
222         for k := len(row_content); k < (state.screen.Height() - 3); k++ {
223                 y := 3 + k
224                 if y < state.screen.Height()-1 {
225                         state.screen.PrintAt(0, y, state.tiwsbt.EmptyRowContent())
226                 }
227         }
228
229         // print out the totals at the bottom
230         state.screen.PrintAt(0, state.screen.Height()-1, state.tiwsbt.TotalRowContent())
231 }
232
233 // show actual I/O latency values
234 func (state State) displayIO() {
235         state.screen.PrintAt(0, 2, state.fsbi.Headings())
236
237         // print out the data
238         max_rows := state.screen.Height() - 3
239         row_content := state.fsbi.RowContent(max_rows)
240
241         // print out rows
242         for k := range row_content {
243                 y := 3 + k
244                 state.screen.PrintAt(0, y, row_content[k])
245         }
246         // print out empty rows
247         for k := len(row_content); k < (state.screen.Height() - 3); k++ {
248                 y := 3 + k
249                 if y < state.screen.Height()-1 {
250                         state.screen.PrintAt(0, y, state.fsbi.EmptyRowContent())
251                 }
252         }
253
254         // print out the totals at the bottom
255         state.screen.PrintAt(0, state.screen.Height()-1, state.fsbi.TotalRowContent())
256 }
257
258 func (state *State) displayLocks() {
259         state.screen.PrintAt(0, 2, state.tlwsbt.Headings())
260
261         // print out the data
262         max_rows := state.screen.Height() - 3
263         row_content := state.tlwsbt.RowContent(max_rows)
264
265         // print out rows
266         for k := range row_content {
267                 y := 3 + k
268                 state.screen.PrintAt(0, y, row_content[k])
269         }
270         // print out empty rows
271         for k := len(row_content); k < (state.screen.Height() - 3); k++ {
272                 y := 3 + k
273                 if y < state.screen.Height()-1 {
274                         state.screen.PrintAt(0, y, state.tlwsbt.EmptyRowContent())
275                 }
276         }
277
278         // print out the totals at the bottom
279         state.screen.PrintAt(0, state.screen.Height()-1, state.tlwsbt.TotalRowContent())
280 }
281
282 // do we want to show all p_s data?
283 func (state State) WantRelativeStats() bool {
284         return state.want_relative_stats
285 }
286
287 // set if we want data from when we started/reset stats.
288 func (state *State) SetWantRelativeStats(want_relative_stats bool) {
289         state.want_relative_stats = want_relative_stats
290
291         state.fsbi.SetWantRelativeStats(want_relative_stats)
292         state.tlwsbt.SetWantRelativeStats(state.want_relative_stats)
293         state.tiwsbt.SetWantRelativeStats(state.want_relative_stats)
294
295         state.Display()
296 }
297
298 // if there's a better way of doing this do it better ...
299 func now_hhmmss() string {
300         t := time.Now()
301         return fmt.Sprintf("%2d:%02d:%02d", t.Hour(), t.Minute(), t.Second())
302 }
303
304 // record the latest screen size
305 func (state *State) ScreenSetSize(width, height int) {
306         state.screen.SetSize(width, height)
307 }
308
309 // clean up screen and disconnect database
310 func (state *State) Cleanup() {
311         state.screen.Close()
312         if state.dbh != nil {
313                 _ = state.dbh.Close()
314         }
315 }