diff --git a/http/api.go b/http/api.go index 01838b7..f8d8d6a 100644 --- a/http/api.go +++ b/http/api.go @@ -88,6 +88,20 @@ func (as *apiServer) handleBlocksPUT(jc jape.Context) { jc.Encode(block.Cid()) } +func (as *apiServer) handleProviderStats(jc jape.Context) { + stats, err := as.ipfs.ReproviderStats() + if err != nil { + jc.Error(err, http.StatusInternalServerError) + return + } + jc.Encode(ProviderStatsResp{ + TotalProvides: stats.TotalProvides, + LastReprovideBatchSize: stats.LastReprovideBatchSize, + AvgProvideDuration: stats.AvgProvideDuration, + LastReprovideDuration: stats.LastReprovideDuration, + }) +} + // NewAPIHandler returns a new http.Handler that handles requests to the api func NewAPIHandler(ipfs *ipfs.Node, cfg config.Config, log *zap.Logger) http.Handler { s := &apiServer{ @@ -101,5 +115,6 @@ func NewAPIHandler(ipfs *ipfs.Node, cfg config.Config, log *zap.Logger) http.Han "PUT /api/blocks/:cid": s.handleBlocksCIDPUT, "GET /api/peers": s.handleListPeers, "PUT /api/peers": s.handleAddPeer, + "GET /api/provider/stats": s.handleProviderStats, }) } diff --git a/http/types.go b/http/types.go new file mode 100644 index 0000000..623817c --- /dev/null +++ b/http/types.go @@ -0,0 +1,12 @@ +package http + +import "time" + +// The ProviderStatsResp struct is used to return stats about the reprovider +// system. +type ProviderStatsResp struct { + TotalProvides uint64 `json:"totalProvides"` + LastReprovideBatchSize uint64 `json:"lastReprovideBatchSize"` + AvgProvideDuration time.Duration `json:"avgProvideDuration"` + LastReprovideDuration time.Duration `json:"lastReprovideDuration"` +} diff --git a/ipfs/node.go b/ipfs/node.go index 9d138cb..242cc8a 100644 --- a/ipfs/node.go +++ b/ipfs/node.go @@ -176,6 +176,11 @@ func (n *Node) PinCAR(ctx context.Context, r io.Reader) error { return nil } +// ReproviderStats returns the reprovider statistics +func (n *Node) ReproviderStats() (provider.ReproviderStats, error) { + return n.provider.Stat() +} + func mustParsePeer(s string) peer.AddrInfo { info, err := peer.AddrInfoFromString(s) if err != nil { @@ -257,7 +262,7 @@ func NewNode(ctx context.Context, privateKey crypto.PrivKey, cfg config.IPFS, ds providerOpts := []provider.Option{ provider.KeyProvider(provider.NewBlockstoreProvider(bs)), provider.Online(frt), - provider.ReproviderInterval(18 * time.Hour), + provider.ReproviderInterval(22 * time.Hour), } prov, err := provider.New(ds, providerOpts...)