automation_provide_job_monitor.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import datetime
  2. from aliyun.log import LogClient
  3. from aliyun.log.auth import AUTH_VERSION_4
  4. from util import feishu_inform_util
  5. endpoint = "cn-hangzhou.log.aliyuncs.com"
  6. access_key = "RfSjdiWwED1sGFlsjXv0DlfTnZTG1P"
  7. access_key_id = "LTAIWYUujJAm7CbH"
  8. project = "crawler-scheduler"
  9. log_store = "aigc-provider"
  10. query_sql = "* | select crawlerMode, result, if(reason='null', '成功', reason) as reason, count(distinct videoId) as videoIdCnt, count(distinct crawlerPlanId) as crawlerPlanIdCnt from log where reason not in ('该账号已经存在爬取计划,跳过执行', '该视频近期已经处理过', '该Topic已经创建过爬取计划', '该关键词已经创建过爬取计划') group by crawlerMode, result, reason order by crawlerMode, result desc, reason"
  11. client = LogClient(endpoint=endpoint, accessKey=access_key, accessKeyId=access_key_id, auth_version=AUTH_VERSION_4, region='cn-hangzhou')
  12. webhook = 'https://open.feishu.cn/open-apis/bot/v2/hook/9f5c5cce-5eb2-4731-b368-33926f5549f9'
  13. def send_feishu_card_msg(title, content):
  14. card_json = {
  15. "schema": "2.0",
  16. "header": {
  17. "title": {
  18. "tag": "plain_text",
  19. "content": title
  20. },
  21. "template": "blue"
  22. },
  23. "body": {
  24. "elements": [
  25. {
  26. "tag": "markdown",
  27. "content": content,
  28. "text_align": "left",
  29. "text_size": "normal",
  30. "element_id": "taskExecuteCnt"
  31. }
  32. ]
  33. }
  34. }
  35. feishu_inform_util.send_card_msg_to_feishu(webhook, card_json)
  36. def main():
  37. # 获取当前日期
  38. today = datetime.datetime.now()
  39. # 当天开始时间(00:00:00)
  40. start_of_day = datetime.datetime.combine(today.date(), datetime.time.min)
  41. # 当天结束时间(23:59:59.999999)
  42. end_of_day = datetime.datetime.combine(today.date(), datetime.time.max)
  43. # 转换为时间戳(秒级)
  44. start_timestamp = int(start_of_day.timestamp())
  45. end_timestamp = int(end_of_day.timestamp())
  46. resp = client.get_log(project=project, logstore=log_store, from_time=start_timestamp, to_time=end_timestamp, query=query_sql)
  47. log_data = resp.get_body().get('data')
  48. crawler_mode_set = set()
  49. for datum in log_data:
  50. crawler_mode_set.add(datum.get('crawlerMode'))
  51. for crawler_mode in crawler_mode_set:
  52. title = f"{crawler_mode} 执行情况监控"
  53. content = "| reason | videoIdCnt | crawlerPlanIdCnt |\n"
  54. content += "| --- | --- | --- |\n"
  55. for datum in resp.get_body().get('data'):
  56. if crawler_mode != datum.get('crawlerMode'):
  57. continue
  58. reason = datum.get('reason')
  59. video_id_cnt = datum.get('videoIdCnt')
  60. crawler_plan_id_cnt = datum.get('crawlerPlanIdCnt')
  61. content += f"| {reason} | {video_id_cnt} | {crawler_plan_id_cnt} |\n"
  62. send_feishu_card_msg(title, content)
  63. if __name__ == "__main__":
  64. main()