test_info.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. def __init__(self):
  100. # self.shangju_db = MysqlDB('shangju')
  101. self.bi_report_db = MysqlDB('bi_report')
  102. def test_detail_info(self, testcase_id):
  103. """
  104. 查看每套测试回收数据的量
  105. :return:
  106. """
  107. response = {}
  108. try:
  109. people = self.bi_report_db.select(self.sql_4, [testcase_id])[0][0]
  110. finished = self.bi_report_db.select(self.sql_3, [testcase_id])[0][0]
  111. result = self.bi_report_db.select(self.sql_1, [testcase_id])
  112. count = self.bi_report_db.select(self.sql_5, [testcase_id])
  113. answers = []
  114. for index, data in enumerate(result):
  115. if index == 0:
  116. response['testcase_id'] = data[0]
  117. response['title'] = data[1]
  118. response['答题记录数'] = count[0][0]
  119. response['答题人数'] = people
  120. response['完成人数'] = finished
  121. if people != 0:
  122. response['答题完成率'] = float(finished) / float(people)
  123. else:
  124. response['答题完成率'] = '无法统计'
  125. answers.append([data[2], data[3], data[4], data[5], data[6]])
  126. answers.sort(key=lambda obj: obj[0])
  127. sub_question_data = []
  128. for sub_question_id, others in groupby(answers, key=lambda obj: obj[0]):
  129. others_data = []
  130. for ot in others:
  131. others_data.append([x for x in ot])
  132. sub_question = {}
  133. sub_question['id'] = sub_question_id
  134. sub_question['题干'] = others_data[0][1]
  135. sub_option = []
  136. for th in others_data:
  137. sub_option.append([th[2], th[3], th[4]])
  138. sub_question['option'] = sub_option
  139. sub_question_data.append(sub_question)
  140. response['答题结果统计'] = sub_question_data
  141. except Exception as e:
  142. response['error'] = str(e)
  143. return response