-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add basic deal stats api server for spacerace slingshot #3963
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9b91628
begin adding simple api server for deal stats
whyrusleeping f96698b
finish up other endpoints
whyrusleeping 3153ab9
allow graceful shutdown
whyrusleeping 3cf2fd5
fix appending to array
whyrusleeping 88ada66
finish up the total bytes endpoint
whyrusleeping a128127
shed dealtracker: fix lint, env var filter
magik6k File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,265 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net" | ||
"net/http" | ||
"os" | ||
"strings" | ||
|
||
"github.com/filecoin-project/go-address" | ||
"github.com/filecoin-project/lotus/api" | ||
lcli "github.com/filecoin-project/lotus/cli" | ||
"github.com/ipfs/go-cid" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
type dealStatsServer struct { | ||
api api.FullNode | ||
} | ||
|
||
var filteredClients map[address.Address]bool | ||
|
||
func init() { | ||
fc := []string{"t0112", "t0113", "t0114", "t010089"} | ||
|
||
filtered, set := os.LookupEnv("FILTERED_CLIENTS") | ||
if set { | ||
fc = strings.Split(filtered, ":") | ||
} | ||
|
||
filteredClients = make(map[address.Address]bool) | ||
for _, a := range fc { | ||
addr, err := address.NewFromString(a) | ||
if err != nil { | ||
panic(err) | ||
} | ||
filteredClients[addr] = true | ||
} | ||
} | ||
|
||
type dealCountResp struct { | ||
Total int64 `json:"total"` | ||
Epoch int64 `json:"epoch"` | ||
} | ||
|
||
func (dss *dealStatsServer) handleStorageDealCount(w http.ResponseWriter, r *http.Request) { | ||
ctx := context.Background() | ||
|
||
head, err := dss.api.ChainHead(ctx) | ||
if err != nil { | ||
log.Warnf("failed to get chain head: %s", err) | ||
w.WriteHeader(500) | ||
return | ||
} | ||
|
||
deals, err := dss.api.StateMarketDeals(ctx, head.Key()) | ||
if err != nil { | ||
log.Warnf("failed to get market deals: %s", err) | ||
w.WriteHeader(500) | ||
return | ||
} | ||
|
||
var count int64 | ||
for _, d := range deals { | ||
if !filteredClients[d.Proposal.Client] { | ||
count++ | ||
} | ||
} | ||
|
||
if err := json.NewEncoder(w).Encode(&dealCountResp{ | ||
Total: count, | ||
Epoch: int64(head.Height()), | ||
}); err != nil { | ||
log.Warnf("failed to write back deal count response: %s", err) | ||
return | ||
} | ||
} | ||
|
||
type dealAverageResp struct { | ||
AverageSize int64 `json:"average_size"` | ||
Epoch int64 `json:"epoch"` | ||
} | ||
|
||
func (dss *dealStatsServer) handleStorageDealAverageSize(w http.ResponseWriter, r *http.Request) { | ||
ctx := context.Background() | ||
|
||
head, err := dss.api.ChainHead(ctx) | ||
if err != nil { | ||
log.Warnf("failed to get chain head: %s", err) | ||
w.WriteHeader(500) | ||
return | ||
} | ||
|
||
deals, err := dss.api.StateMarketDeals(ctx, head.Key()) | ||
if err != nil { | ||
log.Warnf("failed to get market deals: %s", err) | ||
w.WriteHeader(500) | ||
return | ||
} | ||
|
||
var count int64 | ||
var totalBytes int64 | ||
for _, d := range deals { | ||
if !filteredClients[d.Proposal.Client] { | ||
count++ | ||
totalBytes += int64(d.Proposal.PieceSize.Unpadded()) | ||
} | ||
} | ||
|
||
if err := json.NewEncoder(w).Encode(&dealAverageResp{ | ||
AverageSize: totalBytes / count, | ||
Epoch: int64(head.Height()), | ||
}); err != nil { | ||
log.Warnf("failed to write back deal average response: %s", err) | ||
return | ||
} | ||
} | ||
|
||
type dealTotalResp struct { | ||
TotalBytes int64 `json:"total_size"` | ||
Epoch int64 `json:"epoch"` | ||
} | ||
|
||
func (dss *dealStatsServer) handleStorageDealTotalReal(w http.ResponseWriter, r *http.Request) { | ||
ctx := context.Background() | ||
|
||
head, err := dss.api.ChainHead(ctx) | ||
if err != nil { | ||
log.Warnf("failed to get chain head: %s", err) | ||
w.WriteHeader(500) | ||
return | ||
} | ||
|
||
deals, err := dss.api.StateMarketDeals(ctx, head.Key()) | ||
if err != nil { | ||
log.Warnf("failed to get market deals: %s", err) | ||
w.WriteHeader(500) | ||
return | ||
} | ||
|
||
var totalBytes int64 | ||
for _, d := range deals { | ||
if !filteredClients[d.Proposal.Client] { | ||
totalBytes += int64(d.Proposal.PieceSize.Unpadded()) | ||
} | ||
} | ||
|
||
if err := json.NewEncoder(w).Encode(&dealTotalResp{ | ||
TotalBytes: totalBytes, | ||
Epoch: int64(head.Height()), | ||
}); err != nil { | ||
log.Warnf("failed to write back deal average response: %s", err) | ||
return | ||
} | ||
|
||
} | ||
|
||
type clientStatsOutput struct { | ||
Client address.Address `json:"client"` | ||
DataSize int64 `json:"data_size"` | ||
NumCids int `json:"num_cids"` | ||
NumDeals int `json:"num_deals"` | ||
NumMiners int `json:"num_miners"` | ||
|
||
cids map[cid.Cid]bool | ||
providers map[address.Address]bool | ||
} | ||
|
||
func (dss *dealStatsServer) handleStorageClientStats(w http.ResponseWriter, r *http.Request) { | ||
ctx := context.Background() | ||
|
||
head, err := dss.api.ChainHead(ctx) | ||
if err != nil { | ||
log.Warnf("failed to get chain head: %s", err) | ||
w.WriteHeader(500) | ||
return | ||
} | ||
|
||
deals, err := dss.api.StateMarketDeals(ctx, head.Key()) | ||
if err != nil { | ||
log.Warnf("failed to get market deals: %s", err) | ||
w.WriteHeader(500) | ||
return | ||
} | ||
|
||
stats := make(map[address.Address]*clientStatsOutput) | ||
|
||
for _, d := range deals { | ||
if filteredClients[d.Proposal.Client] { | ||
continue | ||
} | ||
|
||
st, ok := stats[d.Proposal.Client] | ||
if !ok { | ||
st = &clientStatsOutput{ | ||
Client: d.Proposal.Client, | ||
cids: make(map[cid.Cid]bool), | ||
providers: make(map[address.Address]bool), | ||
} | ||
stats[d.Proposal.Client] = st | ||
} | ||
|
||
st.DataSize += int64(d.Proposal.PieceSize.Unpadded()) | ||
st.cids[d.Proposal.PieceCID] = true | ||
st.providers[d.Proposal.Provider] = true | ||
st.NumDeals++ | ||
} | ||
|
||
out := make([]*clientStatsOutput, 0, len(stats)) | ||
for _, cso := range stats { | ||
cso.NumCids = len(cso.cids) | ||
cso.NumMiners = len(cso.providers) | ||
|
||
out = append(out, cso) | ||
} | ||
|
||
if err := json.NewEncoder(w).Encode(out); err != nil { | ||
log.Warnf("failed to write back client stats response: %s", err) | ||
return | ||
} | ||
} | ||
|
||
var serveDealStatsCmd = &cli.Command{ | ||
Name: "serve-deal-stats", | ||
Flags: []cli.Flag{}, | ||
Action: func(cctx *cli.Context) error { | ||
api, closer, err := lcli.GetFullNodeAPI(cctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer closer() | ||
ctx := lcli.ReqContext(cctx) | ||
|
||
_ = ctx | ||
|
||
dss := &dealStatsServer{api} | ||
|
||
mux := &http.ServeMux{} | ||
mux.HandleFunc("/api/storagedeal/count", dss.handleStorageDealCount) | ||
mux.HandleFunc("/api/storagedeal/averagesize", dss.handleStorageDealAverageSize) | ||
mux.HandleFunc("/api/storagedeal/totalreal", dss.handleStorageDealTotalReal) | ||
mux.HandleFunc("/api/storagedeal/clientstats", dss.handleStorageClientStats) | ||
|
||
s := &http.Server{ | ||
Addr: ":7272", | ||
Handler: mux, | ||
} | ||
|
||
go func() { | ||
<-ctx.Done() | ||
if err := s.Shutdown(context.TODO()); err != nil { | ||
log.Error(err) | ||
} | ||
}() | ||
|
||
list, err := net.Listen("tcp", ":7272") // nolint | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return s.Serve(list) | ||
}, | ||
} |
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 |
---|---|---|
|
@@ -36,6 +36,7 @@ func main() { | |
mpoolStatsCmd, | ||
exportChainCmd, | ||
consensusCmd, | ||
serveDealStatsCmd, | ||
} | ||
|
||
app := &cli.App{ | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way we can filter out miners as well? E.g. there are terabytes of data from Travis' miners that were stored at genesis on Space Race net, but this isn't legit usage data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, i'm only looking at deals on chain. Do the genesis miners have deals for their data?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I believe they do have deals on chain!