Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
api
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
bigdata
api
Commits
c02cdaf9
Commit
c02cdaf9
authored
May 27, 2021
by
杨林
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
智能导购
parents
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
162 additions
and
0 deletions
+162
-0
api.iml
api.iml
+13
-0
src/main/Python/__pycache__/api.cpython-37.pyc
src/main/Python/__pycache__/api.cpython-37.pyc
+0
-0
src/main/Python/__pycache__/estools.cpython-37.pyc
src/main/Python/__pycache__/estools.cpython-37.pyc
+0
-0
src/main/Python/api.py
src/main/Python/api.py
+38
-0
src/main/Python/estools.py
src/main/Python/estools.py
+103
-0
src/main/Python/gunicorn_config.py
src/main/Python/gunicorn_config.py
+8
-0
No files found.
api.iml
0 → 100644
View file @
c02cdaf9
<?xml version="1.0" encoding="UTF-8"?>
<module
type=
"PYTHON_MODULE"
version=
"4"
>
<component
name=
"NewModuleRootManager"
inherit-compiler-output=
"true"
>
<exclude-output
/>
<content
url=
"file://$MODULE_DIR$"
>
<sourceFolder
url=
"file://$MODULE_DIR$/src/main/Python"
isTestSource=
"false"
/>
<excludeFolder
url=
"file://$MODULE_DIR$/src/main/Python/venv"
/>
</content>
<orderEntry
type=
"jdk"
jdkName=
"Python 3.7 (PythonApi)"
jdkType=
"Python SDK"
/>
<orderEntry
type=
"sourceFolder"
forTests=
"false"
/>
</component>
</module>
\ No newline at end of file
src/main/Python/__pycache__/api.cpython-37.pyc
0 → 100644
View file @
c02cdaf9
File added
src/main/Python/__pycache__/estools.cpython-37.pyc
0 → 100644
View file @
c02cdaf9
File added
src/main/Python/api.py
0 → 100644
View file @
c02cdaf9
from
fastapi
import
FastAPI
from
estools
import
ESTools
from
typing
import
Optional
from
urllib.parse
import
unquote
import
uvicorn
app
=
FastAPI
()
@
app
.
get
(
"/comparison"
)
async
def
comparison
(
price
:
float
,
title
:
str
,
size
:
Optional
[
int
]
=
20
,
userid
:
Optional
[
str
]
=
None
):
item_name
=
unquote
(
title
,
encoding
=
'utf-8'
)
es
=
ESTools
()
if
not
es
:
return
{
"code"
:
2005
,
"msg"
:
"连接es错误"
}
else
:
res
=
es
.
data_format
(
price
=
price
,
title
=
item_name
,
size
=
size
)
if
res
:
return
{
"code"
:
2000
,
"msg"
:
"成功"
,
"data"
:
{
"size"
:
len
(
res
),
"list"
:
res
}
}
else
:
return
{
"code"
:
2004
,
"msg"
:
"没有找到商品"
}
if
__name__
==
'__main__'
:
uvicorn
.
run
(
app
=
'api:app'
,
host
=
"0.0.0.0"
,
port
=
8000
,
reload
=
True
,
debug
=
True
)
src/main/Python/estools.py
0 → 100644
View file @
c02cdaf9
from
elasticsearch
import
Elasticsearch
class
ESTools
:
"""
es工具
"""
_index
=
'goods'
_host
=
[{
"host"
:
'172.18.45.18'
,
"port"
:
9200
},
{
"host"
:
'172.18.45.17'
,
"port"
:
9200
},
{
"host"
:
'172.18.45.16'
,
"port"
:
9200
},
{
"host"
:
'172.18.69.190'
,
"port"
:
9200
}]
_TB_RATE
=
0.7
_JD_RATE
=
0.55
def
__init__
(
self
):
self
.
es
=
Elasticsearch
(
self
.
_host
)
def
search_tools
(
self
,
price
=
0
,
title
=
""
,
size
=
20
):
"""
搜索es方法
:param price: 券后价
:param title: 商品名称
:param size: 商品数量
:return: 搜索结果和分数
"""
if
self
.
es
.
ping
():
query
=
{
"query"
:
{
"bool"
:
{
"filter"
:
{
"range"
:
{
"coupon_price"
:
{
"lte"
:
price
}
}
},
"should"
:
{
"match"
:
{
"item_name"
:
title
}
}
}
},
"from"
:
0
,
"size"
:
size
}
hits
=
self
.
es
.
search
(
index
=
self
.
_index
,
body
=
query
)
res
=
hits
[
'hits'
][
'hits'
]
scores
=
[
i
[
'_score'
]
for
i
in
res
]
scores
=
sorted
(
scores
)
return
res
,
scores
else
:
return
None
def
data_format
(
self
,
price
=
0
,
title
=
""
,
size
=
20
):
"""
格式化数据方法
:param price: 券后价
:param title: 商品标题
:param size: 商品数量
:return: 格式化后的数据
"""
res
,
sco
=
self
.
search_tools
(
price
=
price
,
title
=
title
,
size
=
size
)
if
res
:
temp
=
[]
# 匹配分数平均数
score_avg
=
sum
(
sco
)
/
len
(
sco
)
# 匹配分数中位数
score_median
=
sco
[
int
(
len
(
sco
)
/
2
)]
for
i
in
res
:
# 对每条数据进行处理
if
i
[
"_score"
]
>
score_median
:
if
i
[
"_score"
]
>
score_avg
:
source
=
i
[
'_source'
]
platform
=
source
[
"platform"
]
coupon_commission
=
source
[
"coupon_commission"
]
# 给用户的佣金比例
rate
=
self
.
_JD_RATE
if
platform
==
2
else
self
.
_TB_RATE
coupon_amount
=
source
[
"coupon_amount"
]
discount_text
=
f'¥
{
coupon_amount
}
券'
if
coupon_amount
>
0
else
''
user_commission
=
"{:.2f}"
.
format
(
coupon_commission
*
rate
)
end_price
=
source
[
"coupon_price"
]
final_price
=
round
(
price
-
end_price
,
2
)
result
=
{
"item_id"
:
source
[
"item_id"
],
"item_pic"
:
source
[
"item_pic_url"
].
split
(
','
)[
0
],
"item_title"
:
source
[
"item_name"
],
"discount_type"
:
"1"
,
"discount_amount"
:
str
(
source
[
"coupon_amount"
]),
"platform"
:
str
(
platform
),
"shop_title"
:
source
[
"shop_name"
],
"user_commission"
:
user_commission
,
"price"
:
str
(
source
[
"price"
]),
"end_price"
:
str
(
end_price
),
"volume_text"
:
f'已抢
{
source
[
"item_volume"
]
}
件'
,
"discount_text"
:
discount_text
,
"discount_tips"
:
f'比查券商品便宜
{
final_price
}
元'
,
"user_commission_text"
:
f'返现
{
user_commission
}
'
}
temp
.
append
(
result
)
results
=
sorted
(
temp
,
key
=
lambda
x
:
x
[
"end_price"
])
return
results
else
:
return
None
src/main/Python/gunicorn_config.py
0 → 100644
View file @
c02cdaf9
debug
=
True
loglevel
=
'debug'
bind
=
'0.0.0.0:8000'
# 内部端口
pidfile
=
'log/gunicorn.pid'
logfile
=
'log/debug.log'
accesslog
=
'log/access.log'
workers
=
32
worker_class
=
'uvicorn.workers.UvicornWorker'
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