Blame view

offline_tasks/test/query_sensors_platform_combo.py 4.87 KB
0f35b7e1   tangwang   tests
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
  #!/usr/bin/env python3
  """查询 sensors_events 表的 platform 组合分布"""
  
  import os
  import pymysql
  import sys
  sys.path.append(os.path.dirname(os.path.abspath(__file__)))
  from config.offline_config import DB_CONFIG
  
  def query_platform_combinations():
      """查询 platform 相关字段的组合分布"""
      try:
          connection = pymysql.connect(
              host=DB_CONFIG['host'],
              port=int(DB_CONFIG['port']),
              user=DB_CONFIG['username'],
              password=DB_CONFIG['password'],
              database=DB_CONFIG['database'],
              charset='utf8mb4'
          )
  
          with connection.cursor() as cursor:
              print("=" * 100)
              print("client_platform 与 business_platform 的组合分布")
              print("=" * 100)
  
              sql = """
              SELECT
                  client_platform,
                  business_platform,
                  COUNT(*) as count,
                  ROUND(COUNT(*) * 100.0 / SUM(COUNT(*)) OVER(PARTITION BY client_platform), 2) as percentage_in_client
              FROM sensors_events
              WHERE client_platform IS NOT NULL
                AND business_platform IS NOT NULL
              GROUP BY client_platform, business_platform
              ORDER BY client_platform, count DESC
              """
  
              cursor.execute(sql)
              result = cursor.fetchall()
  
              current_platform = None
              for row in result:
                  client_platform = row[0]
                  business_platform = row[1]
                  count = row[2]
                  percentage = row[3]
  
                  if client_platform != current_platform:
                      print(f"\n【client_platform = {client_platform}】")
                      current_platform = client_platform
  
                  print(f"  business_platform: {business_platform:<20} | 数量: {count:>12,} | 占该平台: {percentage:>6}%")
  
              # 查询不同 client_platform 的用户行为统计
              print("\n" + "=" * 100)
              print("不同 client_platform 的用户行为统计")
              print("=" * 100)
  
              sql_events = """
              SELECT
                  client_platform,
                  event,
                  COUNT(*) as count
              FROM sensors_events
              WHERE client_platform IS NOT NULL
                AND event IS NOT NULL
              GROUP BY client_platform, event
              ORDER BY client_platform, count DESC
              """
  
              cursor.execute(sql_events)
              events = cursor.fetchall()
  
              current_platform = None
              event_count = 0
              max_events = 5  # 每个平台最多显示5个事件
  
              for row in events:
                  client_platform = row[0]
                  event = row[1]
                  count = row[2]
  
                  if client_platform != current_platform:
                      if current_platform is not None:
                          print("")
                      print(f"\n【client_platform = {client_platform}】Top 事件:")
                      current_platform = client_platform
                      event_count = 0
  
                  if event_count < max_events:
                      print(f"  {event:<40} | {count:>12,}")
                      event_count += 1
  
              # 查询不同 client_platform 的时间分布
              print("\n" + "=" * 100)
              print("不同 client_platform 的时间分布(最近30天)")
              print("=" * 100)
  
              sql_time = """
              SELECT
                  client_platform,
                  DATE(create_time) as event_date,
                  COUNT(*) as count
              FROM sensors_events
              WHERE client_platform IS NOT NULL
                AND create_time >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)
              GROUP BY client_platform, DATE(create_time)
              ORDER BY client_platform, event_date DESC
              LIMIT 30
              """
  
              cursor.execute(sql_time)
              time_data = cursor.fetchall()
  
              current_platform = None
              day_count = 0
              max_days = 5  # 每个平台最多显示5天
  
              for row in time_data:
                  client_platform = row[0]
                  event_date = str(row[1])
                  count = row[2]
  
                  if client_platform != current_platform:
                      if current_platform is not None:
                          print("")
                      print(f"\n【client_platform = {client_platform}】最近活动:")
                      current_platform = client_platform
                      day_count = 0
  
                  if day_count < max_days:
                      print(f"  {event_date} | {count:>12,}")
                      day_count += 1
  
      except Exception as e:
          print(f"查询失败: {e}")
          import traceback
          traceback.print_exc()
      finally:
          if 'connection' in locals():
              connection.close()
  
  if __name__ == "__main__":
      query_platform_combinations()