Commit 567e68b3 by 岳巧源

add get api list from mongodb

parent 94e0d6d4
......@@ -2,4 +2,7 @@ module dev-ops
go 1.15
require github.com/gogf/gf/v2 v2.1.1
require (
github.com/gogf/gf/v2 v2.1.1
go.mongodb.org/mongo-driver v1.10.6
)
......@@ -9,4 +9,5 @@ const (
API_INTERFACE_MINCALLMAX_STATISTICS_KEY = "api_apiKey_interface_minCallMax_key"
// 接口每天统计情况redisKey
API_INTERFACE_DAYCALL_STATISTICS_KEY = "api_apiKey_interface_dayCall_statistics"
OPEN_PLATFORM_GROUP_ID = 337
)
......@@ -7,16 +7,19 @@ import (
"dev-ops/utility/response"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/frame/g"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"strings"
"time"
)
type InterfaceNameInfo struct {
InterfaceName string
Version string
URI string
Method string
DayCallNum int
InterfaceName string `json:"interface_name"`
Version string `json:"version"`
URI string `json:"uri"`
Method string `json:"method"`
DayCallNum int `json:"day_call_num"`
}
type sApiList struct{}
......@@ -34,13 +37,11 @@ func (s *sApiList) GetApiList(ctx context.Context, req *v1.ApiListReq) (interfac
if param["page"] != 0 {
}
//mongodb数据源
item := InterfaceNameInfo{
InterfaceName: "查询标准品牌",
Version: "v1",
URI: "/brand/getStandardBrandList",
Method: "GET",
interfaceNameInfoList, err := GetInterfaceNameListFromYapi(ctx)
if err != nil {
//无法从mongodb中获取完整接口列表
return nil, response.CodeInternal
}
interfaceNameInfoList := []InterfaceNameInfo{item}
for i := 0; i < len(interfaceNameInfoList); i++ {
interfaceNameInfoList[i].DayCallNum = GetNumsByInterfaceName(ctx, interfaceNameInfoList[i].URI)
}
......@@ -85,3 +86,47 @@ func GetNumsByApiKey(ctx context.Context, apiKey string, interfaceNames []string
}
return res
}
func GetInterfaceNameListFromYapi(ctx context.Context) ([]InterfaceNameInfo, error) {
interfaceNameInfoList := make([]InterfaceNameInfo, 0)
address, _ := g.Cfg().Get(ctx, "mongodb.address")
client, err := mongo.Connect(ctx, options.Client().ApplyURI(address.String()))
if err != nil {
return nil, err
}
if err := client.Ping(ctx, nil); err != nil {
return nil, err
}
cur, err := client.Database("yapi").Collection("project").Find(ctx, bson.D{{"group_id", consts.OPEN_PLATFORM_GROUP_ID}})
if err != nil {
return nil, err
}
var resultsOfProject []bson.M
var projectIds []int32 = []int32{}
if err := cur.All(ctx, &resultsOfProject); err != nil {
return nil, err
}
for _, result := range resultsOfProject {
id, _ := result["_id"].(int32)
projectIds = append(projectIds, id)
}
for _, projectId := range projectIds {
var resultsOfInterface []bson.M
cursor, err := client.Database("yapi").Collection("interface").Find(ctx, bson.D{{"project_id", projectId}})
if err != nil {
return nil, err
}
if err := cursor.All(ctx, &resultsOfInterface); err != nil {
return nil, err
}
for _, result := range resultsOfInterface {
interfaceNameInfo := InterfaceNameInfo{}
interfaceNameInfo.InterfaceName, _ = result["title"].(string)
interfaceNameInfo.Version = "v1"
interfaceNameInfo.URI, _ = result["path"].(string)
interfaceNameInfo.Method, _ = result["method"].(string)
interfaceNameInfoList = append(interfaceNameInfoList, interfaceNameInfo)
}
}
return interfaceNameInfoList, nil
}
......@@ -10,3 +10,5 @@
pass = "icDb29mLy2s"
[mongodb]
address = "mongodb://yapi:yapi231@192.168.1.237:27017/yapi"
\ No newline at end of file
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