light modification

This commit is contained in:
mr 2025-01-17 10:34:44 +01:00
parent b990fe42d3
commit 367613a9d5
11 changed files with 123 additions and 86 deletions

View File

@ -18,7 +18,7 @@ type Booking struct {
DestPeerID string `json:"dest_peer_id,omitempty"` // DestPeerID is the ID of the destination peer DestPeerID string `json:"dest_peer_id,omitempty"` // DestPeerID is the ID of the destination peer
WorkflowID string `json:"workflow_id,omitempty" bson:"workflow_id,omitempty"` // WorkflowID is the ID of the workflow WorkflowID string `json:"workflow_id,omitempty" bson:"workflow_id,omitempty"` // WorkflowID is the ID of the workflow
ExecutionID string `json:"execution_id,omitempty" bson:"execution_id,omitempty" validate:"required"` ExecutionID string `json:"execution_id,omitempty" bson:"execution_id,omitempty" validate:"required"`
State enum.ScheduledType `json:"state,omitempty" bson:"state,omitempty" validate:"required"` // State is the state of the booking State enum.BookingStatus `json:"state,omitempty" bson:"state,omitempty" validate:"required"` // State is the state of the booking
ExpectedStartDate time.Time `json:"expected_start_date,omitempty" bson:"expected_start_date,omitempty" validate:"required"` // ExpectedStartDate is the expected start date of the booking ExpectedStartDate time.Time `json:"expected_start_date,omitempty" bson:"expected_start_date,omitempty" validate:"required"` // ExpectedStartDate is the expected start date of the booking
ExpectedEndDate *time.Time `json:"expected_end_date,omitempty" bson:"expected_end_date,omitempty" validate:"required"` // ExpectedEndDate is the expected end date of the booking ExpectedEndDate *time.Time `json:"expected_end_date,omitempty" bson:"expected_end_date,omitempty" validate:"required"` // ExpectedEndDate is the expected end date of the booking

View File

@ -1,43 +0,0 @@
package enum
type ScheduledType int
const (
DRAFT ScheduledType = iota
SCHEDULED
STARTED
FAILURE
SUCCESS
FORGOTTEN
DELAYED
CANCELLED
)
var str = [...]string{
"draft",
"scheduled",
"started",
"failure",
"success",
"forgotten",
"delayed",
"cancelled",
}
func FromInt(i int) string {
return str[i]
}
func (d ScheduledType) String() string {
return str[d]
}
// EnumIndex - Creating common behavior-give the type a EnumIndex functio
func (d ScheduledType) EnumIndex() int {
return int(d)
}
// List
func ScheduleList() []ScheduledType {
return []ScheduledType{DRAFT, SCHEDULED, STARTED, FAILURE, SUCCESS, FORGOTTEN, DELAYED, CANCELLED}
}

View File

@ -0,0 +1,64 @@
package enum
type CompletionStatus int
const (
DRAFTED CompletionStatus = iota
PENDING
CANCEL
PARTIAL
PAID
DISPUTED
OVERDUE
REFUND
)
func (d CompletionStatus) String() string {
return [...]string{"drafted", "pending", "cancel", "partial", "paid", "disputed", "overdue", "refund"}[d]
}
func CompletionStatusList() []CompletionStatus {
return []CompletionStatus{DRAFTED, PENDING, CANCEL, PARTIAL, PAID, DISPUTED, OVERDUE, REFUND}
}
type BookingStatus int
const (
DRAFT BookingStatus = iota
SCHEDULED
STARTED
FAILURE
SUCCESS
FORGOTTEN
DELAYED
CANCELLED
)
var str = [...]string{
"draft",
"scheduled",
"started",
"failure",
"success",
"forgotten",
"delayed",
"cancelled",
}
func FromInt(i int) string {
return str[i]
}
func (d BookingStatus) String() string {
return str[d]
}
// EnumIndex - Creating common behavior-give the type a EnumIndex functio
func (d BookingStatus) EnumIndex() int {
return int(d)
}
// List
func StatusList() []BookingStatus {
return []BookingStatus{DRAFT, SCHEDULED, STARTED, FAILURE, SUCCESS, FORGOTTEN, DELAYED, CANCELLED}
}

View File

@ -18,6 +18,14 @@ const (
REFUND_ON_EARLY_END REFUND_ON_EARLY_END
) )
func (t RefundType) String() string {
return [...]string{"REFUND ON DEAD END", "REFUND ON ERROR", "REFUND ON EARLY END"}[t]
}
func RefundTypeList() []RefundType {
return []RefundType{REFUND_DEAD_END, REFUND_ON_ERROR, REFUND_ON_EARLY_END}
}
type AccessPricingProfile[T Strategy] struct { // only use for acces such as : DATA && PROCESSING type AccessPricingProfile[T Strategy] struct { // only use for acces such as : DATA && PROCESSING
Pricing PricingStrategy[T] `json:"pricing,omitempty" bson:"pricing,omitempty"` // Price is the price of the resource Pricing PricingStrategy[T] `json:"pricing,omitempty" bson:"pricing,omitempty"` // Price is the price of the resource
DefaultRefund RefundType `json:"default_refund" bson:"default_refund"` // DefaultRefund is the default refund type of the pricing DefaultRefund RefundType `json:"default_refund" bson:"default_refund"` // DefaultRefund is the default refund type of the pricing
@ -36,8 +44,12 @@ const (
GARANTED GARANTED
) )
func ExploitPrivilegeStrategyList() []ExploitPrivilegeStrategy {
return []ExploitPrivilegeStrategy{BASIC, GARANTED_ON_DELAY, GARANTED}
}
func (t ExploitPrivilegeStrategy) String() string { func (t ExploitPrivilegeStrategy) String() string {
return [...]string{"BASIC", "GARANTED_ON_DELAY", "GARANTED"}[t] return [...]string{"NO GARANTY", "GARANTED ON SPECIFIC DELAY", "GARANTED"}[t]
} }
type ExploitPricingProfile[T Strategy] struct { // only use for exploit such as : STORAGE, COMPUTE, WORKFLOW type ExploitPricingProfile[T Strategy] struct { // only use for exploit such as : STORAGE, COMPUTE, WORKFLOW

View File

@ -15,6 +15,14 @@ const (
PAY_PER_USE PAY_PER_USE
) )
func (t BuyingStrategy) String() string {
return [...]string{"UNLIMITED", "SUBSCRIPTION", "PAY PER USE"}[t]
}
func BuyingStrategyList() []BuyingStrategy {
return []BuyingStrategy{UNLIMITED, SUBSCRIPTION, PAY_PER_USE}
}
type Strategy interface { type Strategy interface {
GetStrategy() string GetStrategy() string
GetStrategyValue() int GetStrategyValue() int
@ -32,6 +40,14 @@ const (
PER_MONTH PER_MONTH
) )
func (t TimePricingStrategy) String() string {
return [...]string{"ONCE", "PER SECOND", "PER MINUTE", "PER HOUR", "PER DAY", "PER WEEK", "PER MONTH"}[t]
}
func TimePricingStrategyList() []TimePricingStrategy {
return []TimePricingStrategy{ONCE, PER_SECOND, PER_MINUTE, PER_HOUR, PER_DAY, PER_WEEK, PER_MONTH}
}
func (t TimePricingStrategy) GetStrategy() string { func (t TimePricingStrategy) GetStrategy() string {
return [...]string{"ONCE", "PER_SECOND", "PER_MINUTE", "PER_HOUR", "PER_DAY", "PER_WEEK", "PER_MONTH"}[t] return [...]string{"ONCE", "PER_SECOND", "PER_MINUTE", "PER_HOUR", "PER_DAY", "PER_WEEK", "PER_MONTH"}[t]
} }
@ -82,7 +98,7 @@ func BookingEstimation(t TimePricingStrategy, price float64, locationDurationInS
case PER_MONTH: case PER_MONTH:
return p * float64(locationDurationInSecond/2592000), nil return p * float64(locationDurationInSecond/2592000), nil
} }
return 0, errors.New("Pricing strategy not found") return 0, errors.New("pricing strategy not found")
} }
type PricingStrategy[T Strategy] struct { type PricingStrategy[T Strategy] struct {

View File

@ -8,6 +8,7 @@ import (
"cloud.o-forge.io/core/oc-lib/dbs" "cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/models/booking" "cloud.o-forge.io/core/oc-lib/models/booking"
"cloud.o-forge.io/core/oc-lib/models/common/enum"
"cloud.o-forge.io/core/oc-lib/models/common/pricing" "cloud.o-forge.io/core/oc-lib/models/common/pricing"
"cloud.o-forge.io/core/oc-lib/models/peer" "cloud.o-forge.io/core/oc-lib/models/peer"
"cloud.o-forge.io/core/oc-lib/models/resources/purchase_resource" "cloud.o-forge.io/core/oc-lib/models/resources/purchase_resource"
@ -20,24 +21,11 @@ import (
* Booking is a struct that represents a booking * Booking is a struct that represents a booking
*/ */
type OrderStatus = int
const (
DRAFT OrderStatus = iota
PENDING
CANCELLED
PARTIAL
PAID
DISPUTED
OVERDUE
REFUND
)
type Order struct { type Order struct {
utils.AbstractObject utils.AbstractObject
OrderBy string `json:"order_by" bson:"order_by" validate:"required"` OrderBy string `json:"order_by" bson:"order_by" validate:"required"`
WorkflowExecutionIDs []string `json:"workflow_execution_ids" bson:"workflow_execution_ids" validate:"required"` WorkflowExecutionIDs []string `json:"workflow_execution_ids" bson:"workflow_execution_ids" validate:"required"`
Status OrderStatus `json:"status" bson:"status" default:"0"` Status enum.CompletionStatus `json:"status" bson:"status" default:"0"`
SubOrders map[string]*PeerOrder `json:"sub_orders" bson:"sub_orders"` SubOrders map[string]*PeerOrder `json:"sub_orders" bson:"sub_orders"`
Total float64 `json:"total" bson:"total" validate:"required"` Total float64 `json:"total" bson:"total" validate:"required"`
} }
@ -69,7 +57,7 @@ func (o *Order) Pay(scheduler *workflow_execution.WorkflowSchedule, request *too
if _, err := o.draftBookOrder(scheduler, request); err != nil { if _, err := o.draftBookOrder(scheduler, request); err != nil {
return err return err
} }
o.Status = PENDING o.Status = enum.PENDING
_, code, err := o.GetAccessor(request).UpdateOne(o, o.GetID()) _, code, err := o.GetAccessor(request).UpdateOne(o, o.GetID())
if code != 200 || err != nil { if code != 200 || err != nil {
return errors.New("could not update the order" + fmt.Sprintf("%v", err)) return errors.New("could not update the order" + fmt.Sprintf("%v", err))
@ -129,7 +117,7 @@ func (o *Order) draftStoreFromModel(scheduler *workflow_execution.WorkflowSchedu
} }
for peerID, resources := range resourcesByPeer { for peerID, resources := range resourcesByPeer {
peerOrder := &PeerOrder{ peerOrder := &PeerOrder{
Status: DRAFT, Status: enum.DRAFTED,
PeerID: peerID, PeerID: peerID,
} }
peerOrder.GenerateID() peerOrder.GenerateID()
@ -231,13 +219,13 @@ func (d *Order) pay(request *tools.APIRequest) error {
if res.Error != "" { if res.Error != "" {
errs += res.Error errs += res.Error
} }
if res.Status != PAID { if res.Status != enum.PAID {
gotAnUnpaid = true gotAnUnpaid = true
} }
d.Status = PARTIAL d.Status = enum.PARTIAL
d.SubOrders[res.GetID()] = res d.SubOrders[res.GetID()] = res
if count == len(d.SubOrders) && !gotAnUnpaid { if count == len(d.SubOrders) && !gotAnUnpaid {
d.Status = PAID d.Status = enum.PAID
} }
} }
} }
@ -250,20 +238,20 @@ func (d *Order) pay(request *tools.APIRequest) error {
type PeerOrder struct { type PeerOrder struct {
utils.AbstractObject utils.AbstractObject
Error string `json:"error,omitempty" bson:"error,omitempty"` Error string `json:"error,omitempty" bson:"error,omitempty"`
PeerID string `json:"peer_id,omitempty" bson:"peer_id,omitempty"` PeerID string `json:"peer_id,omitempty" bson:"peer_id,omitempty"`
Status OrderStatus `json:"status" bson:"status" default:"0"` Status enum.CompletionStatus `json:"status" bson:"status" default:"0"`
BillingAddress string `json:"billing_address,omitempty" bson:"billing_address,omitempty"` BillingAddress string `json:"billing_address,omitempty" bson:"billing_address,omitempty"`
Items []*PeerItemOrder `json:"items,omitempty" bson:"items,omitempty"` Items []*PeerItemOrder `json:"items,omitempty" bson:"items,omitempty"`
Total float64 `json:"total,omitempty" bson:"total,omitempty"` Total float64 `json:"total,omitempty" bson:"total,omitempty"`
} }
func (d *PeerOrder) Pay(request *tools.APIRequest, response chan *PeerOrder, wg *sync.WaitGroup) { func (d *PeerOrder) Pay(request *tools.APIRequest, response chan *PeerOrder, wg *sync.WaitGroup) {
d.Status = PENDING d.Status = enum.PENDING
go func() { go func() {
// DO SOMETHING TO PAY ON BLOCKCHAIN OR WHATEVER ON RETURN UPDATE STATUS // DO SOMETHING TO PAY ON BLOCKCHAIN OR WHATEVER ON RETURN UPDATE STATUS
d.Status = PAID // TO REMOVE LATER IT'S A MOCK d.Status = enum.PAID // TO REMOVE LATER IT'S A MOCK
if d.Status == PAID { if d.Status == enum.PAID {
for _, b := range d.Items { for _, b := range d.Items {
if !b.Item.IsPurchased() { if !b.Item.IsPurchased() {
continue continue
@ -277,7 +265,7 @@ func (d *PeerOrder) Pay(request *tools.APIRequest, response chan *PeerOrder, wg
} }
} }
if d.Status != PENDING { if d.Status != enum.PENDING {
response <- d response <- d
} }
wg.Done() wg.Done()

View File

@ -135,10 +135,10 @@ func (r *PricedComputeResource) GetType() tools.DataType {
func (r *PricedComputeResource) GetPrice() (float64, error) { func (r *PricedComputeResource) GetPrice() (float64, error) {
if r.UsageStart == nil || r.UsageEnd == nil { if r.UsageStart == nil || r.UsageEnd == nil {
return 0, errors.New("Usage start and end must be set") return 0, errors.New("usage start and end must be set")
} }
if r.SelectedPricing == nil { if r.SelectedPricing == nil {
return 0, errors.New("Selected pricing must be set") return 0, errors.New("selected pricing must be set")
} }
pricing := *r.SelectedPricing pricing := *r.SelectedPricing
price := float64(0) price := float64(0)

View File

@ -95,7 +95,7 @@ func (t DataResourcePricingStrategy) GetQuantity(amountOfDataGB float64) (float6
case PER_KB_DOWNLOADED: case PER_KB_DOWNLOADED:
return amountOfDataGB / 1000000, nil return amountOfDataGB / 1000000, nil
} }
return 0, errors.New("Pricing strategy not found") return 0, errors.New("pricing strategy not found")
} }
type DataResourcePricingProfile struct { type DataResourcePricingProfile struct {
@ -125,10 +125,10 @@ func (r *PricedDataResource) GetType() tools.DataType {
func (r *PricedDataResource) GetPrice() (float64, error) { func (r *PricedDataResource) GetPrice() (float64, error) {
if r.UsageStart == nil || r.UsageEnd == nil { if r.UsageStart == nil || r.UsageEnd == nil {
return 0, errors.New("Usage start and end must be set") return 0, errors.New("usage start and end must be set")
} }
if r.SelectedPricing == nil { if r.SelectedPricing == nil {
return 0, errors.New("Selected pricing must be set") return 0, errors.New("selected pricing must be set")
} }
pricing := *r.SelectedPricing pricing := *r.SelectedPricing
var err error var err error

View File

@ -81,10 +81,10 @@ func (abs *PricedResource) GetExplicitDurationInS() float64 {
func (r *PricedResource) GetPrice() (float64, error) { func (r *PricedResource) GetPrice() (float64, error) {
if r.UsageStart == nil || r.UsageEnd == nil { if r.UsageStart == nil || r.UsageEnd == nil {
return 0, errors.New("Usage start and end must be set") return 0, errors.New("usage start and end must be set")
} }
if r.SelectedPricing == nil { if r.SelectedPricing == nil {
return 0, errors.New("Selected pricing must be set") return 0, errors.New("selected pricing must be set")
} }
return (*r.SelectedPricing).GetPrice(1, 0, *r.UsageStart, *r.UsageEnd) return (*r.SelectedPricing).GetPrice(1, 0, *r.UsageStart, *r.UsageEnd)
} }

View File

@ -105,7 +105,7 @@ func (t StorageResourcePricingStrategy) GetQuantity(amountOfDataGB float64) (flo
case PER_KB_STORED: case PER_KB_STORED:
return amountOfDataGB * 1000000, nil return amountOfDataGB * 1000000, nil
} }
return 0, errors.New("Pricing strategy not found") return 0, errors.New("pricing strategy not found")
} }
type StorageResourcePricingProfile struct { type StorageResourcePricingProfile struct {
@ -131,10 +131,10 @@ func (r *PricedStorageResource) GetType() tools.DataType {
func (r *PricedStorageResource) GetPrice() (float64, error) { func (r *PricedStorageResource) GetPrice() (float64, error) {
if r.UsageStart == nil || r.UsageEnd == nil { if r.UsageStart == nil || r.UsageEnd == nil {
return 0, errors.New("Usage start and end must be set") return 0, errors.New("usage start and end must be set")
} }
if r.SelectedPricing == nil { if r.SelectedPricing == nil {
return 0, errors.New("Selected pricing must be set") return 0, errors.New("selected pricing must be set")
} }
pricing := *r.SelectedPricing pricing := *r.SelectedPricing
var err error var err error

View File

@ -23,7 +23,7 @@ type WorkflowExecutions struct {
utils.AbstractObject // AbstractObject contains the basic fields of an object (id, name) utils.AbstractObject // AbstractObject contains the basic fields of an object (id, name)
ExecDate time.Time `json:"execution_date,omitempty" bson:"execution_date,omitempty" validate:"required"` // ExecDate is the execution date of the workflow, is required ExecDate time.Time `json:"execution_date,omitempty" bson:"execution_date,omitempty" validate:"required"` // ExecDate is the execution date of the workflow, is required
EndDate *time.Time `json:"end_date,omitempty" bson:"end_date,omitempty"` // EndDate is the end date of the workflow EndDate *time.Time `json:"end_date,omitempty" bson:"end_date,omitempty"` // EndDate is the end date of the workflow
State enum.ScheduledType `json:"state" bson:"state" default:"0"` // State is the state of the workflow State enum.BookingStatus `json:"state" bson:"state" default:"0"` // State is the state of the workflow
WorkflowID string `json:"workflow_id" bson:"workflow_id,omitempty"` // WorkflowID is the ID of the workflow WorkflowID string `json:"workflow_id" bson:"workflow_id,omitempty"` // WorkflowID is the ID of the workflow
} }