def Handle(self, args, token=None):
    """Retrieves the clients for a hunt."""
    hunt_urn = HUNTS_ROOT_PATH.Add(args.hunt_id)
    hunt = aff4.FACTORY.Open(hunt_urn, aff4_type="GRRHunt", token=token)

    clients_by_status = hunt.GetClientsByStatus()
    hunt_clients = clients_by_status[args.client_status.name]
    total_count = len(hunt_clients)

    if args.count:
      hunt_clients = sorted(hunt_clients)[args.offset:args.offset+args.count]
    else:
      hunt_clients = sorted(hunt_clients)[args.offset:]

    all_flow_urns = hunts.GRRHunt.GetAllSubflowUrns(
        hunt_urn, hunt_clients, token)
    flow_requests = flow.GRRFlow.GetFlowRequests(all_flow_urns, token)
    client_requests = aff4_grr.VFSGRRClient.GetClientRequests(
        hunt_clients, token)

    waitingfor = {}
    status_by_request = {}

    for flow_urn in flow_requests:
      for obj in flow_requests[flow_urn]:
        if isinstance(obj, rdf_flows.RequestState):
          waitingfor.setdefault(flow_urn, obj)
          if waitingfor[flow_urn].id > obj.id:
            waitingfor[flow_urn] = obj
        elif isinstance(obj, rdf_flows.GrrMessage):
          status_by_request.setdefault(flow_urn, {})[obj.request_id] = obj

    response_urns = []

    for request_base_urn, request in waitingfor.iteritems():
      response_urns.append(rdfvalue.RDFURN(request_base_urn).Add(
          "request:%08X" % request.id))

    response_dict = dict(data_store.DB.MultiResolvePrefix(
        response_urns, "flow:", token=token))

    result_items = []
    for flow_urn in sorted(all_flow_urns):
      request_urn = flow_urn.Add("state")
      client_id = flow_urn.Split()[2]

      item = ApiHuntClient()
      item.client_id = client_id
      item.flow_urn = flow_urn
      try:
        request_obj = waitingfor[request_urn]
      except __HOLE__:
        request_obj = None

      if request_obj:
        response_urn = rdfvalue.RDFURN(request_urn).Add(
            "request:%08X" % request_obj.id)
        responses_available = len(response_dict.setdefault(response_urn, []))
        status_available = False
        responses_expected = "Unknown"
        if request_obj.id in status_by_request.setdefault(request_urn, {}):
          status_available = True
          status = status_by_request[request_urn][request_obj.id]
          responses_expected = status.response_id

        client_requests_available = 0
        for client_req in client_requests.setdefault(client_id, []):
          if request_obj.request.session_id == client_req.session_id:
            client_requests_available += 1

        item.incomplete_request_id = str(request_obj.id)
        item.next_state = request_obj.next_state
        item.expected_args = request_obj.request.args_rdf_name
        item.available_responses_count = responses_available
        item.expected_responses = responses_expected
        item.is_status_available = status_available
        item.available_client_requests_count = client_requests_available

      result_items.append(item)

    return ApiGetHuntClientsResult(
        items=result_items, total_count=total_count)
dataset/ETHPy150Open google/grr/grr/gui/api_plugins/hunt.py/ApiGetHuntClientsHandler.Handle