Commit e0f67561 authored by 赵威's avatar 赵威

add time logger

parent 87b52217
import json import json
import os import os
import time
import traceback import traceback
import faiss import faiss
import numpy as np import numpy as np
from gm_rpcd.all import bind from gm_rpcd.all import bind
from utils.cache import redis_client_db from utils.cache import redis_client_db
from utils.message import send_msg_to_dingtalk from utils.message import (send_msg_to_dingtalk, send_performance_msg_to_dingtalk)
from utils.personas import get_user_portrait_tag3_from_redis from utils.personas import get_user_portrait_tag3_from_redis
MODEL_PATH = os.path.join(os.getcwd(), "_models") MODEL_PATH = os.path.join(os.getcwd(), "_models")
...@@ -19,6 +20,7 @@ TAG_EMBEDDING_DICT = redis_client_db.hgetall("personas_tags_embedding") ...@@ -19,6 +20,7 @@ TAG_EMBEDDING_DICT = redis_client_db.hgetall("personas_tags_embedding")
@bind("strategy_embedding/personas_vector/match") @bind("strategy_embedding/personas_vector/match")
def match_tractate_by_device(device_id, n=10): def match_tractate_by_device(device_id, n=10):
try: try:
time_begin = time.time()
business_tags = get_user_portrait_tag3_from_redis(device_id, tags_num=5).get("business_tags", []) business_tags = get_user_portrait_tag3_from_redis(device_id, tags_num=5).get("business_tags", [])
# business_tags = ["假体下巴", "你好", "假体隆胸"] # business_tags = ["假体下巴", "你好", "假体隆胸"]
res = [] res = []
...@@ -30,6 +32,9 @@ def match_tractate_by_device(device_id, n=10): ...@@ -30,6 +32,9 @@ def match_tractate_by_device(device_id, n=10):
if vectors: if vectors:
D, I = FAISS_TAGS_INDEX.search(np.array([np.average(vectors, axis=0)]).astype("float32"), n) D, I = FAISS_TAGS_INDEX.search(np.array([np.average(vectors, axis=0)]).astype("float32"), n)
res = I.tolist()[0] res = I.tolist()[0]
time_end = time.time() - time_begin
# if time_end > 0.03:
send_performance_msg_to_dingtalk("match_tractate_by_device cost {}ms".format(time_end))
return res return res
except Exception as e: except Exception as e:
send_msg_to_dingtalk(str(traceback.format_exc())) send_msg_to_dingtalk(str(traceback.format_exc()))
......
...@@ -30,3 +30,27 @@ def send_msg_to_dingtalk(msg, mobiles=[]): ...@@ -30,3 +30,27 @@ def send_msg_to_dingtalk(msg, mobiles=[]):
except Exception as e: except Exception as e:
print(e) print(e)
return str(e) return str(e)
def send_performance_msg_to_dingtalk(msg, mobiles=[]):
try:
secret = "SEC8ff4070e6cf492b386e5c8baa4fdd2702a69adbfde15e662013313a050795ffb"
timestamp = str(round(time.time() * 1000))
secret_enc = secret.encode("utf-8")
string_to_sign = "{}\n{}".format(timestamp, secret)
string_to_sign_enc = string_to_sign.encode("utf-8")
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
headers = {"Content-Type": "application/json"}
data = {"msgtype": "text", "text": {"content": msg}}
if mobiles:
data = {"msgtype": "text", "text": {"content": msg}, "at": {"atMobiles": mobiles, "isAtAll": False}}
json_data = json.dumps(data)
url = "https://oapi.dingtalk.com/robot/send?access_token=820e20b589e3e5d47023fd20859d1d54177e5b03178c25b02cadbbf95cc7e9b8&sign={}&timestamp={}".format(
sign, timestamp)
response = requests.post(url=url, data=json_data, headers=headers)
return str(response.status_code) + " " + str(response.content.decode("utf-8"))
except Exception as e:
print(e)
return str(e)
import base64
import hashlib
import hmac
import json
import time
import urllib
import requests
def send_feed_msg_to_dingtalk(msg, mobiles=[]):
try:
secret = "SEC8ff4070e6cf492b386e5c8baa4fdd2702a69adbfde15e662013313a050795ffb"
timestamp = str(round(time.time() * 1000))
secret_enc = secret.encode("utf-8")
string_to_sign = "{}\n{}".format(timestamp, secret)
string_to_sign_enc = string_to_sign.encode("utf-8")
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
headers = {"Content-Type": "application/json"}
data = {"msgtype": "text", "text": {"content": msg}}
if mobiles:
data = {"msgtype": "text", "text": {"content": msg}, "at": {"atMobiles": mobiles, "isAtAll": False}}
json_data = json.dumps(data)
url = "https://oapi.dingtalk.com/robot/send?access_token=820e20b589e3e5d47023fd20859d1d54177e5b03178c25b02cadbbf95cc7e9b8&sign={}&timestamp={}".format(
sign, timestamp)
response = requests.post(url=url, data=json_data, headers=headers)
return str(response.status_code) + " " + str(response.content.decode("utf-8"))
except Exception as e:
print(e)
return str(e)
import multiprocessing import multiprocessing
import os import os
import sys import sys
import time
import traceback import traceback
sys.path.append(os.path.realpath(".")) sys.path.append(os.path.realpath("."))
...@@ -9,7 +10,7 @@ from gensim.models import Word2Vec, word2vec ...@@ -9,7 +10,7 @@ from gensim.models import Word2Vec, word2vec
from gm_rpcd.all import bind from gm_rpcd.all import bind
from utils.es import es_scan from utils.es import es_scan
from utils.files import DATA_PATH, MODEL_PATH from utils.files import DATA_PATH, MODEL_PATH
from utils.message import send_msg_to_dingtalk from utils.message import (send_msg_to_dingtalk, send_performance_msg_to_dingtalk)
from word_vector.tractate import TRACTATE_CLICK_IDS_MODEL from word_vector.tractate import TRACTATE_CLICK_IDS_MODEL
...@@ -87,7 +88,12 @@ def projects_item2vec(score_limit=5): ...@@ -87,7 +88,12 @@ def projects_item2vec(score_limit=5):
@bind("strategy_embedding/word_vector/tractate_item2vec") @bind("strategy_embedding/word_vector/tractate_item2vec")
def clicked_tractate_ids_item2vec_model(id, n=5): def clicked_tractate_ids_item2vec_model(id, n=5):
try: try:
return TRACTATE_CLICK_IDS_MODEL.wv.most_similar(id, topn=n) time_begin = time.time()
res = TRACTATE_CLICK_IDS_MODEL.wv.most_similar(id, topn=n)
time_end = time.time() - time_begin
# if time_end > 0.03:
send_performance_msg_to_dingtalk("clicked_tractate_ids_item2vec_model cost {}ms".format(time_end))
return res
except KeyError as e: except KeyError as e:
send_msg_to_dingtalk("tractate_item2vec: " + str(e)) send_msg_to_dingtalk("tractate_item2vec: " + str(e))
except Exception as e: except Exception as e:
......
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