Fix allocations filter option

License: MIT
Signed-off-by: Hector Sanjuan <code@hector.link>
This commit is contained in:
Hector Sanjuan 2018-08-10 14:39:44 +02:00
parent 7b826a5335
commit bbf707d61e
8 changed files with 61 additions and 40 deletions

View File

@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"net/url"
"strings"
"time"
cid "github.com/ipfs/go-cid"
@ -84,9 +85,29 @@ func (c *Client) Unpin(ci *cid.Cid) error {
// Allocations returns the consensus state listing all tracked items and
// the peers that should be pinning them.
func (c *Client) Allocations(pinType api.PinType) ([]api.Pin, error) {
func (c *Client) Allocations(filter api.PinType) ([]api.Pin, error) {
var pins []api.PinSerial
err := c.do("GET", fmt.Sprintf("/allocations?pintype=%s", pinType.String()), nil, nil, &pins)
types := []api.PinType{
api.DataType,
api.MetaType,
api.ClusterDAGType,
api.ShardType,
}
var strFilter []string
if filter == api.AllType {
strFilter = []string{"all"}
} else {
for _, t := range types {
if t&filter > 0 { // the filter includes this type
strFilter = append(strFilter, t.String())
}
}
}
err := c.do("GET", fmt.Sprintf("/allocations?filter=%s", strings.Join(strFilter, ",")), nil, nil, &pins)
result := make([]api.Pin, len(pins))
for i, p := range pins {
result[i] = p.ToPin()

View File

@ -167,7 +167,7 @@ func TestAllocations(t *testing.T) {
defer shutdown(api)
testF := func(t *testing.T, c *Client) {
pins, err := c.Allocations(types.AllType)
pins, err := c.Allocations(types.DataType | types.MetaType)
if err != nil {
t.Fatal(err)
}

View File

@ -635,19 +635,13 @@ func (api *API) unpinHandler(w http.ResponseWriter, r *http.Request) {
}
}
// filterOutPin returns true if the given pin should be filtered out according
// to the input filter type
func (api *API) filterOutPin(filter types.PinType, pin types.Pin) bool {
if filter == types.AllType {
return false
}
return pin.Type != filter
}
func (api *API) allocationsHandler(w http.ResponseWriter, r *http.Request) {
queryValues := r.URL.Query()
pintype := queryValues.Get("pintype")
filter := types.PinTypeFromString(pintype)
filterStr := queryValues.Get("filter")
var filter types.PinType = 0
for _, f := range strings.Split(filterStr, ",") {
filter |= types.PinTypeFromString(f)
}
var pins []types.PinSerial
err := api.rpcClient.Call(
"",
@ -658,7 +652,7 @@ func (api *API) allocationsHandler(w http.ResponseWriter, r *http.Request) {
)
outPins := make([]types.PinSerial, 0)
for _, pinS := range pins {
if !api.filterOutPin(filter, pinS.ToPin()) {
if uint64(filter)&pinS.Type > 0 {
// add this pin to output
outPins = append(outPins, pinS)
}
@ -831,7 +825,7 @@ func parseCidOrError(w http.ResponseWriter, r *http.Request) types.PinSerial {
pin := types.PinSerial{
Cid: hash,
Type: int(types.DataType),
Type: uint64(types.DataType),
}
queryValues := r.URL.Query()

View File

@ -488,7 +488,7 @@ func TestAPIAllocationsEndpoint(t *testing.T) {
tf := func(t *testing.T, url urlF) {
var resp []api.PinSerial
makeGet(t, rest, url(rest)+"/allocations?a=false", &resp)
makeGet(t, rest, url(rest)+"/allocations?filter=pin,meta-pin", &resp)
if len(resp) != 3 ||
resp[0].Cid != test.TestCid1 || resp[1].Cid != test.TestCid2 ||
resp[2].Cid != test.TestCid3 {

View File

@ -570,7 +570,7 @@ func StringsToCidSet(strs []string) *cid.Set {
// [][]..[] [][]..[] Blocks (indirectly pinned on ipfs, not tracked in cluster)
//
//
type PinType int
type PinType uint64
// PinType values. See PinType documentation for further explanation.
const (
@ -619,8 +619,8 @@ func PinTypeFromString(str string) PinType {
}
// String returns a printable value to identify the PinType
func (pT *PinType) String() string {
switch *pT {
func (pT PinType) String() string {
switch pT {
case DataType:
return "pin"
case MetaType:
@ -632,7 +632,7 @@ func (pT *PinType) String() string {
case AllType:
return "all"
default:
panic("String() called on invalid shard type")
return "bad-type"
}
}
@ -695,7 +695,7 @@ type PinSerial struct {
PinOptions
Cid string `json:"cid"`
Type int `json:"type"`
Type uint64 `json:"type"`
Allocations []string `json:"allocations"`
MaxDepth int `json:"max_depth"`
Reference string `json:"reference"`
@ -718,7 +718,7 @@ func (pin Pin) ToSerial() PinSerial {
return PinSerial{
Cid: c,
Allocations: allocs,
Type: int(pin.Type),
Type: uint64(pin.Type),
MaxDepth: pin.MaxDepth,
Reference: ref,
PinOptions: PinOptions{

View File

@ -551,13 +551,19 @@ any monitoring information about the IPFS status of the CIDs, it
merely represents the list of pins which are part of the shared state of
the cluster. For IPFS-status information about the pins, use "status".
Pins related to sharded DAGs are hidden by default (--all to show).
The filter only takes effect when listing all pins. The possible values are:
- all
- pin
- meta-pin
- clusterdag-pin
- shard-pin
`,
ArgsUsage: "[CID]",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "all, a",
Usage: "display hidden CIDs",
cli.StringFlag{
Name: "filter",
Usage: "Comma separated list of pin types. See help above.",
Value: "pin",
},
},
Action: func(c *cli.Context) error {
@ -568,10 +574,12 @@ Pins related to sharded DAGs are hidden by default (--all to show).
resp, cerr := globalClient.Allocation(ci)
formatResponse(c, resp, cerr)
} else {
filter := api.PinType(api.DataType)
if c.Bool("all") {
filter = api.PinType(api.AllType)
var filter api.PinType = 0
strFilter := strings.Split(c.String("filter"), ",")
for _, f := range strFilter {
filter |= api.PinTypeFromString(f)
}
resp, cerr := globalClient.Allocations(filter)
formatResponse(c, resp, cerr)
}

View File

@ -112,7 +112,7 @@ func (st *mapStateV3) next() migrateable {
mst4.PinMap[k] = api.PinSerial{
Cid: v.Cid,
Allocations: v.Allocations,
Type: int(api.DataType),
Type: uint64(api.DataType),
Reference: "",
PinOptions: api.PinOptions{
ReplicationFactorMin: v.ReplicationFactorMin,

View File

@ -53,16 +53,14 @@ func (mock *mockService) Unpin(ctx context.Context, in api.PinSerial, out *struc
}
func (mock *mockService) Pins(ctx context.Context, in struct{}, out *[]api.PinSerial) error {
cid1 := MustDecodeCid(TestCid1)
cid2 := MustDecodeCid(TestCid2)
cid3 := MustDecodeCid(TestCid3)
*out = []api.PinSerial{
{
Cid: TestCid1,
},
{
Cid: TestCid2,
},
{
Cid: TestCid3,
},
api.PinCid(cid1).ToSerial(),
api.PinCid(cid2).ToSerial(),
api.PinCid(cid3).ToSerial(),
}
return nil
}