Explorar o código

update: ad_idea -> creative

liqian hai 1 ano
pai
achega
c7db946b13
Modificáronse 3 ficheiros con 16 adicións e 16 borrados
  1. 7 7
      ad_predict.py
  2. 8 8
      app.py
  3. 1 1
      config.py

+ 7 - 7
ad_predict.py

@@ -10,9 +10,9 @@ config_ = set_config()
 redis_helper = RedisHelper()
 
 
-def thompson_process(ad_idea_id):
-    # 获取ad_idea_id对应的Thompson参数
-    thompson_param = redis_helper.get_data_from_redis(key_name=f"{config_.THOMPSON_PARAM_KEY_PREFIX}{ad_idea_id}")
+def thompson_process(creative_id):
+    # 获取creative_id对应的Thompson参数
+    thompson_param = redis_helper.get_data_from_redis(key_name=f"{config_.THOMPSON_PARAM_KEY_PREFIX}{creative_id}")
     if thompson_param is None or thompson_param == '':
         # 参数不存在,随机生成[0, 1)之间的浮点数
         score = random.random()
@@ -29,22 +29,22 @@ def thompson_process(ad_idea_id):
             # ad_idea_id 曝光数 < 100,随机生成[0, 1)之间的浮点数
             score = random.random()
             random_flag = 'random'
-    thompson_res = [ad_idea_id, score, thompson_param, random_flag]
+    thompson_res = [creative_id, score, thompson_param, random_flag]
     return thompson_res
 
 
-def get_ad_idea_id_with_thompson(mid, ad_idea_id_list):
+def get_creative_id_with_thompson(mid, creative_id_list):
     """利用Thompson采样获取此次要展示的广告创意ID"""
     # 限制协程最大并发数:20
     gevent_pool = pool.Pool(20)
-    tasks = [gevent_pool.spawn(thompson_process, ad_idea_id) for ad_idea_id in ad_idea_id_list]
+    tasks = [gevent_pool.spawn(thompson_process, creative_id) for creative_id in creative_id_list]
     gevent.joinall(tasks)
     thompson_res_list = [t.get() for t in tasks]
     # 按照score排序
     thompson_res_rank = sorted(thompson_res_list, key=lambda x: x[1], reverse=True)
     rank_res = {
         'mid': mid,
-        'ad_idea_id': thompson_res_rank[0][0],
+        'creative_id': thompson_res_rank[0][0],
         'score': thompson_res_rank[0][1],
         'thompson_param': thompson_res_rank[0][2],
         'random_flag': thompson_res_rank[0][3],

+ 8 - 8
app.py

@@ -11,13 +11,13 @@ monkey.patch_all()
 from flask import Flask, request
 from log import Log
 from config import set_config
-from ad_predict import get_ad_idea_id_with_thompson
+from ad_predict import get_creative_id_with_thompson
 
 app = Flask(__name__)
 log_ = Log()
 config_ = set_config()
 
-log_.info(f"server start...")
+# log_.info(f"server start...")
 
 
 @app.route('/healthcheck')
@@ -25,17 +25,17 @@ def health_check():
     return 'ok!'
 
 
-@app.route('/ad/predict/getAdIdeaId', methods=['GET', 'POST'])
-def get_ad_idea_id():
+@app.route('/ad/predict/getCreativeId', methods=['GET', 'POST'])
+def get_creative_id():
     start_time = time.time()
     try:
         request_data = json.loads(request.get_data())
         mid = request_data.get('mid')
-        ad_idea_id_list = request_data.get('adIdeaIdList')
-        thompson_result = get_ad_idea_id_with_thompson(mid=mid, ad_idea_id_list=ad_idea_id_list)
-        result = {'code': 200, 'message': 'success', 'data': {'mid': mid, 'adIdeaId': thompson_result['ad_idea_id']}}
+        creative_id_list = request_data.get('creativeIdList')
+        thompson_result = get_creative_id_with_thompson(mid=mid, creative_id_list=creative_id_list)
+        result = {'code': 200, 'message': 'success', 'data': {'mid': mid, 'creativeId': thompson_result['creative_id']}}
         log_message = {
-            'requestUri': '/ad/predict/getAdIdeaId',
+            'requestUri': '/ad/predict/getCreativeId',
             'logTimestamp': int(time.time() * 1000),
             'requestData': request_data,
             'thompsonResult': thompson_result,

+ 1 - 1
config.py

@@ -2,7 +2,7 @@ import os
 
 
 class BaseConfig(object):
-    # adIdeaId对应Thompson参数结果存放 redis key 前缀,完整格式:thompson:param:{ad_idea_id}
+    # creativeId对应Thompson参数结果存放 redis key 前缀,完整格式:thompson:param:{creative_id}
     THOMPSON_PARAM_KEY_PREFIX = 'thompson:param:'