def _call_delete_database(self, body: Body) -> Response:
     id: str = body["id"]
     database: Optional[Database] = self._databases.pop(id, None)
     if database:
         database.close()
         del database
         return get_response(200)
     else:
         return get_response(404)
    def _call_add_database(self, body: Body) -> Response:
        """Add database and initialize driver for it."""
        if body["id"] in self._databases:
            return get_response(400)

        db_instance = Database(
            body["id"],
            body["number_workers"],
            "tcp://{:s}:{:s}".format(
                self._workload_sub_host,
                self._workload_pubsub_port,
            ),
        )
        self._databases[body["id"]] = db_instance
        return get_response(200)
 def _call_get_workload(self, body: Body) -> Response:
     response = get_response(200)
     response["body"]["workload"] = {
         "workload_name": self._workload,
         "frequency": self._workload_frequency,
     }
     return response
 def _generate_workload(self) -> None:
     if self._workload:
         response = get_response(200)
         response["body"]["querylist"] = [
             self._workload for _ in range(self._workload_frequency)
         ]
         self._pub_socket.send_json(response)
 def _call_get_databases(self, body: Body) -> Response:
     """Get list of all databases."""
     databases = [{
         "id": id,
         "number_workers": database.number_workers
     } for id, database in self._databases.items()]
     response = get_response(200)
     response["body"]["databases"] = databases
     return response
 def _call_get_queue_length(self, body: Body) -> Response:
     response = get_response(200)
     response["body"]["databases"] = [{
         "id":
         id,
         "queue_length":
         database.get_queue_length()
     } for id, database in self._databases.items()]
     return response
    def _call_status(self, body: Body) -> Response:
        status = []

        for database_id, database in self._databases.items():
            status.append({
                "id":
                database_id,
                "worker_pool_status":
                database.get_worker_pool_status(),
            })
        response = get_response(200)
        response["body"]["status"] = status
        return response
 def _call_metric(self, body: Body) -> Response:
     # do some work
     sleep(0.05)
     response = get_response(200)
     return response
 def _call_time_intense_metric(self, body: Body) -> Response:
     # do some work
     sleep(0.2)
     response = get_response(200)
     return response
 def _call_close_worker(self, body: Body) -> Response:
     for database in self._databases.values():
         if not database.close_worker():
             return get_response(400)
     return get_response(200)
 def _call_stop_workload(self, body: Body) -> Response:
     self._workload = None  # type: ignore
     self._workload_frequency = 0
     return get_response(200)
 def _call_start_workload(self, body: Body) -> Response:
     self._workload = body["workload_name"]
     self._workload_frequency = body["frequency"]
     return get_response(200)
 def _handle_request(self, request: Request) -> Response:
     try:
         func = self._calls[request["header"]["message"]]
         return func(request["body"])
     except KeyError:
         return get_response(404)