data_clean_statistic.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. _log = get_logger()
  12. '''
  13. log template:
  14. {
  15. session_id: 一个session_id代表一条上传数据,可能包含多行记录或多个维度
  16. content: 原始数据
  17. content_type: 0list类型数据,即一个维度整体报错,一般为postfix_func和prefix_func报错
  18. 1record类型数据,即某维度的某一行报错,没有错误的数据仍然会上传
  19. tn: 维度名,如果发送成功,则不存在维度名
  20. successful: 1发送成功,0未发送
  21. error: 1存在异常,0不存在异常
  22. error_info:
  23. error_type:0 触发校验规则异常,即ruler_validation_exception,1 fetch_exception,9为其它未知异常
  24. ruler_code:error_type为0时才有ruler_code
  25. error_msg:报错的msg
  26. }
  27. '''
  28. def error(data, session_id, ex: Exception, content_type: int, tn: str, original_data):
  29. error_type = 1 if isinstance(ex, FetchException) else 9
  30. filter_data(data)
  31. filter_data(original_data)
  32. _log.error(msg={
  33. "session_id": session_id,
  34. "content": data,
  35. "original_content": original_data,
  36. "content_type": content_type,
  37. "tn": tn,
  38. "successful": 0,
  39. "error": 1,
  40. "error_info": {
  41. "error_type": error_type,
  42. "error_msg": str(ex),
  43. "traceback": "".join(traceback.format_exception(ex))
  44. }
  45. }, stacklevel=2)
  46. pass
  47. def ruler_error(data, session_id, ex: RulerValidationException, content_type: int, tn: str, original_data):
  48. filter_data(data)
  49. filter_data(original_data)
  50. _log.warn(msg={
  51. "session_id": session_id,
  52. "content": data,
  53. "original_content": original_data,
  54. "content_type": content_type,
  55. "tn": tn,
  56. "successful": 0,
  57. "error": 1,
  58. "error_info": {
  59. "error_type": 0,
  60. "ruler_code": ex.get_ruler_code(),
  61. "error_msg": ex.get_msg()
  62. }
  63. }, stacklevel=2)
  64. pass
  65. def success(data, session_id, original_data):
  66. filter_data(data)
  67. filter_data(original_data)
  68. _log.info(msg={
  69. "session_id": session_id,
  70. "content": data,
  71. "original_content": original_data,
  72. "successful": 1,
  73. "error": 0,
  74. }, stacklevel=1)
  75. pass