-
Notifications
You must be signed in to change notification settings - Fork 1
/
table_metric.go
48 lines (41 loc) · 1.05 KB
/
table_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
package bingodb
import (
"github.com/zoyi/skiplist/lazy"
"time"
)
type TableMetrics struct {
source *Table
output *Table
ttl time.Duration
interval time.Duration
}
func NewTableMetrics(
source *Table,
output *Table,
ttl int64,
interval int64,
) *TableMetrics {
metrics := &TableMetrics{
source: source,
output: output,
ttl: time.Millisecond * time.Duration(ttl),
interval: time.Millisecond * time.Duration(interval)}
go metrics.run()
return metrics
}
func (metrics *TableMetrics) put(hash interface{}, skipList *lazyskiplist.SkipList) bool {
data := Data{}
hashKey := metrics.source.primaryKey.hashKey.Name
data[hashKey] = hash
data[metrics.source.metricsConfig.Count] = skipList.Size()
data[metrics.source.metricsConfig.Time] = time.Now().Unix() * 1000
data[metrics.source.metricsConfig.ExpireKey] = time.Now().Add(metrics.ttl).Unix() * 1000
metrics.output.Put(&data, nil)
return true
}
func (metrics *TableMetrics) run() {
for {
metrics.source.PrimaryIndex().Range(metrics.put)
time.Sleep(metrics.interval)
}
}