Skip to content

Commit

Permalink
Show a preview of tasks for each status in 'alfred watch'.
Browse files Browse the repository at this point in the history
  • Loading branch information
gnutix committed Nov 10, 2023
1 parent 6328677 commit fc197c0
Showing 1 changed file with 61 additions and 26 deletions.
87 changes: 61 additions & 26 deletions client/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/gammadia/alfred/client/ui"
"github.com/gammadia/alfred/proto"
"github.com/samber/lo"
"github.com/spf13/cobra"
)

Expand All @@ -24,64 +25,98 @@ var watchCmd = &cobra.Command{

spinner := ui.NewSpinner("Waiting for job data")

var stats string
itemsPrinter := func(items []string, last bool) string {
nbItems := len(items)
if nbItems < 1 {
return ""
}

// Try to display the first or last 20 items, as long as displaying them doesn't exceed 180 characters
displayItems := 20
lineLength := 180
partial := nbItems > displayItems
var nItems []string
for displayItems > 0 {
if last {
nItems = items[max(0, nbItems-displayItems):]
} else {
nItems = items[:min(nbItems, displayItems)]
}
if len(strings.Join(nItems, " ")) <= lineLength {
break
}
displayItems -= 1
partial = true
}

if last {
return fmt.Sprintf("%s%s (🧮 %d)", lo.Ternary(partial, "… ", ""), strings.Join(nItems, " "), nbItems)
}
return fmt.Sprintf("%s%s (🧮 %d)", strings.Join(nItems, " "), lo.Ternary(partial, " …", ""), nbItems)
}

var stats, timestamp string
var tasks []string

for {
msg, err := c.Recv()
if err != nil {
if err == io.EOF {
spinner.Success(fmt.Sprintf("Job '%s' completed (%s)", args[0], stats))
spinner.Success(fmt.Sprintf("Job '%s' completed (🧮 %d, %s)\n%s", args[0], len(tasks), timestamp, stats))
return nil
}
spinner.Fail()
return err
}

queued := 0
running := 0
aborted := 0
failed := 0
completed := 0
queued := []string{}
running := []string{}
aborted := []string{}
failed := []string{}
completed := []string{}

tasks = lo.Map(msg.Tasks, func(t *proto.TaskStatus, _ int) string { return t.Name })
for _, t := range msg.Tasks {
switch t.Status {
case proto.TaskStatus_QUEUED:
queued += 1
queued = append(queued, t.Name)
case proto.TaskStatus_RUNNING:
running += 1
running = append(running, t.Name)
case proto.TaskStatus_ABORTED:
aborted += 1
aborted = append(aborted, t.Name)
case proto.TaskStatus_FAILED:
failed += 1
failed = append(failed, t.Name)
case proto.TaskStatus_COMPLETED:
completed += 1
completed = append(completed, t.Name)
}
}

statItems := []string{}
if queued > 0 {
statItems = append(statItems, fmt.Sprintf("⏳ %d", queued))
if len(queued) > 0 {
statItems = append(statItems, fmt.Sprintf("⏳ %s", itemsPrinter(queued, false)))
}
if running > 0 {
statItems = append(statItems, fmt.Sprintf("⚙️ %d", running))
if len(running) > 0 {
statItems = append(statItems, fmt.Sprintf("⚙️ %s", itemsPrinter(running, false)))
}
if aborted > 0 {
statItems = append(statItems, fmt.Sprintf("💥 %d", aborted))
if len(aborted) > 0 {
statItems = append(statItems, fmt.Sprintf("💥 %s", itemsPrinter(aborted, true)))
}
if failed > 0 {
statItems = append(statItems, fmt.Sprintf("❌ %d", failed))
if len(failed) > 0 {
statItems = append(statItems, fmt.Sprintf("❌ %s", itemsPrinter(failed, true)))
}
if completed > 0 {
statItems = append(statItems, fmt.Sprintf("✅ %d", completed))
if len(completed) > 0 {
statItems = append(statItems, fmt.Sprintf("✅ %s", itemsPrinter(completed, true)))
}

stats = strings.Join(statItems, "\n")

if msg.CompletedAt != nil {
statItems = append(statItems, fmt.Sprintf("⏱️ %s", msg.CompletedAt.AsTime().Sub(msg.ScheduledAt.AsTime()).Truncate(time.Second)))
timestamp = fmt.Sprintf("🏁 %s", msg.CompletedAt.AsTime().Sub(msg.ScheduledAt.AsTime()).Truncate(time.Second))
} else {
statItems = append(statItems, fmt.Sprintf("⏱️ %s", time.Since(msg.ScheduledAt.AsTime()).Truncate(time.Second)))
timestamp = fmt.Sprintf("⏱️ %s", time.Since(msg.ScheduledAt.AsTime()).Truncate(time.Second))
}

stats = strings.Join(statItems, ", ")
spinner.UpdateMessage(fmt.Sprintf("Job '%s' running (%s)", args[0], stats))
spinner.UpdateMessage(fmt.Sprintf("Job '%s' running (🧮 %d, %s)\n%s", args[0], len(tasks), timestamp, stats))
}
},
}

0 comments on commit fc197c0

Please sign in to comment.