Commit 94e0d6d4 by 岳巧源

init select api

parent a93e50e3
......@@ -5,13 +5,20 @@ import (
v1 "dev-ops/api/v1"
"dev-ops/internal/consts"
"dev-ops/utility/response"
"fmt"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/frame/g"
"strings"
"time"
)
type InterfaceNameInfo struct {
InterfaceName string
Version string
URI string
Method string
DayCallNum int
}
type sApiList struct{}
func ApiList() *sApiList {
......@@ -24,26 +31,57 @@ func (s *sApiList) GetApiList(ctx context.Context, req *v1.ApiListReq) (interfac
"limit": req.Limit,
"offset_time": req.OffsetTime,
}
apiNumsMap := map[string]int{}
fmt.Println(param)
//业务逻辑 先从mongodb中获取全量的openplatform的接口名称,再依次去mysql数据库中查
interfaceNameList := []string{"/brand/getStandardBrandList"} //mongodb中获取的接口信息
if param["page"] != 0 {
}
//mongodb数据源
item := InterfaceNameInfo{
InterfaceName: "查询标准品牌",
Version: "v1",
URI: "/brand/getStandardBrandList",
Method: "GET",
}
interfaceNameInfoList := []InterfaceNameInfo{item}
for i := 0; i < len(interfaceNameInfoList); i++ {
interfaceNameInfoList[i].DayCallNum = GetNumsByInterfaceName(ctx, interfaceNameInfoList[i].URI)
}
return interfaceNameInfoList, response.CodeSuccess
}
// 查询某api调用总次数
func GetNumsByInterfaceName(ctx context.Context, name string) int {
var interfaceCallNumTotal int = 0
now := time.Now()
dayStr := now.Format("2006-01-02")
redisDayApiKeyKey := consts.API_INTERFACE_DAYCALL_STATISTICS_KEY + "_" + dayStr
apiKeysData, _ := g.Redis("235").Do(ctx, "SMEMBERS", redisDayApiKeyKey) //先获取所有的apiKey
apiKeysData, _ := g.Redis("235").Do(ctx, "SMEMBERS", redisDayApiKeyKey)
interfaceName := strings.ReplaceAll(name, "/", "")
if nil != apiKeysData && !apiKeysData.IsEmpty() {
for _, apiKey := range apiKeysData.Array() {
redisKey := redisDayApiKeyKey + "_" + apiKey.(string)
interfaceNames, _ := g.Redis("235").Do(ctx, "HKEYS", redisKey)
if interfaceNames != nil {
for _, interfaceItem := range interfaceNameList {
if strings.ReplaceAll(interfaceItem, "/", "") ==
}
interfaceCallNum, _ := g.Redis("235").Do(ctx, "HGET", redisKey, interfaceName)
if interfaceCallNum == nil {
return 0
}
interfaceCallNumTotal += interfaceCallNum.Int()
}
}
return interfaceCallNumTotal
}
// 根据apiKey查询其调用的所有接口次数
func GetNumsByApiKey(ctx context.Context, apiKey string, interfaceNames []string) map[string]int {
res := make(map[string]int)
now := time.Now()
dayStr := now.Format("2006-01-02")
redisDayApiKeyKey := consts.API_INTERFACE_DAYCALL_STATISTICS_KEY + "_" + dayStr
redisKey := redisDayApiKeyKey + "_" + apiKey
for _, item := range interfaceNames {
name := strings.ReplaceAll(item, "/", "")
callNum, _ := g.Redis("235").Do(ctx, "HGET", redisKey, name)
if callNum == nil {
continue
}
} else {
//redis中暂无数据
res[item] = callNum.Int()
}
return "ok", response.CodeSuccess
return res
}
......@@ -3,5 +3,10 @@
port = 12018
address = ":12018"
[redis]
[redis.235]
address="192.168.1.235:6379"
db = 0
pass = "icDb29mLy2s"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment