def align_by_ids_batch()

in microservices/skill_service/src/routes/skill_unified_alignment.py [0:0]


def align_by_ids_batch(req_body: UnifiedBatchRequestModel):
  """
  Given the Firestore Skill_ids, this method updates all the alignments
  for the given skill(s) in the Firestore.
  Args:
    req_body (RequestModel): Required body of Skill Alignment.

  Raises:
    HTTPException: 500 Internal Server Error if something fails.

  Returns: (BatchJobModel)
      job_name: name of the batchjob created
      status: status of batchjob
  """
  try:
    request_body = req_body.__dict__
    input_type = request_body["input_type"]
    input_ids = request_body.get("ids", [])
    source_name = request_body.get("source_name", [])
    alignment_sources = request_body["output_alignment_sources"]
    if (not input_ids and not source_name) or (input_ids and source_name):
      raise Exception("Either ids or source_name must be provided")
    response = {}
    if input_type == "skill":
      # skill to skill
      # skill to knowledge
      skill_sources = get_data_sources("skill")[0]
      SKILL_SOURCES = skill_sources["source"]
      if source_name:
        for source in source_name:
          if source not in SKILL_SOURCES:
            raise ValidationError\
              ("{0} is not a valid skill source. Allowed sources "
            "are {1}".format(source, SKILL_SOURCES))
      elif input_ids:
        for skill_id in request_body["ids"]:
          Skill.find_by_uuid(skill_id)
      allowed_output_alignment_keys = {"skill_sources", "learning_resource_ids"}
      if alignment_sources.keys() < allowed_output_alignment_keys:
        raise ValidationError("{0} missing in output_alignment_sources".\
        format(allowed_output_alignment_keys-alignment_sources.keys()))
      for key in alignment_sources.keys():
        if key not in allowed_output_alignment_keys:
          raise ValidationError("Invalid key {0} in output_alignment_sources. "
          "Only {1} are allowed for input_type skill.".\
          format(key, allowed_output_alignment_keys))
      if all(value == [] for value in alignment_sources.values()):
        raise ValidationError("No source is provided for alignment.")
      for key, value in alignment_sources.items():
        if len(value) == 1 and value[0] == "*":
          continue
        if key == "skill_sources":
          skill_sources = get_data_sources("skill")[0]
          SKILL_SOURCES = skill_sources["source"]
          MATCHING_ENGINE_INDEX_IDS = skill_sources["matching_engine_index_id"]
          for source in value:
            if source not in SKILL_SOURCES:
              raise ValidationError("{0} not a valid skill source. Allowed "
                                     "\"skill_sources\" are {1}.".format(
                                         source, SKILL_SOURCES))
            elif source not in MATCHING_ENGINE_INDEX_IDS:
              raise Exception("Index is not created for {0}. Please use {1} "
                              "to create index.".format(
                                  source,
                                  "skill-service/api/v1/skill/embeddings"))
        elif key == "learning_resource_ids":
          for source in value:
            KnowledgeServiceLearningContent.find_by_id(source)
    else:
      raise NotImplementedError("Only \"skill\" is allowed as \"input_type\"")
    env_vars = {"DATABASE_PREFIX": DATABASE_PREFIX,
                "EMBEDDING_ENDPOINT_ID": EMBEDDING_ENDPOINT_ID}
    response = initiate_batch_job(request_body, UNIFIED_ALIGNMENT_JOB_TYPE,
                                  env_vars)
    return response
  except ValidationError as e:
    raise BadRequest(str(e)) from e
  except ResourceNotFoundException as e:
    raise ResourceNotFound(str(e)) from e
  except NotImplementedError as e:
    raise APINotImplemented(str(e)) from e
  except Exception as e:
    Logger.error(e)
    Logger.error(traceback.print_exc())
    raise InternalServerError(str(e)) from e