github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/pkg/requester/publicapi/endpoints_list.go (about)

     1  package publicapi
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"net/http"
     7  
     8  	"github.com/filecoin-project/bacalhau/pkg/bacerrors"
     9  	"github.com/filecoin-project/bacalhau/pkg/jobstore"
    10  	"github.com/filecoin-project/bacalhau/pkg/model"
    11  	"github.com/filecoin-project/bacalhau/pkg/publicapi/handlerwrapper"
    12  	"github.com/rs/zerolog/log"
    13  )
    14  
    15  type listRequest struct {
    16  	JobID       string              `json:"id" example:"9304c616-291f-41ad-b862-54e133c0149e"`
    17  	ClientID    string              `json:"client_id" example:"ac13188e93c97a9c2e7cf8e86c7313156a73436036f30da1ececc2ce79f9ea51"`
    18  	IncludeTags []model.IncludedTag `json:"include_tags" example:"['any-tag']"`
    19  	ExcludeTags []model.ExcludedTag `json:"exclude_tags" example:"['any-tag']"`
    20  	MaxJobs     int                 `json:"max_jobs" example:"10"`
    21  	ReturnAll   bool                `json:"return_all" `
    22  	SortBy      string              `json:"sort_by" example:"created_at"`
    23  	SortReverse bool                `json:"sort_reverse"`
    24  }
    25  
    26  type ListRequest = listRequest
    27  
    28  type listResponse struct {
    29  	Jobs []*model.JobWithInfo `json:"jobs"`
    30  }
    31  
    32  type ListResponse = listResponse
    33  
    34  // list godoc
    35  //
    36  //	@ID						pkg/requester/publicapi/list
    37  //	@Summary				Simply lists jobs.
    38  //	@Description.markdown	endpoints_list
    39  //	@Tags					Job
    40  //	@Accept					json
    41  //	@Produce				json
    42  //	@Param					listRequest	body		listRequest	true	"Set `return_all` to `true` to return all jobs on the network (may degrade performance, use with care!)."
    43  //	@Success				200			{object}	listResponse
    44  //	@Failure				400			{object}	string
    45  //	@Failure				500			{object}	string
    46  //	@Router					/requester/list [post]
    47  //
    48  //nolint:lll
    49  func (s *RequesterAPIServer) list(res http.ResponseWriter, req *http.Request) {
    50  	ctx := req.Context()
    51  	var listReq ListRequest
    52  	if err := json.NewDecoder(req.Body).Decode(&listReq); err != nil {
    53  		http.Error(res, err.Error(), http.StatusBadRequest)
    54  		return
    55  	}
    56  	res.Header().Set(handlerwrapper.HTTPHeaderClientID, listReq.ClientID)
    57  	res.Header().Set(handlerwrapper.HTTPHeaderJobID, listReq.JobID)
    58  
    59  	jobList, err := s.getJobsList(ctx, listReq)
    60  	if err != nil {
    61  		_, ok := err.(*bacerrors.JobNotFound)
    62  		if ok {
    63  			http.Error(res, bacerrors.ErrorToErrorResponse(err), http.StatusBadRequest)
    64  			return
    65  		}
    66  	}
    67  
    68  	jobWithInfos := make([]*model.JobWithInfo, len(jobList))
    69  	for i, job := range jobList {
    70  		jobState, innerErr := s.jobStore.GetJobState(ctx, job.Metadata.ID)
    71  		if innerErr != nil {
    72  			log.Ctx(ctx).Error().Err(innerErr).Msg("error getting job states")
    73  			http.Error(res, err.Error(), http.StatusInternalServerError)
    74  			return
    75  		}
    76  		jobWithInfos[i] = &model.JobWithInfo{
    77  			Job:   job,
    78  			State: jobState,
    79  		}
    80  	}
    81  	res.WriteHeader(http.StatusOK)
    82  	err = json.NewEncoder(res).Encode(ListResponse{
    83  		Jobs: jobWithInfos,
    84  	})
    85  	if err != nil {
    86  		http.Error(res, err.Error(), http.StatusInternalServerError)
    87  		return
    88  	}
    89  }
    90  
    91  func (s *RequesterAPIServer) getJobsList(ctx context.Context, listReq ListRequest) ([]model.Job, error) {
    92  	list, err := s.jobStore.GetJobs(ctx, jobstore.JobQuery{
    93  		ClientID:    listReq.ClientID,
    94  		ID:          listReq.JobID,
    95  		Limit:       listReq.MaxJobs,
    96  		IncludeTags: listReq.IncludeTags,
    97  		ExcludeTags: listReq.ExcludeTags,
    98  		ReturnAll:   listReq.ReturnAll,
    99  		SortBy:      listReq.SortBy,
   100  		SortReverse: listReq.SortReverse,
   101  	})
   102  	if err != nil {
   103  		return nil, err
   104  	}
   105  	return list, nil
   106  }