Commit 94e0d6d4 by 岳巧源

init select api

parent a93e50e3
...@@ -5,13 +5,20 @@ import ( ...@@ -5,13 +5,20 @@ import (
v1 "dev-ops/api/v1" v1 "dev-ops/api/v1"
"dev-ops/internal/consts" "dev-ops/internal/consts"
"dev-ops/utility/response" "dev-ops/utility/response"
"fmt"
"github.com/gogf/gf/v2/errors/gcode" "github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/frame/g"
"strings" "strings"
"time" "time"
) )
type InterfaceNameInfo struct {
InterfaceName string
Version string
URI string
Method string
DayCallNum int
}
type sApiList struct{} type sApiList struct{}
func ApiList() *sApiList { func ApiList() *sApiList {
...@@ -24,26 +31,57 @@ func (s *sApiList) GetApiList(ctx context.Context, req *v1.ApiListReq) (interfac ...@@ -24,26 +31,57 @@ func (s *sApiList) GetApiList(ctx context.Context, req *v1.ApiListReq) (interfac
"limit": req.Limit, "limit": req.Limit,
"offset_time": req.OffsetTime, "offset_time": req.OffsetTime,
} }
apiNumsMap := map[string]int{} if param["page"] != 0 {
fmt.Println(param) }
//业务逻辑 先从mongodb中获取全量的openplatform的接口名称,再依次去mysql数据库中查 //mongodb数据源
interfaceNameList := []string{"/brand/getStandardBrandList"} //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() now := time.Now()
dayStr := now.Format("2006-01-02") dayStr := now.Format("2006-01-02")
redisDayApiKeyKey := consts.API_INTERFACE_DAYCALL_STATISTICS_KEY + "_" + dayStr 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() { if nil != apiKeysData && !apiKeysData.IsEmpty() {
for _, apiKey := range apiKeysData.Array() { for _, apiKey := range apiKeysData.Array() {
redisKey := redisDayApiKeyKey + "_" + apiKey.(string) redisKey := redisDayApiKeyKey + "_" + apiKey.(string)
interfaceNames, _ := g.Redis("235").Do(ctx, "HKEYS", redisKey) interfaceCallNum, _ := g.Redis("235").Do(ctx, "HGET", redisKey, interfaceName)
if interfaceNames != nil { if interfaceCallNum == nil {
for _, interfaceItem := range interfaceNameList { return 0
if strings.ReplaceAll(interfaceItem, "/", "") ==
} }
interfaceCallNumTotal += interfaceCallNum.Int()
} }
} }
} else { return interfaceCallNumTotal
//redis中暂无数据 }
// 根据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
}
res[item] = callNum.Int()
} }
return "ok", response.CodeSuccess return res
} }
...@@ -3,5 +3,10 @@ ...@@ -3,5 +3,10 @@
port = 12018 port = 12018
address = ":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