-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetric.go
182 lines (152 loc) · 5.53 KB
/
metric.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"github.com/rach/pome/Godeps/_workspace/src/github.com/jmoiron/sqlx"
_ "github.com/rach/pome/Godeps/_workspace/src/github.com/lib/pq"
"math"
"time"
)
type MetricList struct {
TableBloat map[string]Metric `json:"table_bloat"`
IndexBloat map[string]Metric `json:"index_bloat"`
TopBloatIndexRatio []Metric `json:"top_index_bloat"`
TopBloatTableRatio []Metric `json:"top_table_bloat"`
TotalTableBloatBytes []Metric `json:"total_table_bloat_bytes"`
TotalIndexBloatBytes []Metric `json:"total_index_bloat_bytes"`
DatabaseSize []Metric `json:"database_size"`
NumberOfConnection []Metric `json:"number_of_connection"`
Version string `json:"version"`
}
type metricFct func(db *sqlx.DB, metrics *MetricList, datafct databaseResultFct, limit int)
type Metric interface {
}
type Metrics []Metric
type numberConnectionMetric struct {
Timestamp int64 `json:"timestamp"`
Count int `db:"count" json:"count"`
}
type databaseSizeMetric struct {
Timestamp int64 `json:"timestamp"`
TableSize int `json:"table_size"`
IndexSize int `json:"index_size"`
TotalSize int `json:"total_size"`
IndexRatio float64 `json:"index_ratio"`
}
type topBloatRatioMetric struct {
Timestamp int64 `json:"timestamp"`
BloatRatio float64 `json:"bloat_ratio"`
}
type totalBloatBytesMetric struct {
Timestamp int64 `json:"timestamp"`
BloatBytes int `json:"bloat_bytes"`
}
type bloatMetric struct {
Timestamp int64 `json:"timestamp"`
BloatBytes int `json:"bloat_bytes"`
BloatRatio float64 `json:"bloat_ratio"`
}
type tableBloatMetric struct {
TableSchema string `json:"table_schema"`
TableName string `json:"table_name"`
Bloat []Metric `json:"data"`
}
type indexBloatMetric struct {
TableSchema string `json:"table_schema"`
TableName string `json:"table_name"`
IndexName string `json:"index_name"`
Bloat []Metric `json:"data"`
}
func GetTimestamp() int64 {
return time.Now().Unix()
}
func appendAndFilter(list []Metric, m Metric, limit int) []Metric {
r := append(list, m)
if len(r) > limit {
r = r[len(r)-limit:]
}
return r
}
func initMapMetric(key string, vm *map[string]Metric, metric Metric) {
if *vm == nil {
*vm = make(map[string]Metric)
}
if _, ok := (*vm)[key]; !ok {
(*vm)[key] = metric
}
}
func indexBloatUpdate(db *sqlx.DB, metrics *MetricList, datafct databaseResultFct, limit int) {
timestamp := GetTimestamp()
results := (datafct(db)).([]IndexBloatDatabaseResult)
var total_bytes int = 0
var top_bloat float64 = 0
// iterate over each row
for _, v := range results {
if v.Schema == "information_schema" {
continue
}
total_bytes += v.BloatBytes
top_bloat = math.Max(top_bloat, v.BloatRatio)
initMapMetric(
v.Key,
&((*metrics).IndexBloat),
indexBloatMetric{
TableSchema: v.Schema,
TableName: v.Table,
IndexName: v.Index})
m := bloatMetric{Timestamp: timestamp, BloatBytes: v.BloatBytes, BloatRatio: v.BloatRatio}
current_val := ((*metrics).IndexBloat[v.Key]).(indexBloatMetric)
tmp_metrics := appendAndFilter(current_val.Bloat, m, limit)
current_val.Bloat = tmp_metrics
(*metrics).IndexBloat[v.Key] = current_val
}
tbrm := topBloatRatioMetric{timestamp, top_bloat}
(*metrics).TopBloatIndexRatio = appendAndFilter((*metrics).TopBloatIndexRatio, tbrm, limit)
tbbm := totalBloatBytesMetric{timestamp, total_bytes}
(*metrics).TotalIndexBloatBytes = appendAndFilter((*metrics).TotalIndexBloatBytes, tbbm, limit)
}
func tableBloatUpdate(db *sqlx.DB, metrics *MetricList, datafct databaseResultFct, limit int) {
timestamp := GetTimestamp()
results := (datafct(db)).([]TableBloatDatabaseResult)
var total_bytes int = 0
var top_bloat float64 = 0
// iterate over each row
for _, v := range results {
if v.Schema == "information_schema" {
continue
}
total_bytes += v.BloatBytes
top_bloat = math.Max(top_bloat, v.BloatRatio)
initMapMetric(
v.Key,
&((*metrics).TableBloat),
tableBloatMetric{
TableSchema: v.Schema,
TableName: v.Table})
m := bloatMetric{Timestamp: timestamp, BloatBytes: v.BloatBytes, BloatRatio: v.BloatRatio}
current_val := ((*metrics).TableBloat[v.Key]).(tableBloatMetric)
tmp_metrics := appendAndFilter(current_val.Bloat, m, limit)
current_val.Bloat = tmp_metrics
(*metrics).TableBloat[v.Key] = current_val
}
tbrm := topBloatRatioMetric{timestamp, top_bloat}
(*metrics).TopBloatTableRatio = appendAndFilter((*metrics).TopBloatTableRatio, tbrm, limit)
tbbm := totalBloatBytesMetric{timestamp, total_bytes}
(*metrics).TotalTableBloatBytes = appendAndFilter((*metrics).TotalTableBloatBytes, tbbm, limit)
}
func databaseSizeUpdate(db *sqlx.DB, metrics *MetricList, datafct databaseResultFct, limit int) {
timestamp := GetTimestamp()
res := (datafct(db)).(DatabaseSizeResult)
met := databaseSizeMetric{timestamp, res.TableSize, res.IndexSize, res.TotalSize, res.IndexRatio}
(*metrics).DatabaseSize = appendAndFilter((*metrics).DatabaseSize, met, limit)
}
func numberOfConnectionUpdate(db *sqlx.DB, metrics *MetricList, datafct databaseResultFct, limit int) {
timestamp := GetTimestamp()
res := (datafct(db)).(NumberOfConnectionResult)
met := numberConnectionMetric{timestamp, res.Count}
(*metrics).NumberOfConnection = appendAndFilter((*metrics).NumberOfConnection, met, limit)
}
func metricScheduler(db *sqlx.DB, metrics *MetricList, mfct metricFct, datafct databaseResultFct, delay int, limit int) {
for {
mfct(db, metrics, datafct, limit)
time.Sleep(time.Duration(delay) * time.Second)
}
}