Blame view

offline_tasks/test/check_geo_info.py 4.06 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
  """
  检查 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
  import json
  
  # 创建数据库连接
  print("连接数据库...")
  engine = create_db_connection(
      DB_CONFIG['host'],
      DB_CONFIG['port'],
      DB_CONFIG['database'],
      DB_CONFIG['username'],
      DB_CONFIG['password']
  )
  
  print("\n" + "="*80)
  print("检查地理位置相关信息")
  print("="*80)
  
  # 1. 检查浏览器语言
  print("\n1. 浏览器语言 (__browser_language):")
  try:
      df_lang = pd.read_sql("""
          SELECT __browser_language, COUNT(*) as count
          FROM sensors_events
          WHERE __browser_language IS NOT NULL
          GROUP BY __browser_language
          ORDER BY count DESC
          LIMIT 20
      """, engine)
      print(df_lang.to_string(index=False))
  except Exception as e:
      print(f"查询失败: {e}")
  
  # 2. 检查 IP 地址
  print("\n2. IP地址样本 (ip):")
  try:
      df_ip = pd.read_sql("""
          SELECT DISTINCT ip
          FROM sensors_events
          WHERE ip IS NOT NULL
          LIMIT 10
      """, engine)
      print(df_ip['ip'].tolist())
  except Exception as e:
      print(f"查询失败: {e}")
  
  # 3. 检查 __properties JSON字段中是否包含地理位置信息
  print("\n3. 检查 __properties 字段(可能包含地理位置):")
  try:
      df_props = pd.read_sql("""
          SELECT __properties
          FROM sensors_events
          WHERE __properties IS NOT NULL
          LIMIT 5
      """, engine)
  
      print("\n样本数据(前5条):")
      for i, prop in enumerate(df_props['__properties'].tolist(), 1):
          print(f"\n第{i}条:")
          if isinstance(prop, str):
              try:
                  prop_json = json.loads(prop)
                  print(json.dumps(prop_json, indent=2, ensure_ascii=False)[:500])
              except:
                  print(prop[:200])
          else:
              print(prop)
  except Exception as e:
      print(f"查询失败: {e}")
  
  # 4. 检查 __properties 中的所有键
  print("\n\n4. __properties 中的所有键名(查找地理位置相关):")
  try:
      df_props = pd.read_sql("""
          SELECT __properties
          FROM sensors_events
          WHERE __properties IS NOT NULL
          LIMIT 100
      """, engine)
  
      # 收集所有键
      all_keys = set()
      for prop in df_props['__properties'].tolist():
          if isinstance(prop, str):
              try:
                  prop_json = json.loads(prop)
                  if isinstance(prop_json, dict):
                      all_keys.update(prop_json.keys())
              except:
                  pass
  
      print(f"\n找到 {len(all_keys)} 个不同的键:")
      geo_keywords = ['country', 'geo', 'nation', '地区', '区域', 'city', 'province', 'state', 'location', 'region']
  
      for key in sorted(all_keys):
          is_geo = False
          for keyword in geo_keywords:
              if keyword.lower() in key.lower():
                  is_geo = True
                  break
  
          if is_geo:
              print(f"  ★ {key} (可能是地理位置)")
          else:
              print(f"    {key}")
  
  except Exception as e:
      print(f"查询失败: {e}")
  
  # 5. 检查是否有其他可能的地理位置字段
  print("\n\n5. 检查其他可能包含地理位置的字段:")
  try:
      # 检查 channel_source 和 channel_type 是否包含地理位置信息
      df_geo = pd.read_sql("""
          SELECT channel_type, channel_source, COUNT(*) as count
          FROM sensors_events
          WHERE channel_type LIKE '%国家%'
             OR channel_type LIKE '%地区%'
             OR channel_source LIKE '%国家%'
             OR channel_source LIKE '%地区%'
          GROUP BY channel_type, channel_source
          ORDER BY count DESC
          LIMIT 20
      """, engine)
  
      if len(df_geo) > 0:
          print(df_geo.to_string(index=False))
      else:
          print("未找到包含国家/地区关键词的 channel_type 或 channel_source")
  
  except Exception as e:
      print(f"查询失败: {e}")
  
  print("\n" + "="*80)
  print("检查完成")
  print("="*80)