Fix brokenness with mixing strings and parameters
[pstop.git] / p_s / events_waits_summary_global_by_event_name / public.go
1 // p_s - library routines for pstop.
2 //
3 // This file contains the library routines for managing the
4 // events_waits_summary_global_by_event_name table.
5 package events_waits_summary_global_by_event_name
6
7 import (
8         "database/sql"
9         "fmt"
10         "time"
11
12         "github.com/sjmudd/pstop/lib"
13         "github.com/sjmudd/pstop/p_s"
14 )
15
16 // a table of rows
17 type Object struct {
18         p_s.RelativeStats
19         p_s.InitialTime
20         want_latency bool
21         initial      table_rows // initial data for relative values
22         current      table_rows // last loaded values
23         results      table_rows // results (maybe with subtraction)
24         totals       table_row  // totals of results
25 }
26
27 func (t *Object) SetWantsLatency(want_latency bool) {
28         t.want_latency = want_latency
29 }
30
31 func (t Object) 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 *Object) Collect(dbh *sql.DB) {
39         start := time.Now()
40         // lib.Logger.Println("Object.Collect() BEGIN")
41         t.current = select_rows(dbh)
42         lib.Logger.Println("t.current collected", len(t.current), "row(s) from SELECT")
43
44         if len(t.initial) == 0 && len(t.current) > 0 {
45                 lib.Logger.Println("t.initial: copying from t.current (initial setup)")
46                 t.initial = make(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: copying from t.current (data needs refreshing)")
53                 t.initial = make(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.initial.totals():", t.initial.totals())
62         lib.Logger.Println("t.current.totals():", t.current.totals())
63         // lib.Logger.Println("t.results:", t.results)
64         // lib.Logger.Println("t.totals:", t.totals)
65         lib.Logger.Println("Object.Collect() END, took:", time.Duration(time.Since(start)).String())
66 }
67
68 func (t *Object) make_results() {
69         // lib.Logger.Println( "- t.results set from t.current" )
70         t.results = make(table_rows, len(t.current))
71         copy(t.results, t.current)
72         if t.WantRelativeStats() {
73                 // lib.Logger.Println( "- subtracting t.initial from t.results as WantRelativeStats()" )
74                 t.results.subtract(t.initial)
75         }
76
77         // lib.Logger.Println( "- sorting t.results" )
78         t.results.Sort()
79         // lib.Logger.Println( "- collecting t.totals from t.results" )
80         t.totals = t.results.totals()
81 }
82
83 // reset the statistics to current values
84 func (t *Object) SyncReferenceValues() {
85         // lib.Logger.Println( "Object.SyncReferenceValues() BEGIN" )
86
87         t.SetNow()
88         t.initial = make(table_rows, len(t.current))
89         copy(t.initial, t.current)
90
91         t.make_results()
92
93         // lib.Logger.Println( "Object.SyncReferenceValues() END" )
94 }
95
96 func (t Object) EmptyRowContent() string {
97         return t.emptyRowContent()
98 }
99
100 func (t *Object) Headings() string {
101         var r table_row
102
103         return r.headings()
104 }
105
106 func (t Object) RowContent(max_rows int) []string {
107         rows := make([]string, 0, max_rows)
108
109         for i := range t.results {
110                 if i < max_rows {
111                         rows = append(rows, t.results[i].row_content(t.totals))
112                 }
113         }
114
115         return rows
116 }
117
118 func (t Object) emptyRowContent() string {
119         var r table_row
120
121         return r.row_content(r)
122 }
123
124 func (t Object) TotalRowContent() string {
125         return t.totals.row_content(t.totals)
126 }
127
128 func (t Object) Description() string {
129         count := t.count_rows()
130         return fmt.Sprintf("Mutex Latency (events_waits_summary_global_by_event_name) %d rows", count)
131 }
132
133 func (t Object) count_rows() int {
134         var count int
135         for row := range t.results {
136                 if t.results[row].SUM_TIMER_WAIT > 0 {
137                         count++
138                 }
139         }
140         return count
141 }