Skip to content

Commit f86ea7a

Browse files
committed
Up price scope and price calculator
1 parent 15f00ac commit f86ea7a

8 files changed

Lines changed: 50 additions & 12 deletions

File tree

adquery/bidresponse/response_item_blank.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ func (it *ResponseItemBlank) SetBidViewPrice(bid billing.Money) error {
141141
return nil
142142
}
143143

144+
// PrepareBidViewPrice prepares the price for the action
145+
// The price is adjusted according to the source correction factor and the commission share factor
146+
func (it *ResponseItemBlank) PrepareBidViewPrice(price billing.Money) billing.Money {
147+
return it.PriceScope.PrepareBidViewPrice(price)
148+
}
149+
144150
// PurchasePrice gives the price of view from external resource.
145151
// The cost of this request.
146152
func (it *ResponseItemBlank) PurchasePrice(action adtype.Action) billing.Money {

adtype/response_item_empty.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ func (*ResponseItemEmpty) BidViewPrice() billing.Money { return 0 }
111111
// SetBidViewPrice value for external sources auction the system will pay
112112
func (*ResponseItemEmpty) SetBidViewPrice(price billing.Money) error { return nil }
113113

114+
// PrepareBidViewPrice prepares the price for the action
115+
// The price is adjusted according to the source correction factor and the commission share factor
116+
func (*ResponseItemEmpty) PrepareBidViewPrice(price billing.Money) billing.Money { return price }
117+
114118
// PurchasePrice gives the price of view from external resource.
115119
// The cost of this request.
116120
func (*ResponseItemEmpty) PurchasePrice(action Action) billing.Money { return 0 }

adtype/responser_item.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ type ResponserItem interface {
151151
// The system will pay this bid price.
152152
SetBidViewPrice(price billing.Money) error
153153

154+
// PrepareBidViewPrice prepares the price for the action
155+
// The price is adjusted according to the source correction factor and the commission share factor
156+
PrepareBidViewPrice(price billing.Money) billing.Money
157+
154158
// PurchasePrice returns the price of a specific action from an external resource (e.g., site, app, RTB).
155159
// This represents the cost of the request for the network.
156160
// Without the network commission.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ require (
8585
golang.org/x/text v0.25.0 // indirect
8686
google.golang.org/genproto/googleapis/api v0.0.0-20250512202823-5a2f75b736a9 // indirect
8787
google.golang.org/genproto/googleapis/rpc v0.0.0-20250512202823-5a2f75b736a9 // indirect
88-
google.golang.org/grpc v1.72.0 // indirect
88+
google.golang.org/grpc v1.72.1 // indirect
8989
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.5.1 // indirect
9090
google.golang.org/protobuf v1.36.6 // indirect
9191
gopkg.in/yaml.v3 v3.0.1 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,8 @@ google.golang.org/genproto/googleapis/rpc v0.0.0-20250512202823-5a2f75b736a9 h1:
318318
google.golang.org/genproto/googleapis/rpc v0.0.0-20250512202823-5a2f75b736a9/go.mod h1:qQ0YXyHHx3XkvlzUtpXDkS29lDSafHMZBAZDc03LQ3A=
319319
google.golang.org/grpc v1.72.0 h1:S7UkcVa60b5AAQTaO6ZKamFp1zMZSU0fGDK2WZLbBnM=
320320
google.golang.org/grpc v1.72.0/go.mod h1:wH5Aktxcg25y1I3w7H69nHfXdOG3UiadoBtjh3izSDM=
321+
google.golang.org/grpc v1.72.1 h1:HR03wO6eyZ7lknl75XlxABNVLLFc2PAb6mHlYh756mA=
322+
google.golang.org/grpc v1.72.1/go.mod h1:wH5Aktxcg25y1I3w7H69nHfXdOG3UiadoBtjh3izSDM=
321323
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.5.1 h1:F29+wU6Ee6qgu9TddPgooOdaqsxTMunOoj8KA5yuS5A=
322324
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.5.1/go.mod h1:5KF+wpkbTSbGcR9zteSqZV6fqFOWBl4Yde8En8MryZA=
323325
google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=

price/calculator.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ type priceCalculatorItem interface {
2525
// The current bid price will be adjusted according to the source correction factor and the commission share factor
2626
BidViewPrice() billing.Money
2727

28+
// PrepareBidViewPrice prepares the price for the action
29+
// The price is adjusted according to the source correction factor and the commission share factor
30+
PrepareBidViewPrice(price billing.Money) billing.Money
31+
2832
// Price returns price for the target action (view, click, lead, etc)
2933
Price(action adtype.Action) billing.Money
3034
}
@@ -59,13 +63,19 @@ func CalculatePurchasePrice(item priceCalculatorItem, action adtype.Action) bill
5963
return bidPrice
6064
}
6165
}
62-
price := item.Price(action)
63-
return billing.MoneyFloat(
64-
price.Float64() *
65-
max(1.-item.SourceCorrectionFactor(), 0.) *
66-
max(1.-item.TargetCorrectionFactor(), 0.) *
67-
max(1.-item.CommissionShareFactor(), 0.),
66+
var (
67+
price = item.Price(action)
68+
newPrice = billing.MoneyFloat(
69+
price.Float64() *
70+
max(1.-item.SourceCorrectionFactor(), 0.) *
71+
max(1.-item.TargetCorrectionFactor(), 0.) *
72+
max(1.-item.CommissionShareFactor(), 0.),
73+
)
6874
)
75+
if action == adtype.ActionView {
76+
return item.PrepareBidViewPrice(newPrice)
77+
}
78+
return newPrice
6979
}
7080

7181
// CalculatePotentialPrice returns the base price without any corrections or commissions
@@ -83,11 +93,7 @@ func CalculatePotentialPrice(item priceCalculatorItem, action adtype.Action) bil
8393
return val
8494
}
8595

86-
// CalculateFinalPrice returns final price for the item which is including all possible commissions with all corrections
87-
//
88-
// Formula:
89-
//
90-
// FinalPrice = Price - SourceCorrectionFactor[%] - TargetCorrectionFactor[%]
96+
// CalculateFinalPrice returns final price for the advertiser which will be charged
9197
func CalculateFinalPrice(item priceCalculatorItem, action adtype.Action) billing.Money {
9298
sourceCorrection := item.SourceCorrectionFactor()
9399
targetCorrection := item.TargetCorrectionFactor()

price/price_scope.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,11 @@ func (ps *PriceScope) SetViewPrice(price billing.Money, maxIfZero bool) bool {
6868
ps.ViewPrice = max(price, 0)
6969
return true
7070
}
71+
72+
// PrepareBidViewPrice prepares the bid view price for the auction.
73+
func (ps *PriceScope) PrepareBidViewPrice(price billing.Money) billing.Money {
74+
if price > ps.MaxBidViewPrice {
75+
price = ps.MaxBidViewPrice
76+
}
77+
return price
78+
}

price/price_scope_view.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,11 @@ func (ps *PriceScopeView) SetViewPrice(price billing.Money, maxIfZero bool) bool
5353
ps.ViewPrice = max(price, 0)
5454
return true
5555
}
56+
57+
// PrepareBidViewPrice prepares the bid view price for the auction.
58+
func (ps *PriceScopeView) PrepareBidViewPrice(price billing.Money) billing.Money {
59+
if price > ps.MaxBidViewPrice {
60+
price = ps.MaxBidViewPrice
61+
}
62+
return price
63+
}

0 commit comments

Comments
 (0)