Browse Source

统计逻辑修改

Signed-off-by: Binren Zhnag <zhangbr@elab-plus.com>
Binren Zhnag 4 years ago
parent
commit
a6b6ce6a01
2 changed files with 142 additions and 11 deletions
  1. 53 11
      jianye_report.py
  2. 89 0
      sql.py

+ 53 - 11
jianye_report.py

@@ -33,7 +33,12 @@ class JianYeReport(object):
 
     def __init__(self):
         self.db = MysqlDB('bi_report')
-        pass
+        time_range_1 = rpfu.get_prd_day()
+        time_range_2 = rpfu.get_time_range_month()
+        time_range_3 = rpfu.get_all_time_data_range()
+        self.pv_data_1 = self.pv_count(time_range_1)
+        self.pv_data_2 = self.pv_count(time_range_2)
+        self.pv_data_3 = self.pv_count(time_range_3)
 
     def get_city_house_id(self):
         return self.db.select(Sql.sql_1)
@@ -73,6 +78,15 @@ class JianYeReport(object):
         result = self.db.select(Sql.sql_8, [time_range[0], time_range[1]])
         return result
 
+    def pv_count(self, time_range):
+        """
+            新的用户行为统计
+        :param time_range:
+        :return:
+        """
+        sql_result = self.db.select(Sql.sql_13, [time_range[0], time_range[1]])
+        return sql_result
+
     def user_data_volume_statistics(self, time_range, house_ids):
         """
             用户浏览量,人数,新增获客,新增获点数据统计
@@ -118,10 +132,27 @@ class JianYeReport(object):
                 result.append(x[1])
         return result
 
-    def brand_data_of_time(self, time_range):
+    def get_pv_data(self, time_type):
+        pv_data = None
+        if time_type == 1:
+            pv_data = self.pv_data_1
+        elif time_type == 2:
+            pv_data = self.pv_data_2
+        elif time_type == 3:
+            pv_data = self.pv_data_3
+        return pv_data
+
+    def brand_pv_by_time_type(self, time_type):
+        pv_data = self.get_pv_data(time_type)
+        if pv_data:
+            for x in pv_data:
+                if str(x[0]) == self.brand_id and x[2] is not None and x[2] == '集团':
+                    return x[3:]
+        return [0, 0, 0, 0]
+
+    def brand_data_of_time(self, time_range, time_type):
         result = []
-        ids = self.get_house_id_by_brand_id(self.brand_id)
-        data_1 = self.user_data_volume_statistics(time_range, ids)
+        data_1 = self.brand_pv_by_time_type(time_type)
         data_2 = []
         number_2 = 0
         for x in self.get_recommend_data(time_range):
@@ -150,18 +181,27 @@ class JianYeReport(object):
         time_rang_1 = rpfu.get_prd_day()
         time_rang_2 = rpfu.get_time_range_month()
         time_range_3 = rpfu.get_all_time_data_range()
-        result.extend(self.brand_data_of_time(time_rang_1))
-        result.extend(self.brand_data_of_time(time_rang_2))
-        result.extend(self.brand_data_of_time(time_range_3))
+        result.extend(self.brand_data_of_time(time_rang_1, 1))
+        result.extend(self.brand_data_of_time(time_rang_2, 2))
+        result.extend(self.brand_data_of_time(time_range_3, 3))
         return result
 
-    def house_data_of_time(self, time_range, house_ids):
+    def get_house_pv_data(self, house_id, time_type):
+        pv_data = self.get_pv_data(time_type)
+        if pv_data:
+            for x in pv_data:
+                if str(x[1]) == str(house_id):
+                    return x[3:]
+        else:
+            return [0, 0, 0, 0]
+
+    def house_data_of_time(self, time_range, house_ids, time_type):
         # brand_id, house_id, house_name, city
         city_info = self.get_city_house_id()
         data_1 = []
         for id in house_ids:
             sub = [id]
-            sub.extend(self.user_data_volume_statistics(time_range, [id]))
+            sub.extend(self.get_house_pv_data(id, time_type))
             data_1.append(sub)
         data_2 = []
         for id in house_ids:
@@ -210,8 +250,8 @@ class JianYeReport(object):
         time_range_1 = rpfu.get_prd_day()
         time_range_2 = rpfu.get_time_range_month()
         time_range_3 = rpfu.get_all_time_data_range()
-        result = [self.house_data_of_time(time_range_1, house_ids), self.house_data_of_time(time_range_2, house_ids),
-                  self.house_data_of_time(time_range_3, house_ids)]
+        result = [self.house_data_of_time(time_range_1, house_ids, 1), self.house_data_of_time(time_range_2, house_ids, 2),
+                  self.house_data_of_time(time_range_3, house_ids, 3)]
         return result
 
     def send_mail_to_customer(self, task_key):
@@ -295,6 +335,8 @@ class JianYeReport(object):
                     print(str(e))
                     #  # name, mail, report_name, push_time, send_status, status, error_message
                     send_mail_log.append([name, mail, title, -1, '数据查询失败:{}'.format(str(e))])
+                if len(send_data) == 2:
+                    break
             message['query_data'] = 'success'
             # 生成文件
             for data in send_data:

+ 89 - 0
sql.py

@@ -164,6 +164,95 @@ class Sql:
     """
     pass
 
