@@ -0,0 +1,222 @@ |
||
| 1 |
+# -*- coding: utf-8 -*- |
|
| 2 |
+ |
|
| 3 |
+""" |
|
| 4 |
+把抽奖名额里丢失的姓名 / 手机号,从服务器日志里回填。 |
|
| 5 |
+ |
|
| 6 |
+背景 |
|
| 7 |
+---- |
|
| 8 |
+服务端曾只读 POST 的 'name',而小程序传的字段叫 'title',导致 2026-11-22 |
|
| 9 |
+修复上线之前产生的名额 name 为空(phone 正常,字段名一致)。djlogit 把每次 |
|
| 10 |
+请求的 POST 参数都记在日志里,可以按 (campaign_id, serial_no) 把姓名找回来。 |
|
| 11 |
+ |
|
| 12 |
+用法 |
|
| 13 |
+---- |
|
| 14 |
+ # 只看能补多少,不动数据库 |
|
| 15 |
+ python manage.py backfill_lottery_names --dry-run |
|
| 16 |
+ |
|
| 17 |
+ # 只处理某个活动 |
|
| 18 |
+ python manage.py backfill_lottery_names --dry-run --campaign-id 4SRByFKXeSqH7VfUp5D9Hn |
|
| 19 |
+ |
|
| 20 |
+ # 确认数字没问题后落库(只填空值,不覆盖已有数据) |
|
| 21 |
+ python manage.py backfill_lottery_names --apply |
|
| 22 |
+ |
|
| 23 |
+ # 只解析日志不查库(排查用) |
|
| 24 |
+ python manage.py backfill_lottery_names --parse-only /tmp/kodotamron.logit.log |
|
| 25 |
+""" |
|
| 26 |
+ |
|
| 27 |
+from __future__ import print_function |
|
| 28 |
+ |
|
| 29 |
+import ast |
|
| 30 |
+import glob |
|
| 31 |
+import io |
|
| 32 |
+import os |
|
| 33 |
+import re |
|
| 34 |
+ |
|
| 35 |
+from django.core.management.base import BaseCommand, CommandError |
|
| 36 |
+from django.db import transaction |
|
| 37 |
+ |
|
| 38 |
+from sales.models import LotteryEntryInfo |
|
| 39 |
+ |
|
| 40 |
+JOIN_MARKER = 'func=lottery_join_api' |
|
| 41 |
+POST_MARKER = 'flag=post' |
|
| 42 |
+TIME_RE = re.compile(r'^(?:DEBUG|INFO|WARNING|ERROR)\s+(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})')
|
|
| 43 |
+ |
|
| 44 |
+DEFAULT_LOG_GLOB = '/tmp/kodotamron.logit.log*' |
|
| 45 |
+ |
|
| 46 |
+ |
|
| 47 |
+def parse_content(raw): |
|
| 48 |
+ """把 '<QueryDict: {u'a': [u'b']}>' 解析成普通 dict,解析失败返回 None"""
|
|
| 49 |
+ text = raw.strip() |
|
| 50 |
+ start = text.find('<QueryDict:')
|
|
| 51 |
+ |
|
| 52 |
+ if start < 0: |
|
| 53 |
+ return None |
|
| 54 |
+ |
|
| 55 |
+ text = text[start + len('<QueryDict:'):].strip()
|
|
| 56 |
+ |
|
| 57 |
+ if text.endswith('>'):
|
|
| 58 |
+ text = text[:-1].strip() |
|
| 59 |
+ |
|
| 60 |
+ try: |
|
| 61 |
+ value = ast.literal_eval(text) |
|
| 62 |
+ except (ValueError, SyntaxError): |
|
| 63 |
+ return None |
|
| 64 |
+ |
|
| 65 |
+ if not isinstance(value, dict): |
|
| 66 |
+ return None |
|
| 67 |
+ |
|
| 68 |
+ # 每个 key 取第一个值 |
|
| 69 |
+ return {k: (v[0] if isinstance(v, list) and v else v) for k, v in value.items()}
|
|
| 70 |
+ |
|
| 71 |
+ |
|
| 72 |
+def parse_line(line): |
|
| 73 |
+ """解析一行日志;不是 lottery_join_api 的 post 记录返回 None""" |
|
| 74 |
+ if JOIN_MARKER not in line or POST_MARKER not in line: |
|
| 75 |
+ return None |
|
| 76 |
+ |
|
| 77 |
+ index = line.find('content=')
|
|
| 78 |
+ |
|
| 79 |
+ if index < 0: |
|
| 80 |
+ return None |
|
| 81 |
+ |
|
| 82 |
+ params = parse_content(line[index + len('content='):])
|
|
| 83 |
+ |
|
| 84 |
+ if not params: |
|
| 85 |
+ return None |
|
| 86 |
+ |
|
| 87 |
+ matched = TIME_RE.match(line) |
|
| 88 |
+ |
|
| 89 |
+ return {
|
|
| 90 |
+ 'campaign_id': (params.get('campaign_id') or '').strip(),
|
|
| 91 |
+ 'serial_no': (params.get('serial_no') or '').strip(),
|
|
| 92 |
+ 'name': (params.get('name') or params.get('title') or '').strip(),
|
|
| 93 |
+ 'phone': (params.get('phone') or '').strip(),
|
|
| 94 |
+ 'timestamp': matched.group(1) if matched else '', |
|
| 95 |
+ } |
|
| 96 |
+ |
|
| 97 |
+ |
|
| 98 |
+def collect(log_glob): |
|
| 99 |
+ """ |
|
| 100 |
+ 扫描日志,返回 ({(campaign_id, serial_no): {name, phone, timestamp}}, 扫描到的 join 条数)
|
|
| 101 |
+ |
|
| 102 |
+ 同一条名额出现多次时,优先保留带姓名的那次。 |
|
| 103 |
+ """ |
|
| 104 |
+ found = {}
|
|
| 105 |
+ files = sorted(glob.glob(log_glob)) |
|
| 106 |
+ scanned = 0 |
|
| 107 |
+ |
|
| 108 |
+ for path in files: |
|
| 109 |
+ with io.open(path, 'r', encoding='utf-8', errors='ignore') as handle: |
|
| 110 |
+ for line in handle: |
|
| 111 |
+ record = parse_line(line) |
|
| 112 |
+ |
|
| 113 |
+ if not record: |
|
| 114 |
+ continue |
|
| 115 |
+ |
|
| 116 |
+ scanned += 1 |
|
| 117 |
+ key = (record['campaign_id'], record['serial_no']) |
|
| 118 |
+ old = found.get(key) |
|
| 119 |
+ |
|
| 120 |
+ if old is None or (record['name'] and not old['name']): |
|
| 121 |
+ found[key] = record |
|
| 122 |
+ |
|
| 123 |
+ return found, len(files), scanned |
|
| 124 |
+ |
|
| 125 |
+ |
|
| 126 |
+class Command(BaseCommand): |
|
| 127 |
+ help = '从 djlogit 日志回填抽奖名额的姓名/手机号(只填空值)' |
|
| 128 |
+ |
|
| 129 |
+ def add_arguments(self, parser): |
|
| 130 |
+ parser.add_argument('--log-glob', default=DEFAULT_LOG_GLOB, help='日志路径通配符')
|
|
| 131 |
+ parser.add_argument('--campaign-id', default='', help='只处理指定活动')
|
|
| 132 |
+ parser.add_argument('--dry-run', action='store_true', help='只统计,不写库')
|
|
| 133 |
+ parser.add_argument('--apply', action='store_true', help='真正写库')
|
|
| 134 |
+ parser.add_argument('--parse-only', default='', help='只解析日志并打印,不查数据库')
|
|
| 135 |
+ |
|
| 136 |
+ def handle(self, *args, **options): |
|
| 137 |
+ log_glob = options['log_glob'] |
|
| 138 |
+ |
|
| 139 |
+ if options['parse_only']: |
|
| 140 |
+ return self.handle_parse_only(options['parse_only']) |
|
| 141 |
+ |
|
| 142 |
+ if not options['dry_run'] and not options['apply']: |
|
| 143 |
+ raise CommandError('必须指定 --dry-run(只统计)或 --apply(写库)')
|
|
| 144 |
+ |
|
| 145 |
+ found, file_count, scanned = collect(log_glob) |
|
| 146 |
+ |
|
| 147 |
+ self.stdout.write('日志文件: %d 个,解析到 join 请求: %d 条,可回填组合: %d 个' % (
|
|
| 148 |
+ file_count, scanned, len(found))) |
|
| 149 |
+ |
|
| 150 |
+ if not file_count: |
|
| 151 |
+ self.stdout.write(self.style.WARNING('没有匹配到日志文件:%s' % log_glob))
|
|
| 152 |
+ |
|
| 153 |
+ entries = LotteryEntryInfo.objects.filter(status=True) |
|
| 154 |
+ |
|
| 155 |
+ if options['campaign_id']: |
|
| 156 |
+ entries = entries.filter(campaign_id=options['campaign_id']) |
|
| 157 |
+ |
|
| 158 |
+ missing = [entry for entry in entries.only('id', 'campaign_id', 'serial_no', 'name', 'phone')
|
|
| 159 |
+ if not (entry.name or '').strip()] |
|
| 160 |
+ |
|
| 161 |
+ self.stdout.write('待检查名额: %d 条,其中姓名为空: %d 条' % (entries.count(), len(missing)))
|
|
| 162 |
+ |
|
| 163 |
+ todo = [] |
|
| 164 |
+ |
|
| 165 |
+ for entry in missing: |
|
| 166 |
+ record = found.get((entry.campaign_id, entry.serial_no)) |
|
| 167 |
+ |
|
| 168 |
+ if not record or not record['name']: |
|
| 169 |
+ continue |
|
| 170 |
+ |
|
| 171 |
+ todo.append((entry, record)) |
|
| 172 |
+ |
|
| 173 |
+ self.stdout.write('日志里能匹配到且带姓名的: %d 条' % len(todo))
|
|
| 174 |
+ self.stdout.write('补不上的: %d 条(对应日志已被清理)' % (len(missing) - len(todo)))
|
|
| 175 |
+ |
|
| 176 |
+ for entry, record in todo[:10]: |
|
| 177 |
+ will_fill_phone = record['phone'] and not (entry.phone or '').strip() |
|
| 178 |
+ self.stdout.write(' %s %s → %s%s' % (
|
|
| 179 |
+ entry.campaign_id[:8], |
|
| 180 |
+ entry.serial_no, |
|
| 181 |
+ record['name'], |
|
| 182 |
+ '(含手机号)' if will_fill_phone else '', |
|
| 183 |
+ )) |
|
| 184 |
+ |
|
| 185 |
+ if len(todo) > 10: |
|
| 186 |
+ self.stdout.write(' ... 其余 %d 条省略' % (len(todo) - 10))
|
|
| 187 |
+ |
|
| 188 |
+ if options['dry_run']: |
|
| 189 |
+ self.stdout.write(self.style.SUCCESS('\n[dry-run] 未写库。确认后加 --apply 执行。'))
|
|
| 190 |
+ return |
|
| 191 |
+ |
|
| 192 |
+ updated = 0 |
|
| 193 |
+ |
|
| 194 |
+ with transaction.atomic(): |
|
| 195 |
+ for entry, record in todo: |
|
| 196 |
+ update_fields = [] |
|
| 197 |
+ |
|
| 198 |
+ if not (entry.name or '').strip(): |
|
| 199 |
+ entry.name = record['name'] |
|
| 200 |
+ update_fields.append('name')
|
|
| 201 |
+ |
|
| 202 |
+ if not (entry.phone or '').strip() and record['phone']: |
|
| 203 |
+ entry.phone = record['phone'] |
|
| 204 |
+ update_fields.append('phone')
|
|
| 205 |
+ |
|
| 206 |
+ if update_fields: |
|
| 207 |
+ entry.save(update_fields=update_fields) |
|
| 208 |
+ updated += 1 |
|
| 209 |
+ |
|
| 210 |
+ self.stdout.write(self.style.SUCCESS('\n已更新 %d 条名额' % updated))
|
|
| 211 |
+ |
|
| 212 |
+ def handle_parse_only(self, path): |
|
| 213 |
+ if not os.path.exists(path): |
|
| 214 |
+ raise CommandError('日志文件不存在:%s' % path)
|
|
| 215 |
+ |
|
| 216 |
+ found, file_count, scanned = collect(path) |
|
| 217 |
+ |
|
| 218 |
+ self.stdout.write('文件: %d 个,join 请求: %d 条,可回填组合: %d 个' % (file_count, scanned, len(found)))
|
|
| 219 |
+ |
|
| 220 |
+ for (campaign_id, serial_no), record in list(found.items())[:10]: |
|
| 221 |
+ self.stdout.write(' %s / %s → %s %s' % (
|
|
| 222 |
+ campaign_id[:8], serial_no, record['name'] or '(空)', record['phone'])) |