mvp.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. from db.mysql_db import MysqlDB
  2. from utils.excel_util import ExcelUtil
  3. class Mvp:
  4. """
  5. ce mvp 答题数据统计
  6. 城市特例 北京市,上海市, 重庆市,天津市
  7. """
  8. age_dict = {
  9. '00-04年生': '00后',
  10. '05-09年生': '05后',
  11. '50-59年生': '50后',
  12. '60-69年生': '60后',
  13. '70-74年生': '70后',
  14. '75-79年生': '75后',
  15. '80-84年生': '80后',
  16. '85-89年生': '85后',
  17. '90-94年生': '90后',
  18. '95-99年生': '95后'
  19. }
  20. crowd = ['A', 'B', 'C', 'D', 'E', 'F']
  21. # 获取答题记录中城市列表
  22. sql_1 = 'select city from f_t_daren_score_2 group by city'
  23. # 获取父选项和父题id
  24. sql_2 = 'select a.id, a.content, b.id, b.name from bq_option a left join bq_question b on a.question_id = b.id ' \
  25. 'where a.serial_number = %s and b.serial_number = %s and a.status = b.status = 1 '
  26. # 数据插入表mvp_question_classification
  27. sql_3 = 'insert into mvp_question_classification(question_serial_number, question_content, ' \
  28. 'option_serial_number, option_content, tag, corr) values(%s, %s, %s, %s, %s, %s) '
  29. # 获取答题人的年龄段集合
  30. sql_4 = 'select nld from f_t_daren_score_2 group by nld'
  31. # 根据城市,年龄段,人群分类统计答题记录数
  32. sql_5 = 'select group_type, COUNT(uuid) from f_t_daren_score_2 where (city = %s or province = %s) and nld ' \
  33. '= %s and uuid in %s group by group_type '
  34. # 根据父选项获取子选项id列表
  35. sql_6 = 'SELECT c.id, c.sub_question_id, c.content FROM bq_sub_option c WHERE c.father_id in (SELECT a.id FROM ' \
  36. 'bq_option a ' \
  37. 'LEFT JOIN bq_question b ON a.question_id = b.id WHERE a.serial_number = %s AND b.serial_number = %s ' \
  38. 'and a.status = 1 and b.status = 1) and c.status = 1 '
  39. # 根据子题id获取包含子题id的测试
  40. sql_7 = 'select group_type from bq_testcase where status = 1 and FIND_IN_SET(%s, question_ids)'
  41. # 根据子选项id统计答题数
  42. sql_8 = 'SELECT count(uuid) FROM f_t_daren_score_2 a LEFT JOIN d_shangju_tiku_02 b ON a.sub_question_id = ' \
  43. 'b.sub_question_id AND a.score = b.score WHERE a.testcase_id = b.testcase_id and b.sub_option_id in %s' \
  44. 'and (a.city = %s or a.province = %s) and a.nld = %s and a.uuid in %s'
  45. # 计算值写入表汇总
  46. sql_9 = 'insert into mvp_standard_score(city, age, tag, crowd_type, score) VALUES(%s, %s, %s, %s, %s)'
  47. # 获取一个uuid下答题的子选项id列表
  48. sql_10 = 'select DISTINCT uuid, GROUP_CONCAT(DISTINCT b.sub_option_id) from f_t_daren_score_2 a left join ' \
  49. 'd_shangju_tiku_02 b on a.sub_question_id = b.sub_question_id and a.score = b.score where a.status = ' \
  50. 'b.status = 1 group by uuid '
  51. def __init__(self, path=None):
  52. self.shangju_db = MysqlDB('shangju')
  53. self.marketing_db = MysqlDB('marketing_db')
  54. self.shangju_db.truncate('mvp_standard_score')
  55. self.tag_data = ExcelUtil(path=path).init_mvp_data()
  56. self.crowd_info = ExcelUtil(path=path, sheet_name='选项-人群分类对应表').init_crowd_info()
  57. self.citys = self.init_city()
  58. self.age = self.init_age()
  59. self.people_sub_option_ids = self.marketing_db.select(self.sql_10)
  60. self.crowd_contain_sub_option_ids = self.get_crowd_contain_sub_option_ids()
  61. def init_city(self):
  62. """
  63. 获取答题数据中的城市。
  64. :return:
  65. """
  66. citys = ['北京市', '上海市', '重庆市', '天津市']
  67. citys_info = self.marketing_db.select(self.sql_1)
  68. citys.extend([x[0] for x in citys_info if x[0] is not None])
  69. return citys
  70. def write_tag(self, city=None, age=None, crowd=None):
  71. """
  72. 将excel中的配置信息写入到数据库表中
  73. :return:
  74. """
  75. # datas = []
  76. # for key in self.tag_data.keys():
  77. # values = self.tag_data[key]
  78. # for value in values:
  79. # question = value[0].split('-')[0]
  80. # option = value[0].split('-')[1]
  81. # corr = value[1]
  82. # data = self.shangju_db.select(self.sql_2, [option, question])
  83. # if len(data) > 0:
  84. # print([question, option, data[0][3], data[0][1], key, corr])
  85. # datas.append([question, option, data[0][3], data[0][1], key, corr])
  86. # self.shangju_db.truncate('mvp_question_classification')
  87. # self.shangju_db.add_some(self.sql_3, datas)
  88. result = self.city_age_crowd(city, age, crowd)
  89. print('update finished!!!')
  90. return result
  91. def init_age(self):
  92. """
  93. 获取答题数据中的年龄
  94. """
  95. age_info = self.marketing_db.select(self.sql_4)
  96. # print([x[0] for x in age_info])
  97. return [x[0] for x in age_info if x[0] is not None]
  98. def city_age_crowd(self, city=None, age=None, crowd=None):
  99. result = []
  100. if city is not None and age is not None and crowd is not None:
  101. print('获取指定城市,年龄段,人群类型的数据...')
  102. people_uuids = self.get_people_uuid_by_type(crowd)
  103. if len(people_uuids) > 0:
  104. print('{}-{}-{}'.format(city, age, crowd))
  105. datas = self.behavior_tag_init(city, age, people_uuids)
  106. result.extend(self.calculation_standard_score(datas, city, age, crowd))
  107. pass
  108. else:
  109. print('获取所有case的数据...')
  110. for city in self.citys:
  111. for age in self.age:
  112. if city != '上海市' and age != '85-89年生':
  113. for crowd_type in self.crowd:
  114. # print(' {}{}'.format(city, age))
  115. people_uuids = self.get_people_uuid_by_type(crowd_type)
  116. if len(people_uuids) > 0:
  117. print('{}-{}-{}'.format(city, age, crowd_type))
  118. datas = self.behavior_tag_init(city, age, people_uuids)
  119. result.extend(self.calculation_standard_score(datas, city, age, crowd_type))
  120. return result
  121. def behavior_tag_init(self, city, age, people_uuids):
  122. result = {}
  123. self.group_type_count = self.marketing_db.select(self.sql_5, [city, city, age, people_uuids])
  124. for key in self.tag_data:
  125. values = self.tag_data[key]
  126. elements = []
  127. for value in values:
  128. question = value[0].split('-')[0]
  129. option = value[0].split('-')[1]
  130. corr = value[1]
  131. fz, fm = self.molecular_value(question, option, city, age, people_uuids)
  132. if fm == 0:
  133. c = 0
  134. else:
  135. c = fz / fm
  136. elements.append([question, option, corr, fz, fm, c])
  137. result[key] = elements
  138. return self.indicator_calculation_d_e(result)
  139. def molecular_value(self, queston, option, city, age, people_uuids):
  140. # 获取当前父选项包含的子选项id和子题id列表
  141. result = self.shangju_db.select(self.sql_6, [option, queston])
  142. sub_option_ids = []
  143. group_types = []
  144. for rt in result:
  145. sub_option_id, sub_question_id, content = rt[0], rt[1], rt[2]
  146. grouptypes = self.shangju_db.select(self.sql_7, [sub_question_id])
  147. for g_t in grouptypes:
  148. if g_t[0] not in group_types:
  149. group_types.append(g_t[0])
  150. sub_option_ids.append(sub_option_id)
  151. # 计算子选项在答题记录中的点击数
  152. sub_options_count = 0
  153. if len(sub_option_ids) > 0:
  154. result_1 = self.marketing_db.select(self.sql_8, [sub_option_ids, city, city, age, people_uuids])
  155. sub_options_count = result_1[0][0]
  156. # 计算父选项包含的子选项对应的子题所在的测试gt包含的点击数。
  157. denominator_value = 0
  158. for info in self.group_type_count:
  159. if info[0] in group_types:
  160. denominator_value += info[1]
  161. return sub_options_count, denominator_value
  162. def indicator_calculation_d_e(self, data):
  163. result = {}
  164. for key in data.keys():
  165. values = data[key]
  166. c_list = [x[5] for x in values]
  167. fm_list = [x[4] for x in values]
  168. sum_c = sum(fm_list)
  169. min_c = min(c_list)
  170. elements = []
  171. for value in values:
  172. _value = []
  173. c = value[5]
  174. if sum_c == 0:
  175. d = 0
  176. else:
  177. d = c / sum_c
  178. e = c - min_c
  179. _value.extend(value)
  180. _value.append(d)
  181. _value.append(e)
  182. elements.append(_value)
  183. result[key] = elements
  184. return result
  185. def calculation_standard_score(self, datas, city, age, crowd_type):
  186. scores = []
  187. for key in datas.keys():
  188. print(key)
  189. print(' 父题序号 父选项序号 相关系系数 分子值 分母值 百分比 人数权重 偏离值')
  190. values = [x[5] for x in datas[key]]
  191. min_c = min(values)
  192. f = min_c
  193. for value in datas[key]:
  194. print(' {}'.format(value))
  195. if value[2] is not None and value[7] is not None:
  196. f += float(value[2] * value[7])
  197. print(' 标准分:{}'.format(f))
  198. scores.append([city, age, key, crowd_type, f])
  199. # self.shangju_db.add_some(self.sql_9, scores)
  200. return scores
  201. def get_crowd_people(self):
  202. result = {}
  203. for type in self.crowd:
  204. uuids = self.get_people_uuid_by_type(type)
  205. result[type] = len(uuids)
  206. return result
  207. def get_people_uuid_by_type(self, type):
  208. # 获取每个答题者所答题的子选项id
  209. uuids = []
  210. type_sub_option_ids = self.crowd_contain_sub_option_ids[type]
  211. for people in self.people_sub_option_ids:
  212. uuid = people[0]
  213. sub_option_ids = str(people[1]).split(',')
  214. # list(set(a).intersection(set(b)))
  215. if len(list(set(sub_option_ids).intersection(set(type_sub_option_ids)))) > 0:
  216. uuids.append(uuid)
  217. return uuids
  218. def get_crowd_contain_sub_option_ids(self):
  219. """
  220. 获取ABCDEF人群包含的子选项id
  221. :return:
  222. """
  223. infos = {}
  224. for key in self.crowd_info.keys():
  225. values = self.crowd_info[key]
  226. sub_option_ids = []
  227. for value in values:
  228. if value is not None:
  229. vals = str(value).split('-')
  230. option, question = vals[1], vals[0]
  231. query_result = self.shangju_db.select(self.sql_6, [option, question])
  232. for qr in query_result:
  233. sub_option_id, sub_question_id, content = qr[0], qr[1], qr[2]
  234. sub_option_ids.append(sub_option_id)
  235. infos[key] = sub_option_ids
  236. return infos
  237. if __name__ == '__main__':
  238. mvp = Mvp()
  239. mvp.write_tag()