fix log
Showing
3 changed files
with
89 additions
and
3 deletions
This diff is collapsed.
Click to expand it.
1 | import re | ||
2 | import os | ||
3 | import ast | ||
4 | import datetime | ||
5 | from openpyxl import Workbook | ||
6 | from django.core.management import BaseCommand | ||
7 | from settings import conf | ||
8 | from common.mixins import LoggerMixin | ||
9 | from apps.doc.models import HILDoc, AFCDoc | ||
10 | from apps.doc import consts | ||
11 | |||
12 | |||
13 | class Command(BaseCommand, LoggerMixin): | ||
14 | |||
15 | def __init__(self): | ||
16 | super().__init__() | ||
17 | self.sheet_names = ('AFC', 'HIL') | ||
18 | self.header = ('申请号', '身份证号', '民族', '时间戳') | ||
19 | |||
20 | def add_arguments(self, parser): | ||
21 | parser.add_argument( | ||
22 | '--date', | ||
23 | default=datetime.date.today() - datetime.timedelta(days=1), | ||
24 | dest='date', | ||
25 | help='将要计算的日期,格式: 2018-01-01' | ||
26 | ) | ||
27 | |||
28 | def handle(self, *args, **kwargs): | ||
29 | date = kwargs.get('date') | ||
30 | if isinstance(date, str): | ||
31 | if not re.match(r'\d{4}-\d{2}-\d{2}', date): | ||
32 | print('date format error') | ||
33 | return | ||
34 | date_str = date | ||
35 | else: | ||
36 | date_str = date.strftime('%Y-%m-%d') | ||
37 | |||
38 | excel_dir = os.path.join(conf.DATA_DIR, 'AFC', 'Logs') | ||
39 | if not os.path.exists(excel_dir): | ||
40 | print('excel dir not exists') | ||
41 | return | ||
42 | excel_path = os.path.join(excel_dir, 'idcard_{0}.xlsx'.format(date_str)) | ||
43 | log_path = os.path.join(conf.LOG_DIR, 'idcard.log.{0}'.format(date_str)) | ||
44 | if not os.path.exists(log_path): | ||
45 | print('log_path not exists') | ||
46 | return | ||
47 | |||
48 | wb = Workbook() | ||
49 | for name in self.sheet_names: | ||
50 | ws = wb.create_sheet(name) | ||
51 | ws.append(self.header) | ||
52 | wb.remove(wb.get_sheet_by_name('Sheet')) | ||
53 | |||
54 | with open(log_path, 'r', encoding='utf-8') as fp: | ||
55 | for line in fp: | ||
56 | search_obj = re.search(r'[(.*)] [task=(.*)] [idcard=(.*)]', line) | ||
57 | task_str = search_obj.group(1) | ||
58 | license_summary = ast.literal_eval(search_obj.group(2)) | ||
59 | business_type, doc_id_str = task_str.split(consts.SPLIT_STR) | ||
60 | doc_id = int(doc_id_str) | ||
61 | doc_class = HILDoc if business_type == consts.HIL_PREFIX else AFCDoc | ||
62 | application_id = doc_class.objects.filter(id=doc_id).values_list('application_id', flat=True) | ||
63 | |||
64 | for classify, (_, name, field_order, side_diff, _, _) in consts.LICENSE_ORDER: | ||
65 | license_list = license_summary.get(classify) | ||
66 | if not license_list: | ||
67 | continue | ||
68 | ws = wb.get_sheet_by_name(name) | ||
69 | for license_dict in license_list: | ||
70 | if classify == consts.IC_CLASSIFY and license_dict.get('类别') == '1': # 居住证处理 | ||
71 | license_summary.setdefault(consts.RP_CLASSIFY, []).append(license_dict) | ||
72 | continue | ||
73 | if side_diff: | ||
74 | key, field_order_yes, field_order_no = consts.FIELD_ORDER_MAP.get(classify) | ||
75 | field_order = field_order_yes if key in license_dict else field_order_no | ||
76 | all_value = [] | ||
77 | for search_field, write_field in field_order: | ||
78 | if write_field is None: | ||
79 | continue | ||
80 | field_value = license_dict.get(search_field, '') | ||
81 | if isinstance(field_value, list): | ||
82 | all_value.append('\n'.join(field_value)) | ||
83 | else: | ||
84 | all_value.append(field_value) | ||
85 | ws.append((application_id[0], *all_value)) | ||
86 | wb.save(excel_path) |
... | @@ -641,14 +641,14 @@ class Command(BaseCommand, LoggerMixin): | ... | @@ -641,14 +641,14 @@ class Command(BaseCommand, LoggerMixin): |
641 | '[license_summary={4}]'.format(self.log_base, task_str, bs_summary, | 641 | '[license_summary={4}]'.format(self.log_base, task_str, bs_summary, |
642 | unknown_summary, license_summary)) | 642 | unknown_summary, license_summary)) |
643 | 643 | ||
644 | self.license_log.info('[license_summary={0}]'.format(license_summary)) | 644 | self.license_log.info('[task={0}] [license_summary={1}]'.format(task_str, license_summary)) |
645 | idcard_list = license_summary.get(consts.IC_CLASSIFY) | 645 | idcard_list = license_summary.get(consts.IC_CLASSIFY) |
646 | if idcard_list: | 646 | if idcard_list: |
647 | self.idcard_log.info('[idcard={0}]'.format(idcard_list)) | 647 | self.idcard_log.info('[task={0}] [idcard={1}]'.format(task_str, idcard_list)) |
648 | 648 | ||
649 | merged_bs_summary = self.rebuild_bs_summary(bs_summary, unknown_summary) | 649 | merged_bs_summary = self.rebuild_bs_summary(bs_summary, unknown_summary) |
650 | 650 | ||
651 | self.bs_log.info('[bs_summary={0}]'.format(merged_bs_summary)) | 651 | self.bs_log.info('[task={0}] [bs_summary={1}]'.format(task_str, merged_bs_summary)) |
652 | 652 | ||
653 | self.cronjob_log.info('{0} [task={1}] [merged_bs_summary={2}] [unknown_summary={3}] ' | 653 | self.cronjob_log.info('{0} [task={1}] [merged_bs_summary={2}] [unknown_summary={3}] ' |
654 | '[res_list={4}]'.format(self.log_base, task_str, merged_bs_summary, | 654 | '[res_list={4}]'.format(self.log_base, task_str, merged_bs_summary, | ... | ... |
-
Please register or sign in to post a comment