Blame view

offline_tasks/test/query_sensors_client_platform.py 4.44 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
  #!/usr/bin/env python3
  """查询 sensors_events 表的 client_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_client_platform():
      """查询 client_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("=" * 80)
              print("sensors_events 表的 client_platform 字段值分布")
              print("=" * 80)
  
              # 查询 client_platform 的分布情况
              sql = """
              SELECT
                  client_platform,
                  COUNT(*) as count,
                  ROUND(COUNT(*) * 100.0 / SUM(COUNT(*)) OVER(), 2) as percentage
              FROM sensors_events
              WHERE client_platform IS NOT NULL
              GROUP BY client_platform
              ORDER BY count DESC
              """
  
              cursor.execute(sql)
              result = cursor.fetchall()
  
              print(f"{'client_platform':<30} {'数量':>15} {'占比':>10}")
              print("-" * 80)
  
              total_count = 0
              for row in result:
                  platform = row[0] if row[0] else '(NULL)'
                  count = row[1]
                  percentage = row[2]
                  total_count += count
  
                  print(f"{platform:<30} {count:>15,} {percentage:>10}%")
  
              print("-" * 80)
              print(f"{'总计':<30} {total_count:>15,}")
  
              # 查询一些示例记录
              print("\n" + "=" * 100)
              print("示例记录(包含 client_platform 相关字段)")
              print("=" * 100)
  
              sql_sample = """
              SELECT
                  id,
                  client_platform,
                  platform,
                  browser,
                  browser_language,
                  device_id,
                  event,
                  time
              FROM sensors_events
              WHERE client_platform IS NOT NULL
              LIMIT 20
              """
  
              cursor.execute(sql_sample)
              samples = cursor.fetchall()
  
              print(f"{'ID':<15} {'client_platform':<20} {'platform':<15} {'browser':<20} {'browser_language':<15} {'event':<30}")
              print("-" * 140)
  
              for sample in samples:
                  id_val = str(sample[0])
                  client_platform = sample[1] if sample[1] else ''
                  platform = sample[2] if sample[2] else ''
                  browser = sample[3] if sample[3] else ''
                  browser_language = sample[4] if sample[4] else ''
                  event = sample[6] if sample[6] else ''
  
                  # 截断过长的字段
                  event = (event[:27] + '..') if len(event) > 30 else event
  
                  print(f"{id_val:<15} {client_platform:<20} {platform:<15} {browser:<20} {browser_language:<15} {event:<30}")
  
              print("-" * 140)
              print(f"总计: {len(samples)} 条示例记录")
  
              # 查询 client_platform 和 platform 的组合分布
              print("\n" + "=" * 100)
              print("client_platform 与 platform 的组合分布")
              print("=" * 100)
  
              sql_combo = """
              SELECT
                  client_platform,
                  platform,
                  COUNT(*) as count
              FROM sensors_events
              WHERE client_platform IS NOT NULL
                AND platform IS NOT NULL
              GROUP BY client_platform, platform
              ORDER BY client_platform, count DESC
              """
  
              cursor.execute(sql_combo)
              combos = cursor.fetchall()
  
              current_platform = None
              for combo in combos:
                  client_platform = combo[0]
                  platform = combo[1]
                  count = combo[2]
  
                  if client_platform != current_platform:
                      print(f"\n【{client_platform}】")
                      current_platform = client_platform
  
                  print(f"  {platform:<30} {count:>12,} 条")
  
      except Exception as e:
          print(f"查询失败: {e}")
          import traceback
          traceback.print_exc()
      finally:
          if 'connection' in locals():
              connection.close()
  
  if __name__ == "__main__":
      query_client_platform()