|
| 1 | +// Copyright (c) All respective contributors to the Peridot Project. All rights reserved. |
| 2 | +// Copyright (c) 2021-2022 Rocky Enterprise Software Foundation, Inc. All rights reserved. |
| 3 | +// Copyright (c) 2021-2022 Ctrl IQ, Inc. All rights reserved. |
| 4 | +// |
| 5 | +// Redistribution and use in source and binary forms, with or without |
| 6 | +// modification, are permitted provided that the following conditions are met: |
| 7 | +// |
| 8 | +// 1. Redistributions of source code must retain the above copyright notice, |
| 9 | +// this list of conditions and the following disclaimer. |
| 10 | +// |
| 11 | +// 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | +// this list of conditions and the following disclaimer in the documentation |
| 13 | +// and/or other materials provided with the distribution. |
| 14 | +// |
| 15 | +// 3. Neither the name of the copyright holder nor the names of its contributors |
| 16 | +// may be used to endorse or promote products derived from this software without |
| 17 | +// specific prior written permission. |
| 18 | +// |
| 19 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 23 | +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | +// POSSIBILITY OF SUCH DAMAGE. |
| 30 | + |
| 31 | +package main |
| 32 | + |
| 33 | +import ( |
| 34 | + "errors" |
| 35 | + "fmt" |
| 36 | + "log" |
| 37 | + "os" |
| 38 | + "slices" |
| 39 | + "strings" |
| 40 | + "time" |
| 41 | + |
| 42 | + "github.com/google/uuid" |
| 43 | + "github.com/olekukonko/tablewriter" |
| 44 | + "github.com/spf13/cobra" |
| 45 | + "openapi.peridot.resf.org/peridotopenapi" |
| 46 | +) |
| 47 | + |
| 48 | +var taskInfo = &cobra.Command{ |
| 49 | + Use: "info [name-or-buildId]", |
| 50 | + Args: cobra.ExactArgs(1), |
| 51 | + Run: taskInfoMn, |
| 52 | +} |
| 53 | + |
| 54 | +var ( |
| 55 | + showLogLink bool |
| 56 | + showSubmitterInfo bool |
| 57 | + showDuration bool |
| 58 | +) |
| 59 | + |
| 60 | +func init() { |
| 61 | + taskInfo.Flags().BoolVar(&succeeded, "succeeded", true, "only query successful tasks") |
| 62 | + taskInfo.Flags().BoolVar(&cancelled, "cancelled", false, "only query cancelled tasks") |
| 63 | + taskInfo.Flags().BoolVar(&failed, "failed", false, "only query failed tasks") |
| 64 | + taskInfo.MarkFlagsMutuallyExclusive("cancelled", "failed", "succeeded") |
| 65 | + |
| 66 | + taskInfo.Flags().BoolVarP(&showLogLink, "logs", "L", false, "include log link in output (table format only)") |
| 67 | + taskInfo.Flags().BoolVar(&showSubmitterInfo, "submitter", false, "include submitter details (table format only)") |
| 68 | + taskInfo.Flags().BoolVar(&showDuration, "duration", true, "include duration from start to stop (table format only)") |
| 69 | +} |
| 70 | + |
| 71 | +func getNextColor(color int) int { |
| 72 | + switch color { |
| 73 | + case 0: |
| 74 | + return tablewriter.FgRedColor |
| 75 | + case tablewriter.FgCyanColor: |
| 76 | + return tablewriter.FgHiRedColor |
| 77 | + case tablewriter.FgHiWhiteColor: |
| 78 | + return tablewriter.FgRedColor |
| 79 | + default: |
| 80 | + color++ |
| 81 | + return color |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func convertSubTaskSliceToCSV(task peridotopenapi.V1AsyncTask) { |
| 86 | + subtasks, ok := task.GetSubtasksOk() |
| 87 | + if !ok { |
| 88 | + errFatal(fmt.Errorf("error getting subtasks: %v", ok)) |
| 89 | + } |
| 90 | + |
| 91 | + var parentTask = (*subtasks)[0] |
| 92 | + |
| 93 | + var table = tablewriter.NewWriter(os.Stdout) |
| 94 | + // var data [][]string |
| 95 | + var header = []string{"ptid", "tid", "status", "type", "arch", "created", "finished"} |
| 96 | + var autoMergeCells = []int{ |
| 97 | + 0, // parent task id |
| 98 | + 3, // type |
| 99 | + 4, // architecture |
| 100 | + } |
| 101 | + |
| 102 | + var mergable = []string{"ptid", "type", "arch", "submitter"} |
| 103 | + |
| 104 | + if showDuration { |
| 105 | + header = append(header, "duration") |
| 106 | + } |
| 107 | + |
| 108 | + if showSubmitterInfo { |
| 109 | + header = append(header, "submitter") |
| 110 | + } |
| 111 | + |
| 112 | + if showLogLink { |
| 113 | + header = append(header, "logs") |
| 114 | + } |
| 115 | + |
| 116 | + for _, item := range mergable { |
| 117 | + autoMergeCells = append(autoMergeCells, slices.Index(header, item)) |
| 118 | + } |
| 119 | + |
| 120 | + var parentTaskIds []string |
| 121 | + var seenTasksColors = make(map[string]int) |
| 122 | + var lastColor = 0 |
| 123 | + |
| 124 | + // precache all the subtask's parent tasks so we know if we should color them |
| 125 | + for _, subtask := range *subtasks { |
| 126 | + parentTaskIds = append(parentTaskIds, subtask.GetParentTaskId()) |
| 127 | + } |
| 128 | + |
| 129 | + for _, subtask := range *subtasks { |
| 130 | + json, err := subtask.MarshalJSON() |
| 131 | + if err != nil { |
| 132 | + errFatal(err) |
| 133 | + } |
| 134 | + |
| 135 | + if debug() { |
| 136 | + err = PrettyPrintJSON(json) |
| 137 | + if err != nil { |
| 138 | + errFatal(err) |
| 139 | + } |
| 140 | + // taskResponse, _ := subtask.GetResponse().MarshalJSON() |
| 141 | + // taskMetadata, _ := subtask.GetMetadata().MarshalJSON() |
| 142 | + } |
| 143 | + |
| 144 | + subtaskId := subtask.GetId() |
| 145 | + subtaskParentTaskId := subtask.GetParentTaskId() |
| 146 | + createdAt := subtask.GetCreatedAt() |
| 147 | + finishedAt := subtask.GetFinishedAt() |
| 148 | + |
| 149 | + row := []string{ |
| 150 | + subtaskParentTaskId, |
| 151 | + subtaskId, |
| 152 | + string(subtask.GetStatus()), |
| 153 | + string(subtask.GetType()), |
| 154 | + subtask.GetArch(), |
| 155 | + createdAt.Format("2006-01-02 15:04:05"), |
| 156 | + finishedAt.Format("2006-01-02 15:04:05"), |
| 157 | + } |
| 158 | + |
| 159 | + if showDuration { |
| 160 | + duration := finishedAt.Sub(createdAt) |
| 161 | + formatted := time.Time{}.Add(duration).Format("15:04:05") |
| 162 | + row = append(row, formatted) |
| 163 | + } |
| 164 | + |
| 165 | + if showSubmitterInfo { |
| 166 | + effectiveSubmitter := fmt.Sprintf("%s <%s>", parentTask.GetSubmitterId(), parentTask.GetSubmitterEmail()) |
| 167 | + row = append(row, effectiveSubmitter) |
| 168 | + } |
| 169 | + |
| 170 | + if showLogLink { |
| 171 | + logLink := fmt.Sprintf("https://%s/api/v1/projects/%s/tasks/%s/logs", strings.Replace(endpoint(), "-api", "", 1), mustGetProjectID(), subtaskId) |
| 172 | + row = append(row, logLink) |
| 173 | + } |
| 174 | + |
| 175 | + var nextColor = tablewriter.FgWhiteColor |
| 176 | + _, seen := seenTasksColors[subtaskId] |
| 177 | + var shouldColor = taskIdIsAnyParentTaskId(parentTaskIds, subtaskId) |
| 178 | + |
| 179 | + if !seen && shouldColor { |
| 180 | + // log.Printf("new: lastcolor: %s nextcolor %s", lastColor, nextColor) |
| 181 | + nextColor = getNextColor(lastColor) |
| 182 | + lastColor = nextColor |
| 183 | + // log.Printf("after: lastcolor: %s nextcolor %s", lastColor, nextColor) |
| 184 | + // insert our color into the table |
| 185 | + seenTasksColors[subtaskId] = nextColor |
| 186 | + } |
| 187 | + |
| 188 | + ptidColor, seen := seenTasksColors[subtaskParentTaskId] |
| 189 | + if !seen { |
| 190 | + ptidColor = tablewriter.FgWhiteColor |
| 191 | + } |
| 192 | + |
| 193 | + var colors = make([]tablewriter.Colors, len(row)) |
| 194 | + |
| 195 | + // log.Printf("row: %s color: %s ptidcolor %s", i, nextColor, ptidColor) |
| 196 | + for i, v := range header { |
| 197 | + switch v { |
| 198 | + case "ptid": |
| 199 | + colors[i] = tablewriter.Colors{ptidColor} |
| 200 | + case "tid": |
| 201 | + if shouldColor { |
| 202 | + colors[i] = tablewriter.Colors{nextColor} // if it is a parent |
| 203 | + } else { |
| 204 | + colors[i] = tablewriter.Colors{tablewriter.BgBlackColor, tablewriter.FgWhiteColor} // childless cat ladies |
| 205 | + } |
| 206 | + default: |
| 207 | + colors[i] = tablewriter.Colors{} |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + table.Rich(row, colors) |
| 212 | + } |
| 213 | + |
| 214 | + table.SetHeader(header) |
| 215 | + table.SetAutoMergeCellsByColumnIndex(autoMergeCells) |
| 216 | + table.SetRowLine(true) |
| 217 | + table.Render() |
| 218 | + |
| 219 | +} |
| 220 | + |
| 221 | +func taskIdIsAnyParentTaskId(parentTaskIds []string, subtaskId string) bool { |
| 222 | + if idx := slices.Index(parentTaskIds, subtaskId); idx > 0 { |
| 223 | + return true |
| 224 | + } |
| 225 | + return false |
| 226 | +} |
| 227 | + |
| 228 | +func taskInfoMn(_ *cobra.Command, args []string) { |
| 229 | + // Ensure project id exists |
| 230 | + projectId := mustGetProjectID() |
| 231 | + |
| 232 | + taskId := args[0] |
| 233 | + |
| 234 | + err := uuid.Validate(taskId) |
| 235 | + if err != nil { |
| 236 | + errFatal(errors.New("invalid task id")) |
| 237 | + } |
| 238 | + |
| 239 | + taskCl := getClient(serviceTask).(peridotopenapi.TaskServiceApi) |
| 240 | + log.Printf("Searching for task %s in project %s\n", taskId, projectId) |
| 241 | + |
| 242 | + res, _, err := taskCl.GetTask(getContext(), projectId, taskId).Execute() |
| 243 | + if err != nil { |
| 244 | + errFatal(fmt.Errorf("error getting task: %s", err.Error())) |
| 245 | + } |
| 246 | + |
| 247 | + switch output() { |
| 248 | + case "table": |
| 249 | + convertSubTaskSliceToCSV(res.GetTask()) |
| 250 | + |
| 251 | + case "json": |
| 252 | + taskJSON, err := res.MarshalJSON() |
| 253 | + if err != nil { |
| 254 | + errFatal(err) |
| 255 | + } |
| 256 | + |
| 257 | + err = PrettyPrintJSON(taskJSON) |
| 258 | + if err != nil { |
| 259 | + errFatal(err) |
| 260 | + } |
| 261 | + } |
| 262 | +} |
0 commit comments