active.go 2.58 KB
Newer Older
1 2 3 4 5 6
package commands

import (
	"bytes"
	"fmt"
	"io"
7
	"sort"
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
	"text/tabwriter"
	"time"

	cmds "github.com/ipfs/go-ipfs/commands"
)

var ActiveReqsCmd = &cmds.Command{
	Helptext: cmds.HelpText{
		Tagline: "List commands run on this ipfs node",
		ShortDescription: `
Lists running and recently run commands.
`,
	},
	Run: func(req cmds.Request, res cmds.Response) {
		res.SetOutput(req.InvocContext().ReqLog.Report())
	},
Jeromy's avatar
Jeromy committed
24 25 26
	Options: []cmds.Option{
		cmds.BoolOption("v", "verbose", "print more verbose output"),
	},
27
	Subcommands: map[string]*cmds.Command{
28 29
		"clear":    clearInactiveCmd,
		"set-time": setRequestClearCmd,
30
	},
31 32 33 34 35 36 37 38 39
	Marshalers: map[cmds.EncodingType]cmds.Marshaler{
		cmds.Text: func(res cmds.Response) (io.Reader, error) {
			out, ok := res.Output().(*[]*cmds.ReqLogEntry)
			if !ok {
				log.Errorf("%#v", res.Output())
				return nil, cmds.ErrIncorrectType
			}
			buf := new(bytes.Buffer)

Jeromy's avatar
Jeromy committed
40 41
			verbose, _, _ := res.Request().Option("v").Bool()

42
			w := tabwriter.NewWriter(buf, 4, 4, 2, ' ', 0)
43 44 45
			if verbose {
				fmt.Fprint(w, "ID\t")
			}
Jeromy's avatar
Jeromy committed
46 47 48 49 50 51
			fmt.Fprint(w, "Command\t")
			if verbose {
				fmt.Fprint(w, "Arguments\tOptions\t")
			}
			fmt.Fprintln(w, "Active\tStartTime\tRunTime")

52
			for _, req := range *out {
53 54 55
				if verbose {
					fmt.Fprintf(w, "%d\t", req.ID)
				}
Jeromy's avatar
Jeromy committed
56 57 58
				fmt.Fprintf(w, "%s\t", req.Command)
				if verbose {
					fmt.Fprintf(w, "%v\t[", req.Args)
59 60 61 62 63 64 65 66
					var keys []string
					for k, _ := range req.Options {
						keys = append(keys, k)
					}
					sort.Strings(keys)

					for _, k := range keys {
						fmt.Fprintf(w, "%s=%v,", k, req.Options[k])
Jeromy's avatar
Jeromy committed
67 68 69 70 71
					}
					fmt.Fprintf(w, "]\t")
				}

				var live time.Duration
72
				if req.Active {
Jeromy's avatar
Jeromy committed
73
					live = time.Now().Sub(req.StartTime)
74
				} else {
Jeromy's avatar
Jeromy committed
75
					live = req.EndTime.Sub(req.StartTime)
76
				}
Jeromy's avatar
Jeromy committed
77 78
				t := req.StartTime.Format(time.Stamp)
				fmt.Fprintf(w, "%t\t%s\t%s\n", req.Active, t, live)
79 80 81 82 83 84 85 86
			}
			w.Flush()

			return buf, nil
		},
	},
	Type: []*cmds.ReqLogEntry{},
}
87 88 89 90 91 92 93 94 95

var clearInactiveCmd = &cmds.Command{
	Helptext: cmds.HelpText{
		Tagline: "Clear inactive requests from the log",
	},
	Run: func(req cmds.Request, res cmds.Response) {
		req.InvocContext().ReqLog.ClearInactive()
	},
}
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

var setRequestClearCmd = &cmds.Command{
	Helptext: cmds.HelpText{
		Tagline: "Set how long to keep inactive requests in the log",
	},
	Arguments: []cmds.Argument{
		cmds.StringArg("time", true, false, "time to keep inactive requests in log"),
	},
	Run: func(req cmds.Request, res cmds.Response) {
		tval, err := time.ParseDuration(req.Arguments()[0])
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

		req.InvocContext().ReqLog.SetKeepTime(tval)
	},
}