mirror of
https://github.com/dagucloud/dagu.git
synced 2026-12-05 21:34:48 +00:00
Co-authored-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com> Co-authored-by: Yota Hamada <yohamta@gmail.com>
278 lines
7.0 KiB
Go
278 lines
7.0 KiB
Go
// Copyright (C) 2026 Yota Hamada
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"math"
|
|
"sort"
|
|
"strings"
|
|
"text/tabwriter"
|
|
"time"
|
|
|
|
"github.com/dagucloud/dagu/v2/internal/ir"
|
|
"github.com/dagucloud/dagu/v2/internal/pagination"
|
|
"github.com/dagucloud/dagu/v2/internal/persis"
|
|
"github.com/dagucloud/dagu/v2/internal/service/scheduler"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// Ls creates and returns a cobra command for listing DAG definitions.
|
|
func Ls() *cobra.Command {
|
|
return NewCommand(
|
|
&cobra.Command{
|
|
Use: "ls [flags] [pattern]",
|
|
Short: "List DAG definitions",
|
|
Long: `List DAG definitions with optional enrichment and sorting.
|
|
|
|
Optional pattern filters by DAG name or file name (substring match).
|
|
This command only lists DAGs from the local Dagu installation.
|
|
|
|
Flags:
|
|
-n, --next Show next scheduled run time
|
|
--local-time Show next scheduled run time in the configured timezone instead of UTC
|
|
-l, --last Show last run status and time
|
|
-H, --history Show a compact recent-history summary
|
|
-t, --sort-last Sort by last run time, newest first
|
|
-r, --reverse Reverse sort order
|
|
|
|
Examples:
|
|
dagu ls
|
|
dagu ls my-
|
|
dagu ls -n -l
|
|
dagu ls -t -r
|
|
dagu ls -H my-workflow
|
|
`,
|
|
Args: cobra.MaximumNArgs(1),
|
|
},
|
|
lsFlags,
|
|
runLs,
|
|
)
|
|
}
|
|
|
|
var lsFlags = []commandLineFlag{
|
|
lsNextFlag,
|
|
lsLocalTimeFlag,
|
|
lsLastFlag,
|
|
lsHistoryFlag,
|
|
lsSortLastFlag,
|
|
lsReverseFlag,
|
|
}
|
|
|
|
type lsRow struct {
|
|
dag *ir.DAG
|
|
nextRun time.Time
|
|
lastStatus string
|
|
lastStarted string
|
|
lastTime time.Time
|
|
history string
|
|
}
|
|
|
|
func runLs(ctx *Context, args []string) error {
|
|
showNext, _ := ctx.Command.Flags().GetBool("next")
|
|
localTime, _ := ctx.Command.Flags().GetBool("local-time")
|
|
showLast, _ := ctx.Command.Flags().GetBool("last")
|
|
showHistory, _ := ctx.Command.Flags().GetBool("history")
|
|
sortLast, _ := ctx.Command.Flags().GetBool("sort-last")
|
|
reverse, _ := ctx.Command.Flags().GetBool("reverse")
|
|
|
|
pattern := ""
|
|
if len(args) > 0 {
|
|
pattern = args[0]
|
|
}
|
|
|
|
if ctx.Persistence.DAGRepository == nil {
|
|
return fmt.Errorf("DAG store is not available")
|
|
}
|
|
if showHistory && ctx.Persistence.DAGRunRepository == nil {
|
|
return fmt.Errorf("DAG-run repository is not available")
|
|
}
|
|
|
|
now := time.Now()
|
|
pg := pagination.NewPaginator(1, math.MaxInt)
|
|
listOpts := persis.DAGListOptions{
|
|
Paginator: &pg,
|
|
Name: pattern,
|
|
Sort: "name",
|
|
Order: "asc",
|
|
Time: &now,
|
|
}
|
|
var nextRunProjection func(*ir.DAG, time.Time) time.Time
|
|
if showNext {
|
|
nextRunProjection = lsNextRunProjection(ctx)
|
|
listOpts.NextRunProjection = nextRunProjection
|
|
}
|
|
if showNext && !sortLast {
|
|
listOpts.Sort = "nextRun"
|
|
if reverse {
|
|
listOpts.Order = "desc"
|
|
}
|
|
} else if reverse && !sortLast {
|
|
listOpts.Order = "desc"
|
|
}
|
|
|
|
result, errs, err := ctx.Persistence.DAGRepository.List(ctx, listOpts)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to list DAGs: %w", err)
|
|
}
|
|
for _, e := range errs {
|
|
_, _ = fmt.Fprintf(ctx.Command.ErrOrStderr(), "warning: %s\n", e)
|
|
}
|
|
|
|
needEnrich := showLast || showHistory || sortLast
|
|
rows := make([]lsRow, 0, len(result.Items))
|
|
for _, item := range result.Items {
|
|
row := lsRow{dag: item.DAG}
|
|
if showNext && !item.Suspended {
|
|
row.nextRun = nextRunProjection(item.DAG, now)
|
|
}
|
|
if needEnrich {
|
|
if err := enrichLsRow(ctx, &row, showLast || sortLast, showHistory); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
rows = append(rows, row)
|
|
}
|
|
|
|
if sortLast {
|
|
sortLsRowsByLastRun(rows, reverse)
|
|
}
|
|
|
|
var nextRunLoc *time.Location
|
|
if localTime {
|
|
nextRunLoc = ctx.Config.Core.Location
|
|
}
|
|
|
|
return renderLsTable(ctx.Command.OutOrStdout(), rows, showNext, showLast, showHistory, nextRunLoc)
|
|
}
|
|
|
|
func lsNextRunProjection(ctx *Context) func(*ir.DAG, time.Time) time.Time {
|
|
state, err := ctx.Persistence.SchedulerStateStore.Load(ctx)
|
|
if err != nil {
|
|
_, _ = fmt.Fprintf(ctx.Command.ErrOrStderr(), "warning: failed to load scheduler state: %s\n", err)
|
|
state = nil
|
|
}
|
|
|
|
projectNextRun := scheduler.NewNextRunProjection(ctx.Config.Core.Location, state)
|
|
|
|
// A pause only reaches the persisted projection on the scheduler's next
|
|
// tick, so report no next run as soon as the pause is recorded.
|
|
if store := ctx.Persistence.SchedulerPauseStore; store != nil {
|
|
paused, err := store.IsPaused(ctx)
|
|
if err != nil {
|
|
_, _ = fmt.Fprintf(ctx.Command.ErrOrStderr(), "warning: failed to read scheduler pause state: %s\n", err)
|
|
} else if paused {
|
|
return func(*ir.DAG, time.Time) time.Time { return time.Time{} }
|
|
}
|
|
}
|
|
return projectNextRun
|
|
}
|
|
|
|
func sortLsRowsByLastRun(rows []lsRow, reverse bool) {
|
|
sort.SliceStable(rows, func(i, j int) bool {
|
|
ti, tj := rows[i].lastTime, rows[j].lastTime
|
|
if ti.Equal(tj) {
|
|
nameI := strings.ToLower(rows[i].dag.Name)
|
|
nameJ := strings.ToLower(rows[j].dag.Name)
|
|
if reverse {
|
|
return nameI > nameJ
|
|
}
|
|
return nameI < nameJ
|
|
}
|
|
if ti.IsZero() {
|
|
return false
|
|
}
|
|
if tj.IsZero() {
|
|
return true
|
|
}
|
|
if reverse {
|
|
return ti.Before(tj)
|
|
}
|
|
return ti.After(tj)
|
|
})
|
|
}
|
|
|
|
func enrichLsRow(ctx *Context, row *lsRow, wantLast, wantHistory bool) error {
|
|
if wantLast {
|
|
st, err := ctx.DAGRunMgr.GetLatestStatus(ctx, row.dag)
|
|
if err == nil && st.Status != ir.NotStarted {
|
|
row.lastStatus = formatStatusText(st.Status)
|
|
row.lastStarted = formatTimestamp(st.StartedAt)
|
|
if t := parseTimeString(st.StartedAt); !t.IsZero() {
|
|
row.lastTime = t
|
|
}
|
|
} else {
|
|
row.lastStatus = "-"
|
|
row.lastStarted = "-"
|
|
}
|
|
}
|
|
|
|
if wantHistory {
|
|
statuses, err := ctx.Persistence.DAGRunRepository.RecentStatuses(ctx, row.dag.Name, 5)
|
|
if err != nil {
|
|
_, _ = fmt.Fprintf(ctx.Command.ErrOrStderr(), "warning: failed to load recent DAG-run history for %s: %s\n", row.dag.Name, err)
|
|
row.history = "-"
|
|
return nil
|
|
}
|
|
if len(statuses) == 0 {
|
|
row.history = "-"
|
|
return nil
|
|
}
|
|
parts := make([]string, 0, len(statuses))
|
|
for _, st := range statuses {
|
|
parts = append(parts, formatStatusText(st.Status))
|
|
}
|
|
row.history = strings.Join(parts, ",")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func renderLsTable(out io.Writer, rows []lsRow, showNext, showLast, showHistory bool, nextRunLoc *time.Location) error {
|
|
if len(rows) == 0 {
|
|
_, err := fmt.Fprintln(out, "No DAGs found")
|
|
return err
|
|
}
|
|
|
|
headers := []string{"NAME"}
|
|
if showNext {
|
|
headers = append(headers, "NEXT_RUN")
|
|
}
|
|
if showLast {
|
|
headers = append(headers, "LAST_STATUS", "LAST_STARTED")
|
|
}
|
|
if showHistory {
|
|
headers = append(headers, "HISTORY")
|
|
}
|
|
|
|
w := tabwriter.NewWriter(out, 0, 0, 2, ' ', 0)
|
|
if _, err := fmt.Fprintln(w, strings.Join(headers, "\t")); err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, row := range rows {
|
|
cols := []string{row.dag.Name}
|
|
if showNext {
|
|
if row.nextRun.IsZero() {
|
|
cols = append(cols, "-")
|
|
} else if nextRunLoc != nil {
|
|
cols = append(cols, row.nextRun.In(nextRunLoc).Format(time.RFC3339))
|
|
} else {
|
|
cols = append(cols, row.nextRun.UTC().Format(time.RFC3339))
|
|
}
|
|
}
|
|
if showLast {
|
|
cols = append(cols, row.lastStatus, row.lastStarted)
|
|
}
|
|
if showHistory {
|
|
cols = append(cols, row.history)
|
|
}
|
|
if _, err := fmt.Fprintln(w, strings.Join(cols, "\t")); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return w.Flush()
|
|
}
|