| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- import datetime
- from aliyun.log import LogClient
- from aliyun.log.auth import AUTH_VERSION_4
- from util import feishu_inform_util
- endpoint = "cn-hangzhou.log.aliyuncs.com"
- access_key = "RfSjdiWwED1sGFlsjXv0DlfTnZTG1P"
- access_key_id = "LTAIWYUujJAm7CbH"
- project = "crawler-scheduler"
- log_store = "aigc-provider"
- 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"
- client = LogClient(endpoint=endpoint, accessKey=access_key, accessKeyId=access_key_id, auth_version=AUTH_VERSION_4, region='cn-hangzhou')
- webhook = 'https://open.feishu.cn/open-apis/bot/v2/hook/9f5c5cce-5eb2-4731-b368-33926f5549f9'
- def send_feishu_card_msg(title, content):
- card_json = {
- "schema": "2.0",
- "header": {
- "title": {
- "tag": "plain_text",
- "content": title
- },
- "template": "blue"
- },
- "body": {
- "elements": [
- {
- "tag": "markdown",
- "content": content,
- "text_align": "left",
- "text_size": "normal",
- "element_id": "taskExecuteCnt"
- }
- ]
- }
- }
- feishu_inform_util.send_card_msg_to_feishu(webhook, card_json)
- def main():
- # 获取当前日期
- today = datetime.datetime.now()
- # 当天开始时间(00:00:00)
- start_of_day = datetime.datetime.combine(today.date(), datetime.time.min)
- # 当天结束时间(23:59:59.999999)
- end_of_day = datetime.datetime.combine(today.date(), datetime.time.max)
- # 转换为时间戳(秒级)
- start_timestamp = int(start_of_day.timestamp())
- end_timestamp = int(end_of_day.timestamp())
- resp = client.get_log(project=project, logstore=log_store, from_time=start_timestamp, to_time=end_timestamp, query=query_sql)
- log_data = resp.get_body().get('data')
- crawler_mode_set = []
- for datum in log_data:
- if datum.get('crawlerMode') not in crawler_mode_set:
- crawler_mode_set.append(datum.get('crawlerMode'))
- for crawler_mode in crawler_mode_set:
- title = f"【自动化供给】日任务执行情况监控 -- {crawler_mode}"
- content = "| reason | videoIdCnt | crawlerPlanIdCnt |\n"
- content += "| --- | --- | --- |\n"
- for datum in resp.get_body().get('data'):
- if crawler_mode != datum.get('crawlerMode'):
- continue
- reason = datum.get('reason')
- video_id_cnt = datum.get('videoIdCnt')
- crawler_plan_id_cnt = datum.get('crawlerPlanIdCnt')
- content += f"| {reason} | {video_id_cnt} | {crawler_plan_id_cnt} |\n"
- send_feishu_card_msg(title, content)
- if __name__ == "__main__":
- main()
|