async def client_for_app(request: aiohttp.web.Request): app = request.app try: app_id = request.query["app_id"] metric_name = request.query.get("metric_name") or "iforest_processor" detector_factory, fitter_factory, bypass_factory = app["METRIC_PROCESSORS"][ metric_name ] detector = detector_factory( app_id, JobGroupedExtractor, detector_cls=AutoencoderDetector ) fitter = fitter_factory( app_id, JobGroupedExtractor, detector_cls=AutoencoderDetector, timeout=120, batch=1500, ) bypass = bypass_factory(app_id, JobGroupedExtractor, timeout=10) except KeyError as exc: raise http_exceptions.HttpBadRequest("No key " + str(exc)) redis = app["REDIS"] task_f = asyncio.create_task(fitter.loop_process(redis)) task_p = asyncio.create_task(detector.loop_process(redis)) task_b = asyncio.create_task(bypass.loop_process(redis)) app["APP_METRICS"][app_id][metric_name] = { "processors": (fitter, detector), "task_f": task_f, "task_p": task_p, "task_b": task_b, } return aiohttp.web.json_response({"status": "created new processor"})
async def rm_metrics_for_app(request: aiohttp.web.Request): app = request.app try: app_id = request.query["app_id"] except KeyError as exc: raise http_exceptions.HttpBadRequest("No key " + str(exc)) data = app["APP_METRICS"].get(app_id) if data is None: return aiohttp.web.json_response({"error": "App not found"}) total_tasks_closed = 0 for metric_name, metric_data in data.items(): tasks = [x for x in metric_data.values() if isinstance(x, asyncio.Task)] for x in tasks: x.cancel() total_tasks_closed += len(tasks) del app["APP_METRICS"][app_id] return aiohttp.web.json_response( { "status": f"Deleted processors for {app_id}. Closed {total_tasks_closed} tasks" } )
async def rm_metric_for_app(request: aiohttp.web.Request): app = request.app try: app_id = request.query["app_id"] metric_name = request.query["metric_name"] except KeyError as exc: raise http_exceptions.HttpBadRequest("No key " + str(exc)) data = app["APP_METRICS"].get(app_id, {}).get(metric_name) if data is None: return aiohttp.web.json_response({"error": "Processor not found"}) data["task"].cancel() del app["APP_METRICS"][app_id][metric_name] return aiohttp.web.json_response({"status": "Deleted processor"})
async def client_for_app(request: aiohttp.web.Request): app = request.app try: app_id = request.query["app_id"] metric_name = request.query.get("metric_name") or "skewness_score" processor = app["METRIC_PROCESSORS"][metric_name]() except KeyError as exc: raise http_exceptions.HttpBadRequest("No key " + str(exc)) redis = app["REDIS"] task = asyncio.create_task( processor.loop_app_apply(redis, app["GRAPHITE"], app_id)) app["APP_METRICS"][app_id][metric_name] = { "processor": processor, "task": task, } return aiohttp.web.json_response({"status": "created new processor"})
async def rm_metric_proc(request: aiohttp.web.Request): app = request.app try: app_id = request.query["app_id"] metric_name = request.query["metric_name"] except KeyError as exc: raise http_exceptions.HttpBadRequest("No key " + str(exc)) data = app["APP_METRICS"].get(app_id, {}).get(metric_name) if data is None: return aiohttp.web.json_response({"error": "Processor not found"}) tasks = [x for x in data.values() if isinstance(x, asyncio.Task)] for x in tasks: x.cancel() del app["APP_METRICS"][app_id][metric_name] return aiohttp.web.json_response( {"status": f"Deleted processor. Closed {len(tasks)} tasks"} )
async def rm_metrics_for_app(request: aiohttp.web.Request): app = request.app try: app_id = request.query["app_id"] except KeyError as exc: raise http_exceptions.HttpBadRequest("No key " + str(exc)) data = app["APP_METRICS"].get(app_id) if data is None: return aiohttp.web.json_response({"error": "App not found"}) keys_to_delete = set() for key, item in data.items(): item["task"].cancel() keys_to_delete.add(key) for k in keys_to_delete: del data[k] return aiohttp.web.json_response( {"status": f"Deleted processors for {app_id}"})
async def __aenter__(self, *args, **kwargs): mock_obj = mock.AsyncMock() mock_obj.text.side_effect = http_exceptions.HttpBadRequest( 'mocked bad HTTP request') return mock_obj