Skip to content

Commit

Permalink
test: online v2 test (bittorrent#276)
Browse files Browse the repository at this point in the history
Co-authored-by: fish <[email protected]>
  • Loading branch information
2 people authored and daniel-tron committed Dec 13, 2022
1 parent 5065b77 commit 081a3a4
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 22 deletions.
4 changes: 2 additions & 2 deletions chain/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ func SetReportOnlineLastTimeDailyOK() (*ReportOnlineLastTimeDaily, error) {
if err != nil {
return nil, err
}
fmt.Println("... SetReportOnlineLastTimeDailyOK: ok! ")
//fmt.Println("... SetReportOnlineLastTimeDailyOK: ok! ")
return &info, nil
}

Expand Down Expand Up @@ -388,7 +388,7 @@ func SetReportOnlineListDailyOK(r *LastOnlineInfo) ([]*LastOnlineInfo, error) {
if err != nil {
return nil, err
}
fmt.Println("... SetReportOnlineListDailyOK: ok! rList = ", rList)
fmt.Println("SetReportOnlineListDailyOK: ok! nonce =", r.LastSignedInfo.Nonce)
return rList, nil
}

Expand Down
2 changes: 1 addition & 1 deletion core/commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ func TestCommands(t *testing.T) {
"/statuscontract/config",
"/statuscontract/report_online_server",
//"/statuscontract/report_status_contract",
"/statuscontract/daily_report_online",
"/statuscontract/daily_report_online_server",
"/statuscontract/daily_report_list",
"/statuscontract/daily_total",
"/statuscontract/daily_last_report_time",
Expand Down
8 changes: 4 additions & 4 deletions core/commands/statuscontract.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ report status-contract cmd, total cmd and list cmd.`,
"lastinfo": LastInfoCmd,
"report_online_server": ReportOnlineServerCmd,

"daily_report_online": ReportOnlineDailyCmd,
"daily_report_list": ReportListDailyCmd,
"daily_total": TotalDailyCmd,
"daily_last_report_time": ReportLastTimeDailyCmd,
"daily_report_online_server": ReportOnlineDailyCmd,
"daily_report_list": ReportListDailyCmd,
"daily_total": TotalDailyCmd,
"daily_last_report_time": ReportLastTimeDailyCmd,
},
}

Expand Down
27 changes: 19 additions & 8 deletions core/commands/statusonline.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,20 @@ var ReportOnlineDailyCmd = &cmds.Command{
return err
}

spin.DC.SendOnlineDaily(node, cfg)
msg, err := spin.DC.SendOnlineDaily(node, cfg)
if err != nil {
return err
}

return cmds.EmitOnce(res, "daily report online server ok!")
return cmds.EmitOnce(res, "daily report online server ok! "+msg)
},
}

type RetReportOnlineListDaily struct {
Records []*chain.LastOnlineInfo `json:"records"`
Total int `json:"total"`
PeerId string `json:"peer_id"`
Records []*chain.LastOnlineInfo `json:"records"`
Total int `json:"total"`
PeerId string `json:"peer_id"`
BttcAddr string `json:"bttc_addr"`
}

// ReportListDailyCmd (report list daily)
Expand All @@ -58,6 +62,12 @@ var ReportListDailyCmd = &cmds.Command{
}
peerId := n.Identity.Pretty()

cfg, err := cmdenv.GetConfig(env)
if err != nil {
return err
}
bttcAddr := cfg.Identity.BttcAddr

from, err := strconv.Atoi(req.Arguments[0])
if err != nil {
return fmt.Errorf("parse from:%v failed", req.Arguments[0])
Expand Down Expand Up @@ -94,9 +104,10 @@ var ReportListDailyCmd = &cmds.Command{
}

return cmds.EmitOnce(res, &RetReportOnlineListDaily{
Records: list,
Total: total,
PeerId: peerId,
Records: list,
Total: total,
PeerId: peerId,
BttcAddr: bttcAddr,
})
},
Type: RetReportOnlineListDaily{},
Expand Down
25 changes: 18 additions & 7 deletions spin/analytics_online_daily.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ var (
startTime = time.Now()
)

func (dc *dcWrap) doSendOnlineDaily(ctx context.Context, config *config.Config, sm *onlinePb.ReqSignMetrics) error {
func (dc *dcWrap) doSendOnlineDaily(ctx context.Context, config *config.Config, sm *onlinePb.ReqSignMetrics) (msg string, err error) {
onlineService := config.Services.OnlineServerDomain
if len(onlineService) <= 0 {
onlineService = chain.GetOnlineServer(config.ChainInfo.ChainId)
}
cb := cgrpc.OnlineClient(onlineService)
return cb.WithContext(ctx, func(ctx context.Context, client onlinePb.OnlineServiceClient) error {
err = cb.WithContext(ctx, func(ctx context.Context, client onlinePb.OnlineServiceClient) error {
resp, err := client.DoDailyStatusReport(ctx, sm)
//fmt.Printf("--- online DoDailyStatusReport, resp = %+v, err = %+v \n", resp, err)
if err != nil {
fmt.Printf("daily report online, resp = %+v, err = %+v \n", resp, err)
chain.CodeStatus = chain.ConstCodeError
chain.ErrStatus = err
return err
Expand All @@ -38,6 +38,10 @@ func (dc *dcWrap) doSendOnlineDaily(ctx context.Context, config *config.Config,
chain.ErrStatus = nil
}

if resp != nil && len(resp.Message) > 0 {
msg = resp.Message
}

//return errors.New("xxx") //test err
if resp.Code != onlinePb.ResponseCode_SUCCESS {
if resp.Message == "to many request" {
Expand Down Expand Up @@ -65,9 +69,11 @@ func (dc *dcWrap) doSendOnlineDaily(ctx context.Context, config *config.Config,

return nil
})

return msg, err
}

func (dc *dcWrap) SendOnlineDaily(node *core.IpfsNode, config *config.Config) {
func (dc *dcWrap) SendOnlineDaily(node *core.IpfsNode, config *config.Config) (msg string, err error) {
sm, errs, err := dc.doPrepDataOnline(node)
if errs == nil {
errs = make([]error, 0)
Expand All @@ -83,13 +89,16 @@ func (dc *dcWrap) SendOnlineDaily(node *core.IpfsNode, config *config.Config) {
log.Debug(sb.String())
// If complete prep failure we return
if err != nil {
return
return "", err
}

// retry: max 3 times
bo := backoff.NewExponentialBackOff()
bo.MaxElapsedTime = maxRetryTotal
bo.MaxElapsedTime = 3 * 60 * time.Second
bo.InitialInterval = 60 * time.Second
bo.MaxInterval = 60 * time.Second
backoff.Retry(func() error {
err := dc.doSendOnlineDaily(node.Context(), config, sm)
msg, err = dc.doSendOnlineDaily(node.Context(), config, sm)
if err != nil {
log.Infof("failed: doSendDataOnline to online server: %+v ", err)
} else {
Expand All @@ -98,6 +107,8 @@ func (dc *dcWrap) SendOnlineDaily(node *core.IpfsNode, config *config.Config) {

return err
}, bo)

return msg, err
}

func (dc *dcWrap) collectionAgentOnlineDaily(node *core.IpfsNode) {
Expand Down

0 comments on commit 081a3a4

Please sign in to comment.