Blame view

offline_tasks/scripts/fetch_item_attributes.py 6.25 KB
12118125   tangwang   offline tasks: me...
1
2
3
4
5
  """
  获取商品基础属性(前置任务)
  从数据库获取ID->名称的映射,保存到本地文件供其他任务使用
  避免每个任务重复查询数据库
  """
12118125   tangwang   offline tasks: me...
6
  import pandas as pd
e89d7a84   tangwang   deepwalk refactor...
7
  import os
12118125   tangwang   offline tasks: me...
8
9
10
11
  import json
  import argparse
  from datetime import datetime
  from db_service import create_db_connection
c9f77c8f   tangwang   deepwalk refactor...
12
13
  from config.offline_config import DB_CONFIG, OUTPUT_DIR
  from scripts.debug_utils import setup_debug_logger
8b9ea8ee   tangwang   feat(recommendati...
14
  from scripts.item_freshness import save_item_create_times
12118125   tangwang   offline tasks: me...
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
  
  
  def fetch_and_save_mappings(engine, output_dir, logger=None, debug=False):
      """
      从数据库获取各种ID->名称映射并保存
      
      Args:
          engine: 数据库连接
          output_dir: 输出目录
          logger: 日志记录器
          debug: 是否开启debug模式
      
      Returns:
          mappings字典和输出文件路径
      """
      if logger:
          logger.info("开始获取ID到名称的映射...")
      
      mappings = {
          'item': {},
          'category': {},
          'platform': {},
          'supplier': {},
          'client_platform': {}
      }
      
      stats = {}
      
8b9ea8ee   tangwang   feat(recommendati...
43
44
      # 1. 获取商品名称和创建时间(新鲜度截断依赖 create_time)
      item_create_times = {}
12118125   tangwang   offline tasks: me...
45
46
      try:
          if logger:
8b9ea8ee   tangwang   feat(recommendati...
47
48
49
50
51
52
53
              logger.info("获取商品名称和创建时间...")
          query = """
          SELECT id, name, DATE(create_time) AS create_date
          FROM prd_goods_sku
          WHERE status IN (2,4,5)
          LIMIT 5000000
          """
12118125   tangwang   offline tasks: me...
54
55
56
          df = pd.read_sql(query, engine)
          mappings['item'] = dict(zip(df['id'].astype(str), df['name']))
          stats['item'] = len(mappings['item'])
8b9ea8ee   tangwang   feat(recommendati...
57
58
59
60
61
          if 'create_date' in df.columns:
              for item_id, create_date in zip(df['id'].astype(str), df['create_date']):
                  if pd.notna(create_date):
                      item_create_times[item_id] = pd.Timestamp(create_date).strftime('%Y-%m-%d')
          stats['item_create_times'] = len(item_create_times)
12118125   tangwang   offline tasks: me...
62
          if logger:
8b9ea8ee   tangwang   feat(recommendati...
63
              logger.info(f"✓ 获取到 {stats['item']} 个商品名称, {stats['item_create_times']} 个创建时间")
12118125   tangwang   offline tasks: me...
64
65
66
67
      except Exception as e:
          if logger:
              logger.error(f"✗ 获取商品名称失败: {e}")
          stats['item'] = 0
8b9ea8ee   tangwang   feat(recommendati...
68
          stats['item_create_times'] = 0
12118125   tangwang   offline tasks: me...
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
      
      # 2. 获取分类名称
      try:
          if logger:
              logger.info("获取分类名称...")
          query = "SELECT id, name FROM prd_category LIMIT 100000"
          df = pd.read_sql(query, engine)
          mappings['category'] = dict(zip(df['id'].astype(str), df['name']))
          stats['category'] = len(mappings['category'])
          if logger:
              logger.info(f"✓ 获取到 {stats['category']} 个分类名称")
      except Exception as e:
          if logger:
              logger.error(f"✗ 获取分类名称失败: {e}")
          stats['category'] = 0
      
      # 3. 获取供应商名称
      try:
          if logger:
              logger.info("获取供应商名称...")
          query = "SELECT id, name FROM sup_supplier LIMIT 100000"
          df = pd.read_sql(query, engine)
          mappings['supplier'] = dict(zip(df['id'].astype(str), df['name']))
          stats['supplier'] = len(mappings['supplier'])
          if logger:
              logger.info(f"✓ 获取到 {stats['supplier']} 个供应商名称")
      except Exception as e:
          if logger:
              logger.error(f"✗ 获取供应商名称失败: {e}")
          stats['supplier'] = 0
      
      # 4. 平台名称(硬编码)
      mappings['platform'] = {
          'pc': 'PC端',
          'h5': 'H5移动端',
          'app': 'APP',
          'miniprogram': '小程序',
          'wechat': '微信'
      }
      stats['platform'] = len(mappings['platform'])
      
      # 5. 客户端平台(硬编码)
      mappings['client_platform'] = {
          'iOS': 'iOS',
          'Android': 'Android',
          'Web': 'Web',
          'H5': 'H5'
      }
      stats['client_platform'] = len(mappings['client_platform'])
      
      # 保存到文件
      os.makedirs(output_dir, exist_ok=True)
      output_file = os.path.join(output_dir, 'item_attributes_mappings.json')
      
      if logger:
          logger.info(f"保存映射到: {output_file}")
      
      with open(output_file, 'w', encoding='utf-8') as f:
          json.dump(mappings, f, ensure_ascii=False, indent=2)
8b9ea8ee   tangwang   feat(recommendati...
128
129
  
      create_times_file = save_item_create_times(item_create_times, output_dir, logger=logger)
12118125   tangwang   offline tasks: me...
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
      
      # 保存统计信息
      stats_file = os.path.join(output_dir, 'item_attributes_stats.txt')
      with open(stats_file, 'w', encoding='utf-8') as f:
          f.write(f"商品属性映射统计信息\n")
          f.write(f"生成时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")
          f.write(f"=" * 60 + "\n")
          for key, count in stats.items():
              f.write(f"{key}: {count}\n")
          f.write(f"=" * 60 + "\n")
          f.write(f"总计: {sum(stats.values())}\n")
      
      if logger:
          logger.info(f"统计信息已保存到: {stats_file}")
          logger.info(f"总计获取 {sum(stats.values())} 个映射")
      
8b9ea8ee   tangwang   feat(recommendati...
146
      return mappings, output_file, create_times_file
12118125   tangwang   offline tasks: me...
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
  
  
  def main():
      parser = argparse.ArgumentParser(description='Fetch item attributes and save mappings')
      parser.add_argument('--output_dir', type=str, default=OUTPUT_DIR,
                         help='Output directory for mappings file')
      parser.add_argument('--debug', action='store_true',
                         help='Enable debug mode with detailed logging')
      
      args = parser.parse_args()
      
      # 设置日志
      logger = setup_debug_logger('fetch_item_attributes', debug=args.debug)
      
      logger.info("="*60)
      logger.info("商品属性获取任务(前置任务)")
      logger.info("="*60)
      
      # 创建数据库连接
      logger.info("连接数据库...")
      engine = create_db_connection(
          DB_CONFIG['host'],
          DB_CONFIG['port'],
          DB_CONFIG['database'],
          DB_CONFIG['username'],
          DB_CONFIG['password']
      )
      
      # 获取并保存映射
8b9ea8ee   tangwang   feat(recommendati...
176
      mappings, output_file, create_times_file = fetch_and_save_mappings(
12118125   tangwang   offline tasks: me...
177
178
179
180
181
182
183
184
185
          engine,
          args.output_dir,
          logger=logger,
          debug=args.debug
      )
      
      logger.info("="*60)
      logger.info("✓ 商品属性获取完成!")
      logger.info(f"映射文件: {output_file}")
8b9ea8ee   tangwang   feat(recommendati...
186
      logger.info(f"创建时间文件: {create_times_file}")
12118125   tangwang   offline tasks: me...
187
188
189
190
191
      logger.info("="*60)
  
  
  if __name__ == '__main__':
      main()