def get_infrastructure(): # Make a copy of infrastructure infrastructure = copy.deepcopy( TensorHiveManager().infrastructure_manager.infrastructure) # Try to save new and update existing GPU resources to database try: resources = Resource.all() id_list = [resource.id for resource in resources] for hostname, value in infrastructure.items(): gpu_list = value.get('GPU') if gpu_list is not None: for gpu_uuid, gpu_metrics in gpu_list.items(): if gpu_uuid not in id_list: new_resource = Resource(id=gpu_uuid, name=gpu_metrics.get('name'), hostname=hostname) new_resource.save() else: for resource in resources: if resource.id == gpu_uuid and resource.hostname != hostname: resource.hostname = hostname resource.save() break except Exception: # In case of failure just return infrastructure pass if not is_admin(): try: user = User.get(get_jwt_identity()) infrastructure = user.filter_infrastructure_by_user_restrictions( infrastructure) except NoResultFound: # Such user does not exist return {} return infrastructure
def get() -> Tuple[List[Any], HttpStatusCode]: get_infrastructure() # Save new GPUs in database return [resource.as_dict() for resource in Resource.all()], HTTPStatus.OK.value