test_info.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. def __init__(self):
  80. # self.shangju_db = MysqlDB('shangju')
  81. self.bi_report_db = MysqlDB('bi_report')
  82. def test_detail_info(self, testcase_id):
  83. """
  84. 查看每套测试回收数据的量
  85. :return:
  86. """
  87. response = {}
  88. try:
  89. people = self.bi_report_db.select(self.sql_4, [testcase_id])[0][0]
  90. finished = self.bi_report_db.select(self.sql_3, [testcase_id])[0][0]
  91. result = self.bi_report_db.select(self.sql_1, [testcase_id])
  92. count = self.bi_report_db.select(self.sql_5, [testcase_id])
  93. answers = []
  94. for index, data in enumerate(result):
  95. if index == 0:
  96. response['testcase_id'] = data[0]
  97. response['title'] = data[1]
  98. response['答题记录数'] = count[0][0]
  99. response['答题人数'] = people
  100. response['完成人数'] = finished
  101. if people != 0:
  102. response['答题完成率'] = float(finished) / float(people)
  103. else:
  104. response['答题完成率'] = '无法统计'
  105. answers.append([data[2], data[3], data[4], data[5], data[6]])
  106. answers.sort(key=lambda obj: obj[0])
  107. sub_question_data = []
  108. for sub_question_id, others in groupby(answers, key=lambda obj: obj[0]):
  109. others_data = []
  110. for ot in others:
  111. others_data.append([x for x in ot])
  112. sub_question = {}
  113. sub_question['id'] = sub_question_id
  114. sub_question['题干'] = others_data[0][1]
  115. sub_option = []
  116. for th in others_data:
  117. sub_option.append([th[2], th[3], th[4]])
  118. sub_question['option'] = sub_option
  119. sub_question_data.append(sub_question)
  120. response['答题结果统计'] = sub_question_data
  121. except Exception as e:
  122. response['error'] = str(e)
  123. return response