functions.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. """
  2. @author: luojunhui
  3. """
  4. import pymysql
  5. from datetime import datetime, timedelta
  6. class MatchRate(object):
  7. """
  8. 匹配率
  9. """
  10. @classmethod
  11. def generate_stamp_list(cls, start_date, end_date):
  12. """
  13. Generate daily date_str
  14. :param start_date:
  15. :param end_date:
  16. :return:
  17. """
  18. start = datetime.strptime(start_date, "%Y%m%d")
  19. end = datetime.strptime(end_date, "%Y%m%d")
  20. current = start
  21. timestamp_list = []
  22. while current <= end:
  23. timestamp_list.append(current.timestamp() * 1000)
  24. current += timedelta(days=1)
  25. return timestamp_list
  26. @classmethod
  27. def generate_today_stamp(cls, date_string):
  28. """
  29. :param date_string:
  30. :return:
  31. """
  32. return datetime.strptime(date_string, "%Y%m%d").timestamp() * 1000
  33. @classmethod
  34. def generate_yesterday_stamp(cls, now_dt):
  35. """
  36. Generate date in 3 days
  37. :param now_dt:
  38. :return:
  39. """
  40. now_date = datetime.strptime(now_dt, "%Y%m%d")
  41. yesterday = now_date - timedelta(days=1)
  42. return yesterday.timestamp() * 1000
  43. @classmethod
  44. def match_rate(cls, start_time_stamp, end_time_stamp):
  45. """
  46. sensitive words
  47. :return:
  48. """
  49. connection = pymysql.connect(
  50. host="rm-t4na9qj85v7790tf84o.mysql.singapore.rds.aliyuncs.com", # 数据库IP地址,内网地址
  51. port=3306, # 端口号
  52. user="crawler_readonly", # mysql用户名
  53. passwd="cyber#crawler_2023", # mysql用户登录密码
  54. db="aigc-admin-prod", # 数据库名
  55. charset="utf8mb4" # 如果数据库里面的文本是utf8编码的,charset指定是utf8
  56. )
  57. sql = f"""
  58. select status, trace_id, error_msg
  59. from publish_content_miniprogram
  60. where create_timestamp >= {start_time_stamp} and create_timestamp < {end_time_stamp};
  61. """
  62. cursor = connection.cursor()
  63. cursor.execute(sql)
  64. data = cursor.fetchall()
  65. result = [list(line) for line in data]
  66. return result
  67. class RateDetail(object):
  68. """
  69. Rate Detail
  70. """
  71. @classmethod
  72. def rate_and_error_list(cls, result_list):
  73. """
  74. 成功,失败的数据
  75. :param result_list:
  76. :return:
  77. """
  78. success_count = 0
  79. fail_count = 0
  80. processing_count = 0
  81. total_requests = len(result_list)
  82. error_list = []
  83. if result_list:
  84. for temp in result_list:
  85. status = temp[0]
  86. error = temp[2]
  87. if status == 1:
  88. processing_count += 1
  89. elif status == 2:
  90. success_count += 1
  91. elif status == 3:
  92. fail_count += 1
  93. else:
  94. continue
  95. if error:
  96. error_list.append(temp)
  97. obj = {
  98. "success_count": success_count,
  99. "fail_count": fail_count,
  100. "processing_count": processing_count,
  101. "total_count": total_requests,
  102. "error_list": error_list
  103. }
  104. else:
  105. obj = {
  106. "success_count": None,
  107. "fail_count": None,
  108. "processing_count": None,
  109. "total_count": None,
  110. "error_list": []
  111. }
  112. return obj