flask_app.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. from flask import Flask, request, render_template, jsonify
  2. import os
  3. from werkzeug.utils import secure_filename
  4. import time
  5. import json
  6. import pandas as pd
  7. import openpyxl as ox
  8. from itertools import groupby
  9. import os
  10. import pymysql as ps
  11. app = Flask(__name__)
  12. class Mvp:
  13. """
  14. ce mvp 答题数据统计
  15. 城市特例 北京市,上海市, 重庆市,天津市
  16. """
  17. age_dict = {
  18. '00-04年生': '00后',
  19. '05-09年生': '05后',
  20. '50-59年生': '50后',
  21. '60-69年生': '60后',
  22. '70-74年生': '70后',
  23. '75-79年生': '75后',
  24. '80-84年生': '80后',
  25. '85-89年生': '85后',
  26. '90-94年生': '90后',
  27. '95-99年生': '95后'
  28. }
  29. crowd = ['A', 'B', 'C', 'D', 'E', 'F']
  30. # 获取答题记录中城市列表
  31. sql_1 = 'select city from f_t_daren_score_2 group by city'
  32. # 获取父选项和父题id
  33. 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 ' \
  34. 'where a.serial_number = %s and b.serial_number = %s and a.status = b.status = 1 '
  35. # 获取答题人的年龄段集合
  36. sql_4 = 'select nld from f_t_daren_score_2 group by nld'
  37. # 根据城市,年龄段,人群分类统计答题记录数
  38. sql_5 = 'select group_type, COUNT(uuid) from f_t_daren_score_2 where (city = %s or province = %s) and nld ' \
  39. '= %s and uuid in %s group by group_type '
  40. # 根据父选项获取子选项id列表
  41. 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 ' \
  42. 'bq_option a ' \
  43. 'LEFT JOIN bq_question b ON a.question_id = b.id WHERE a.serial_number = %s AND b.serial_number = %s ' \
  44. 'and a.status = 1 and b.status = 1) and c.status = 1 '
  45. # 根据子题id获取包含子题id的测试
  46. sql_7 = 'select group_type from bq_testcase where status = 1 and FIND_IN_SET(%s, question_ids)'
  47. # 根据子选项id统计答题数
  48. sql_8 = 'SELECT count(1) FROM f_t_daren_score_2 a LEFT JOIN d_shangju_tiku_02 b ON a.sub_question_id = ' \
  49. 'b.sub_question_id AND a.score = b.score and a.testcase_id = b.testcase_id WHERE b.sub_option_id in %s' \
  50. 'and (a.city = %s or a.province = %s) and a.nld = %s and a.uuid in %s'
  51. # 获取一个uuid下答题的子选项id列表
  52. sql_10 = 'select DISTINCT uuid, GROUP_CONCAT(DISTINCT b.sub_option_id) from f_t_daren_score_2 a left join ' \
  53. 'd_shangju_tiku_02 b on a.sub_question_id = b.sub_question_id and a.score = b.score where a.status = ' \
  54. 'b.status = 1 group by uuid '
  55. # 向表mvp_crowd_info插入数据
  56. sql_11 = 'insert into mvp_crowd_info(age_area, city_name, crowd_type, status) values(%s, %s, %s, 1)'
  57. # 向表mvp_crowd_info_behavior中插入数据
  58. sql_12 = 'insert into mvp_crowd_info_behavior(crowd_info_id, behavioral_interest, standard_value, status) values(' \
  59. '%s, %s, ' \
  60. '%s, 1) '
  61. # 向表mvp_crowd_info_module中插入数据
  62. sql_13 = 'insert into mvp_crowd_info_module(crowd_info_id, module_name, standard_value, status) values (%s, %s, ' \
  63. '%s, 1) '
  64. sql_14 = 'select a.id, a.age_area, a.city_name, a.crowd_type from mvp_crowd_info a where a.status = 1'
  65. def __init__(self, path=None):
  66. self.shangju_db = MysqlDB('shangju')
  67. self.marketing_db = MysqlDB('bi_report')
  68. # self.shangju_db.truncate('mvp_standard_score')
  69. self.tag_data = ExcelUtil(file_name=path).init_mvp_data()
  70. self.crowd_info = ExcelUtil(file_name=path, sheet_name='选项-人群分类对应表').init_crowd_info()
  71. self.citys = self.init_city()
  72. self.age = self.init_age()
  73. self.people_sub_option_ids = self.marketing_db.select(self.sql_10)
  74. self.crowd_contain_sub_option_ids = self.get_crowd_contain_sub_option_ids()
  75. self.module_scores = ExcelUtil(file_name='set-behavior-tag.xlsx', sheet_name='算法关系表').init_module_info()
  76. # self.scores_tag = ExcelUtil(file_name='行为与模块分值汇总.xlsx', sheet_name='行为').init_scores()
  77. # self.score_module = ExcelUtil(file_name='行为与模块分值汇总.xlsx', sheet_name='模块').init_scores()
  78. self.scores_tag = None
  79. self.score_module = None
  80. def init_city(self):
  81. """
  82. 获取答题数据中的城市。
  83. :return:
  84. """
  85. citys = ['北京市', '上海市', '重庆市', '天津市']
  86. citys_info = self.marketing_db.select(self.sql_1)
  87. citys.extend([x[0] for x in citys_info if x[0] is not None])
  88. return citys
  89. def query_behavioral_info(self, city=None, age=None, crowd=None):
  90. """
  91. 查询行为兴趣信息
  92. :return:
  93. """
  94. # datas = []
  95. # for key in self.tag_data.keys():
  96. # values = self.tag_data[key]
  97. # for value in values:
  98. # question = value[0].split('-')[0]
  99. # option = value[0].split('-')[1]
  100. # corr = value[1]
  101. # data = self.shangju_db.select(self.sql_2, [option, question])
  102. # if len(data) > 0:
  103. # print([question, option, data[0][3], data[0][1], key, corr])
  104. # datas.append([question, option, data[0][3], data[0][1], key, corr])
  105. # self.shangju_db.truncate('mvp_question_classification')
  106. # self.shangju_db.add_some(self.sql_3, datas)
  107. scores_behavioral = self.city_age_crowd(city, age, crowd)
  108. # scores_module = self.module_score(crowd, city, age, scores_behavioral['score'])
  109. # result = {'行为兴趣分值': scores_behavioral['score'], '模块分值': scores_module}
  110. print('update finished!!!')
  111. return scores_behavioral
  112. def module_score(self, crowd, city, age, scores):
  113. """
  114. 模块分数计算
  115. 城市 年龄 人群分类 模块名称 分数
  116. :return:
  117. """
  118. modules = self.module_scores[crowd]
  119. result = []
  120. for key in modules.keys():
  121. values = modules[key]
  122. module_name = key
  123. score = 0
  124. for value in values:
  125. behavioral_name = value[0]
  126. weight = float(value[2])
  127. standard_score = [x[4] for x in scores if x[2] == behavioral_name]
  128. if len(standard_score) > 0:
  129. score += standard_score[0] * weight
  130. result.append([city, age, crowd, module_name, score])
  131. return result
  132. # def insert_data(self, scores_behavioral, scores_module):
  133. def insert(self):
  134. """
  135. 计算数据写入数据库中,供接口查看
  136. :return:
  137. """
  138. infos = []
  139. for city in ['上海市', '宁波市', '苏州市', '杭州市', ' 无锡市']:
  140. for age in ['50-59年生', '60-69年生', '70-74年生', '75-79年生', '80-84年生', '85-89年生', '90-94年生', '95-99年生', '00'
  141. '-04年生', '05-09年生']:
  142. for c_type in ['A', 'B', 'C', 'D', 'E', 'F']:
  143. age_area = self.age_dict.get(age)
  144. if age_area:
  145. infos.append([age_area, city, c_type])
  146. self.shangju_db.add_some(self.sql_11, infos)
  147. def query_data(self):
  148. ids = self.shangju_db.select(self.sql_14)
  149. return ids
  150. def shanghai_85_module_score_insert(self):
  151. """
  152. 上海市,85后模块分数计算
  153. :return:
  154. """
  155. result = []
  156. for crowd in self.crowd:
  157. modules = self.module_scores[crowd]
  158. for key in modules.keys():
  159. values = modules[key]
  160. module_name = key
  161. score = 0
  162. for value in values:
  163. behavioral_name = value[0]
  164. weight = float(value[2])
  165. # standard_score = [x[4] for x in scores if x[2] == behavioral_name]
  166. standard_score = float(value[1])
  167. if standard_score is not None:
  168. score += standard_score * weight
  169. result.append(['上海市', '85后', crowd, module_name, score])
  170. return {'score': result, 'data': self.module_scores}
  171. def tag_module_score_insert(self):
  172. """
  173. 标签模块分数写入数据库
  174. :return:
  175. """
  176. ids = self.query_data()
  177. insert_data = []
  178. insert_data_1 = []
  179. for tag, module in zip(self.scores_tag, self.score_module):
  180. city = tag[0]
  181. age = tag[1]
  182. crowd = tag[2]
  183. tag_name = tag[3]
  184. tag_score = tag[4]
  185. city_2 = module[0]
  186. age_2 = module[1]
  187. crowd_2 = module[2]
  188. module_name_2 = module[3]
  189. module_score_2 = module[4]
  190. for id in ids:
  191. city_1 = id[2]
  192. age_1 = id[1]
  193. crowd_1 = id[3]
  194. id_1 = id[0]
  195. if city == city_1 and self.age_dict[age] == age_1 and crowd == crowd_1:
  196. insert_data.append([id_1, tag_name, tag_score])
  197. if city_2 == city_1 and self.age_dict[age_2] == age_1 and crowd_2 == crowd_1:
  198. insert_data_1.append([id_1, module_name_2, module_score_2])
  199. self.shangju_db.add_some(self.sql_12, insert_data)
  200. self.shangju_db.add_some(self.sql_13, insert_data_1)
  201. def init_age(self):
  202. """
  203. 获取答题数据中的年龄
  204. """
  205. age_info = self.marketing_db.select(self.sql_4)
  206. # print([x[0] for x in age_info])
  207. return [x[0] for x in age_info if x[0] is not None]
  208. def city_age_crowd(self, city=None, age=None, crowd=None):
  209. data_start = []
  210. result = []
  211. module_scores = []
  212. if city is not None and age is not None and crowd is not None:
  213. print('获取指定城市,年龄段,人群类型的数据...')
  214. people_uuids = self.get_people_uuid_by_type(crowd)
  215. if len(people_uuids) > 0:
  216. print('{}-{}-{}'.format(city, age, crowd))
  217. datas = self.behavior_tag_init(city, age, people_uuids)
  218. data_start.append(datas)
  219. result.extend(self.calculation_standard_score(datas, city, age, crowd))
  220. module_scores.extend(self.module_score(crowd, city, age, result))
  221. pass
  222. else:
  223. print('获取所有case的数据...')
  224. # for city in self.citys:
  225. # for city in [city]:
  226. for age in self.age:
  227. for crowd_type in self.crowd:
  228. if age == '85-89年生' and city == '上海市':
  229. print('上海市85后数据导入人工值,无需计算...')
  230. pass
  231. else:
  232. # print(' {}{}'.format(city, age))
  233. people_uuids = self.get_people_uuid_by_type(crowd_type)
  234. if len(people_uuids) > 0:
  235. print('{}-{}-{}'.format(city, age, crowd_type))
  236. datas = self.behavior_tag_init(city, age, people_uuids)
  237. data_start.append(datas)
  238. result.extend(self.calculation_standard_score(datas, city, age, crowd_type))
  239. module_scores.extend(self.module_score(crowd_type, city, age, result))
  240. # return result
  241. # data_list = []
  242. # for e in data_start:
  243. # for key in e.keys():
  244. # values = e[key]
  245. # for sub_e in values:
  246. # ele = [key]
  247. # ele.extend(sub_e)
  248. # data_list.append(ele)
  249. # pass
  250. return {'tag_score': result, 'module_score': module_scores}
  251. # return {'score': result, 'data': data_list}
  252. def behavior_tag_init(self, city, age, people_uuids):
  253. result = {}
  254. self.group_type_count = self.marketing_db.select(self.sql_5, [city, city, age, people_uuids])
  255. for key in self.tag_data:
  256. values = self.tag_data[key]
  257. elements = []
  258. for value in values:
  259. question = value[0].split('-')[0]
  260. option = value[0].split('-')[1]
  261. corr = value[1]
  262. fz, fm = self.molecular_value(question, option, city, age, people_uuids)
  263. if fm == 0:
  264. c = 0
  265. else:
  266. c = fz / fm
  267. elements.append([question, option, corr, fz, fm, c])
  268. result[key] = elements
  269. return self.indicator_calculation_d_e(result)
  270. def molecular_value(self, queston, option, city, age, people_uuids):
  271. # 获取当前父选项包含的子选项id和子题id列表
  272. result = self.shangju_db.select(self.sql_6, [option, queston])
  273. sub_option_ids = []
  274. group_types = []
  275. for rt in result:
  276. sub_option_id, sub_question_id, content = rt[0], rt[1], rt[2]
  277. grouptypes = self.shangju_db.select(self.sql_7, [sub_question_id])
  278. for g_t in grouptypes:
  279. if g_t[0] not in group_types:
  280. group_types.append(g_t[0])
  281. sub_option_ids.append(sub_option_id)
  282. # 计算子选项在答题记录中的点击数
  283. sub_options_count = 0
  284. if len(sub_option_ids) > 0:
  285. result_1 = self.marketing_db.select(self.sql_8, [sub_option_ids, city, city, age, people_uuids])
  286. sub_options_count = result_1[0][0]
  287. # 计算父选项包含的子选项对应的子题所在的测试gt包含的点击数。
  288. denominator_value = 0
  289. for info in self.group_type_count:
  290. if info[0] in group_types:
  291. denominator_value += info[1]
  292. return sub_options_count, denominator_value
  293. def indicator_calculation_d_e(self, data):
  294. result = {}
  295. for key in data.keys():
  296. values = data[key]
  297. c_list = []
  298. for x in values:
  299. _x = x[5]
  300. if _x is not None and x != 0:
  301. c_list.append(_x)
  302. fm_list = [x[4] for x in values]
  303. sum_c = sum(fm_list)
  304. if len(c_list) == 0:
  305. min_c = 0
  306. else:
  307. min_c = min(c_list)
  308. elements = []
  309. for value in values:
  310. _value = []
  311. c = value[5]
  312. if sum_c == 0:
  313. d = 0
  314. else:
  315. d = c / sum_c
  316. e = c - min_c
  317. _value.extend(value)
  318. _value.append(d)
  319. _value.append(e)
  320. elements.append(_value)
  321. result[key] = elements
  322. return result
  323. def calculation_standard_score(self, datas, city, age, crowd_type):
  324. scores = []
  325. for key in datas.keys():
  326. print(key)
  327. print(' 父题序号 父选项序号 相关系系数 分子值 分母值 百分比 人数权重 偏离值')
  328. values = [x[5] for x in datas[key]]
  329. min_c = min(values)
  330. f = min_c
  331. for value in datas[key]:
  332. print(' {}'.format(value))
  333. if value[2] is not None and value[7] is not None:
  334. f += float(value[2] * value[7])
  335. print(' 标准分:{}'.format(f))
  336. scores.append([city, age, key, crowd_type, f])
  337. # self.shangju_db.add_some(self.sql_9, scores)
  338. return scores
  339. def get_crowd_people(self):
  340. result = {}
  341. for type in self.crowd:
  342. uuids = self.get_people_uuid_by_type(type)
  343. result[type] = len(uuids)
  344. return result
  345. def get_people_uuid_by_type(self, type):
  346. uuids = []
  347. type_sub_option_ids = self.crowd_contain_sub_option_ids[type]
  348. for people in self.people_sub_option_ids:
  349. uuid = people[0]
  350. sub_option_ids = list(map(int, str(people[1]).split(',')))
  351. # list(set(a).intersection(set(b)))
  352. if len(list(set(sub_option_ids).intersection(set(type_sub_option_ids)))) > 0 and uuid not in uuids:
  353. uuids.append(uuid)
  354. return uuids
  355. def get_crowd_contain_sub_option_ids(self):
  356. """
  357. 获取ABCDEF人群包含的子选项id
  358. :return:
  359. """
  360. infos = {}
  361. for key in self.crowd_info.keys():
  362. values = self.crowd_info[key]
  363. sub_option_ids = []
  364. for value in values:
  365. if value is not None:
  366. vals = str(value).split('-')
  367. option, question = vals[1], vals[0]
  368. query_result = self.shangju_db.select(self.sql_6, [option, question])
  369. for qr in query_result:
  370. sub_option_id, sub_question_id, content = qr[0], qr[1], qr[2]
  371. sub_option_ids.append(int(sub_option_id))
  372. infos[key] = sub_option_ids
  373. print(infos)
  374. return infos
  375. class ExcelUtil:
  376. # 当前项目路径
  377. dir_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + r'/elab_mvp/resources'
  378. """
  379. 解析excel文件
  380. """
  381. def __init__(self, sheet_name=None, file_name=None):
  382. if file_name:
  383. self.path = os.path.join(self.dir_path, file_name)
  384. else:
  385. self.path = os.path.join(self.dir_path, 'mvp.xlsx')
  386. if sheet_name:
  387. self.sheet_name = sheet_name
  388. else:
  389. self.sheet_name = '硬标签+行为'
  390. def read_excel_by_pd(self):
  391. df = pd.read_excel(self.path)
  392. data = df.head()
  393. print('获取到的数据{}'.format(data))
  394. def read_excel_by_ox(self):
  395. work_book = ox.load_workbook(self.path, data_only=True)
  396. work_sheet = work_book.get_sheet_by_name(self.sheet_name)
  397. # print('max_row:{}, max_col:{}'.format(work_sheet.max_row, work_sheet.max_column))
  398. return work_sheet
  399. def init_crowd_info(self):
  400. """
  401. 整理不同人群包含的父选序号
  402. :return:
  403. """
  404. rows = [row for row in self.read_excel_by_ox().rows]
  405. crowd_a = []
  406. crowd_b = []
  407. crowd_c = []
  408. crowd_d = []
  409. crowd_e = []
  410. crowd_f = []
  411. for row in rows[2:]:
  412. option = row[4].value
  413. a = row[6].value
  414. if a is not None and a == 1 and option not in crowd_a:
  415. crowd_a.append(option)
  416. b = row[7].value
  417. if b is not None and b == 1 and option not in crowd_b:
  418. crowd_b.append(option)
  419. c = row[8].value
  420. if c is not None and c == 1 and option not in crowd_d:
  421. crowd_c.append(option)
  422. d = row[9].value
  423. if d is not None and d == 1 and option not in crowd_d:
  424. crowd_d.append(option)
  425. e = row[10].value
  426. if e is not None and e == 1 and option not in crowd_e:
  427. crowd_e.append(option)
  428. f = row[11].value
  429. if f is not None and f == 1 and option not in crowd_f:
  430. crowd_f.append(option)
  431. return {'A': crowd_a, 'B': crowd_b, 'C': crowd_c, 'D': crowd_d, 'E': crowd_e, 'F': crowd_f}
  432. def init_mvp_data(self):
  433. """
  434. 获取每个标签包括的父题父选项编号
  435. :return:
  436. """
  437. rows = [row for row in self.read_excel_by_ox().rows][24:]
  438. tag_name = None
  439. datas = []
  440. for row in rows:
  441. tag = row[1].value
  442. values = row[3].value
  443. corr = row[4].value
  444. if tag:
  445. tag_name = tag
  446. if values is not None:
  447. datas.append([tag_name, values, corr])
  448. result = {}
  449. for name, items in groupby(datas, key=lambda obj: obj[0]):
  450. orders = []
  451. for n in items:
  452. orders.append([n[1], n[2]])
  453. result[name] = orders
  454. return result
  455. def init_scores(self):
  456. work_sheet = self.read_excel_by_ox()
  457. rows = [row for row in work_sheet.rows]
  458. datas = []
  459. for row in rows[1:]:
  460. if row[0].value is not None:
  461. datas.append([row[0].value, row[1].value, row[2].value, row[3].value, row[4].value])
  462. return datas
  463. def init_module_info(self):
  464. work_sheet = self.read_excel_by_ox()
  465. max_column = work_sheet.max_column
  466. rows = [row for row in work_sheet.rows][3:]
  467. crowd_name = None
  468. datas = []
  469. for row in rows:
  470. crowd = row[1].value
  471. if crowd is not None:
  472. crowd_name = crowd
  473. behavior = row[2].value
  474. score = row[4].value
  475. for index in range(6, max_column - 1, 2):
  476. module_name = row[index].value
  477. if module_name is not None:
  478. weight = row[index + 1].value
  479. datas.append([crowd_name, behavior, score, module_name, weight])
  480. results = {}
  481. datas.sort(key=lambda obj: obj[0])
  482. for name, items in groupby(datas, key=lambda obj: obj[0]):
  483. sub_results = {}
  484. sub_list = []
  485. for it in items:
  486. sub_list.append([x for x in it])
  487. sub_list.sort(key=lambda obj: obj[3])
  488. for name_1, itmes_1 in groupby(sub_list, key=lambda obj: obj[3]):
  489. sub_data = []
  490. for n in itmes_1:
  491. # print(' {}'.format(n[1]))
  492. sub_data.append([n[1], n[2], n[4]])
  493. sub_results[name_1] = sub_data
  494. results[name] = sub_results
  495. return results
  496. class MysqlDB:
  497. """
  498. mysql操作
  499. """
  500. con = None
  501. cursor = None
  502. def __init__(self, db_name):
  503. self.db_name = db_name
  504. self.con = ps.connect(host='172.19.189.136', port=3306, user='bi_etl', password='XPtpswuU5lwGo4kx',
  505. db=self.db_name, charset='utf8')
  506. # self.con = ps.connect(host='192.168.0.13', port=3306, user='root', password='elab@123'
  507. # , db=self.db_name, charset='utf8')
  508. self.cursor = self.con.cursor()
  509. def show_tables(self):
  510. self.cursor.execute('show tables')
  511. for talbe in self.cursor.fetchall():
  512. print(talbe)
  513. def select(self, sql, params=None):
  514. if params:
  515. self.cursor.execute(sql, params)
  516. else:
  517. self.cursor.execute(sql)
  518. return self.cursor.fetchall()
  519. def add_some(self, sql, data):
  520. try:
  521. self.cursor.executemany(sql, data)
  522. self.con.commit()
  523. except:
  524. print('数据插入异常...')
  525. self.con.rollback()
  526. def add_one(self, sql, data):
  527. try:
  528. self.cursor.execute(sql, data)
  529. self.con.commit()
  530. except:
  531. self.con.rollback()
  532. def truncate(self, table_name):
  533. sql = 'truncate table {}'.format(table_name)
  534. self.cursor.execute(sql)
  535. self.con.commit()
  536. def close(self):
  537. self.cursor.close()
  538. self.con.close()
  539. @app.route('/behavioral_statistics', methods=['GET', 'POST'])
  540. def behavioral_statistics():
  541. """
  542. 父选项对应的标准化值
  543. :return:
  544. """
  545. city = request.args.get('city', default=None, type=str)
  546. age = request.args.get('age', default=None, type=str)
  547. crowd = request.args.get('crowd', default=None, type=str)
  548. print(city, age, crowd)
  549. mvp = Mvp()
  550. scores = mvp.query_behavioral_info(city, age, crowd)
  551. mvp.shangju_db.close()
  552. mvp.marketing_db.close()
  553. return json.dumps(scores, ensure_ascii=False)
  554. @app.route('/infos', methods=["GET", 'POST'])
  555. def get_city_age_crowd():
  556. """
  557. 测试数据中城市 年龄 人群分类信息
  558. :return:
  559. """
  560. mvp = Mvp()
  561. infos = {'城市': mvp.citys, '年龄段': mvp.age, '人群分类': mvp.crowd}
  562. mvp.shangju_db.close()
  563. mvp.marketing_db.close()
  564. return json.dumps(infos, ensure_ascii=False)
  565. @app.route('/crowd_people', methods=['GET', 'POST'])
  566. def crowd_people():
  567. """
  568. 人群分类人数统计
  569. :return:
  570. """
  571. mvp = Mvp()
  572. people_count = mvp.get_crowd_people()
  573. mvp.shangju_db.close()
  574. mvp.marketing_db.close()
  575. return json.dumps(people_count, ensure_ascii=False)
  576. @app.route('/set_behavior_tag', methods=['GET', 'POST'])
  577. def set_behavior_tag():
  578. """
  579. 模块标准化值
  580. :return:
  581. """
  582. mvp = Mvp()
  583. return json.dumps(mvp.module_scores, ensure_ascii=False)
  584. @app.route('/insert_into', methods=['GET', 'POST'])
  585. def insert_info():
  586. mvp = Mvp()
  587. mvp.insert()
  588. query_data = mvp.query_data()
  589. return json.dumps(query_data, ensure_ascii=False)
  590. @app.route('/insert_score', methods=['GET', 'POST'])
  591. def insert_score():
  592. mvp = Mvp()
  593. mvp.tag_module_score_insert()
  594. return '!!!分数添加成功!!!'
  595. @app.route('/shanghai_85', methods=['GET', 'POST'])
  596. def shanghai_85():
  597. mvp = Mvp()
  598. data = mvp.shanghai_85_module_score_insert()
  599. return json.dumps(data, ensure_ascii=False)
  600. if __name__ == '__main__':
  601. app.run(
  602. host='0.0.0.0',
  603. port=5001
  604. )