|
@@ -41,9 +41,10 @@ class AiTargetCombine(tornado.web.RequestHandler):
|
|
|
# 1、创建类实例
|
|
|
ins = GetTargetAndAssemblyParameters(account_id, project_id)
|
|
|
|
|
|
- # 2、判断final_target_combine是否为空,为空则不执行后续的操作
|
|
|
- if not ins.final_target_combine:
|
|
|
- self.write(json.dumps({"account_id": account_id, "message": "没有合适的定向组合", "code": 1}))
|
|
|
+ # 2、判断target_combine_to_create是否为空,为空则不执行后续的操作
|
|
|
+ if not ins.target_combine_to_create:
|
|
|
+ logger.info("account_id = %s, message = '没有定向组合: 定向冲突或者素材关联的创意个数达到上限' " % ins.account_id)
|
|
|
+ self.write(json.dumps({"account_id": account_id, "message": "没有定向组合: 定向冲突或者素材关联的创意个数达到上限", "code": 1}))
|
|
|
self.flush()
|
|
|
else:
|
|
|
# 2、判断当天是否已经存在包含【优质定向】的广告计划,如果有则获取该计划id,否则需要新建计划
|
|
@@ -214,12 +215,12 @@ class GetTargetAndAssemblyParameters(object):
|
|
|
df_1 = df[(df['target_type'] == 'action_ratio') &
|
|
|
(df['improve_ratio'] >= config['filterTargetCombine']['actionRatio']['improveRatio']) &
|
|
|
(df['sample_size'] >= config['filterTargetCombine']['actionRatio']['sampleSize']) &
|
|
|
- df['p_value'] <= 0.05]
|
|
|
+ (df['p_value'] <= 0.05)]
|
|
|
|
|
|
df_2 = df[(df['target_type'] == 'convertRatio') &
|
|
|
(df['improve_ratio'] >= config['filterTargetCombine']['convertRatio']['improveRatio']) &
|
|
|
(df['sample_size'] >= config['filterTargetCombine']['convertRatio']['sampleSize']) &
|
|
|
- df['p_value'] <= 0.05]
|
|
|
+ (df['p_value'] <= 0.05)]
|
|
|
|
|
|
merge_df = pd.concat([df_1, df_2], axis=0)
|
|
|
|
|
@@ -276,8 +277,9 @@ class GetTargetAndAssemblyParameters(object):
|
|
|
target_combine.update(region_dict) if 'city' in item.keys() else None
|
|
|
self.target_combine_filter_by_subset.append(target_combine)
|
|
|
else:
|
|
|
- logger.info("推荐定向:%s 与 账户配置信息里的定向(gender:%s, age_min: %s,age_max: %s, ages_ranges:%s,region:%s )存在冲突" %
|
|
|
+ logger.info("推荐定向:%s 与 账户%s 配置信息里的定向(gender:%s, age_min: %s,age_max: %s, ages_ranges:%s,region:%s )存在冲突" %
|
|
|
(item,
|
|
|
+ self.account_id,
|
|
|
self.advertiser_strategy['gender'],
|
|
|
self.advertiser_strategy['age_min'],
|
|
|
self.advertiser_strategy['age_max'],
|
|
@@ -290,6 +292,11 @@ class GetTargetAndAssemblyParameters(object):
|
|
|
"""
|
|
|
# 1-1 获取素材在该账户下关联的创意个数,过滤掉素材关联创意个数超过200的情况
|
|
|
df = pd.DataFrame(self.target_combine_filter_by_subset)
|
|
|
+ # signature in ('03b93728f0d82ea7865c3c7cf632bc1b',),避免这样的sql报错
|
|
|
+ if df.signature.nunique() == 1:
|
|
|
+ sig_lst = tuple(list(df.signature.unique()) * 2)
|
|
|
+ else:
|
|
|
+ sig_lst = tuple(list(df.signature.unique()))
|
|
|
|
|
|
sql = """
|
|
|
select t1.signature, t2.creative_count
|
|
@@ -300,8 +307,8 @@ class GetTargetAndAssemblyParameters(object):
|
|
|
left join
|
|
|
ctop_kuaishou_video_relate_creatives t2
|
|
|
on t1.account_id = t2.account_id and t1.photo_id = t2.photo_id
|
|
|
- """ % (self.account_id, tuple(df.signature.unique()))
|
|
|
- creative_cnt_df = pd.read_sql(sql, self.engine)
|
|
|
+ """ % (self.account_id, sig_lst)
|
|
|
+ creative_cnt_df = pd.read_sql(sql, self.product_engine)
|
|
|
creative_cnt_df = creative_cnt_df[creative_cnt_df.creative_count < 200]
|
|
|
|
|
|
# 1-2 计算每个素材还能创建的广告组(定向)个数: (200 - 已关联创意个数) / 15
|
|
@@ -311,6 +318,8 @@ class GetTargetAndAssemblyParameters(object):
|
|
|
for sig in creative_cnt_df.signature.unique():
|
|
|
n = creative_cnt_df[creative_cnt_df.signature == sig].target_combine_cnt.values[0]
|
|
|
now = datetime.datetime.now()
|
|
|
+ # Cannot take a larger sample than population when 'replace=False'
|
|
|
+ n = n if n <= len(df[df.signature == sig]) else len(df[df.signature == sig])
|
|
|
sig_target_df = df[df.signature == sig].sample(n, axis=0, random_state=(now.year + now.month + now.day))
|
|
|
final_target_combine = final_target_combine.append(sig_target_df)
|
|
|
|