import itertools import json import os import requests CE_ENDPOINT = os.environ["CE_ENDPOINT"] LOG_DIAG_TASK = os.environ["LOG_DIAG_TASK_NAME"] METRIC_DIAG_TASK = os.environ["METRIC_DIAG_TASK_NAME"] def map_task(task, tp): result = { "timestamp": task["created_at"], "type": tp, "duplicate": False, "advice": [], } ids = set() for a in task["data"]["advice"]: _id = a.get("id") if _id in ids: continue advice = { "id": _id, "name": a.get("name"), "category": a.get("category"), "component": a.get("component"), "content": a.get("content"), "qualified": a.get("qualified"), "instance": a.get("instance"), "level": a.get("level"), "type": a.get("type"), } result["advice"].append(advice) return result def group_task(tasks): m = {} for task in tasks: k = f'{task["type"]}-{task["timestamp"]//60}' if k not in m: m[k] = [] m[k].append(task) return [ { "timestamp": g[0]["timestamp"] // 60 * 60, "type": g[0]["type"], "advice": list(itertools.chain(*[t["advice"] for t in g])), } for g in m.values() ] def lambda_handler(event, context): tasks = [] resp = requests.post( f'https://{CE_ENDPOINT}/tasks/{LOG_DIAG_TASK}?status=success&begin={event["kwargs"]["start_ts"]}&end={event["kwargs"]["stop_ts"]}', json.dumps( { "args": { "cluster": event["kwargs"]["cluster"], }, "data": { "abnormal": True, }, } ), ) tasks += [map_task(t, "log") for t in resp.json()] resp = requests.post( f'https://{CE_ENDPOINT}/tasks/{METRIC_DIAG_TASK}?status=success&begin={event["kwargs"]["start_ts"]}&end={event["kwargs"]["stop_ts"]}', json.dumps( { "args": { "cluster": event["kwargs"]["cluster"], }, "data": { "abnormal": True, }, } ), ) tasks += [map_task(t, "metric") for t in resp.json()] tasks = group_task(tasks) tasks = sorted(tasks, key=lambda x: -x["timestamp"]) # mark duplicated results _ids = set() for t in tasks: _id = t["advice"]["id"] if _id in _ids: t["duplicate"] = True continue _ids.add(_id) if len(tasks) == 0: return {"abnormal": False, "content": []} return {"abnormal": True, "content": tasks}