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

import (
	"bytes"
	"fmt"
	"io"
7
	"sort"
8 9 10 11
	"text/tabwriter"
	"time"

	cmds "github.com/ipfs/go-ipfs/commands"
Jan Winkelmann's avatar
Jan Winkelmann committed
12 13
	e "github.com/ipfs/go-ipfs/core/commands/e"

14
	"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
15 16 17
)

var ActiveReqsCmd = &cmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
18
	Helptext: cmdkit.HelpText{
19
		Tagline: "List commands run on this IPFS node.",
20 21 22 23 24 25 26
		ShortDescription: `
Lists running and recently run commands.
`,
	},
	Run: func(req cmds.Request, res cmds.Response) {
		res.SetOutput(req.InvocContext().ReqLog.Report())
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
27
	Options: []cmdkit.Option{
28
		cmdkit.BoolOption("verbose", "v", "Print extra information."),
Jeromy's avatar
Jeromy committed
29
	},
30
	Subcommands: map[string]*cmds.Command{
31 32
		"clear":    clearInactiveCmd,
		"set-time": setRequestClearCmd,
33
	},
34 35
	Marshalers: map[cmds.EncodingType]cmds.Marshaler{
		cmds.Text: func(res cmds.Response) (io.Reader, error) {
Jan Winkelmann's avatar
Jan Winkelmann committed
36 37 38 39 40 41
			v, err := unwrapOutput(res.Output())
			if err != nil {
				return nil, err
			}

			out, ok := v.(*[]*cmds.ReqLogEntry)
42
			if !ok {
Jan Winkelmann's avatar
Jan Winkelmann committed
43
				return nil, e.TypeErr(out, v)
44 45 46
			}
			buf := new(bytes.Buffer)

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

49
			w := tabwriter.NewWriter(buf, 4, 4, 2, ' ', 0)
50 51 52
			if verbose {
				fmt.Fprint(w, "ID\t")
			}
Jeromy's avatar
Jeromy committed
53 54 55 56 57 58
			fmt.Fprint(w, "Command\t")
			if verbose {
				fmt.Fprint(w, "Arguments\tOptions\t")
			}
			fmt.Fprintln(w, "Active\tStartTime\tRunTime")

59
			for _, req := range *out {
60 61 62
				if verbose {
					fmt.Fprintf(w, "%d\t", req.ID)
				}
Jeromy's avatar
Jeromy committed
63 64 65
				fmt.Fprintf(w, "%s\t", req.Command)
				if verbose {
					fmt.Fprintf(w, "%v\t[", req.Args)
66
					var keys []string
Jan Winkelmann's avatar
Jan Winkelmann committed
67
					for k := range req.Options {
68 69 70 71 72 73
						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
74 75 76 77 78
					}
					fmt.Fprintf(w, "]\t")
				}

				var live time.Duration
79
				if req.Active {
80
					live = time.Since(req.StartTime)
81
				} else {
Jeromy's avatar
Jeromy committed
82
					live = req.EndTime.Sub(req.StartTime)
83
				}
Jeromy's avatar
Jeromy committed
84 85
				t := req.StartTime.Format(time.Stamp)
				fmt.Fprintf(w, "%t\t%s\t%s\n", req.Active, t, live)
86 87 88 89 90 91 92 93
			}
			w.Flush()

			return buf, nil
		},
	},
	Type: []*cmds.ReqLogEntry{},
}
94 95

var clearInactiveCmd = &cmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
96
	Helptext: cmdkit.HelpText{
97
		Tagline: "Clear inactive requests from the log.",
98 99 100 101 102
	},
	Run: func(req cmds.Request, res cmds.Response) {
		req.InvocContext().ReqLog.ClearInactive()
	},
}
103 104

var setRequestClearCmd = &cmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
105
	Helptext: cmdkit.HelpText{
106
		Tagline: "Set how long to keep inactive requests in the log.",
107
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
108 109
	Arguments: []cmdkit.Argument{
		cmdkit.StringArg("time", true, false, "Time to keep inactive requests in log."),
110 111 112 113
	},
	Run: func(req cmds.Request, res cmds.Response) {
		tval, err := time.ParseDuration(req.Arguments()[0])
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
114
			res.SetError(err, cmdkit.ErrNormal)
115 116 117 118 119 120
			return
		}

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