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
# -*- coding:UTF-8 -*-
# @Time : 2020/9/11 10:59
# @File : portary_article_distribution.py
# @email : litao@igengmei.com
# @author : litao
import hashlib
import json
import pymysql,datetime
# import xlwt
# import redis
from meta_base_code.utils.func_from_redis_get_portrait import user_portrait_scan_info
from meta_base_code.utils.func_from_es_get_article import get_es_article_num
# from pyhive import hive
from maintenance.func_send_email_with_file import send_file_email
from typing import Dict, List
from elasticsearch_7 import Elasticsearch
from elasticsearch_7.helpers import scan
import sys
import time
from pyspark import SparkConf
from pyspark.sql import SparkSession, DataFrame
# from pyspark.sql.functions import lit
# import pytispark.pytispark as pti
def con_sql(sql):
# 从数据库的表里获取数据
db = pymysql.connect(host='172.16.40.158', port=4000, user='st_user', passwd='aqpuBLYzEV7tML5RPsN1pntUzFy',
db='jerry_prod')
cursor = db.cursor()
cursor.execute(sql)
result = cursor.fetchall()
db.close()
return result
second_demands_zero_dict = {
"answer":{},
"tractate":{},
"diary":{},
}
project_zero_dict = {
"answer":{},
"tractate":{},
"diary":{},
}
bulk_dict = {
0: [0, 0, 0],
10: [0, 0, 0],
50: [0, 0, 0],
100: [0, 0, 0],
200: [0, 0, 0],
500: [0, 0, 0],
1000: [0, 0, 0],
}
task_list = []
user_portrait_scan = user_portrait_scan_info()
for redis_count,res in enumerate(user_portrait_scan):
# if redis_count >= 50:break
second_demands = []
projects = []
total_answer_content_num = 0
total_tractate_content_num = 0
total_diary_content_num = 0
if res.get("second_demands"):
second_demands = res.get("second_demands")
count_res = get_es_article_num({"second_demands": second_demands[0:3]}, allow_tag=["second_demands"])
# print(count_res)
for article_type in count_res:
for tag in count_res[article_type]:
for key in tag:
total_answer_content, total_tractate_content, total_diary_content, sum_num = \
tag[key]
if not total_answer_content:
if second_demands_zero_dict["answer"].get(key):
second_demands_zero_dict["answer"][key] += 1
else:
second_demands_zero_dict["answer"][key] = 1
if not total_tractate_content:
if second_demands_zero_dict["tractate"].get(key):
second_demands_zero_dict["tractate"][key] += 1
else:
second_demands_zero_dict["tractate"][key] = 1
if not total_diary_content:
if second_demands_zero_dict["diary"].get(key):
second_demands_zero_dict["diary"][key] += 1
else:
second_demands_zero_dict["diary"][key] = 1
total_answer_content_num += total_answer_content
total_tractate_content_num += total_tractate_content
total_diary_content_num += total_diary_content
if res.get("projects"):
projects = res.get("projects")
count_res = get_es_article_num({"tags_v3": projects[0:3]}, allow_tag=["tags_v3"])
# print(count_res)
for article_type in count_res:
for tag in count_res[article_type]:
for key in tag:
total_answer_content, total_tractate_content, total_diary_content, sum_num = \
tag[key]
if not total_answer_content:
if project_zero_dict["answer"].get(key):
project_zero_dict["answer"][key] += 1
else:
project_zero_dict["answer"][key] = 1
if not total_tractate_content:
if project_zero_dict["tractate"].get(key):
project_zero_dict["tractate"][key] += 1
else:
project_zero_dict["tractate"][key] = 1
if not total_diary_content:
if project_zero_dict["diary"].get(key):
project_zero_dict["diary"][key] += 1
else:
project_zero_dict["diary"][key] = 1
total_answer_content_num += total_answer_content
total_tractate_content_num += total_tractate_content
total_diary_content_num += total_diary_content
# print(total_answer_content_num, total_tractate_content_num, total_diary_content_num)
tmp_count_num = 0
if 0 <= total_answer_content_num < 10:
bulk_dict[0][0] += 1
elif 10 <= total_answer_content_num < 50:
bulk_dict[10][0] += 1
elif 50 <= total_answer_content_num < 100:
bulk_dict[50][0] += 1
elif 100 <= total_answer_content_num < 200:
bulk_dict[100][0] += 1
elif 200 <= total_answer_content_num < 500:
bulk_dict[200][0] += 1
elif 500 <= total_answer_content_num < 1000:
bulk_dict[500][0] += 1
else:
bulk_dict[1000][0] += 1
if 0 <= total_tractate_content_num < 10:
bulk_dict[0][1] += 1
elif 10 <= total_tractate_content_num < 50:
bulk_dict[10][1] += 1
elif 50 <= total_tractate_content_num < 100:
bulk_dict[50][1] += 1
elif 100 <= total_tractate_content_num < 200:
bulk_dict[100][1] += 1
elif 200 <= total_tractate_content_num < 500:
bulk_dict[200][1] += 1
elif 500 <= total_tractate_content_num < 1000:
bulk_dict[500][1] += 1
else:
bulk_dict[1000][1] += 1
if 0 <= total_diary_content_num < 10:
bulk_dict[0][2] += 1
elif 10 <= total_diary_content_num < 50:
bulk_dict[10][2] += 1
elif 50 <= total_diary_content_num < 100:
bulk_dict[50][2] += 1
elif 100 <= total_diary_content_num < 200:
bulk_dict[100][2] += 1
elif 200 <= total_diary_content_num < 500:
bulk_dict[200][2] += 1
elif 500 <= total_diary_content_num < 1000:
bulk_dict[500][2] += 1
else:
bulk_dict[1000][2] += 1
# print(bulk_dict)
if redis_count % 50000 == 0:
print(redis_count)
for page_type in range(3):
if page_type == 0:
page_type_str = "回答"
elif page_type == 1:
page_type_str = "帖子"
else:
page_type_str = "日记"
for count_num in bulk_dict:
pid = hashlib.md5((page_type_str + str(count_num)).encode("utf8")).hexdigest()
instert_sql = """replace into portary_article_distribution(
page_type,num_type,num_value,pid) VALUES('{page_type_str}','{num_type}',{num_value},'{pid}');""".format(
page_type_str=page_type_str, pid=pid, num_type=str(count_num),
num_value=bulk_dict[count_num][page_type]
)
# print(instert_sql)
# cursor.execute("set names 'UTF8'")
db = pymysql.connect(host='172.16.40.158', port=4000, user='st_user',
passwd='aqpuBLYzEV7tML5RPsN1pntUzFy',
db='jerry_prod')
cursor = db.cursor()
res = cursor.execute(instert_sql)
db.commit()
# print(res)
# print(second_demands_zero_dict)
# print(project_zero_dict)
for page_type in range(3):
if page_type == 0:
page_type_str = "回答"
elif page_type == 1:
page_type_str = "帖子"
else:
page_type_str = "日记"
for count_num in bulk_dict:
pid = hashlib.md5((page_type_str + str(count_num)).encode("utf8")).hexdigest()
instert_sql = """replace into portary_article_distribution(
page_type,num_type,num_value,pid) VALUES('{page_type_str}','{num_type}',{num_value},'{pid}');""".format(
page_type_str=page_type_str,pid=pid ,num_type=str(count_num),num_value=bulk_dict[count_num][page_type]
)
# print(instert_sql)
# cursor.execute("set names 'UTF8'")
db = pymysql.connect(host='172.16.40.158', port=4000, user='st_user', passwd='aqpuBLYzEV7tML5RPsN1pntUzFy',
db='jerry_prod')
cursor = db.cursor()
res = cursor.execute(instert_sql)
db.commit()
# print(res)
# cursor.executemany()
db.close()
print(second_demands_zero_dict)
print(project_zero_dict)