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

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

10 11 12
	oldcmds "github.com/ipfs/go-ipfs/commands"
	cmds "gx/ipfs/QmRRovo1DE6i5cMjCbf19mQCSuszF6SKwdZNUMS7MtBnH1/go-ipfs-cmds"
	cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
13 14
)

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

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

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

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

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

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

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

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

111
		return nil
112 113
	},
}