Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
open-source
/
dev-ops
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
94e0d6d4
authored
Jun 25, 2024
by
岳巧源
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
init select api
parent
a93e50e3
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
57 additions
and
14 deletions
internal/service/apiList.go
manifest/config/config.toml
internal/service/apiList.go
View file @
94e0d6d4
...
...
@@ -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
.
CodeSucces
s
return
re
s
}
manifest/config/config.toml
View file @
94e0d6d4
...
...
@@ -3,5 +3,10 @@
port
=
12018
address
=
":12018"
[redis]
[redis.235]
address
=
"192.168.1.235:6379"
db
=
0
pass
=
"icDb29mLy2s"
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment