Blame view

offline_tasks/test/check_country_fields.py 2.3 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
  """
  检查 sensors_events 表是否有国家相关字段
  """
  import sys
  import os
  sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  
  from db_service import create_db_connection
  from offline_tasks.config.offline_config import DB_CONFIG
  import pandas as pd
  
  # 创建数据库连接
  print("连接数据库...")
  engine = create_db_connection(
      DB_CONFIG['host'],
      DB_CONFIG['port'],
      DB_CONFIG['database'],
      DB_CONFIG['username'],
      DB_CONFIG['password']
  )
  
  print("\n" + "="*80)
  print("检查 sensors_events 表的所有字段")
  print("="*80)
  
  # 查看表结构
  try:
      df = pd.read_sql("DESCRIBE sensors_events", engine)
      print("\n表所有字段列表:")
      print(df.to_string())
  
      # 查找可能包含国家信息的字段
      country_keywords = ['country', '国家', 'geo', 'nation', '地区', 'region', 'location']
      print("\n" + "="*80)
      print("可能包含国家/地区信息的字段:")
      print("="*80)
  
      for field in df['Field'].values:
          field_lower = field.lower()
          for keyword in country_keywords:
              if keyword in field_lower:
                  print(f"  - {field}")
  
      # 特别检查一些可能的字段
      potential_fields = ['country', 'geo_country', 'user_country', 'ip_country',
                         'location_country', 'browser_country', 'device_country']
  
      print("\n" + "="*80)
      print("检查常见国家字段名称:")
      print("="*80)
  
      all_fields = df['Field'].values.tolist()
      for field in potential_fields:
          if field in all_fields:
              print(f"  ✓ 找到字段: {field}")
              # 查询该字段的样本数据
              try:
                  sample_df = pd.read_sql(f"SELECT DISTINCT {field} FROM sensors_events WHERE {field} IS NOT NULL LIMIT 20", engine)
                  print(f"    样本值: {sample_df[field].tolist()}")
              except:
                  pass
          else:
              print(f"  ✗ 未找到: {field}")
  
      # 查看所有字段名称的列表(方便手动检查)
      print("\n" + "="*80)
      print("所有字段名称列表(用于手动检查):")
      print("="*80)
      for i, field in enumerate(all_fields, 1):
          print(f"{i:3d}. {field}")
  
  except Exception as e:
      print(f"无法获取表结构: {e}")
  
  print("\n" + "="*80)
  print("检查完成")
  print("="*80)