async def get_workers(request: Request, rearq: ReArq = Depends(get_rearq)): redis = rearq.redis workers_info = await redis.hgetall(constants.WORKER_KEY) workers = [] for worker_name, value in workers_info.items(): job_stat = (await JobResult.filter(worker=worker_name).annotate( count=Count("job_id")).group_by("job__status").values( "count", status="job__status", )) item = { "name": worker_name, "job_stat": {job["status"]: job["count"] for job in job_stat} } item.update(json.loads(value)) time = ms_to_datetime(item["ms"]) item["time"] = time item["is_offline"] = ( timezone.now() - time).seconds > constants.WORKER_HEARTBEAT_SECONDS + 10 workers.append(item) return templates.TemplateResponse("worker.html", { "request": request, "page_title": "worker", "workers": workers })
async def get_tasks(request: Request, rearq: ReArq = Depends(get_rearq)): task_map = rearq.task_map tasks = [] cron_tasks = [] for task_name, task in task_map.items(): item = { "name": task_name, "queue": task.queue, } job_result = await JobResult.filter(job__task=task_name).order_by("-id").first() if job_result: item["last_time"] = job_result.finish_time else: item["last_time"] = None if isinstance(task, CronTask): item["cron"] = task.cron task.set_next() item["next_time"] = ms_to_datetime(task.next_run) cron_tasks.append(item) else: tasks.append(item) return templates.TemplateResponse( "task.html", {"request": request, "page_title": "task", "tasks": tasks, "cron_tasks": cron_tasks}, )
async def result( request: Request, rearq: ReArq = Depends(get_rearq), ): workers_info = await rearq.redis.hgetall(constants.WORKER_KEY) return templates.TemplateResponse( "result.html", { "request": request, "page_title": "result", "tasks": rearq.task_map.keys(), "workers": workers_info.keys(), }, )
async def index(request: Request, rearq: ReArq = Depends(get_rearq)): task_map = rearq.task_map task_num = len(task_map) workers_info = await rearq.redis.hgetall(constants.WORKER_KEY) worker_num = len(workers_info) run_times = await JobResult.all().count() result = (await Job.all().annotate(count=Count("id"), date=ToDate("enqueue_time")).group_by( "date", "status").order_by("date").values( "date", "status", "count")) x_axis = [] series = [ { "name": "Deferred", "type": "line", "data": [], "stack": "total", "label": { "show": "true" }, }, { "name": "Queued", "type": "line", "stack": "total", "data": [], "label": { "show": "true" }, }, { "name": "InProgress", "type": "line", "stack": "total", "data": [], "label": { "show": "true" }, }, { "name": "Success", "type": "line", "stack": "total", "data": [], "label": { "show": "true" }, }, { "name": "Failed", "type": "line", "stack": "total", "data": [], "label": { "show": "true" }, }, { "name": "Expired", "type": "line", "stack": "total", "data": [], "label": { "show": "true" }, }, ] for item in result: date = str(item.get("date")) if date not in x_axis: x_axis.append(date) count = item.get("count") status = item.get("status") if status == "deferred": series[0]["data"].append(count) elif status == "queued": series[1]["data"].append(count) elif status == "in_progress": series[2]["data"].append(count) elif status == "success": series[3]["data"].append(count) elif status == "failed": series[4]["data"].append(count) elif status == "expired": series[5]["data"].append(count) return templates.TemplateResponse( "dashboard.html", { "request": request, "page_title": "dashboard", "worker_num": worker_num, "task_num": task_num, "run_times": run_times, "x_axis": x_axis, "series": series, }, )