1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import pymysql
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
import redis
import datetime
import time
import json
import numpy as np
import pandas as pd
def send_email(app,id,e):
# 第三方 SMTP 服务
mail_host = 'smtp.exmail.qq.com' # 设置服务器
mail_user = "gaoyazhe@igengmei.com" # 用户名
mail_pass = "VCrKTui99a7ALhiK" # 口令
sender = 'gaoyazhe@igengmei.com'
receivers = ['gaoyazhe@igengmei.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
e = str(e)
msg = MIMEMultipart()
part = MIMEText('app_id:'+id+':fail', 'plain', 'utf-8')
msg.attach(part)
msg['From'] = formataddr(["gaoyazhe", sender])
# 括号里的对应收件人邮箱昵称、收件人邮箱账号
msg['To'] = ";".join(receivers)
# message['Cc'] = ";".join(cc_reciver)
msg['Subject'] = 'spark streaming:app_name:'+app
with open('error.txt','w') as f:
f.write(e)
f.close()
part = MIMEApplication(open('error.txt', 'r').read())
part.add_header('Content-Disposition', 'attachment', filename="error.txt")
msg.attach(part)
try:
smtpObj = smtplib.SMTP_SSL(mail_host, 465)
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receivers, msg.as_string())
except smtplib.SMTPException:
print('error')
def get_data_by_mysql(host, port, user, passwd, db, sql):
try:
db = pymysql.connect(host=host, port=port, user=user, passwd=passwd, db=db, cursorclass=pymysql.cursors.DictCursor)
cursor = db.cursor()
cursor.execute(sql)
results = cursor.fetchall()
db.close()
return results
except Exception as e:
print(e)
def get_all_search_word_synonym_tags():
"""
:return:dict {"search_word1":[tag_list1],"search_word2":[tag_list2]...}
"""
try:
sql = "select a.keyword , c.id from api_wordrel a " \
"left join api_wordrelsynonym b on a.id = b.wordrel_id " \
"left join api_tag c on b.word=c.name " \
"where a.category in (1,13,10,11,12) and c.tag_type+0<'4'+0 and c.is_online=1"
mysql_results = get_data_by_mysql('172.16.30.141', 3306, 'work', 'BJQaT9VzDcuPBqkd', 'zhengxing', sql)
result_dict = dict()
for data in mysql_results:
if data['keyword'] not in result_dict:
result_dict[data['keyword']] = [data['id']]
else:
result_dict[data['keyword']].append(data['id'])
return result_dict
except Exception as e:
print(e)
def get_all_synonym_tags():
"""
:return:dict {"search_word1":[tag_list1],"search_word2":[tag_list2]...}
"""
try:
sql = "select a.word, b.id from api_wordrelsynonym a left join api_tag b " \
"on a.word=b.name where b.tag_type+0<'4'+0 and b.is_online=1"
mysql_results = get_data_by_mysql('172.16.30.141', 3306, 'work', 'BJQaT9VzDcuPBqkd', 'zhengxing', sql)
result_dict = dict()
for data in mysql_results:
if data['word'] not in result_dict:
result_dict[data['word']] = [data['id']]
else:
result_dict[data['word']].append(data['id'])
return result_dict
except Exception as e:
print(e)
def get_all_api_tags():
"""
:return:dict {"search_word1":[tag_list1],"search_word2":[tag_list2]...}
"""
try:
sql = "select name, id from api_tag where tag_type+0<'4'+0 and is_online=1"
mysql_results = get_data_by_mysql('172.16.30.141', 3306, 'work', 'BJQaT9VzDcuPBqkd', 'zhengxing', sql)
result_dict = dict()
for data in mysql_results:
if data['name'] not in result_dict:
result_dict[data['name']] = [data['id']]
else:
result_dict[data['name']].append(data['id'])
return result_dict
except Exception as e:
print(e)
def get_all_word_tags():
try:
search_word_synonym_tags = get_all_search_word_synonym_tags()
synonym_tags = get_all_synonym_tags()
api_tags = get_all_api_tags()
return {**search_word_synonym_tags, **synonym_tags, **api_tags}
except Exception as e:
print(e)
def get_all_tag_tag_type():
"""
:return:dict {tag_id1:tag_type1,tag_id2:tag_type2...}
"""
try:
sql = "select id,tag_type from api_tag where tag_type+0<'4'+0 and is_online=1"
mysql_results = get_data_by_mysql('172.16.30.141', 3306, 'work', 'BJQaT9VzDcuPBqkd', 'zhengxing', sql)
result_dict = dict()
for data in mysql_results:
result_dict[data['id']] = data['tag_type']
return result_dict
except Exception as e:
print(e)
def get_all_3tag_2tag():
try:
sql = "select a.child_id,a.parent_id from api_tagrelation a" \
" left join api_tag b on a.parent_id=b.id " \
"where a.child_id in (select id from api_tag where tag_type='3' and is_online=1) " \
"and b.tag_type='2'"
mysql_results = get_data_by_mysql('172.16.30.141', 3306, 'work', 'BJQaT9VzDcuPBqkd', 'zhengxing', sql)
result_dict = dict()
for data in mysql_results:
if data['child_id'] not in result_dict:
result_dict[data['child_id']] = [data['parent_id']]
else:
result_dict[data['child_id']].append(data['parent_id'])
return result_dict
except Exception as e:
print(e)
def get_tag2_from_tag3(tag3, all_3tag_2tag, user_log_df_tag2_list):
try:
tag2s = []
if tag3 in all_3tag_2tag:
tag2s = all_3tag_2tag[tag3]
for tag2 in tag2s:
if tag2 in user_log_df_tag2_list:
return tag2
return tag3
except Exception as e:
print(e)
def compute_henqiang(x, decay_days=180, normalization_size=7, exponential=1):
if exponential:
alpha = exponential_decay(x, decay_days, normalization_size)
score = 15 - 2**alpha * ((15-0.5)/decay_days)
else:
score = 15 - x * ((15-0.5)/decay_days)
if score > 0.5:
return score
else:
return 0.5
def compute_jiaoqiang(x, decay_days=180, normalization_size=7, exponential=1):
if exponential:
alpha = exponential_decay(x, decay_days, normalization_size)
score = 12 - 2**alpha * ((12-0.5)/decay_days)
else:
score = 12 - x * ((12-0.5)/decay_days)
if score > 0.5:
return score
else:
return 0.5
def compute_ruoyixiang(x, decay_days=180, normalization_size=7, exponential=1):
if exponential:
alpha = exponential_decay(x, decay_days, normalization_size)
score = 5 - 2**alpha * ((5-0.5)/decay_days)
else:
score = 5 - x * ((5-0.5)/decay_days)
if score > 0.5:
return score
else:
return 0.5
def compute_validate(x, decay_days=180, normalization_size=7, exponential=1):
if exponential:
alpha = exponential_decay(x, decay_days, normalization_size)
score = 10 - 2**alpha * ((10-0.5)/decay_days)
else:
score = 10 - x * ((10-0.5)/decay_days)
if score > 0.5:
return score
else:
return 0.5
def compute_ai_scan(x, decay_days=180, normalization_size=7, exponential=1):
if exponential:
alpha = exponential_decay(x, decay_days, normalization_size)
score = 2 - 2**alpha * ((2-0.5)/decay_days)
else:
score = 2 - x * ((2-0.5)/decay_days)
if score > 0.5:
return score
else:
return 0.5
def get_action_tag_count(df, action_time):
try:
if not df[df['time'] == action_time].empty:
return len(df[df['time'] == action_time])
else:
return 1
except Exception as e:
print(e)
def exponential_decay(days_diff, decay_days=180, normalization_size=7):
x = np.arange(1, decay_days+1, 1)
# 天数差归一化到[0, normalization_size]
a = (normalization_size - 0) * (days_diff - min(x)) / (max(x) - min(x))
return a