data_clean_statistic.py 2.2 KB

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