Skip to content
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

pi: Make billing status changes batched. #1535

Merged
merged 5 commits into from
Oct 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 26 additions & 24 deletions politeiad/client/pi.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package client
import (
"context"
"encoding/json"
"fmt"

pdv2 "github.com/decred/politeia/politeiad/api/v2"
"github.com/decred/politeia/politeiad/plugins/pi"
Expand Down Expand Up @@ -49,9 +48,9 @@ func (c *Client) PiSetBillingStatus(ctx context.Context, sbs pi.SetBillingStatus
func (c *Client) PiSummaries(ctx context.Context, tokens []string) (map[string]pi.SummaryReply, error) {
// Setup request
cmds := make([]pdv2.PluginCmd, 0, len(tokens))
for _, v := range tokens {
for _, t := range tokens {
cmds = append(cmds, pdv2.PluginCmd{
Token: v,
Token: t,
ID: pi.PluginID,
Command: pi.CmdSummary,
Payload: "",
Expand Down Expand Up @@ -84,40 +83,43 @@ func (c *Client) PiSummaries(ctx context.Context, tokens []string) (map[string]p
return ssr, nil
}

// PiBillingStatusChanges sends the pi plugin BillingStatusChanges command
// to the politeiad v2 API.
func (c *Client) PiBillingStatusChanges(ctx context.Context, token string) (*pi.BillingStatusChangesReply, error) {
// PiBillingStatusChanges sends a page of pi plugin BillingStatusChanges
// commands to the politeiad v2 API.
func (c *Client) PiBillingStatusChanges(ctx context.Context, tokens []string) (map[string]pi.BillingStatusChangesReply, error) {
// Setup request
cmds := []pdv2.PluginCmd{
{
Token: token,
cmds := make([]pdv2.PluginCmd, 0, len(tokens))
for _, t := range tokens {
cmds = append(cmds, pdv2.PluginCmd{
Token: t,
ID: pi.PluginID,
Command: pi.CmdBillingStatusChanges,
Payload: "",
},
})
}

// Send request
replies, err := c.PluginReads(ctx, cmds)
if err != nil {
return nil, err
}
if len(replies) == 0 {
return nil, fmt.Errorf("no replies found")
}
pcr := replies[0]
err = extractPluginCmdError(pcr)
if err != nil {
return nil, err
}

// Decode reply
var bscsr pi.BillingStatusChangesReply
err = json.Unmarshal([]byte(pcr.Payload), &bscsr)
if err != nil {
return nil, err
// Prepare reply
bscsr := make(map[string]pi.BillingStatusChangesReply, len(replies))
for _, v := range replies {
err = extractPluginCmdError(v)
if err != nil {
// Individual summary errors are ignored. The token will not
// be included in the returned summaries map.
continue
}
var bscr pi.BillingStatusChangesReply
err = json.Unmarshal([]byte(v.Payload), &bscr)
if err != nil {
return nil, err
}
bscsr[v.Token] = bscr
}

return &bscsr, nil
return bscsr, nil

}
61 changes: 37 additions & 24 deletions politeiawww/api/pi/v1/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,20 @@ type Policy struct{}
// We have not updated the field names here to avoid introducing breaking
// changes.
type PolicyReply struct {
TextFileSizeMax uint32 `json:"textfilesizemax"` // In bytes
ImageFileCountMax uint32 `json:"imagefilecountmax"`
ImageFileSizeMax uint32 `json:"imagefilesizemax"` // In bytes
NameLengthMin uint32 `json:"namelengthmin"` // In characters
NameLengthMax uint32 `json:"namelengthmax"` // In characters
NameSupportedChars []string `json:"namesupportedchars"`
AmountMin uint64 `json:"amountmin"` // In cents
AmountMax uint64 `json:"amountmax"` // In cents
StartDateMin int64 `json:"startdatemin"` // Seconds from current time
EndDateMax int64 `json:"enddatemax"` // Seconds from current time
Domains []string `json:"domains"`
SummariesPageSize uint32 `json:"summariespagesize"`
BillingStatusChangesMax uint32 `json:"billingstatuschangesmax"`
TextFileSizeMax uint32 `json:"textfilesizemax"` // In bytes
ImageFileCountMax uint32 `json:"imagefilecountmax"`
ImageFileSizeMax uint32 `json:"imagefilesizemax"` // In bytes
NameLengthMin uint32 `json:"namelengthmin"` // In characters
NameLengthMax uint32 `json:"namelengthmax"` // In characters
NameSupportedChars []string `json:"namesupportedchars"`
AmountMin uint64 `json:"amountmin"` // In cents
AmountMax uint64 `json:"amountmax"` // In cents
StartDateMin int64 `json:"startdatemin"` // Seconds from current time
EndDateMax int64 `json:"enddatemax"` // Seconds from current time
Domains []string `json:"domains"`
SummariesPageSize uint32 `json:"summariespagesize"`
BillingStatusChangesPageSize uint32 `json:"billingstatuschangespagesize"`
BillingStatusChangesMax uint32 `json:"billingstatuschangesmax"`
}

const (
Expand Down Expand Up @@ -267,6 +268,29 @@ type SetBillingStatusReply struct {
Timestamp int64 `json:"timestamp"` // Unix timestamp
}

const (
// BillingStatusChangesPageSize is the maximum number of billing status
// changes that can be requested at any one time.
BillingStatusChangesPageSize uint32 = 5
)

// BillingStatusChanges requests the billing status changes for the provided
// proposal tokens.
type BillingStatusChanges struct {
Tokens []string `json:"tokens"`
}

// BillingStatusChangesReply is the reply to the BillingStatusChanges command.
//
// BillingStatusChanges contains the billing status changes for each of the
// provided tokens. The map will not contain an entry for any tokens that
// did not correspond to an actual proposal. It is the callers responsibility
// to ensure that the billing status changes are returned for all provided
// tokens.
type BillingStatusChangesReply struct {
BillingStatusChanges map[string][]BillingStatusChange `json:"billingstatuschanges"`
}

const (
// ProposalUpdateHint is the hint that is included in a comment's
// ExtraDataHint field to indicate that the comment is an update
Expand Down Expand Up @@ -309,14 +333,3 @@ type SummariesReply struct {
type Summary struct {
Status string `json:"status"`
}

// BillingStatusChanges requests the billing status changes for the provided
// proposal token.
type BillingStatusChanges struct {
Token string `json:"token"`
}

// BillingStatusChangesReply is the reply to the BillingStatusChanges command.
type BillingStatusChangesReply struct {
BillingStatusChanges []BillingStatusChange `json:"billingstatuschanges"`
}
28 changes: 19 additions & 9 deletions politeiawww/cmd/pictl/cmdproposalbillingstatuschanges.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// proposal.
type cmdProposalBillingStatusChanges struct {
amass01 marked this conversation as resolved.
Show resolved Hide resolved
Args struct {
Token string `positional-arg-name:"token" required:"true"`
Tokens []string `positional-arg-name:"tokens" required:"true"`
} `positional-args:"true"`
}

Expand All @@ -36,7 +36,7 @@ func (c *cmdProposalBillingStatusChanges) Execute(args []string) error {

// Setup request
bscs := piv1.BillingStatusChanges{
Token: c.Args.Token,
Tokens: c.Args.Tokens,
}

// Send request
Expand All @@ -45,20 +45,30 @@ func (c *cmdProposalBillingStatusChanges) Execute(args []string) error {
return err
}

// Print billing status changes
for _, bsc := range bscsr.BillingStatusChanges {
printBillingStatusChange(bsc)
printf("-----\n")
// Print billing status changes for all tokens
for t, bscs := range bscsr.BillingStatusChanges {
printf("Proposal %v\n", t)
if len(bscs) > 0 {
for i, bsc := range bscs {
printBillingStatusChange(bsc)
if i != len(bscs)-1 {
printf(" -----\n")
}
}
} else {
printf(" No billing status changes\n")
}
printf("\n")
}

return nil
}

// proposalBillingStatusChangesHelpMsg is printed to stdout by the help command.
const proposalBillingStatusChangesHelpMsg = `proposalbillingstatuschanges "token"
const proposalBillingStatusChangesHelpMsg = `proposalbillingstatuschanges "tokens..."

Return the billing status changes of a proposal.
Return the billing status changes for a page of propsoals.

Arguments:
1. token (string, required) Proposal censorship token
1. tokens (string, required) Proposal censorship tokens
`
123 changes: 62 additions & 61 deletions politeiawww/cmd/pictl/pictl.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,83 +129,84 @@ const helpMsg = `Application Options:
--silent Suppress all output

Help commands
help Print detailed help message for a command
help Print detailed help message for a command

Basic commands
version (public) Get politeiawww server version and CSRF
policy (public) Get politeiawww server policy
secret (public) Ping the server
login (public) Login to politeiawww
logout (user) Logout from politeiawww
me (user) Get details of the logged in user
version (public) Get politeiawww server version and CSRF
policy (public) Get politeiawww server policy
secret (public) Ping the server
login (public) Login to politeiawww
logout (user) Logout from politeiawww
me (user) Get details of the logged in user

User commands
usernew (public) Create a new user
useredit (user) Edit the logged in user
usermanage (admin) Edit a user as an admin
useremailverify (public) Verify email address
userverificationresend (public) Resend verification email
userpasswordreset (public) Reset password
userpasswordchange (user) Change password
userusernamechange (user) Change username
userkeyupdate (user) Update user key (i.e. identity)
userregistrationpayment (user) Verify registration payment
userpaymentsrescan (user) Rescan all user payments
userproposalpaywall (user) Get user paywall details
userproposalpaywalltx (user) Get pending user payments
userproposalcredits (user) Get user proposal credits
userdetails (public) Get user details
users (public) Get users
usernew (public) Create a new user
useredit (user) Edit the logged in user
usermanage (admin) Edit a user as an admin
useremailverify (public) Verify email address
userverificationresend (public) Resend verification email
userpasswordreset (public) Reset password
userpasswordchange (user) Change password
userusernamechange (user) Change username
userkeyupdate (user) Update user key (i.e. identity)
userregistrationpayment (user) Verify registration payment
userpaymentsrescan (user) Rescan all user payments
userproposalpaywall (user) Get user paywall details
userproposalpaywalltx (user) Get pending user payments
userproposalcredits (user) Get user proposal credits
userdetails (public) Get user details
users (public) Get users

Proposal commands
proposalpolicy (public) Get the pi api policy
proposalnew (user) Submit a new proposal
proposaledit (user) Edit an existing proposal
proposalsetstatus (admin) Set the status of a proposal
proposalsetbillingstatus (admin) Set the billing status of a proposal
proposaldetails (public) Get a full proposal record
proposaltimestamps (public) Get timestamps for a proposal
proposals (public) Get proposals without their files
proposalsummaries (public) Get proposal summaries
proposalinv (public) Get inventory by proposal status
proposalinvordered (public) Get inventory ordered chronologically
userproposals (public) Get proposals submitted by a user
proposalpolicy (public) Get the pi api policy
proposalnew (user) Submit a new proposal
proposaledit (user) Edit an existing proposal
proposalsetstatus (admin) Set the status of a proposal
proposalsetbillingstatus (admin) Set the billing status of a proposal
proposalbillingstatuschanges (public) Get billing status changes
proposaldetails (public) Get a full proposal record
proposaltimestamps (public) Get timestamps for a proposal
proposals (public) Get proposals without their files
proposalsummaries (public) Get proposal summaries
proposalinv (public) Get inventory by proposal status
proposalinvordered (public) Get inventory ordered chronologically
userproposals (public) Get proposals submitted by a user

Record commands
recordpolicy (public) Get the records api policy
recordpolicy (public) Get the records api policy

Comment commands
commentpolicy (public) Get the comments api policy
commentnew (user) Submit a new comment
commentvote (user) Upvote/downvote a comment
commentcensor (admin) Censor a comment
commentcount (public) Get the number of comments
comments (public) Get comments
commentvotes (public) Get comment votes
commenttimestamps (public) Get comment timestamps
commentpolicy (public) Get the comments api policy
commentnew (user) Submit a new comment
commentvote (user) Upvote/downvote a comment
commentcensor (admin) Censor a comment
commentcount (public) Get the number of comments
comments (public) Get comments
commentvotes (public) Get comment votes
commenttimestamps (public) Get comment timestamps

Vote commands
votepolicy (public) Get the ticketvote api policy
voteauthorize (user) Authorize a proposal vote
votestart (admin) Start a proposal vote
castballot (public) Cast a ballot of votes
votedetails (public) Get details for a vote
voteresults (public) Get full vote results
votesummaries (public) Get vote summaries
votesubmissions (public) Get runoff vote submissions
voteinv (public) Get proposal inventory by vote status
votetimestamps (public) Get vote timestamps
votepolicy (public) Get the ticketvote api policy
voteauthorize (user) Authorize a proposal vote
votestart (admin) Start a proposal vote
castballot (public) Cast a ballot of votes
votedetails (public) Get details for a vote
voteresults (public) Get full vote results
votesummaries (public) Get vote summaries
votesubmissions (public) Get runoff vote submissions
voteinv (public) Get proposal inventory by vote status
votetimestamps (public) Get vote timestamps

Websocket commands
subscribe (public) Subscribe/unsubscribe to websocket event
subscribe (public) Subscribe/unsubscribe to websocket event

Dev commands
sendfaucettx Send a dcr faucet tx
testrun Execute a test run of the pi routes
seedproposals Seed the backend with proposals
votetestsetup Setup a vote test
votetest Execute a vote test
legacytest Test legacy routes that do not have a command
sendfaucettx Send a dcr faucet tx
testrun Execute a test run of the pi routes
seedproposals Seed the backend with proposals
votetestsetup Setup a vote test
votetest Execute a vote test
legacytest Test legacy routes that do not have a command
`

func _main() error {
Expand Down
14 changes: 7 additions & 7 deletions politeiawww/cmd/pictl/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,15 @@ func printProposalSummary(token string, s piv1.Summary) {

// printBillingStatusChanges prints a proposal billing status change.
func printBillingStatusChange(bsc piv1.BillingStatusChange) {
printf("Token : %v\n", bsc.Token)
printf("Status : %v\n", piv1.BillingStatuses[bsc.Status])
printf(" Token : %v\n", bsc.Token)
printf(" Status : %v\n", piv1.BillingStatuses[bsc.Status])
if bsc.Reason != "" {
printf("Reason : %v\n", bsc.Reason)
printf(" Reason : %v\n", bsc.Reason)
}
printf("PublicKey: %v\n", bsc.PublicKey)
printf("Signature: %v\n", bsc.Signature)
printf("Receipt : %v\n", bsc.Receipt)
printf("Timestamp: %v\n", timestampFromUnix(bsc.Timestamp))
printf(" PublicKey: %v\n", bsc.PublicKey)
printf(" Signature: %v\n", bsc.Signature)
printf(" Receipt : %v\n", bsc.Receipt)
printf(" Timestamp: %v\n", timestampFromUnix(bsc.Timestamp))
}

// indexFileRandom returns a proposal index file filled with random data.
Expand Down
Loading