+    sql_13 = """
+            SELECT
+                x.brand_id,
+                x.house_id,
+                x.house_name,
+                ifnull(x.pv, 0),
+                x.uv,
+                x.new_cust,
+                x.shouquan_cust
+            FROM
+                (
+                    SELECT
+                        c.pv,
+                        c.uv,
+                        a.brand_id,
+                        a.house_id,
+                        a.house_name,
+                        a.brand_name,
+                        ifnull(b.house_layout_num, 0) house_layout_num,
+                        ifnull(d.launch_time, '--') launch_time,
+                        c.new_cust,
+                        c.shouquan_cust,
+                        c.revisit_cust
+                    FROM
+                        (
+                            SELECT
+                                brand_id,
+                                ifnull(house_id, '0') house_id,
+                                sum(pv) pv,
+                                count(DISTINCT brand_user_id) uv,
+                                count(
+                                    DISTINCT CASE
+                                    WHEN is_new_user = 1 THEN
+                                        brand_user_id
+                                    END
+                                ) new_cust,
+                                count(
+                                    DISTINCT CASE
+                                    WHEN is_shouquan_user = 1 THEN
+                                        brand_user_id
+                                    END
+                                ) shouquan_cust,
+                                count(
+                                    DISTINCT CASE
+                                    WHEN is_new_user = 0 THEN
+                                        brand_user_id
+                                    END
+                                ) revisit_cust
+                            FROM
+                                a_behavior_brand_mini_day
+                            WHERE
+                                report_d >= %s
+                            AND report_d <= %s
+                            GROUP BY
+                                brand_id,
+                                ifnull(house_id, '0')
+                        ) c
+                    LEFT JOIN (
+                        SELECT
+                            house_id,
+                            count(1) house_layout_num
+                        FROM
+                            d_content_layout
+                        WHERE
+                            `status` = '1'
+                        AND house_id <> 1
+                        GROUP BY
+                            house_id
+                        UNION ALL
+                            SELECT
+                                bb.brand_id house_id,
+                                count(1) house_layout_num
+                            FROM
+                                d_content_layout aa
+                            JOIN d_house bb ON aa.house_id = bb.house_id
+                            WHERE
+                                aa.`status` = '1'
+                            AND bb. STATUS = '1'
+                            AND aa.house_id <> 1
+                            GROUP BY
+                                bb.brand_id
+                    ) b ON c.house_id = b.house_id
+                    JOIN d_house a ON a.house_id = c.house_id
+                    AND a.brand_id = c.brand_id
+                    LEFT JOIN d_house_attr d ON c.house_id = d.house_id
+                    AND c.brand_id = d.brand_id
+                ) x
+    """
+
 
 if __name__ == '__main__':
     print(len('222'))