GetAllAPIs returns all batch apis, for each API returning the most recently submitted job and all running jobs
(virtualServices []istioclientnetworking.VirtualService, batchJobList []batch.BatchJob)
| 131 | |
| 132 | // GetAllAPIs returns all batch apis, for each API returning the most recently submitted job and all running jobs |
| 133 | func GetAllAPIs(virtualServices []istioclientnetworking.VirtualService, batchJobList []batch.BatchJob) ([]schema.APIResponse, error) { |
| 134 | batchAPIsMap := map[string]*schema.APIResponse{} |
| 135 | |
| 136 | jobIDToBatchJobMap := map[string]*batch.BatchJob{} |
| 137 | apiNameToBatchJobsMap := map[string][]*batch.BatchJob{} |
| 138 | for i, batchJob := range batchJobList { |
| 139 | jobIDToBatchJobMap[batchJob.Name] = &batchJobList[i] |
| 140 | apiNameToBatchJobsMap[batchJob.Spec.APIName] = append(apiNameToBatchJobsMap[batchJob.Spec.APIName], &batchJobList[i]) |
| 141 | } |
| 142 | |
| 143 | for i := range virtualServices { |
| 144 | apiName := virtualServices[i].Labels["apiName"] |
| 145 | metadata, err := spec.MetadataFromVirtualService(&virtualServices[i]) |
| 146 | if err != nil { |
| 147 | return nil, errors.Wrap(err, fmt.Sprintf("api %s", apiName)) |
| 148 | } |
| 149 | |
| 150 | var jobStatuses []status.BatchJobStatus |
| 151 | batchJobs := apiNameToBatchJobsMap[metadata.Name] |
| 152 | |
| 153 | if len(batchJobs) == 0 { |
| 154 | jobStates, err := job.GetMostRecentlySubmittedJobStates(metadata.Name, 1, userconfig.BatchAPIKind) |
| 155 | if err != nil { |
| 156 | return nil, err |
| 157 | } |
| 158 | |
| 159 | if len(jobStates) > 0 { |
| 160 | jobStatus, err := getJobStatusFromJobState(jobStates[0]) |
| 161 | if err != nil { |
| 162 | return nil, err |
| 163 | } |
| 164 | |
| 165 | jobStatuses = append(jobStatuses, *jobStatus) |
| 166 | } |
| 167 | } else { |
| 168 | for i := range batchJobs { |
| 169 | batchJob := batchJobs[i] |
| 170 | jobStatus, err := getJobStatusFromBatchJob(*batchJob) |
| 171 | if err != nil { |
| 172 | return nil, err |
| 173 | } |
| 174 | |
| 175 | jobStatuses = append(jobStatuses, *jobStatus) |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | batchAPIsMap[metadata.Name] = &schema.APIResponse{ |
| 180 | Metadata: metadata, |
| 181 | BatchJobStatuses: jobStatuses, |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | batchAPIList := make([]schema.APIResponse, 0, len(batchAPIsMap)) |
| 186 | |
| 187 | for _, batchAPI := range batchAPIsMap { |
| 188 | batchAPIList = append(batchAPIList, *batchAPI) |
| 189 | } |
| 190 |
no test coverage detected