408f445bf5f8c873d2fa00281df50a35e0b67899
[pstop.git] / p_s / file_summary_by_instance / public.go
1 // p_s - library routines for pstop.
2 //
3 // This file contains the library routines for managing the
4 // file_summary_by_instance table.
5 package file_summary_by_instance
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         initial          table_rows
21         current          table_rows
22         results          table_rows
23         totals           table_row
24         global_variables map[string]string
25 }
26
27 // reset the statistics to current values
28 func (t *Object) SyncReferenceValues() {
29         t.SetNow()
30         t.initial = make(table_rows, len(t.current))
31         copy(t.initial, t.current)
32
33         t.results = make(table_rows, len(t.current))
34         copy(t.results, t.current)
35
36         if t.WantRelativeStats() {
37                 t.results.subtract(t.initial) // should be 0 if relative
38         }
39
40         t.results.sort()
41         t.totals = t.results.totals()
42 }
43
44 // Collect data from the db, then merge it in.
45 func (t *Object) Collect(dbh *sql.DB) {
46         start := time.Now()
47         // UPDATE current from db handle
48         t.current = merge_by_table_name(select_rows(dbh), t.global_variables)
49
50         // copy in initial data if it was not there
51         if len(t.initial) == 0 && len(t.current) > 0 {
52                 t.initial = make(table_rows, len(t.current))
53                 copy(t.initial, t.current)
54         }
55
56         // check for reload initial characteristics
57         if t.initial.needs_refresh(t.current) {
58                 t.initial = make(table_rows, len(t.current))
59                 copy(t.initial, t.current)
60         }
61
62         // update results to current value
63         t.results = make(table_rows, len(t.current))
64         copy(t.results, t.current)
65
66         // make relative if need be
67         if t.WantRelativeStats() {
68                 t.results.subtract(t.initial)
69         }
70
71         // sort the results
72         t.results.sort()
73
74         // setup the totals
75         t.totals = t.results.totals()
76         lib.Logger.Println("Object.Collect() took:", time.Duration(time.Since(start)).String())
77 }
78
79 // return the headings for a table
80 func (t Object) Headings() string {
81         var r table_row
82
83         return r.headings()
84 }
85
86 // return the rows we need for displaying
87 func (t Object) RowContent(max_rows int) []string {
88         rows := make([]string, 0, max_rows)
89
90         for i := range t.results {
91                 if i < max_rows {
92                         rows = append(rows, t.results[i].row_content(t.totals))
93                 }
94         }
95
96         return rows
97 }
98
99 // return all the totals
100 func (t Object) TotalRowContent() string {
101         return t.totals.row_content(t.totals)
102 }
103
104 // return an empty string of data (for filling in)
105 func (t Object) EmptyRowContent() string {
106         var emtpy table_row
107         return emtpy.row_content(emtpy)
108 }
109
110 func (t Object) Description() string {
111         count := t.count_rows()
112         return fmt.Sprintf("I/O Latency by File (file_summary_by_instance) %4d row(s)    ", count)
113 }
114
115 // create a new structure and include various variable values:
116 // - datadir, relay_log
117 // There's no checking that these are actually provided!
118 func NewFileSummaryByInstance(global_variables map[string]string) *Object {
119         n := new(Object)
120
121         n.global_variables = global_variables
122
123         return n
124 }
125
126 func (t Object) count_rows() int {
127         var count int
128         for row := range t.results {
129                 if t.results[row].SUM_TIMER_WAIT > 0 {
130                         count++
131                 }
132         }
133         return count
134 }