Remove time.Ticker and adjust polling mechanism
[pstop.git] / state / state.go
index 824d9fb..0bacb2e 100644 (file)
@@ -6,6 +6,7 @@ package state
 import (
        "database/sql"
        "fmt"
+       "strings"
        "time"
 
        "github.com/sjmudd/pstop/lib"
@@ -65,7 +66,11 @@ func (state *State) Setup(dbh *sql.DB) {
        state.show = showLatency
        state.tiwsbt.SetWantsLatency(true)
 
+       // get short name (to save space)
        _, hostname := lib.SelectGlobalVariableByVariableName(state.dbh, "HOSTNAME")
+       if index := strings.Index(hostname, "."); index >= 0 {
+               hostname = hostname[0:index]
+       }
        _, mysql_version := lib.SelectGlobalVariableByVariableName(state.dbh, "VERSION")
        _, datadir := lib.SelectGlobalVariableByVariableName(state.dbh, "DATADIR")
        state.SetHostname(hostname)
@@ -75,22 +80,40 @@ func (state *State) Setup(dbh *sql.DB) {
 
 // do a fresh collection of data and then update the initial values based on that.
 func (state *State) ResetDBStatistics() {
-       state.Collect()
-       state.UpdateInitialValues()
+       state.CollectAll()
+       state.SyncReferenceValues()
 }
 
-func (state *State) UpdateInitialValues() {
-       state.fsbi.UpdateInitialValues()
-       state.tlwsbt.UpdateInitialValues()
-       state.tiwsbt.UpdateInitialValues()
+func (state *State) SyncReferenceValues() {
+       start := time.Now()
+       state.fsbi.SyncReferenceValues()
+       state.tlwsbt.SyncReferenceValues()
+       state.tiwsbt.SyncReferenceValues()
+       lib.Logger.Println("state.SyncReferenceValues() took", time.Duration(time.Since(start)).String())
 }
 
-func (state *State) Collect() {
+// collect all initial values on startup / reset
+func (state *State) CollectAll() {
        state.fsbi.Collect(state.dbh)
        state.tlwsbt.Collect(state.dbh)
        state.tiwsbt.Collect(state.dbh)
 }
 
+// Only collect the data we are looking at.
+func (state *State) Collect() {
+       start := time.Now()
+
+       switch state.show {
+       case showLatency, showOps:
+               state.tiwsbt.Collect(state.dbh)
+       case showIO:
+               state.fsbi.Collect(state.dbh)
+       case showLocks:
+               state.tlwsbt.Collect(state.dbh)
+       }
+       lib.Logger.Println("state.Collect() took", time.Duration(time.Since(start)).String())
+}
+
 func (state State) MySQLVersion() string {
        return state.mysql_version
 }
@@ -159,7 +182,7 @@ func (state *State) DisplayNext() {
 
 func (state State) displayHeading() {
        state.displayLine0()
-       state.displayLine1()
+       state.displayDescription()
 }
 
 func (state State) displayLine0() {
@@ -190,17 +213,19 @@ func (state State) displayLine0() {
        state.screen.PrintAt(0, 0, top_line)
 }
 
-func (state State) displayLine1() {
+func (state State) displayDescription() {
+       description := "UNKNOWN"
+
        switch state.show {
        case showLatency, showOps:
-               state.screen.PrintAt(0, 1, state.tiwsbt.Description())
+               description = state.tiwsbt.Description()
        case showIO:
-               state.screen.PrintAt(0, 1, state.fsbi.Description())
+               description = state.fsbi.Description()
        case showLocks:
-               state.screen.PrintAt(0, 1, state.tlwsbt.Description())
-       default:
-               state.screen.PrintAt(0, 1, "UNKNOWN")
+               description = state.tlwsbt.Description()
        }
+
+       state.screen.PrintAt(0, 1, description)
 }
 
 func (state *State) displayOpsOrLatency() {
@@ -287,8 +312,6 @@ func (state *State) SetWantRelativeStats(want_relative_stats bool) {
        state.fsbi.SetWantRelativeStats(want_relative_stats)
        state.tlwsbt.SetWantRelativeStats(state.want_relative_stats)
        state.tiwsbt.SetWantRelativeStats(state.want_relative_stats)
-
-       state.Display()
 }
 
 // if there's a better way of doing this do it better ...