test_info.py 3.4 KB

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