Commit 156448d4 authored by 段英荣's avatar 段英荣

Merge branch '2.3_relaces' into 'master'

客户端debug

See merge request alpha/physical!463
parents 6e9ee2c5 c042674c
......@@ -18,6 +18,18 @@ from trans2es.models.pictorial import CommunityPictorialHomeFeed
from libs.error import logging_exception
import os
from search.views.tag import get_same_tagset_ids
import msgpack
def loads_data(data):
try:
result = json.loads(data)
msg = True
return result,msg
except:
result = msgpack.loads(data)
msg = False
return result,msg
class KafkaManager(object):
......@@ -28,8 +40,9 @@ class KafkaManager(object):
if not cls.consumser_obj:
topic_name = settings.KAFKA_TOPIC_NAME if not topic_name else topic_name
cls.consumser_obj = KafkaConsumer(topic_name,bootstrap_servers=settings.KAFKA_BROKER_LIST)
# cls.consumser_obj.subscribe([topic_name])
gm_logging_name = settings.KAFKA_GM_LOGGING_TOPIC_NAME
cls.consumser_obj = KafkaConsumer(bootstrap_servers=settings.KAFKA_BROKER_LIST)
cls.consumser_obj.subscribe([topic_name, gm_logging_name])
return cls.consumser_obj
......@@ -41,6 +54,7 @@ class CollectData(object):
self.linucb_recommend_redis_prefix = "physical:linucb:tag_recommend:device_id:"
#推荐帖子
self.linucb_recommend_topic_id_prefix = "physical:linucb:topic_recommend:device_id:"
# 推荐榜单
self.linucb_recommend_pictorial_id_prefix = "physical:linucb:pictorial_recommend:device_id:"
self.tag_topic_id_redis_prefix = "physical:tag_id:topic_id_list:"
self.click_recommend_redis_key_prefix = "physical:click_recommend:device_id:"
......@@ -176,13 +190,11 @@ class CollectData(object):
consume_msg = msg_dict[msg_key]
for ori_msg in consume_msg:
try:
logging.info(ori_msg)
raw_val_dict = json.loads(ori_msg.value)
raw_val_dict,msg = loads_data(ori_msg.value)
if msg:
logging.info(ori_msg.value)
if "type" in raw_val_dict and \
(raw_val_dict["type"] in ("on_click_feed_topic_card","on_click_button")):
click_topic_tag_list = list()
if "on_click_feed_topic_card" == raw_val_dict["type"]:
topic_id = raw_val_dict["params"]["topic_id"]
......@@ -318,6 +330,60 @@ class CollectData(object):
new_user_click_tag_list=tag_query_results)
else:
logging.warning("unknown type msg:%s" % raw_val_dict.get("type", "missing type"))
# 用户点击个性化push进linucb
elif "type" in raw_val_dict and raw_val_dict["type"] == "on_click_push":
if isinstance(raw_val_dict["params"]["tag_ids"], str):
tagid_list = json.loads(raw_val_dict["params"]["tag_ids"])
elif isinstance(raw_val_dict["params"]["tag_ids"], list):
tagid_list = raw_val_dict["params"]["tag_ids"]
else:
tagid_list = list()
device_id = raw_val_dict["device"]["device_id"]
user_id = raw_val_dict["user_id"] if "user_id" in raw_val_dict else None
if len(tagid_list) > 0:
tag_query_results = Tag.objects.using(settings.SLAVE_DB_NAME).filter(
id__in=tagid_list, is_online=True, is_deleted=False,
is_category=False).values_list("id", flat=True)
is_click = 1
is_vote = 0
reward = 1 if is_click or is_vote else 0
for tag_id in tag_query_results:
self.update_user_linucb_tag_info(reward, device_id, tag_id, user_feature)
self.update_recommend_tag_list(device_id, user_feature, user_id,
new_user_click_tag_list=tag_query_results)
logging.info("on_click_push topic type:%s, device_id:%s, answer_tag:%s" %
(raw_val_dict.get("type", "missing type"), str(device_id),
str(tagid_list)))
# 用户点击问题清单进linucb
elif b'content' in raw_val_dict:
data = json.loads(raw_val_dict[b'content'])
if 'SYS' in data and 'APP' in data and 'action' in data['SYS'] and data['SYS']['action'] == "venus/community/skin_check/submit_questions":
device_id = data['SYS']['cl_id']
tagid_list = list(data['APP']['answer_tag']) or []
user_id = data['SYS']['user_id'] or None
logging.info("skin_check topic type:%s, device_id:%s, answer_tag:%s" %
(str(data['SYS']['action']), str(device_id), str(tagid_list)))
if len(tagid_list) > 0:
tag_query_results = list(Tag.objects.using(settings.SLAVE1_DB_NAME).filter(
id__in=tagid_list, is_online=True, is_deleted=False,
is_category=False).values_list("id", flat=True))
tag_query_results_multi = [i for i in tagid_list if i in tag_query_results]
is_click = 1
is_vote = 0
reward = 1 if is_click or is_vote else 0
for i in range(5):
for tag_id in tag_query_results_multi:
self.update_user_linucb_tag_info(reward, device_id, tag_id, user_feature)
# 更新该用户的推荐tag数据,放在 更新完成user tag行为信息之后
self.update_recommend_tag_list(device_id, user_feature, user_id,
new_user_click_tag_list=tag_query_results)
logging.info("skin_check topic type:%s, device_id:%s, tag_query_results:%s" %
(str(data['SYS']['action']), str(device_id), str(tag_query_results)))
else:
if msg:
logging.warning("unknown type msg:%s" % raw_val_dict.get("type", "missing type"))
except:
logging_exception()
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
......
......@@ -165,6 +165,8 @@ class LinUCB:
}
redis_cli.hset(redis_key, tag_id, pickle.dumps(user_tag_dict))
else:
logging.warning("not standard linucb reward")
return True
except:
logging_exception()
......
......@@ -124,7 +124,7 @@ class TopicUtils(object):
query_type=TopicPageType.HOME_RECOMMEND,
filter_topic_id_list=[], test_score=False, must_topic_id_list=[], recommend_tag_list=[],
user_similar_score_list=[], index_type="topic", routing=None, attention_tag_list=[],
linucb_user_id_list=[], disable_collpase=False):
linucb_user_id_list=[], disable_collpase=False, has_score=False):
"""
:remark:获取首页推荐帖子列表
:param user_id:
......@@ -160,7 +160,7 @@ class TopicUtils(object):
ret_data_list = list()
topic_id_list = list()
topic_score_list = list()
q["query"] = dict()
functions_list = list()
......@@ -311,6 +311,8 @@ class TopicUtils(object):
for item in result_dict["hits"]:
topic_id_list.append(item["_source"]["id"])
if has_score:
topic_score_list.append(item["_score"])
else:
multi_match = {
......@@ -431,11 +433,17 @@ class TopicUtils(object):
ret_data_list.append({"id": item["_source"]["id"], "highlight": item.get("highlight", {})})
topic_id_list.append(item["_source"]["id"])
return topic_id_list,ret_data_list
if has_score:
return topic_id_list,ret_data_list,topic_score_list
else:
return topic_id_list,ret_data_list
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return list(),list()
if has_score:
return list(), list(), list()
else:
return list(), list()
@classmethod
def userful_tag_topic_list(cls, user_id, have_read_topic_list, size,
......@@ -520,6 +528,73 @@ class TopicUtils(object):
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return []
@classmethod
def get_linucb_topic_info_for_debug(cls, size,
index_type="topic-high-star", routing=None, linucb_topic_list=[]):
try:
es_cli_obj = ESPerform.get_cli()
if len(linucb_topic_list) == 0:
return {}
else:
q = dict()
q["query"] = {
"bool": {
"must": [
{"terms": {"id": linucb_topic_list}}
]
}
}
q["_source"] = {
"includes": ["id","content_level","edit_tag_list"]
}
result_dict = ESPerform.get_search_results(es_cli_obj, sub_index_name=index_type, query_body=q,
size=size,
routing=routing)
topic_id_dict = dict()
for item in result_dict["hits"]:
topic_id_dict.update({item["_source"]["id"]:{"content_level":item["_source"]["content_level"],"edit_tag_list":item["_source"]["edit_tag_list"]}})
return topic_id_dict
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return []
@classmethod
def get_linucb_pictorial_info_for_debug(cls,size,linucb_pictorial_list = []):
try:
q = {
"query": {
"function_score": {
"query": {
"bool": {
"must": [
{"terms": {"id": linucb_pictorial_list}}
]
}
},
"boost_mode": "sum",
"score_mode": "sum",
}
},
"_source": {
"include": ["id", "edit_tag_list"]
}
}
result_dict = ESPerform.get_search_results(ESPerform.get_cli(), sub_index_name="pictorial",
query_body=q,
offset=0, size=size)
pictorial_id_dict = dict()
for item in result_dict["hits"]:
pictorial_id_dict.update({item["_source"]["id"]: {"edit_tag_list": item["_source"]["edit_tag_list"]}})
return pictorial_id_dict
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return list()
@classmethod
def get_topic_detail_recommend_list(cls, user_id, topic_id, topic_tag_list, topic_pictorial_id, topic_user_id,
filter_topic_user_id, have_read_topic_list, offset, size, es_cli_obj=None,
......
......@@ -17,6 +17,8 @@ from libs.tools import get_have_read_lin_pictorial_id_list
import datetime
from trans2es.models.tag import Tag
from search.views.tag import get_same_tagset_ids
from linucb.views.collect_data import CollectData
from linucb.views.linucb import LinUCB
def get_discover_page_topic_ids(user_id, device_id, size, query_type=TopicPageType.FIND_PAGE):
......@@ -54,7 +56,7 @@ def get_discover_page_topic_ids(user_id, device_id, size, query_type=TopicPageTy
def get_home_recommend_topic_ids(user_id, device_id, tag_id, offset, size, query=None,
query_type=TopicPageType.HOME_RECOMMEND, promote_topic_list=[], disable_collpase=False,
usefulrecall=-1, useful_tag_list=[]):
usefulrecall=-1, useful_tag_list=[], has_score= False):
try:
topic_star_routing = "6"
index_type = "topic-high-star"
......@@ -166,17 +168,32 @@ def get_home_recommend_topic_ids(user_id, device_id, tag_id, offset, size, query
# have_read_topic_id_list_add_promote.append(topic_id)
rank_topic_id_list = list()
ret_data_list = list()
rank_topic_score = list()
if size > 0:
rank_topic_id_list, ret_data_list = TopicUtils.get_recommend_topic_ids(user_id=user_id, tag_id=tag_id,
offset=0, size=size,
single_size=size, query=query,
query_type=query_type,
filter_topic_id_list=have_read_topic_id_list,
index_type=index_type,
routing=topic_star_routing,
attention_tag_list=attention_tag_list,
linucb_user_id_list=recommend_topic_user_list,
disable_collpase=disable_collpase)
if has_score:
rank_topic_id_list, ret_data_list, rank_topic_score = TopicUtils.get_recommend_topic_ids(
user_id=user_id, tag_id=tag_id,
offset=0, size=size,
single_size=size, query=query,
query_type=query_type,
filter_topic_id_list=have_read_topic_id_list,
index_type=index_type,
routing=topic_star_routing,
attention_tag_list=attention_tag_list,
linucb_user_id_list=recommend_topic_user_list,
disable_collpase=disable_collpase, has_score=has_score)
else:
rank_topic_id_list, ret_data_list = TopicUtils.get_recommend_topic_ids(
user_id=user_id, tag_id=tag_id,
offset=0, size=size,
single_size=size, query=query,
query_type=query_type,
filter_topic_id_list=have_read_topic_id_list,
index_type=index_type,
routing=topic_star_routing,
attention_tag_list=attention_tag_list,
linucb_user_id_list=recommend_topic_user_list,
disable_collpase=disable_collpase)
# if len(recommend_topic_list) == 6 and query is None:
# if (size < 11):
......@@ -211,15 +228,27 @@ def get_home_recommend_topic_ids(user_id, device_id, tag_id, offset, size, query
ret_list = rank_topic_id_list if query is None else ret_data_list
if usefulrecall != -1:
return recommend_topic_list, ret_list, useful_topic_id_list
if has_score:
return recommend_topic_list, ret_list, useful_topic_id_list, rank_topic_score
else:
return recommend_topic_list, ret_list, useful_topic_id_list
else:
return recommend_topic_list, ret_list
if has_score:
return recommend_topic_list, ret_list, rank_topic_score
else:
return recommend_topic_list, ret_list
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
if usefulrecall != -1:
return [], [], []
if has_score:
return [], [], [], []
else:
return [], [], []
else:
return [], []
if has_score:
return [], [], []
else:
return [], []
def get_home_recommend_pictorial_ids(user_id=-1, device_id="", size=4):
......@@ -258,6 +287,74 @@ def get_home_recommend_pictorial_ids(user_id=-1, device_id="", size=4):
return []
def get_topic_and_pictorial_detail_for_debug(device_id = "",linucb_topic_id_list = [],rank_topic_id_list = [],linucb_pictorial_id_list = [],rank_topic_score = []):
try:
linucb_tag_dict = get_device_linucb_tag(device_id,size=20,has_score=True)
linucb_topic_results = dict()
linucb_pictorial_results = dict()
rank_topic_results = dict()
if len(linucb_topic_id_list) >0:
linucb_topic_dict = TopicUtils.get_linucb_topic_info_for_debug(len(linucb_topic_id_list),linucb_topic_list=linucb_topic_id_list)
linucb_topic_results = dict()
if len(linucb_topic_dict) > 0:
# num = 1
for topic_id in linucb_topic_id_list:
topic_id = topic_id
edit_tag_List = linucb_topic_dict[topic_id]['edit_tag_list']
content_level = linucb_topic_dict[topic_id]['content_level']
edit_tag_name_score_list = list()
for edit_tag in edit_tag_List:
edit_tag_List_dict = get_edit_tag_name(edit_tag_List)
if edit_tag in linucb_tag_dict:
edit_tag_name_score_list.append(
{"id": edit_tag, "score": linucb_tag_dict[edit_tag]["score"],
"name": linucb_tag_dict[edit_tag]["name"]})
else:
edit_tag_name_score_list.append({"id": edit_tag, "score": 0, "name": edit_tag_List_dict[edit_tag]})
linucb_topic_results.update({str(topic_id):{"id":topic_id,"content_level":content_level,"edit_tags":edit_tag_name_score_list}})
logging.info("linucb_topic:{}".format(linucb_topic_results))
# num += 1
if len(rank_topic_id_list) >0:
rank_topic_dict = TopicUtils.get_linucb_topic_info_for_debug(len(rank_topic_id_list),
linucb_topic_list=rank_topic_id_list)
rank_topic_results = dict()
if len(rank_topic_dict) > 0:
# num = 1
for i in range(len(rank_topic_id_list)):
topic_id = rank_topic_id_list[i]
score = rank_topic_score[i]
rank_topic_results.update({str(topic_id): {"id": topic_id, "rank":score}})
# num += 1
logging.info("rank_topic:{}".format(rank_topic_results))
if len(linucb_pictorial_id_list) >0:
if len(linucb_pictorial_id_list) > 0:
linucb_pictorial_dict = TopicUtils.get_linucb_pictorial_info_for_debug(len(linucb_pictorial_id_list),
linucb_pictorial_list=linucb_pictorial_id_list)
linucb_pictorial_results = dict()
if len(linucb_pictorial_dict) > 0:
# num = 1
for pictorial_id in linucb_pictorial_id_list:
pictorial_id = pictorial_id
edit_tag_List = linucb_pictorial_dict[pictorial_id]['edit_tag_list']
edit_tag_name_score_list = list()
for edit_tag in edit_tag_List:
edit_tag_List_dict = get_edit_tag_name(edit_tag_List)
if edit_tag in linucb_tag_dict:
edit_tag_name_score_list.append(
{"id": edit_tag, "score": linucb_tag_dict[edit_tag]["score"],
"name": linucb_tag_dict[edit_tag]["name"]})
else:
edit_tag_name_score_list.append({"id": edit_tag, "score": 0, "name": edit_tag_List_dict[edit_tag]})
linucb_pictorial_results.update({str(pictorial_id): {"id": pictorial_id,
"edit_tags": edit_tag_name_score_list}})
logging.info("linucb_pictorial:{}".format(linucb_pictorial_results))
# num += 1
logging.info("get_topic_and_pictorial_detail_for_debug:result:{}".format({ "linucb_topic":linucb_topic_results,"rank_topic":rank_topic_results,"linucb_pictorial":linucb_pictorial_results}))
return { "linucb_topic":linucb_topic_results,"rank_topic":rank_topic_results,"linucb_pictorial":linucb_pictorial_results}
except:
return { "linucb_topic":{},"rank_topic":{},"linucb_pictorial":{}}
@bind("physical/search/query_tag_id_by_topic")
def query_tag_id_by_topic(offset=0, size=10, topic_id_list=[], user_id=-1):
try:
......@@ -269,7 +366,7 @@ def query_tag_id_by_topic(offset=0, size=10, topic_id_list=[], user_id=-1):
@bind("physical/search/home_recommend")
def home_recommend(device_id="", user_id=-1, offset=0, size=10, query_type=TopicPageType.HOME_RECOMMEND,
promote_topic_list=[], usefulrecall=-1, useful_tag_list=[]):
promote_topic_list=[], usefulrecall=-1, useful_tag_list=[], is_debug=False):
"""
:remark:首页推荐,目前只推荐日记
:param session_id:
......@@ -288,6 +385,7 @@ def home_recommend(device_id="", user_id=-1, offset=0, size=10, query_type=Topic
recommend_pictorial_ids = list()
rank_topic_ids = list()
useful_topic_ids = list()
rank_topic_score = list()
es_node_load_high_flag = False
# try:
# es_node_load_high_flag = ESPerform.if_es_node_load_high(ESPerform.get_cli())
......@@ -311,31 +409,75 @@ def home_recommend(device_id="", user_id=-1, offset=0, size=10, query_type=Topic
if usefulrecall != -1 and len(useful_tag_list) > 0:
recommend_pictorial_ids = get_home_recommend_pictorial_ids(user_id, device_id, 4)
size = size - len(recommend_pictorial_ids)
recommend_topic_ids, rank_topic_ids, useful_topic_ids = get_home_recommend_topic_ids(user_id, device_id,
tag_id=0, offset=0,
size=size,
query_type=query_type,
promote_topic_list=promote_topic_list,
usefulrecall=usefulrecall,
useful_tag_list=useful_tag_list)
return {"linucb_topic_ids": recommend_topic_ids, "rank_topic_ids": rank_topic_ids,
"useful_topic_ids": useful_topic_ids, "linucb_pictorial_ids": recommend_pictorial_ids}
if is_debug:
has_score = True
recommend_topic_ids, rank_topic_ids, useful_topic_ids, rank_topic_score = get_home_recommend_topic_ids(
user_id, device_id,
tag_id=0, offset=0,
size=size,
query_type=query_type,
promote_topic_list=promote_topic_list,
usefulrecall=usefulrecall,
useful_tag_list=useful_tag_list, has_score=has_score)
else:
has_score = False
recommend_topic_ids, rank_topic_ids, useful_topic_ids = get_home_recommend_topic_ids(user_id,
device_id,
tag_id=0,
offset=0,
size=size,
query_type=query_type,
promote_topic_list=promote_topic_list,
usefulrecall=usefulrecall,
useful_tag_list=useful_tag_list,
has_score=has_score)
if not is_debug:
return {"linucb_topic_ids": recommend_topic_ids, "rank_topic_ids": rank_topic_ids,
"useful_topic_ids": useful_topic_ids, "linucb_pictorial_ids": recommend_pictorial_ids}
else:
results = get_topic_and_pictorial_detail_for_debug(device_id, recommend_topic_ids, rank_topic_ids,
recommend_pictorial_ids, rank_topic_score)
return {"linucb_topic_ids": recommend_topic_ids, "rank_topic_ids": rank_topic_ids,
"useful_topic_ids": useful_topic_ids, "linucb_pictorial_ids": recommend_pictorial_ids,
"debug_model_data": results}
else:
recommend_pictorial_ids = get_home_recommend_pictorial_ids(user_id, device_id, 4)
size = size - len(recommend_pictorial_ids)
recommend_topic_ids, rank_topic_ids = get_home_recommend_topic_ids(user_id, device_id, tag_id=0,
offset=0, size=size,
query_type=query_type,
promote_topic_list=promote_topic_list)
if is_debug:
has_score = True
recommend_topic_ids, rank_topic_ids, rank_topic_score = get_home_recommend_topic_ids(user_id,
device_id,
tag_id=0,
offset=0,
size=size,
query_type=query_type,
promote_topic_list=promote_topic_list,
has_score=has_score)
return {"linucb_topic_ids": recommend_topic_ids, "rank_topic_ids": rank_topic_ids,
"linucb_pictorial_ids": recommend_pictorial_ids}
else:
has_score = False
recommend_topic_ids, rank_topic_ids = get_home_recommend_topic_ids(user_id, device_id, tag_id=0,
offset=0, size=size,
query_type=query_type,
promote_topic_list=promote_topic_list,
has_score=has_score)
if not is_debug:
return {"linucb_topic_ids": recommend_topic_ids, "rank_topic_ids": rank_topic_ids,
"linucb_pictorial_ids": recommend_pictorial_ids}
else:
results = get_topic_and_pictorial_detail_for_debug(device_id, recommend_topic_ids, rank_topic_ids,
recommend_pictorial_ids, rank_topic_score)
return {"linucb_topic_ids": recommend_topic_ids, "rank_topic_ids": rank_topic_ids,
"linucb_pictorial_ids": recommend_pictorial_ids, "debug_model_data": results}
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
if usefulrecall != -1:
return {"linucb_topic_ids": [], "rank_topic_ids": [], "useful_topic_ids": [], "linucb_pictorial_ids": []}
return {"linucb_topic_ids": [], "rank_topic_ids": [], "useful_topic_ids": [],"linucb_pictorial_ids": [], "debug_model_data": {}}
else:
return {"linucb_topic_ids": [], "rank_topic_ids": [], "linucb_pictorial_ids": []}
return {"linucb_topic_ids": [], "rank_topic_ids": [], "linucb_pictorial_ids": [],"debug_model_data": {}}
@bind("physical/search/discover_page")
......@@ -608,3 +750,140 @@ def query_topic_by_user_similarity(topic_similarity_score_dict, offset=0, size=1
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return {"recommend_topic_id": []}
@bind("physical/search/clear_history_for_debug")
def clear_history(device_id=None,user_id=-1):
try:
msg = list()
redis_key_list = list()
if device_id:
redis_key_list.extend(('physical:linucb:device_id:%s','physical:home_recommend:device_id:%s:query_type:1',
'physical:linucb:topic_recommend:device_id:%s',
'physical:linucb:pictorial_recommend:device_id:%s',
'physical:home_pictorial_recommend:device_id:%s:query_type:1'))
for redis_key in redis_key_list:
key = redis_key % device_id
if redis_client.type(key) != "b'none'":
redis_client.delete(key)
msg.append('device_id_clear')
if user_id != -1:
redis_user_key_list = ["physical:home_recommend:user_id:%s:query_type:1",'physical:home_pictorial_recommend:user_id:%s:query_type:1']
for redis_key in redis_user_key_list:
key = redis_key % user_id
if redis_client.type(key) != "b'none'":
redis_client.delete(key)
msg.append('user_id_clear')
logging.info("physical/search/clear_history_for_debug:done:device_id:{}".format(device_id))
return msg
except:
return ['fail']
@bind("physical/search/home_recommend_debug")
def home_recommend_debug(device_id="", user_id=-1, recommend_topic_ids = [],rank_topic_ids = [],recommend_pictorial_ids =[]):
"""
:remark:首页推荐,目前只推荐日记
:param session_id:
:param user_id:
:param offset:
:param size:
:return:
"""
try:
if device_id:
results = get_topic_and_pictorial_detail_for_debug(device_id,recommend_topic_ids,rank_topic_ids,recommend_pictorial_ids)
return results
else:
return {}
except:
logging.error("catch exception,err_msg:%s" % traceback.format_exc())
return {}
def get_device_linucb_tag(device_id = None,size = 20,has_score = False):
# {
# "lincub": [{"id": 1, "name": 名字}, {"id": 2, "name": 名字}, {"id": 3, "name": 名字}, {"id": 5, "name": 名字},
# {"id": 6, "name": 名字}, ]
# }
try:
user_feature = [1,1]
if device_id:
linucb_matrix_redis_prefix = "physical:linucb:device_id:"
redis_key = linucb_matrix_redis_prefix + str(device_id)
redis_linucb_tag_data_dict = redis_client.hgetall(redis_key)
if len(redis_linucb_tag_data_dict) == 0:
return {"linucb": []}
else:
(recommend_tag_dict,recommend_tag_set) = LinUCB.linucb_recommend_tag(device_id,redis_linucb_tag_data_dict,user_feature,list(redis_linucb_tag_data_dict.keys()))
recommend_tag_list = list(recommend_tag_dict.keys())
linucb_tag_dict_list = list()
linucb_tag_dict = dict()
part_recommend_tag_list = recommend_tag_list[0:size]
tag_query_results = Tag.objects.using(settings.SLAVE_DB_NAME).filter(
id__in=part_recommend_tag_list, is_online=True, is_deleted=False).values_list("id", "name")
if has_score:
for id, name in tag_query_results:
linucb_tag_dict.update({id: {"name": name, "score": recommend_tag_dict[str(id)]}})
return linucb_tag_dict
else:
for id, name in tag_query_results:
linucb_tag_dict_list.append({"id": id, "name": name})
return linucb_tag_dict_list
else:
return {"linucb":[]}
except:
return {"linucb": []}
def get_edit_tag_name(tag_lst):
try:
tag_query_results = Tag.objects.using(settings.SLAVE_DB_NAME).filter(
id__in=tag_lst, is_online=True, is_deleted=False).values_list("id", "name")
tag_dict = dict()
for id, name in tag_query_results:
tag_dict.update({id: name})
return tag_dict
except:
return dict()
@bind("physical/search/device_linucb_tag_debug")
def get_device_linucb_tag2(device_id = None,size = 20,has_score = False):
# {
# "lincub": [{"id": 1, "name": 名字}, {"id": 2, "name": 名字}, {"id": 3, "name": 名字}, {"id": 5, "name": 名字},
# {"id": 6, "name": 名字}, ]
# }
try:
user_feature = [1,1]
if device_id:
linucb_matrix_redis_prefix = "physical:linucb:device_id:"
redis_key = linucb_matrix_redis_prefix + str(device_id)
redis_linucb_tag_data_dict = redis_client.hgetall(redis_key)
if len(redis_linucb_tag_data_dict) == 0:
return {"linucb": []}
else:
(recommend_tag_dict,recommend_tag_set) = LinUCB.linucb_recommend_tag(device_id,redis_linucb_tag_data_dict,user_feature,list(redis_linucb_tag_data_dict.keys()))
recommend_tag_list = list(recommend_tag_dict.keys())
linucb_tag_dict_list = list()
linucb_tag_dict = dict()
part_recommend_tag_list = recommend_tag_list[0:size]
tag_query_results = Tag.objects.using(settings.SLAVE_DB_NAME).filter(
id__in=part_recommend_tag_list, is_online=True, is_deleted=False).values_list("id", "name")
if has_score:
result_lst = []
for id, name in tag_query_results:
result_lst.append({"name": name, "score": recommend_tag_dict[str(id)]})
linucb_tag_dict = {"linucb":result_lst}
logging.info("physical/search/device_linucb_tag_debug:%s" % str(linucb_tag_dict))
return linucb_tag_dict
else:
for id, name in tag_query_results:
linucb_tag_dict_list.append({"id": id, "name": name})
return linucb_tag_dict_list
else:
return {"linucb":[]}
except:
return {"linucb": []}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment