monitor.go 2.2 KB
Newer Older
Y
Your Name 已提交
1
package monitorread
E
eoLinker API Management 已提交
2 3 4

import (
	"fmt"
黄孟柱 已提交
5 6 7 8
	redis_manager "github.com/eolinker/goku-api-gateway/common/redis-manager"
	"github.com/eolinker/goku-api-gateway/server/dao/console-mysql/dao-monitor"
	"github.com/eolinker/goku-api-gateway/server/entity"
	"github.com/eolinker/goku-api-gateway/server/monitor/monitor-key"
E
eoLinker API Management 已提交
9 10 11
	"strconv"
	"time"
)
Y
Your Name 已提交
12

E
eoLinker API Management 已提交
13
var (
Y
Your Name 已提交
14
	period = 30 * time.Second
E
eoLinker API Management 已提交
15 16
)

Y
Your Name 已提交
17
//SetPeriod 设置更新周期
Y
Your Name 已提交
18 19
func SetPeriod(sec int) {
	period = time.Duration(sec) * time.Second
E
eoLinker API Management 已提交
20
}
Y
Your Name 已提交
21 22

//InitMonitorRead init monitor read
E
eoLinker API Management 已提交
23
func InitMonitorRead(clusters []*entity.Cluster) error {
Y
Your Name 已提交
24 25 26 27
	for _, c := range clusters {
		_, has := redis_manager.Get(c.Name)
		if !has {
			return fmt.Errorf("no redis for cluster:%s", c.Name)
E
eoLinker API Management 已提交
28 29
		}
	}
Y
Your Name 已提交
30
	for _, c := range clusters {
Y
Your Name 已提交
31
		go doLoopForCluster(c.Name, c.ID)
E
eoLinker API Management 已提交
32 33 34 35
	}
	return nil
}

Y
Your Name 已提交
36
func doLoopForCluster(clusterName string, clusterID int) {
E
eoLinker API Management 已提交
37

Y
Your Name 已提交
38
	t := time.NewTimer(period)
E
eoLinker API Management 已提交
39

Y
Your Name 已提交
40
	for {
E
eoLinker API Management 已提交
41
		select {
Y
Your Name 已提交
42 43
		case <-t.C:
			{
Y
Your Name 已提交
44
				read(clusterName, clusterID, time.Now())
Y
Your Name 已提交
45
			}
E
eoLinker API Management 已提交
46 47 48 49 50
		}
		t.Reset(period)
	}

}
Y
Your Name 已提交
51
func read(clusterName string, clusterID int, t time.Time) {
Y
Your Name 已提交
52 53
	hour := t.Format("2006010215")
	now := t.Format("2006-01-02 15:04:05")
E
eoLinker API Management 已提交
54

Y
Your Name 已提交
55
	hourValue, _ := strconv.Atoi(hour)
E
eoLinker API Management 已提交
56 57

	// 包含 strate == ""
Y
Your Name 已提交
58
	strategyIds, err := readStrategyID(hour, clusterName)
Y
Your Name 已提交
59
	if err != nil {
E
eoLinker API Management 已提交
60 61
		return
	}
Y
Your Name 已提交
62
	for _, strategyID := range strategyIds {
E
eoLinker API Management 已提交
63

Y
Your Name 已提交
64
		apiIds, err := readAPIId(hour, clusterName, strategyID)
Y
Your Name 已提交
65
		if err != nil {
E
eoLinker API Management 已提交
66 67 68
			continue
		}

Y
Your Name 已提交
69
		for _, apiID := range apiIds {
E
eoLinker API Management 已提交
70

Y
Your Name 已提交
71
			valus, err := readValue(hour, clusterName, strategyID, apiID)
Y
Your Name 已提交
72
			if err != nil {
E
eoLinker API Management 已提交
73 74
				continue
			}
Y
Your Name 已提交
75
			apiID, _ := strconv.Atoi(apiID)
E
eoLinker API Management 已提交
76

Y
Your Name 已提交
77
			dao_monitor.Save(strategyID, apiID, clusterID, hourValue, now, valus)
E
eoLinker API Management 已提交
78 79 80 81
		}
	}

}
Y
Your Name 已提交
82 83
func readStrategyID(now, cluster string) ([]string, error) {
	key := monitorkey.StrategyMapKey(cluster, now)
Y
Your Name 已提交
84
	conn, _ := redis_manager.Get(cluster)
E
eoLinker API Management 已提交
85 86 87
	return conn.HKeys(key).Result()

}
Y
Your Name 已提交
88 89
func readAPIId(now, cluster, strategyID string) ([]string, error) {
	key := monitorkey.APIMapKey(cluster, strategyID, now)
Y
Your Name 已提交
90
	conn, _ := redis_manager.Get(cluster)
E
eoLinker API Management 已提交
91 92
	return conn.HKeys(key).Result()
}
Y
Your Name 已提交
93
func readValue(now, cluster, strategyID string, apiID string) (map[string]string, error) {
Y
Your Name 已提交
94
	conn, _ := redis_manager.Get(cluster)
Y
Your Name 已提交
95
	key := monitorkey.APIValueKey(cluster, strategyID, apiID, now)
E
eoLinker API Management 已提交
96
	return conn.HGetAll(key).Result()
Y
Your Name 已提交
97
}