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
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
56 additions
and
13 deletions
internal/service/apiList.go
manifest/config/config.toml
internal/service/apiList.go
View file @
94e0d6d4
...
@@ -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
.
CodeSucces
s
return
re
s
}
}
manifest/config/config.toml
View file @
94e0d6d4
...
@@ -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"
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