-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add command to view active api requests
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
- Loading branch information
1 parent
62ba5eb
commit 366d7db
Showing
5 changed files
with
169 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package commands | ||
|
||
import ( | ||
"strings" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type ReqLogEntry struct { | ||
StartTime time.Time | ||
EndTime time.Time | ||
Active bool | ||
Command string | ||
Options map[string]interface{} | ||
Args []string | ||
ID int | ||
|
||
req Request | ||
log *ReqLog | ||
} | ||
|
||
func (r *ReqLogEntry) Finish() { | ||
r.log.lock.Lock() | ||
defer r.log.lock.Unlock() | ||
|
||
r.Active = false | ||
r.EndTime = time.Now() | ||
|
||
r.log.maybeCleanup() | ||
} | ||
|
||
func (r *ReqLogEntry) Copy() *ReqLogEntry { | ||
out := *r | ||
out.log = nil | ||
return &out | ||
} | ||
|
||
type ReqLog struct { | ||
Requests []*ReqLogEntry | ||
nextID int | ||
lock sync.Mutex | ||
} | ||
|
||
func (rl *ReqLog) Add(req Request) *ReqLogEntry { | ||
rl.lock.Lock() | ||
defer rl.lock.Unlock() | ||
|
||
log.Error("ADD: ", req) | ||
rle := &ReqLogEntry{ | ||
StartTime: time.Now(), | ||
Active: true, | ||
Command: strings.Join(req.Path(), "/"), | ||
Options: req.Options(), | ||
Args: req.Arguments(), | ||
ID: rl.nextID, | ||
req: req, | ||
log: rl, | ||
} | ||
|
||
rl.nextID++ | ||
rl.Requests = append(rl.Requests, rle) | ||
return rle | ||
} | ||
|
||
func (rl *ReqLog) maybeCleanup() { | ||
// only do it every so often or it might | ||
// become a perf issue | ||
if len(rl.Requests) == 0 { | ||
rl.cleanup() | ||
} | ||
} | ||
|
||
func (rl *ReqLog) cleanup() { | ||
var i int | ||
for ; i < len(rl.Requests); i++ { | ||
req := rl.Requests[i] | ||
if req.Active || req.EndTime.Add(time.Hour).After(time.Now()) { | ||
break | ||
} | ||
} | ||
|
||
if i > 0 { | ||
var j int | ||
for i < len(rl.Requests) { | ||
rl.Requests[j] = rl.Requests[i] | ||
j++ | ||
i++ | ||
} | ||
rl.Requests = rl.Requests[:len(rl.Requests)-i] | ||
} | ||
} | ||
|
||
func (rl *ReqLog) Report() []*ReqLogEntry { | ||
rl.lock.Lock() | ||
defer rl.lock.Unlock() | ||
out := make([]*ReqLogEntry, len(rl.Requests)) | ||
|
||
log.Error("REPORT: ", rl.Requests) | ||
|
||
for i, e := range rl.Requests { | ||
out[i] = e.Copy() | ||
} | ||
|
||
return out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package commands | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"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()) | ||
}, | ||
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) | ||
|
||
w := tabwriter.NewWriter(buf, 4, 4, 2, ' ', 0) | ||
fmt.Fprintln(w, "Command\tActive\tStartTime\tRunTime") | ||
for _, req := range *out { | ||
if req.Active { | ||
fmt.Fprintf(w, "%s\t%s\t%s\n", req.Command, "true", req.StartTime, time.Now().Sub(req.StartTime)) | ||
} else { | ||
fmt.Fprintf(w, "%s\t%s\t%s\n", req.Command, "false", req.StartTime, req.EndTime.Sub(req.StartTime)) | ||
} | ||
} | ||
w.Flush() | ||
|
||
return buf, nil | ||
}, | ||
}, | ||
Type: []*cmds.ReqLogEntry{}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters