test_info.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. from mysql_db import MysqlDB
  2. from itertools import groupby
  3. class TestInfo(object):
  4. """
  5. 测试回收情况
  6. """
  7. # 获取测试的答题记录情况
  8. sql_1 = '''
  9. SELECT
  10. a.testcase_id,
  11. a.title,
  12. a.sub_question_id,
  13. b.sub_question_content,
  14. a.score,
  15. b.sub_option_content,
  16. count(1)
  17. FROM
  18. f_t_daren_score_2 a
  19. LEFT JOIN d_shangju_tiku_02 b ON a. STATUS = b. STATUS = 1
  20. WHERE
  21. a.testcase_id = %s and
  22. a.testcase_id = b.testcase_id
  23. AND a.sub_question_id = b.sub_question_id
  24. AND (
  25. a.score = b.score
  26. OR a.score = b.sub_option_id
  27. )
  28. GROUP BY
  29. a.testcase_id,
  30. a.title,
  31. a.sub_question_id,
  32. b.sub_question_content,
  33. a.score,
  34. b.sub_option_content
  35. '''
  36. # 获取测试题信息
  37. sql_2 = '''
  38. SELECT
  39. id,
  40. group_type,
  41. title,
  42. house_ids,
  43. creator,
  44. created,
  45. updator,
  46. updated
  47. FROM
  48. bq_testcase
  49. WHERE
  50. STATUS = 1
  51. '''
  52. # 统计测试完成人数
  53. sql_3 = '''
  54. SELECT
  55. COUNT(DISTINCT uuid)
  56. FROM
  57. f_t_daren_score_2 a
  58. WHERE
  59. testcase_id = %s
  60. AND is_last = 1
  61. '''
  62. # 统计参与答题人数
  63. sql_4 = '''
  64. SELECT
  65. COUNT(DISTINCT uuid)
  66. FROM
  67. f_t_daren_score_2 a
  68. WHERE
  69. testcase_id = %s
  70. '''
  71. sql_5 = '''
  72. SELECT
  73. count(1)
  74. FROM
  75. f_t_daren_score_2
  76. WHERE
  77. testcase_id = %s
  78. '''
  79. sql_6 = '''
  80. SELECT
  81. score,
  82. COUNT(uuid)
  83. FROM
  84. (
  85. SELECT
  86. score,
  87. COUNT(DISTINCT uuid) AS uuid
  88. FROM
  89. f_t_daren_score_2
  90. WHERE
  91. testcase_id = 86
  92. AND sub_question_id = 377
  93. GROUP BY
  94. uuid
  95. ) a
  96. GROUP BY
  97. a.score
  98. '''
  99. # 支付力占比统计
  100. sql_7 = '''
  101. SELECT
  102. score,
  103. COUNT(uuid)
  104. FROM
  105. (
  106. SELECT
  107. score,
  108. COUNT(DISTINCT uuid) AS uuid
  109. FROM
  110. f_t_daren_score_2
  111. WHERE
  112. testcase_id in (84, 85, 86, 87)
  113. AND sub_question_id = 376
  114. GROUP BY
  115. uuid
  116. ) a
  117. GROUP BY
  118. a.score
  119. '''
  120. def __init__(self):
  121. # self.shangju_db = MysqlDB('shangju')
  122. self.bi_report_db = MysqlDB('bi_report')
  123. def test_detail_info(self, testcase_id):
  124. """
  125. 查看每套测试回收数据的量
  126. :return:
  127. """
  128. response = {}
  129. try:
  130. people = self.bi_report_db.select(self.sql_4, [testcase_id])[0][0]
  131. finished = self.bi_report_db.select(self.sql_3, [testcase_id])[0][0]
  132. result = self.bi_report_db.select(self.sql_1, [testcase_id])
  133. count = self.bi_report_db.select(self.sql_5, [testcase_id])
  134. answers = []
  135. for index, data in enumerate(result):
  136. if index == 0:
  137. response['testcase_id'] = data[0]
  138. response['title'] = data[1]
  139. response['答题记录数'] = count[0][0]
  140. response['答题人数'] = people
  141. response['完成人数'] = finished
  142. if people != 0:
  143. response['答题完成率'] = float(finished) / float(people)
  144. else:
  145. response['答题完成率'] = '无法统计'
  146. answers.append([data[2], data[3], data[4], data[5], data[6]])
  147. answers.sort(key=lambda obj: obj[0])
  148. sub_question_data = []
  149. for sub_question_id, others in groupby(answers, key=lambda obj: obj[0]):
  150. others_data = []
  151. for ot in others:
  152. others_data.append([x for x in ot])
  153. sub_question = {}
  154. sub_question['id'] = sub_question_id
  155. sub_question['题干'] = others_data[0][1]
  156. sub_option = []
  157. for th in others_data:
  158. sub_option.append([th[2], th[3], th[4]])
  159. sub_question['option'] = sub_option
  160. sub_question_data.append(sub_question)
  161. response['答题结果统计'] = sub_question_data
  162. except Exception as e:
  163. response['error'] = str(e)
  164. return response