active.go 2.86 KB
Newer Older
1 2 3
package commands

import (
Overbool's avatar
Overbool committed
4
	"bytes"
5 6
	"fmt"
	"io"
7
	"sort"
8 9 10
	"text/tabwriter"
	"time"

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

	cmds "gx/ipfs/QmdTmGruUz23vgzym3uWpnAEQdGdGifQqBvP8UXSRjG8gZ/go-ipfs-cmds"
14
	cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
15 16
)

Kejie Zhang's avatar
Kejie Zhang committed
17 18 19 20
const (
	verboseOptionName = "v"
)

21
var ActiveReqsCmd = &cmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
22
	Helptext: cmdkit.HelpText{
23
		Tagline: "List commands run on this IPFS node.",
24 25 26 27
		ShortDescription: `
Lists running and recently run commands.
`,
	},
28 29 30
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
		ctx := env.(*oldcmds.Context)
		return res.Emit(ctx.ReqLog.Report())
31
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
32
	Options: []cmdkit.Option{
Kejie Zhang's avatar
Kejie Zhang committed
33
		cmdkit.BoolOption("verbose", verboseOptionName, "Print extra information."),
Jeromy's avatar
Jeromy committed
34
	},
35
	Subcommands: map[string]*cmds.Command{
36 37
		"clear":    clearInactiveCmd,
		"set-time": setRequestClearCmd,
38
	},
39
	Encoders: cmds.EncoderMap{
40 41
		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
42

Overbool's avatar
Overbool committed
43 44 45
			buf := new(bytes.Buffer)

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

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

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

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

			fmt.Fprint(w, buf)

			return nil
88
		}),
89 90 91
	},
	Type: []*cmds.ReqLogEntry{},
}
92 93

var clearInactiveCmd = &cmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
94
	Helptext: cmdkit.HelpText{
95
		Tagline: "Clear inactive requests from the log.",
96
	},
97 98 99 100
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
		ctx := env.(*oldcmds.Context)
		ctx.ReqLog.ClearInactive()
		return nil
101 102
	},
}
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
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
		tval, err := time.ParseDuration(req.Arguments[0])
113
		if err != nil {
114
			return err
115
		}
116 117
		ctx := env.(*oldcmds.Context)
		ctx.ReqLog.SetKeepTime(tval)
118

119
		return nil
120 121
	},
}