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

import (
	"fmt"
	"io"
6
	"sort"
7 8 9
	"text/tabwriter"
	"time"

10
	oldcmds "github.com/ipfs/go-ipfs/commands"
Overbool's avatar
Overbool committed
11

Jakub Sztandera's avatar
Jakub Sztandera committed
12
	cmds "github.com/ipfs/go-ipfs-cmds"
13 14
)

Kejie Zhang's avatar
Kejie Zhang committed
15
const (
16
	verboseOptionName = "verbose"
Kejie Zhang's avatar
Kejie Zhang committed
17 18
)

19
var ActiveReqsCmd = &cmds.Command{
Steven Allen's avatar
Steven Allen committed
20
	Helptext: cmds.HelpText{
21
		Tagline: "List commands run on this IPFS node.",
22 23 24 25
		ShortDescription: `
Lists running and recently run commands.
`,
	},
26
	NoLocal: true,
27 28
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
		ctx := env.(*oldcmds.Context)
29
		return cmds.EmitOnce(res, ctx.ReqLog.Report())
30
	},
Steven Allen's avatar
Steven Allen committed
31 32
	Options: []cmds.Option{
		cmds.BoolOption(verboseOptionName, "v", "Print extra information."),
Jeromy's avatar
Jeromy committed
33
	},
34
	Subcommands: map[string]*cmds.Command{
35 36
		"clear":    clearInactiveCmd,
		"set-time": setRequestClearCmd,
37
	},
38
	Encoders: cmds.EncoderMap{
39 40
		cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *[]*cmds.ReqLogEntry) error {
			verbose, _ := req.Options[verboseOptionName].(bool)
Jeromy's avatar
Jeromy committed
41

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

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

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

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

			return nil
83
		}),
84 85 86
	},
	Type: []*cmds.ReqLogEntry{},
}
87 88

var clearInactiveCmd = &cmds.Command{
Steven Allen's avatar
Steven Allen committed
89
	Helptext: cmds.HelpText{
90
		Tagline: "Clear inactive requests from the log.",
91
	},
92 93 94 95
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
		ctx := env.(*oldcmds.Context)
		ctx.ReqLog.ClearInactive()
		return nil
96 97
	},
}
98 99

var setRequestClearCmd = &cmds.Command{
Steven Allen's avatar
Steven Allen committed
100
	Helptext: cmds.HelpText{
101
		Tagline: "Set how long to keep inactive requests in the log.",
102
	},
Steven Allen's avatar
Steven Allen committed
103 104
	Arguments: []cmds.Argument{
		cmds.StringArg("time", true, false, "Time to keep inactive requests in log."),
105
	},
106 107
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
		tval, err := time.ParseDuration(req.Arguments[0])
108
		if err != nil {
109
			return err
110
		}
111 112
		ctx := env.(*oldcmds.Context)
		ctx.ReqLog.SetKeepTime(tval)
113

114
		return nil
115 116
	},
}