data_clean_statistic.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # -*- coding: utf-8 -*-
  2. # @Time : 2023/7/31 14:05
  3. # @Author : XuJiakai
  4. # @File : data_clean_statistic
  5. # @Software: PyCharm
  6. import traceback
  7. from data_clean.exception.fetch_exception import FetchException
  8. from data_clean.exception.ruler_validation_exception import RulerValidationException
  9. from data_clean.statistic.sls_log import get_logger
  10. from data_clean.statistic.statistic_filter import filter_data
  11. from data_clean.utils import is_windows
  12. from loguru import logger as log
  13. _log = get_logger() if not is_windows() else log
  14. '''
  15. log template:
  16. {
  17. session_id: 一个session_id代表一条上传数据,可能包含多行记录或多个维度
  18. content: 原始数据
  19. content_type: 0list类型数据,即一个维度整体报错,一般为postfix_func和prefix_func报错
  20. 1record类型数据,即某维度的某一行报错,没有错误的数据仍然会上传
  21. tn: 维度名,如果发送成功,则不存在维度名
  22. successful: 1发送成功,0未发送
  23. error: 1存在异常,0不存在异常
  24. error_info:
  25. error_type:0 触发校验规则异常,即ruler_validation_exception,1 fetch_exception,9为其它未知异常
  26. ruler_code:error_type为0时才有ruler_code
  27. error_msg:报错的msg
  28. }
  29. '''
  30. def error(data, session_id, ex: Exception, content_type: int, tn: str, original_data):
  31. error_type = 1 if isinstance(ex, FetchException) else 9
  32. filter_data(data)
  33. filter_data(original_data)
  34. _log.error({
  35. "session_id": session_id,
  36. "content": data,
  37. "original_content": original_data,
  38. "content_type": content_type,
  39. "tn": tn,
  40. "successful": 0,
  41. "error": 1,
  42. "error_info": {
  43. "error_type": error_type,
  44. "error_msg": str(ex),
  45. "traceback": "".join(traceback.format_exception(ex))
  46. }
  47. })
  48. pass
  49. def ruler_error(data, session_id, ex: RulerValidationException, content_type: int, tn: str, original_data):
  50. filter_data(data)
  51. filter_data(original_data)
  52. _log.warning({
  53. "session_id": session_id,
  54. "content": data,
  55. "original_content": original_data,
  56. "content_type": content_type,
  57. "tn": tn,
  58. "successful": 0,
  59. "error": 1,
  60. "error_info": {
  61. "error_type": 0,
  62. "ruler_code": ex.get_ruler_code(),
  63. "error_msg": ex.get_msg()
  64. }
  65. })
  66. pass
  67. def success(data, session_id, original_data):
  68. filter_data(data)
  69. filter_data(original_data)
  70. _log.info({
  71. "session_id": session_id,
  72. "content": data,
  73. "original_content": original_data,
  74. "successful": 1,
  75. "error": 0,
  76. })
  77. pass