增加内置的统计指标

This commit is contained in:
GoEdgeLab
2021-07-19 20:38:30 +08:00
parent 7e64d44d4c
commit 940e8c46b0
5 changed files with 157 additions and 0 deletions

View File

@@ -5,6 +5,7 @@ type MetricChart struct {
Id uint32 `field:"id"` // ID Id uint32 `field:"id"` // ID
ItemId uint32 `field:"itemId"` // 指标ID ItemId uint32 `field:"itemId"` // 指标ID
Name string `field:"name"` // 名称 Name string `field:"name"` // 名称
Code string `field:"code"` // 代号
Type string `field:"type"` // 图形类型 Type string `field:"type"` // 图形类型
WidthDiv int32 `field:"widthDiv"` // 宽度划分 WidthDiv int32 `field:"widthDiv"` // 宽度划分
Params string `field:"params"` // 图形参数 Params string `field:"params"` // 图形参数
@@ -18,6 +19,7 @@ type MetricChartOperator struct {
Id interface{} // ID Id interface{} // ID
ItemId interface{} // 指标ID ItemId interface{} // 指标ID
Name interface{} // 名称 Name interface{} // 名称
Code interface{} // 代号
Type interface{} // 图形类型 Type interface{} // 图形类型
WidthDiv interface{} // 宽度划分 WidthDiv interface{} // 宽度划分
Params interface{} // 图形参数 Params interface{} // 图形参数

View File

@@ -1,12 +1,15 @@
package setup package setup
import ( import (
"encoding/json"
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const" teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
"github.com/TeaOSLab/EdgeAPI/internal/errors" "github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/go-yaml/yaml" "github.com/go-yaml/yaml"
"github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs" "github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/rands" "github.com/iwind/TeaGo/rands"
"github.com/iwind/TeaGo/types" "github.com/iwind/TeaGo/types"
stringutil "github.com/iwind/TeaGo/utils/string" stringutil "github.com/iwind/TeaGo/utils/string"
@@ -95,6 +98,12 @@ func (this *SQLExecutor) checkData(db *dbs.DB) error {
return err return err
} }
// 检查指标设置
err = this.checkMetricItems(db)
if err != nil {
return err
}
// 更新版本号 // 更新版本号
err = this.updateVersion(db, teaconst.Version) err = this.updateVersion(db, teaconst.Version)
if err != nil { if err != nil {
@@ -245,6 +254,134 @@ func (this *SQLExecutor) checkIPList(db *dbs.DB) error {
return nil return nil
} }
// 检查统计指标
func (this *SQLExecutor) checkMetricItems(db *dbs.DB) error {
var createMetricItem = func(code string,
category string,
name string,
keys []string,
period int,
periodUnit string,
value string,
chartMaps []maps.Map,
) error {
// 检查是否已创建
itemMap, err := db.FindOne("SELECT id FROM edgeMetricItems WHERE code=? LIMIT 1", code)
if err != nil {
return err
}
var itemId int64 = 0
if len(itemMap) == 0 {
keysJSON, err := json.Marshal(keys)
if err != nil {
return err
}
_, err = db.Exec("INSERT INTO edgeMetricItems (isOn, code, category, name, `keys`, period, periodUnit, value, state, isPublic) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", 1, code, category, name, keysJSON, period, periodUnit, value, 1, 1)
if err != nil {
return err
}
// 再次查询
itemMap, err = db.FindOne("SELECT id FROM edgeMetricItems WHERE code=? LIMIT 1", code)
if err != nil {
return err
}
}
itemId = itemMap.GetInt64("id")
// chart
for _, chartMap := range chartMaps {
chartCode := chartMap.GetString("code")
one, err := db.FindOne("SELECT id FROM edgeMetricCharts WHERE itemId=? AND code=? LIMIT 1", itemId, chartCode)
if err != nil {
return err
}
if len(one) == 0 {
_, err = db.Exec("INSERT INTO edgeMetricCharts (itemId, name, code, type, widthDiv, params, isOn, state) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", itemId, chartMap.GetString("name"), chartCode, chartMap.GetString("type"), chartMap.GetInt("widthDiv"), "{}", 1, 1)
if err != nil {
return err
}
}
}
return nil
}
{
err := createMetricItem("ip_requests", serverconfigs.MetricItemCategoryHTTP, "独立IP请求数", []string{"${remoteAddr}"}, 1, "day", "${countRequest}", []maps.Map{
{
"name": "独立IP排行",
"type": "bar",
"widthDiv": 0,
"code": "ip_requests_bar",
},
})
if err != nil {
return err
}
}
{
err := createMetricItem("ip_traffic_out", serverconfigs.MetricItemCategoryHTTP, "独立IP下行流量", []string{"${remoteAddr}"}, 1, "day", "${countTrafficOut}", []maps.Map{
{
"name": "独立IP排行",
"type": "bar",
"widthDiv": 0,
"code": "ip_traffic_out_bar",
},
})
if err != nil {
return err
}
}
{
err := createMetricItem("request_path", serverconfigs.MetricItemCategoryHTTP, "请求路径统计", []string{"${requestPath}"}, 1, "day", "${countRequest}", []maps.Map{
{
"name": "请求路径排行",
"type": "bar",
"widthDiv": 0,
"code": "request_path_bar",
},
})
if err != nil {
return err
}
}
{
err := createMetricItem("request_method", serverconfigs.MetricItemCategoryHTTP, "请求方法统计", []string{"${requestMethod}"}, 1, "day", "${countRequest}", []maps.Map{
{
"name": "请求方法分布",
"type": "pie",
"widthDiv": 2,
"code": "request_method_pie",
},
})
if err != nil {
return err
}
}
{
err := createMetricItem("status", serverconfigs.MetricItemCategoryHTTP, "状态码统计", []string{"${status}"}, 1, "day", "${countRequest}", []maps.Map{
{
"name": "状态码分布",
"type": "pie",
"widthDiv": 2,
"code": "status_pie",
},
})
if err != nil {
return err
}
}
return nil
}
// 更新版本号 // 更新版本号
func (this *SQLExecutor) updateVersion(db *dbs.DB, version string) error { func (this *SQLExecutor) updateVersion(db *dbs.DB, version string) error {
stmt, err := db.Prepare("SELECT COUNT(*) FROM edgeVersions") stmt, err := db.Prepare("SELECT COUNT(*) FROM edgeVersions")

View File

@@ -17,3 +17,21 @@ func TestSQLExecutor_Run(t *testing.T) {
} }
t.Log("ok") t.Log("ok")
} }
func TestSQLExecutor_checkMetricItems(t *testing.T) {
executor := NewSQLExecutor(&dbs.DBConfig{
Driver: "mysql",
Prefix: "edge",
Dsn: "root:123456@tcp(127.0.0.1:3306)/db_edge_new?charset=utf8mb4&multiStatements=true",
})
db, err := dbs.NewInstanceFromConfig(executor.dbConfig)
if err != nil {
t.Fatal(err)
}
err = executor.checkMetricItems(db)
if err != nil {
t.Fatal(err)
}
t.Log("ok")
}