Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
L
LAE
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
文靖昊
LAE
Commits
f1fb0cb1
Commit
f1fb0cb1
authored
Nov 08, 2024
by
tinywell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
在线率排名接口
parent
f50cc798
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
141 additions
and
9 deletions
+141
-9
http_tools.py
src/agent/http_tools.py
+17
-9
server.py
src/mock/server.py
+124
-0
No files found.
src/agent/http_tools.py
View file @
f1fb0cb1
...
@@ -74,39 +74,47 @@ class MonitorClient(BaseHttpClient):
...
@@ -74,39 +74,47 @@ class MonitorClient(BaseHttpClient):
# 示例:添加新的数据接口客户端
# 示例:添加新的数据接口客户端
class
RateClient
(
BaseHttpClient
):
class
RateClient
(
BaseHttpClient
):
"""在线率查询客户端"""
"""在线率查询客户端"""
async
def
query_rates
(
self
,
params
:
Dict
)
->
BaseResponse
[
Dict
]:
async
def
query_rates
(
self
,
areacode
:
str
,
startDate
:
str
,
endDate
:
str
)
->
BaseResponse
[
Dict
]:
"""异步查询在线率信息"""
"""异步查询在线率信息"""
data
=
await
self
.
_request_async
(
data
=
await
self
.
_request_async
(
"POST"
,
"POST"
,
"/api/device/rate"
,
"/api/device/rate"
,
json
=
params
json
=
{
'areaCode'
:
areacode
,
'startDate'
:
startDate
,
'endDate'
:
endDate
}
)
)
return
BaseResponse
[
Dict
](
**
data
)
return
BaseResponse
[
Dict
](
**
data
)
def
query_rates_sync
(
self
,
params
:
Dict
)
->
BaseResponse
[
Dict
]:
def
query_rates_sync
(
self
,
areacode
:
str
,
startDate
:
str
,
endDate
:
str
)
->
BaseResponse
[
Dict
]:
"""同步查询在线率信息"""
"""同步查询在线率信息"""
data
=
self
.
_request_sync
(
data
=
self
.
_request_sync
(
"POST"
,
"POST"
,
"/api/device/rate"
,
"/api/device/rate"
,
json
=
params
json
=
{
'areaCode'
:
areacode
,
'startDate'
:
startDate
,
'endDate'
:
endDate
}
)
)
return
BaseResponse
[
Dict
](
**
data
)
return
BaseResponse
[
Dict
](
**
data
)
def
query_rates_ranking_sync
(
self
,
params
:
Dic
t
)
->
BaseResponse
[
Dict
]:
def
query_rates_ranking_sync
(
self
,
rank_type
:
in
t
)
->
BaseResponse
[
Dict
]:
"""同步查询在线率排名信息"""
"""同步查询在线率排名信息"""
data
=
self
.
_request_sync
(
data
=
self
.
_request_sync
(
"POST"
,
"POST"
,
"/api/device/rate/ranking"
,
"/api/device/rate/ranking"
,
json
=
params
json
=
{
'type'
:
rank_type
}
)
)
return
BaseResponse
[
Dict
](
**
data
)
return
BaseResponse
[
Dict
](
**
data
)
async
def
query_rates_ranking
(
self
,
params
:
Dic
t
)
->
BaseResponse
[
Dict
]:
async
def
query_rates_ranking
(
self
,
rank_type
:
in
t
)
->
BaseResponse
[
Dict
]:
"""异步查询在线率排名信息"""
"""异步查询在线率排名信息"""
data
=
await
self
.
_request_async
(
data
=
await
self
.
_request_async
(
"POST"
,
"POST"
,
"/api/device/rate/ranking"
,
"/api/device/rate/ranking"
,
json
=
params
json
=
{
'type'
:
rank_type
}
)
)
return
BaseResponse
[
Dict
](
**
data
)
return
BaseResponse
[
Dict
](
**
data
)
...
@@ -127,7 +135,7 @@ async def example_async_usage():
...
@@ -127,7 +135,7 @@ async def example_async_usage():
# 在线率查询示例
# 在线率查询示例
rate_client
=
RateClient
()
rate_client
=
RateClient
()
try
:
try
:
response
=
await
rate_client
.
query_rates
(
{
"region"
:
"湖南"
,
"type"
:
"residential"
}
)
response
=
await
rate_client
.
query_rates
(
"520100"
,
"2024-01-01"
,
"2024-01-31"
)
if
response
.
resultcode
==
1
:
if
response
.
resultcode
==
1
:
print
(
f
"在线率数据: {response.resultdata}"
)
print
(
f
"在线率数据: {response.resultdata}"
)
except
httpx
.
HTTPError
as
e
:
except
httpx
.
HTTPError
as
e
:
...
...
src/mock/server.py
View file @
f1fb0cb1
...
@@ -235,6 +235,130 @@ async def query_device_rate(request: DeviceRateRequest):
...
@@ -235,6 +235,130 @@ async def query_device_rate(request: DeviceRateRequest):
print
(
f
"查询设备在线率失败, 错误信息: {str(e)}"
)
print
(
f
"查询设备在线率失败, 错误信息: {str(e)}"
)
raise
HTTPException
(
status_code
=
500
,
detail
=
str
(
e
))
raise
HTTPException
(
status_code
=
500
,
detail
=
str
(
e
))
# 添加厂商数据结构
manufacturer_data
=
[
{
"name"
:
"中物九联"
,
"fullname"
:
"中物九联(成都)科技有限责任公司"
,
"base_rate"
:
(
95
,
100
)},
{
"name"
:
"沃特兰德"
,
"fullname"
:
"北京沃特兰德科技有限公司"
,
"base_rate"
:
(
95
,
100
)},
{
"name"
:
"川德克斯"
,
"fullname"
:
"四川德克斯科技有限公司"
,
"base_rate"
:
(
95
,
100
)},
{
"name"
:
"武汉中地恒"
,
"fullname"
:
"武汉中地恒达科技有限公司"
,
"base_rate"
:
(
95
,
100
)},
{
"name"
:
"智联时空"
,
"fullname"
:
"深圳市智联时空科技有限公司"
,
"base_rate"
:
(
95
,
100
)},
{
"name"
:
"成都来回"
,
"fullname"
:
"成都来回科技有限公司"
,
"base_rate"
:
(
90
,
99.5
)},
{
"name"
:
"诚飞科技"
,
"fullname"
:
"四川诚飞信息科技有限公司"
,
"base_rate"
:
(
90
,
99
)},
{
"name"
:
"京工高科"
,
"fullname"
:
"京工高科成都光电有限公司"
,
"base_rate"
:
(
90
,
98
)},
{
"name"
:
"中移物联"
,
"fullname"
:
"中移物联网有限公司"
,
"base_rate"
:
(
85
,
95
)},
{
"name"
:
"华测检测"
,
"fullname"
:
"华测检测认证集团股份有限公司"
,
"base_rate"
:
(
80
,
95
)},
{
"name"
:
"中地装备"
,
"fullname"
:
"中地装备工程有限公司"
,
"base_rate"
:
(
75
,
90
)},
{
"name"
:
"中煤科工"
,
"fullname"
:
"中煤科工集团重庆研究院有限公司"
,
"base_rate"
:
(
70
,
90
)},
{
"name"
:
"地质云"
,
"fullname"
:
"地质云科技有限公司"
,
"base_rate"
:
(
60
,
85
)},
]
province_data
=
[
{
"name"
:
"贵州"
,
"fullname"
:
"贵州省"
,
"base_rate"
:
(
75
,
95
)},
{
"name"
:
"四川"
,
"fullname"
:
"四川省"
,
"base_rate"
:
(
75
,
95
)},
{
"name"
:
"云南"
,
"fullname"
:
"云南省"
,
"base_rate"
:
(
75
,
95
)},
{
"name"
:
"重庆"
,
"fullname"
:
"重庆市"
,
"base_rate"
:
(
75
,
95
)},
{
"name"
:
"陕西"
,
"fullname"
:
"陕西省"
,
"base_rate"
:
(
75
,
95
)},
{
"name"
:
"甘肃"
,
"fullname"
:
"甘肃省"
,
"base_rate"
:
(
75
,
95
)},
{
"name"
:
"青海"
,
"fullname"
:
"青海省"
,
"base_rate"
:
(
75
,
95
)},
{
"name"
:
"宁夏"
,
"fullname"
:
"宁夏回族自治区"
,
"base_rate"
:
(
75
,
95
)},
{
"name"
:
"新疆"
,
"fullname"
:
"新疆维吾尔自治区"
,
"base_rate"
:
(
75
,
95
)},
]
# 添加请求模型
class
DeviceRateRankingRequest
(
BaseModel
):
type
:
str
# "1" 为省份排名,"2" 为厂商排名
class
RankingItem
(
BaseModel
):
name
:
str
fullname
:
str
onlineRate
:
float
class
RankingResponse
(
BaseModel
):
type
:
int
=
1
resultcode
:
int
=
1
message
:
str
=
""
resultdata
:
List
[
RankingItem
]
otherinfo
:
Optional
[
Dict
]
=
None
def
generate_rate_ranking_data_by_manufacturer
()
->
List
[
RankingItem
]:
"""生成厂商设备在线率排名数据"""
result_data
=
[]
# 为每个厂商生成在线率
for
mfr
in
manufacturer_data
:
base_min
,
base_max
=
mfr
[
"base_rate"
]
rate
=
round
(
random
.
uniform
(
base_min
,
base_max
),
2
)
result_data
.
append
(
RankingItem
(
name
=
mfr
[
"name"
],
fullname
=
mfr
[
"fullname"
],
onlineRate
=
rate
))
# 按在线率降序排序
result_data
.
sort
(
key
=
lambda
x
:
x
.
onlineRate
,
reverse
=
True
)
return
result_data
def
generate_rate_ranking_data_by_province
()
->
List
[
RankingItem
]:
"""生成省份设备在线率排名数据"""
result_data
=
[]
# 为每个省份生成在线率
for
province
in
province_data
:
# 生成一个基准在线率(75-95之间)
rate
=
round
(
random
.
uniform
(
75
,
95
),
2
)
result_data
.
append
(
RankingItem
(
name
=
province
[
"name"
],
fullname
=
province
[
"fullname"
],
onlineRate
=
rate
))
# 按在线率降序排序
result_data
.
sort
(
key
=
lambda
x
:
x
.
onlineRate
,
reverse
=
True
)
return
result_data
@app.post
(
"/api/device/rate/ranking"
,
response_model
=
RankingResponse
)
async
def
query_device_rate_ranking
(
request
:
DeviceRateRankingRequest
):
"""
查询设备在线率排名
Args:
request: DeviceRateRankingRequest
- type: 排名类型 "1"省份排名,"2"厂商排名
- startDate: 开始日期(可选)
- endDate: 结束日期(可选)
- areaCode: 区域代码(可选)
Returns:
RankingResponse:
- type: 固定值1
- resultcode: 1成功,0失败
- message: 响应消息
- resultdata: 排名数据列表
- otherinfo: 其他信息(可选)
"""
print
(
f
"进入 query_device_rate_ranking 接口, 查询参数: {request}"
)
try
:
if
request
.
type
==
"2"
:
# 厂商排名
result_data
=
generate_rate_ranking_data_by_manufacturer
()
else
:
# 省份排名
result_data
=
generate_rate_ranking_data_by_province
()
response
=
RankingResponse
(
type
=
1
,
resultcode
=
1
,
message
=
""
,
resultdata
=
result_data
,
otherinfo
=
None
)
print
(
f
"查询设备在线率排名成功, 返回 {len(result_data)} 条数据"
)
return
response
except
Exception
as
e
:
print
(
f
"查询设备在线率排名失败, 错误信息: {str(e)}"
)
raise
HTTPException
(
status_code
=
500
,
detail
=
str
(
e
))
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
import
uvicorn
import
uvicorn
uvicorn
.
run
(
app
,
host
=
"0.0.0.0"
,
port
=
5001
)
uvicorn
.
run
(
app
,
host
=
"0.0.0.0"
,
port
=
5001
)
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