d2b24497 by 周伟奇

add offline report

1 parent c7852512
...@@ -2331,3 +2331,5 @@ MPOS_MAP = { ...@@ -2331,3 +2331,5 @@ MPOS_MAP = {
2331 BC_CLASSIFY, 2331 BC_CLASSIFY,
2332 BL_CLASSIFY, 2332 BL_CLASSIFY,
2333 } 2333 }
2334
2335 FOLDER_WSC_CLASSIFY = 199
......
...@@ -7,6 +7,7 @@ import base64 ...@@ -7,6 +7,7 @@ import base64
7 import signal 7 import signal
8 import requests 8 import requests
9 import traceback 9 import traceback
10 from django import db
10 from PIL import Image 11 from PIL import Image
11 from datetime import datetime 12 from datetime import datetime
12 from django.core.management import BaseCommand 13 from django.core.management import BaseCommand
...@@ -19,6 +20,8 @@ from common.tools.pdf_to_img import PDFHandler ...@@ -19,6 +20,8 @@ from common.tools.pdf_to_img import PDFHandler
19 from apps.doc import consts 20 from apps.doc import consts
20 from apps.doc.exceptions import OCR1Exception, OCR2Exception, LTGTException 21 from apps.doc.exceptions import OCR1Exception, OCR2Exception, LTGTException
21 from apps.doc.ocr.wb import BSWorkbook 22 from apps.doc.ocr.wb import BSWorkbook
23 from apps.doc.models import OfflineReport
24 from apps.doc.named_enum import OfflineFailureReason
22 25
23 26
24 class TIFFHandler: 27 class TIFFHandler:
...@@ -384,6 +387,9 @@ class Command(BaseCommand, LoggerMixin): ...@@ -384,6 +387,9 @@ class Command(BaseCommand, LoggerMixin):
384 if len(true_file_set) == 0 and len(os_error_filename_set) > 0: 387 if len(true_file_set) == 0 and len(os_error_filename_set) > 0:
385 true_file_set.add(os_error_filename_set.pop()) 388 true_file_set.add(os_error_filename_set.pop())
386 for name in true_file_set: 389 for name in true_file_set:
390 is_success = True
391 failure_reason = OfflineFailureReason.OS_ERROR.value
392 start_time = time.time()
387 path = os.path.join(input_dir, name) 393 path = os.path.join(input_dir, name)
388 try: 394 try:
389 if not os.path.exists(path): 395 if not os.path.exists(path):
...@@ -408,16 +414,21 @@ class Command(BaseCommand, LoggerMixin): ...@@ -408,16 +414,21 @@ class Command(BaseCommand, LoggerMixin):
408 failed_path = os.path.join(failed_output_dir, '{0}_{1}'.format(time.time(), name)) 414 failed_path = os.path.join(failed_output_dir, '{0}_{1}'.format(time.time(), name))
409 shutil.move(path, failed_path) 415 shutil.move(path, failed_path)
410 except OSError: 416 except OSError:
417 is_success = False
418 failure_reason = OfflineFailureReason.OS_ERROR.value
411 os_error_filename_set.add(name) 419 os_error_filename_set.add(name)
412 self.folder_log.error('{0} [os error] [path={1}] [error={2}]'.format( 420 self.folder_log.error('{0} [os error] [path={1}] [error={2}]'.format(
413 self.log_base, path, traceback.format_exc())) 421 self.log_base, path, traceback.format_exc()))
414 except Exception as e: 422 except Exception as e:
423 is_success = False
424 failure_reason = OfflineFailureReason.PROCESS_ERROR.value
415 try: 425 try:
416 self.folder_log.error('{0} [file error] [path={1}] [error={2}]'.format(self.log_base, path, 426 self.folder_log.error('{0} [file error] [path={1}] [error={2}]'.format(self.log_base, path,
417 traceback.format_exc())) 427 traceback.format_exc()))
418 failed_path = os.path.join(failed_output_dir, '{0}_{1}'.format(time.time(), name)) 428 failed_path = os.path.join(failed_output_dir, '{0}_{1}'.format(time.time(), name))
419 shutil.move(path, failed_path) 429 shutil.move(path, failed_path)
420 except Exception as e: 430 except Exception as e:
431 failure_reason = OfflineFailureReason.OS_ERROR.value
421 os_error_filename_set.add(name) 432 os_error_filename_set.add(name)
422 self.folder_log.error('{0} [file move error] [path={1}] [error={2}]'.format( 433 self.folder_log.error('{0} [file move error] [path={1}] [error={2}]'.format(
423 self.log_base, path, traceback.format_exc())) 434 self.log_base, path, traceback.format_exc()))
...@@ -433,8 +444,23 @@ class Command(BaseCommand, LoggerMixin): ...@@ -433,8 +444,23 @@ class Command(BaseCommand, LoggerMixin):
433 self.DATE_KEY: date_str 444 self.DATE_KEY: date_str
434 } 445 }
435 ) 446 )
447 finally:
448 end_time = time.time()
449 try:
450 OfflineReport.objects.create(
451 input_folder=input_dir,
452 doc_type=consts.DDA_CLASSIFY,
453 file_name=name,
454 status=is_success,
455 failure_reason=failure_reason,
456 duration=int(end_time - start_time)
457 )
458 except Exception as e:
459 self.folder_log.error('{0} [db save failed] [path={1}] [error={2}]'.format(
460 self.log_base, path, traceback.format_exc()))
436 461
437 def handle(self, *args, **kwargs): 462 def handle(self, *args, **kwargs):
463 db.close_old_connections()
438 if len(self.input_dirs) == 0: 464 if len(self.input_dirs) == 0:
439 return 465 return
440 result_queue = Queue() 466 result_queue = Queue()
......
...@@ -7,6 +7,7 @@ import base64 ...@@ -7,6 +7,7 @@ import base64
7 import signal 7 import signal
8 import requests 8 import requests
9 import traceback 9 import traceback
10 from django import db
10 from PIL import Image 11 from PIL import Image
11 from datetime import datetime 12 from datetime import datetime
12 from django.core.management import BaseCommand 13 from django.core.management import BaseCommand
...@@ -20,6 +21,8 @@ from common.electronic_afc_contract.afc_contract_ocr import predict as afc_predi ...@@ -20,6 +21,8 @@ from common.electronic_afc_contract.afc_contract_ocr import predict as afc_predi
20 from apps.doc import consts 21 from apps.doc import consts
21 from apps.doc.exceptions import OCR1Exception, OCR2Exception, LTGTException 22 from apps.doc.exceptions import OCR1Exception, OCR2Exception, LTGTException
22 from apps.doc.ocr.wb import BSWorkbook 23 from apps.doc.ocr.wb import BSWorkbook
24 from apps.doc.models import OfflineReport
25 from apps.doc.named_enum import OfflineFailureReason
23 26
24 27
25 class TIFFHandler: 28 class TIFFHandler:
...@@ -688,6 +691,9 @@ class Command(BaseCommand, LoggerMixin): ...@@ -688,6 +691,9 @@ class Command(BaseCommand, LoggerMixin):
688 for name in true_file_set: 691 for name in true_file_set:
689 time.sleep(5) 692 time.sleep(5)
690 path = os.path.join(input_dir, name) 693 path = os.path.join(input_dir, name)
694 is_success = True
695 failure_reason = OfflineFailureReason.OS_ERROR.value
696 start_time = time.time()
691 697
692 try: 698 try:
693 if not os.path.exists(path): 699 if not os.path.exists(path):
...@@ -716,16 +722,21 @@ class Command(BaseCommand, LoggerMixin): ...@@ -716,16 +722,21 @@ class Command(BaseCommand, LoggerMixin):
716 failed_path = os.path.join(failed_output_dir, '{0}_{1}'.format(time.time(), name)) 722 failed_path = os.path.join(failed_output_dir, '{0}_{1}'.format(time.time(), name))
717 shutil.move(path, failed_path) 723 shutil.move(path, failed_path)
718 except OSError: 724 except OSError:
725 is_success = False
726 failure_reason = OfflineFailureReason.OS_ERROR.value
719 os_error_filename_set.add(name) 727 os_error_filename_set.add(name)
720 self.folder_log.error('{0} [os error] [path={1}] [error={2}]'.format( 728 self.folder_log.error('{0} [os error] [path={1}] [error={2}]'.format(
721 self.log_base, path, traceback.format_exc())) 729 self.log_base, path, traceback.format_exc()))
722 except Exception as e: 730 except Exception as e:
731 is_success = False
732 failure_reason = OfflineFailureReason.PROCESS_ERROR.value
723 try: 733 try:
724 self.folder_log.error('{0} [file error] [path={1}] [error={2}]'.format(self.log_base, path, 734 self.folder_log.error('{0} [file error] [path={1}] [error={2}]'.format(self.log_base, path,
725 traceback.format_exc())) 735 traceback.format_exc()))
726 failed_path = os.path.join(failed_output_dir, '{0}_{1}'.format(time.time(), name)) 736 failed_path = os.path.join(failed_output_dir, '{0}_{1}'.format(time.time(), name))
727 shutil.move(path, failed_path) 737 shutil.move(path, failed_path)
728 except Exception as e: 738 except Exception as e:
739 failure_reason = OfflineFailureReason.OS_ERROR.value
729 os_error_filename_set.add(name) 740 os_error_filename_set.add(name)
730 self.folder_log.error('{0} [file move error] [path={1}] [error={2}]'.format( 741 self.folder_log.error('{0} [file move error] [path={1}] [error={2}]'.format(
731 self.log_base, path, traceback.format_exc())) 742 self.log_base, path, traceback.format_exc()))
...@@ -755,8 +766,23 @@ class Command(BaseCommand, LoggerMixin): ...@@ -755,8 +766,23 @@ class Command(BaseCommand, LoggerMixin):
755 self.FILE_KEY: name, 766 self.FILE_KEY: name,
756 } 767 }
757 ) 768 )
769 finally:
770 end_time = time.time()
771 try:
772 OfflineReport.objects.create(
773 input_folder=input_dir,
774 doc_type=classify,
775 file_name=name,
776 status=is_success,
777 failure_reason=failure_reason,
778 duration=int(end_time - start_time)
779 )
780 except Exception as e:
781 self.folder_log.error('{0} [db save failed] [path={1}] [error={2}]'.format(
782 self.log_base, path, traceback.format_exc()))
758 783
759 def handle(self, *args, **kwargs): 784 def handle(self, *args, **kwargs):
785 db.close_old_connections()
760 if len(self.input_dirs) == 0: 786 if len(self.input_dirs) == 0:
761 return 787 return
762 result_queue = Queue() 788 result_queue = Queue()
......
...@@ -6,6 +6,7 @@ import base64 ...@@ -6,6 +6,7 @@ import base64
6 import signal 6 import signal
7 import requests 7 import requests
8 import traceback 8 import traceback
9 from django import db
9 from PIL import Image 10 from PIL import Image
10 from datetime import datetime 11 from datetime import datetime
11 from django.core.management import BaseCommand 12 from django.core.management import BaseCommand
...@@ -17,6 +18,8 @@ from common.tools.pdf_to_img import PDFHandler ...@@ -17,6 +18,8 @@ from common.tools.pdf_to_img import PDFHandler
17 from apps.doc import consts 18 from apps.doc import consts
18 from apps.doc.exceptions import OCR1Exception, OCR4Exception 19 from apps.doc.exceptions import OCR1Exception, OCR4Exception
19 from apps.doc.ocr.wb import BSWorkbook 20 from apps.doc.ocr.wb import BSWorkbook
21 from apps.doc.models import OfflineReport
22 from apps.doc.named_enum import OfflineFailureReason
20 23
21 24
22 class TIFFHandler: 25 class TIFFHandler:
...@@ -337,6 +340,9 @@ class Command(BaseCommand, LoggerMixin): ...@@ -337,6 +340,9 @@ class Command(BaseCommand, LoggerMixin):
337 if len(true_file_set) == 0 and len(os_error_filename_set) > 0: 340 if len(true_file_set) == 0 and len(os_error_filename_set) > 0:
338 true_file_set.add(os_error_filename_set.pop()) 341 true_file_set.add(os_error_filename_set.pop())
339 for name in true_file_set: 342 for name in true_file_set:
343 is_success = True
344 failure_reason = OfflineFailureReason.OS_ERROR.value
345 start_time = time.time()
340 path = os.path.join(input_dir, name) 346 path = os.path.join(input_dir, name)
341 347
342 try: 348 try:
...@@ -358,21 +364,41 @@ class Command(BaseCommand, LoggerMixin): ...@@ -358,21 +364,41 @@ class Command(BaseCommand, LoggerMixin):
358 failed_path = os.path.join(failed_output_dir, '{0}_{1}'.format(time.time(), name)) 364 failed_path = os.path.join(failed_output_dir, '{0}_{1}'.format(time.time(), name))
359 shutil.move(path, failed_path) 365 shutil.move(path, failed_path)
360 except OSError: 366 except OSError:
367 is_success = False
368 failure_reason = OfflineFailureReason.OS_ERROR.value
361 os_error_filename_set.add(name) 369 os_error_filename_set.add(name)
362 self.folder_log.error('{0} [os error] [path={1}] [error={2}]'.format( 370 self.folder_log.error('{0} [os error] [path={1}] [error={2}]'.format(
363 self.log_base, path, traceback.format_exc())) 371 self.log_base, path, traceback.format_exc()))
364 except Exception as e: 372 except Exception as e:
373 is_success = False
374 failure_reason = OfflineFailureReason.PROCESS_ERROR.value
365 try: 375 try:
366 self.folder_log.error('{0} [file error] [path={1}] [error={2}]'.format(self.log_base, path, 376 self.folder_log.error('{0} [file error] [path={1}] [error={2}]'.format(self.log_base, path,
367 traceback.format_exc())) 377 traceback.format_exc()))
368 failed_path = os.path.join(failed_output_dir, '{0}_{1}'.format(time.time(), name)) 378 failed_path = os.path.join(failed_output_dir, '{0}_{1}'.format(time.time(), name))
369 shutil.move(path, failed_path) 379 shutil.move(path, failed_path)
370 except Exception as e: 380 except Exception as e:
381 failure_reason = OfflineFailureReason.OS_ERROR.value
371 os_error_filename_set.add(name) 382 os_error_filename_set.add(name)
372 self.folder_log.error('{0} [file move error] [path={1}] [error={2}]'.format( 383 self.folder_log.error('{0} [file move error] [path={1}] [error={2}]'.format(
373 self.log_base, path, traceback.format_exc())) 384 self.log_base, path, traceback.format_exc()))
385 finally:
386 end_time = time.time()
387 try:
388 OfflineReport.objects.create(
389 input_folder=input_dir,
390 doc_type=classify,
391 file_name=name,
392 status=is_success,
393 failure_reason=failure_reason,
394 duration=int(end_time-start_time)
395 )
396 except Exception as e:
397 self.folder_log.error('{0} [db save failed] [path={1}] [error={2}]'.format(
398 self.log_base, path, traceback.format_exc()))
374 399
375 def handle(self, *args, **kwargs): 400 def handle(self, *args, **kwargs):
401 db.close_old_connections()
376 process_list = [] 402 process_list = []
377 for classify_idx, input_dir in self.input_dirs.items(): 403 for classify_idx, input_dir in self.input_dirs.items():
378 classify = int(classify_idx.split('_')[0]) 404 classify = int(classify_idx.split('_')[0])
......
...@@ -6,6 +6,7 @@ import base64 ...@@ -6,6 +6,7 @@ import base64
6 import signal 6 import signal
7 import requests 7 import requests
8 import traceback 8 import traceback
9 from django import db
9 from PIL import Image 10 from PIL import Image
10 from datetime import datetime 11 from datetime import datetime
11 from django.core.management import BaseCommand 12 from django.core.management import BaseCommand
...@@ -20,6 +21,8 @@ from common.tools.pdf_to_img import PDFHandler ...@@ -20,6 +21,8 @@ from common.tools.pdf_to_img import PDFHandler
20 from apps.doc import consts 21 from apps.doc import consts
21 from apps.doc.exceptions import OCR1Exception, OCR4Exception 22 from apps.doc.exceptions import OCR1Exception, OCR4Exception
22 from apps.doc.ocr.wb import BSWorkbook, PatternFill 23 from apps.doc.ocr.wb import BSWorkbook, PatternFill
24 from apps.doc.models import OfflineReport
25 from apps.doc.named_enum import OfflineFailureReason
23 26
24 27
25 class Finder: 28 class Finder:
...@@ -582,6 +585,9 @@ class Command(BaseCommand, LoggerMixin): ...@@ -582,6 +585,9 @@ class Command(BaseCommand, LoggerMixin):
582 for name in true_file_set: 585 for name in true_file_set:
583 time.sleep(10) # 防止文件较大时,读取到不完整文件 586 time.sleep(10) # 防止文件较大时,读取到不完整文件
584 path = os.path.join(input_dir, name) 587 path = os.path.join(input_dir, name)
588 is_success = True
589 failure_reason = OfflineFailureReason.OS_ERROR.value
590 start_time = time.time()
585 591
586 try: 592 try:
587 if not os.path.exists(path): 593 if not os.path.exists(path):
...@@ -605,20 +611,40 @@ class Command(BaseCommand, LoggerMixin): ...@@ -605,20 +611,40 @@ class Command(BaseCommand, LoggerMixin):
605 failed_path = os.path.join(failed_output_dir, '{0}_{1}'.format(time.time(), name)) 611 failed_path = os.path.join(failed_output_dir, '{0}_{1}'.format(time.time(), name))
606 shutil.move(path, failed_path) 612 shutil.move(path, failed_path)
607 except OSError: 613 except OSError:
614 is_success = False
615 failure_reason = OfflineFailureReason.OS_ERROR.value
608 os_error_filename_set.add(name) 616 os_error_filename_set.add(name)
609 self.folder_log.error('{0} [os error] [path={1}] [error={2}]'.format( 617 self.folder_log.error('{0} [os error] [path={1}] [error={2}]'.format(
610 self.log_base, path, traceback.format_exc())) 618 self.log_base, path, traceback.format_exc()))
611 except Exception as e: 619 except Exception as e:
620 is_success = False
621 failure_reason = OfflineFailureReason.PROCESS_ERROR.value
612 try: 622 try:
613 self.folder_log.error('{0} [file error] [path={1}] [error={2}]'.format(self.log_base, path, 623 self.folder_log.error('{0} [file error] [path={1}] [error={2}]'.format(self.log_base, path,
614 traceback.format_exc())) 624 traceback.format_exc()))
615 failed_path = os.path.join(failed_output_dir, '{0}_{1}'.format(time.time(), name)) 625 failed_path = os.path.join(failed_output_dir, '{0}_{1}'.format(time.time(), name))
616 shutil.move(path, failed_path) 626 shutil.move(path, failed_path)
617 except Exception as e: 627 except Exception as e:
628 failure_reason = OfflineFailureReason.OS_ERROR.value
618 os_error_filename_set.add(name) 629 os_error_filename_set.add(name)
619 self.folder_log.error('{0} [file move error] [path={1}] [error={2}]'.format( 630 self.folder_log.error('{0} [file move error] [path={1}] [error={2}]'.format(
620 self.log_base, path, traceback.format_exc())) 631 self.log_base, path, traceback.format_exc()))
632 finally:
633 end_time = time.time()
634 try:
635 OfflineReport.objects.create(
636 input_folder=input_dir,
637 doc_type=consts.FOLDER_WSC_CLASSIFY,
638 file_name=name,
639 status=is_success,
640 failure_reason=failure_reason,
641 duration=int(end_time - start_time)
642 )
643 except Exception as e:
644 self.folder_log.error('{0} [db save failed] [path={1}] [error={2}]'.format(
645 self.log_base, path, traceback.format_exc()))
621 646
622 def handle(self, *args, **kwargs): 647 def handle(self, *args, **kwargs):
648 db.close_old_connections()
623 self.folder_process(self.input_dir) 649 self.folder_process(self.input_dir)
624 self.folder_log.info('{0} [stop safely]'.format(self.log_base)) 650 self.folder_log.info('{0} [stop safely]'.format(self.log_base))
......
...@@ -933,23 +933,37 @@ class MposReport(models.Model): ...@@ -933,23 +933,37 @@ class MposReport(models.Model):
933 db_table = 'mpos_report' 933 db_table = 'mpos_report'
934 934
935 935
936 # class HILOfflineReport(models.Model): 936 class OfflineReport(models.Model):
937 id = models.AutoField(primary_key=True, verbose_name="id") # 主键
938
939 input_folder = models.CharField(max_length=512, verbose_name="文件夹路径")
940 doc_type = models.SmallIntegerField(default=0, verbose_name="文件类型")
941 file_name = models.CharField(max_length=1024, verbose_name="文件名")
942 status = models.BooleanField(default=True, verbose_name="是否成功")
943 failure_reason = models.SmallIntegerField(default=0, verbose_name="失败原因")
944 duration = models.IntegerField(verbose_name='处理时长')
945
946 create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
947
948 class Meta:
949 managed = False
950 db_table = 'offline_report'
951
952
953 # class AFCOfflineReport(models.Model):
937 # id = models.AutoField(primary_key=True, verbose_name="id") # 主键 954 # id = models.AutoField(primary_key=True, verbose_name="id") # 主键
938 # 955 #
939 # update_time = models.DateTimeField(auto_now=True, verbose_name='修改时间') 956 # input_folder = models.CharField(max_length=512, verbose_name="文件夹路径")
957 # doc_type = models.SmallIntegerField(default=0, verbose_name="文件类型")
958 # file_name = models.CharField(max_length=1024, verbose_name="文件名")
959 # status = models.BooleanField(default=True, verbose_name="是否成功")
960 # failure_reason = models.SmallIntegerField(default=0, verbose_name="失败原因")
961 # duration = models.IntegerField(verbose_name='处理时长')
962 #
940 # create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间') 963 # create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
941 # 964 #
942 # class Meta: 965 # class Meta:
943 # managed = False 966 # managed = False
944 # db_table = 'hil_offline_report' 967 # db_table = 'afc_offline_report'
945 968 # situ_db_label = 'afc'
946
947 # new teble: hil/afc_offline_ocr_report
948 # 1. file_name string eg. 'CH-B2432.pdf'
949 # 2. doc_type int eg. 2(VAT Invoice)
950 # 3. successful_at_this_level boolean eg. 0
951 # 4. failure_reason int eg. 2(PDF)
952 # 5. duration int eg. 100
953 #
954 # id/input_folder/start_time/end_time/create_time
955 969
......
...@@ -90,3 +90,8 @@ class BSCheckResult(NamedEnum): ...@@ -90,3 +90,8 @@ class BSCheckResult(NamedEnum):
90 CHECK_TRUE = (1, 'CHECK_TRUE') 90 CHECK_TRUE = (1, 'CHECK_TRUE')
91 CHECK_FALSE = (2, 'CHECK_FALSE') 91 CHECK_FALSE = (2, 'CHECK_FALSE')
92 CHECK_FAILED = (3, 'CHECK_FAILED') 92 CHECK_FAILED = (3, 'CHECK_FAILED')
93
94
95 class OfflineFailureReason(NamedEnum):
96 OS_ERROR = (0, 'OS_ERROR')
97 PROCESS_ERROR = (1, 'PROCESS_ERROR')
......
...@@ -14,6 +14,18 @@ hil_sql = """ ...@@ -14,6 +14,18 @@ hil_sql = """
14 ALTER TABLE hil_ocr_report ADD bank_name nvarchar(2048); 14 ALTER TABLE hil_ocr_report ADD bank_name nvarchar(2048);
15 ALTER TABLE hil_ocr_report ADD is_ebank bit default 0 not null; 15 ALTER TABLE hil_ocr_report ADD is_ebank bit default 0 not null;
16 ALTER TABLE hil_ocr_report ADD bs_check_result tinyint default 0 not null; 16 ALTER TABLE hil_ocr_report ADD bs_check_result tinyint default 0 not null;
17
18 create table offline_report
19 (
20 id bigint identity primary key,
21 input_folder nvarchar(512) not null,
22 doc_type tinyint default 0 not null,
23 file_name nvarchar(1024) not null,
24 status bit default 1 not null,
25 failure_reason tinyint default 0 not null,
26 duration smallint not null,
27 create_time datetime not null
28 );
17 """ 29 """
18 30
19 afc_sql = """ 31 afc_sql = """
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!