forked from bittorrent/go-btfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Fix/status page error (bittorrent#225)" (bittorrent#226)
This reverts commit 05bc559.
- Loading branch information
1 parent
17469b9
commit 71c8274
Showing
5 changed files
with
679 additions
and
6 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,235 @@ | ||
package commands | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"github.com/bittorrent/go-btfs/core/commands/cmdenv" | ||
"io" | ||
"math/big" | ||
"strconv" | ||
"time" | ||
|
||
cmds "github.com/bittorrent/go-btfs-cmds" | ||
"github.com/bittorrent/go-btfs/chain" | ||
) | ||
|
||
var StatusContractCmd = &cmds.Command{ | ||
Helptext: cmds.HelpText{ | ||
Tagline: "report status-contract cmd.", | ||
ShortDescription: ` | ||
report status-contract cmd, total cmd and list cmd.`, | ||
}, | ||
Subcommands: map[string]*cmds.Command{ | ||
"total": TotalCmd, | ||
"reportlist": ReportListCmd, | ||
"lastinfo": LastInfoCmd, | ||
"config": StatusConfigCmd, | ||
}, | ||
} | ||
|
||
type TotalCmdRet struct { | ||
PeerId string `json:"peer_id"` | ||
StatusContract string `json:"status_contract"` | ||
TotalCount int `json:"total_count"` | ||
TotalGasSpend string `json:"total_gas_spend"` | ||
} | ||
|
||
var TotalCmd = &cmds.Command{ | ||
Helptext: cmds.HelpText{ | ||
Tagline: "report status-contract total info, (total count, total gas spend, and contract address)", | ||
}, | ||
RunTimeout: 5 * time.Minute, | ||
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { | ||
n, err := cmdenv.GetNode(env) | ||
if err != nil { | ||
return err | ||
} | ||
peerId := n.Identity.Pretty() | ||
|
||
list, err := chain.GetReportStatusListOK() | ||
if err != nil { | ||
return err | ||
} | ||
if list == nil { | ||
return nil | ||
} | ||
|
||
// get list to cal spend, from string to big.int... | ||
totalGasSpend := new(big.Int) | ||
for _, r := range list { | ||
n := new(big.Int) | ||
if len(r.GasSpend) <= 0 { | ||
//fmt.Println("r.GasSpend is zero. ") | ||
continue | ||
} | ||
//fmt.Println("r.GasSpend = ", r.GasSpend) | ||
|
||
n, ok := n.SetString(r.GasSpend, 10) | ||
if !ok { | ||
return errors.New("parse gas_spend is error. ") | ||
} | ||
totalGasSpend = totalGasSpend.Add(totalGasSpend, n) | ||
} | ||
|
||
return cmds.EmitOnce(res, &TotalCmdRet{ | ||
PeerId: peerId, | ||
StatusContract: list[len(list)-1].StatusContract, | ||
TotalCount: len(list), | ||
TotalGasSpend: totalGasSpend.String(), | ||
}) | ||
}, | ||
Type: TotalCmdRet{}, | ||
Encoders: cmds.EncoderMap{ | ||
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *TotalCmdRet) error { | ||
marshaled, err := json.MarshalIndent(out, "", "\t") | ||
if err != nil { | ||
return err | ||
} | ||
marshaled = append(marshaled, byte('\n')) | ||
fmt.Fprintln(w, string(marshaled)) | ||
return nil | ||
}), | ||
}, | ||
} | ||
|
||
type ReportListCmdRet struct { | ||
Records []*chain.LevelDbReportStatusInfo `json:"records"` | ||
Total int `json:"total"` | ||
PeerId string `json:"peer_id"` | ||
} | ||
|
||
var ReportListCmd = &cmds.Command{ | ||
Helptext: cmds.HelpText{ | ||
Tagline: "report status-contract list, and input from and limit to get its.", | ||
}, | ||
RunTimeout: 5 * time.Minute, | ||
Arguments: []cmds.Argument{ | ||
cmds.StringArg("from", true, false, "page offset"), | ||
cmds.StringArg("limit", true, false, "page limit."), | ||
}, | ||
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { | ||
n, err := cmdenv.GetNode(env) | ||
if err != nil { | ||
return err | ||
} | ||
peerId := n.Identity.Pretty() | ||
|
||
from, err := strconv.Atoi(req.Arguments[0]) | ||
if err != nil { | ||
return fmt.Errorf("parse from:%v failed", req.Arguments[0]) | ||
} | ||
limit, err := strconv.Atoi(req.Arguments[1]) | ||
if err != nil { | ||
return fmt.Errorf("parse limit:%v failed", req.Arguments[1]) | ||
} | ||
if from < 0 { | ||
return fmt.Errorf("invalid from: %d", from) | ||
} | ||
if limit < 0 { | ||
return fmt.Errorf("invalid limit: %d", limit) | ||
} | ||
|
||
list, err := chain.GetReportStatusListOK() | ||
if err != nil { | ||
return err | ||
} | ||
if list == nil { | ||
return nil | ||
} | ||
// | ||
//from := 0 | ||
//limit := 10 | ||
|
||
From := len(list) - 1 - from - limit | ||
if From <= 0 { | ||
From = 0 | ||
} | ||
To := len(list) - 1 - from | ||
if To > len(list)-1 { | ||
To = len(list) - 1 | ||
} | ||
fmt.Println("From, To = ", From, To) | ||
|
||
s := list[From:To] | ||
l := len(s) | ||
for i, j := 0, l-1; i < j; i, j = i+1, j-1 { | ||
s[i], s[j] = s[j], s[i] | ||
} | ||
|
||
return cmds.EmitOnce(res, &ReportListCmdRet{ | ||
Records: s, | ||
Total: len(list), | ||
PeerId: peerId, | ||
}) | ||
}, | ||
Type: ReportListCmdRet{}, | ||
Encoders: cmds.EncoderMap{ | ||
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *ReportListCmdRet) error { | ||
marshaled, err := json.MarshalIndent(out, "", "\t") | ||
if err != nil { | ||
return err | ||
} | ||
marshaled = append(marshaled, byte('\n')) | ||
fmt.Fprintln(w, string(marshaled)) | ||
return nil | ||
}), | ||
}, | ||
} | ||
|
||
var LastInfoCmd = &cmds.Command{ | ||
Helptext: cmds.HelpText{ | ||
Tagline: "get reporting status-contract last info", | ||
}, | ||
RunTimeout: 5 * time.Minute, | ||
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { | ||
last, err := chain.GetLastOnline() | ||
if err != nil { | ||
return err | ||
} | ||
if last == nil { | ||
return errors.New("not found. ") | ||
} | ||
|
||
return cmds.EmitOnce(res, last) | ||
}, | ||
Type: chain.LastOnlineInfo{}, | ||
Encoders: cmds.EncoderMap{ | ||
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *chain.LastOnlineInfo) error { | ||
marshaled, err := json.MarshalIndent(out, "", "\t") | ||
if err != nil { | ||
return err | ||
} | ||
marshaled = append(marshaled, byte('\n')) | ||
fmt.Fprintln(w, string(marshaled)) | ||
return nil | ||
}), | ||
}, | ||
} | ||
|
||
var StatusConfigCmd = &cmds.Command{ | ||
Helptext: cmds.HelpText{ | ||
Tagline: "get reporting status-contract config. ", | ||
}, | ||
RunTimeout: 5 * time.Minute, | ||
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { | ||
rs, err := chain.GetReportStatus() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return cmds.EmitOnce(res, rs) | ||
}, | ||
Type: chain.ReportStatusInfo{}, | ||
Encoders: cmds.EncoderMap{ | ||
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *chain.ReportStatusInfo) error { | ||
marshaled, err := json.MarshalIndent(out, "", "\t") | ||
if err != nil { | ||
return err | ||
} | ||
marshaled = append(marshaled, byte('\n')) | ||
fmt.Fprintln(w, string(marshaled)) | ||
return nil | ||
}), | ||
}, | ||
} |
Oops, something went wrong.