Browse Source

elab_mvp: 同策测试数据清洗程序

Signed-off-by: binren <zhangbr@elab-plus.com>
binren 5 years ago
parent
commit
a10f6f7d34
2 changed files with 139 additions and 6 deletions
  1. 1 1
      flask_app.py
  2. 138 5
      tongce.py

+ 1 - 1
flask_app.py

@@ -154,7 +154,7 @@ def tongce():
     response = {}
     try:
         tongce = TongCe()
-        result = tongce.get_question_info_from_db()
+        result = tongce.tongce()
         response['code'] = 0
         response['message'] = '成功'
         response['data'] = result

+ 138 - 5
tongce.py

@@ -1,5 +1,7 @@
 from excel_util import ExcelUtil
 from mysql_db import MysqlDB
+from itertools import groupby
+
 
 
 class TongCe:
@@ -9,8 +11,6 @@ class TongCe:
     # 统计筒体结果
     sql_1 = '''
         SELECT
-			b.father_id,
-			b.father_content,
             a.sub_question_id,
             b.sub_question_content,
             a.score,
@@ -20,7 +20,7 @@ class TongCe:
             f_t_daren_score_2 a
         LEFT JOIN d_shangju_tiku_02 b ON a. STATUS = b. STATUS = 1
         WHERE
-            a.testcase_id in (84, 85, 86, 87) and
+            a.testcase_id in %s and
             a.testcase_id = b.testcase_id
         AND a.sub_question_id = b.sub_question_id
         AND (
@@ -28,8 +28,6 @@ class TongCe:
             OR a.score = b.sub_option_id
         )
         GROUP BY
-			b.father_id,
-            a.sub_question_id,
             b.sub_question_content,
             a.score,
             b.sub_option_content
@@ -71,6 +69,89 @@ class TongCe:
             a.id
     '''
 
+    # 表
+    sql_3 = '''
+        INSERT INTO mvp_page_display_match (
+            house_id,
+            question_id,
+            question_title,
+            sub_question_id,
+            sub_question_title,
+            option_id,
+            option_content,
+            sub_option_id,
+            sub_option_content,
+            data_item_tab,
+            data_item_title,
+            data_item_name,
+            STATUS,
+            creator,
+            created
+        )
+        VALUES
+            (
+                %s,
+                %s,
+                %s,
+                %s,
+                %s,
+                %s,
+                %s,
+                %s,
+                %s,
+                %s,
+                %s,
+                %s,
+                1,
+                'binren',
+                now()
+            )
+    '''
+
+    sql_4 = '''
+        SELECT
+            id,
+            sub_question_id,
+            sub_option_id
+        FROM
+            mvp_page_display_match
+        WHERE
+            STATUS = 1
+    '''
+
+    sql_5 = '''
+            SELECT
+                id
+            FROM
+                bq_testcase
+            WHERE
+                STATUS = 1
+            AND FIND_IN_SET(
+                (
+                    SELECT
+                        id
+                    FROM
+                        bq_house
+                    WHERE
+                        STATUS = 1
+                    AND NAME = %s
+                ),
+                house_ids
+            )
+    '''
+
+    sql_6 = '''
+        insert INTO mvp_page_display_data (
+            match_id,
+            value,
+            STATUS,
+            creator,
+            created
+        )
+        VALUES
+            (%s, %s, 1, 'binren', now())
+    '''
+
     def __init__(self):
         self.shangju_db = MysqlDB('shangju')
         self.marketing_db = MysqlDB('bi_report')
@@ -84,8 +165,60 @@ class TongCe:
             rt = list(rt)
             option_configuration = self.options_info.get('67' + str(rt[6]))
             if option_configuration and len(option_configuration) == 3:
+                rt.insert(0, 67)
                 rt.extend(option_configuration)
                 insert_data.append(rt)
         return insert_data
 
+    def get_option_match_info(self):
+        result = self.shangju_db.select(self.sql_4)
+        return result
+
+    def get_testcase_ids_by_house_name(self, house_name):
+        testcase_ids = self.shangju_db.select(self.sql_5, [house_name])
+        return testcase_ids
+
+    def scores(self):
+        testcase_ids = self.get_testcase_ids_by_house_name('同策 领地')
+        db_data = self.marketing_db.select(self.sql_1, [testcase_ids])
+        answer = []
+        for data in db_data:
+            answer.append([data[0], data[2], data[4]])
+        answer.sort(key=lambda obj: obj[0])
+        sub_option_score = []
+        for sub_question_id, others in groupby(answer, key=lambda obj: obj[0]):
+            others_data = []
+            for ot in others:
+                others_data.append([x for x in ot])
+            sub_question_count = sum([x[2] for x in others_data])
+            for td in others_data:
+                sub_option_id = td[1]
+                sub_option_count = td[2]
+                rate = int(sub_option_count) / sub_question_count
+                sub_option_score.append([sub_question_id, sub_option_id, rate])
+        return sub_option_score
+
+    def tongce(self):
+        """
+            tongce测试数据清洗
+        :return:
+        """
+        match_data = self.get_question_info_from_db()
+        self.linshi_db.add_some(self.sql_3, match_data)
+        scores = self.scores()
+        match_data_info = self.get_option_match_info()
+        dispaly_data = []
+        for score in scores:
+            sub_question_id = score[0]
+            sub_option_id = score[1]
+            value = score[2]
+            for mi in match_data_info:
+                if str(mi[1]) == str(sub_question_id) and str(mi[2]) == str(sub_option_id):
+                    dispaly_data.append([mi[0], value])
+        if len(dispaly_data) > 0:
+            self.shangju_db.add_some(self.sql_6, [dispaly_data])
+        return {'插入数据条数': len(dispaly_data)}
+
+
+if __name__ == '__main__':
     pass