diff --git a/models/booking/booking.go b/models/booking/booking.go index 316d931..53f3ea7 100644 --- a/models/booking/booking.go +++ b/models/booking/booking.go @@ -18,7 +18,7 @@ type Booking struct { 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 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 ExpectedEndDate *time.Time `json:"expected_end_date,omitempty" bson:"expected_end_date,omitempty" validate:"required"` // ExpectedEndDate is the expected end date of the booking diff --git a/models/common/enum/schedule.go b/models/common/enum/schedule.go deleted file mode 100644 index f27331a..0000000 --- a/models/common/enum/schedule.go +++ /dev/null @@ -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} -} diff --git a/models/common/enum/status.go b/models/common/enum/status.go new file mode 100644 index 0000000..1166a26 --- /dev/null +++ b/models/common/enum/status.go @@ -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} +} diff --git a/models/common/pricing/pricing_profile.go b/models/common/pricing/pricing_profile.go index ff0fded..fa81629 100644 --- a/models/common/pricing/pricing_profile.go +++ b/models/common/pricing/pricing_profile.go @@ -18,6 +18,14 @@ const ( 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 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 @@ -36,8 +44,12 @@ const ( GARANTED ) +func ExploitPrivilegeStrategyList() []ExploitPrivilegeStrategy { + return []ExploitPrivilegeStrategy{BASIC, GARANTED_ON_DELAY, GARANTED} +} + 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 diff --git a/models/common/pricing/pricing_strategy.go b/models/common/pricing/pricing_strategy.go index 2da445b..fb8bd43 100644 --- a/models/common/pricing/pricing_strategy.go +++ b/models/common/pricing/pricing_strategy.go @@ -15,6 +15,14 @@ const ( 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 { GetStrategy() string GetStrategyValue() int @@ -32,6 +40,14 @@ const ( 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 { 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: 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 { diff --git a/models/order/order.go b/models/order/order.go index 9d04567..ec268ee 100644 --- a/models/order/order.go +++ b/models/order/order.go @@ -8,6 +8,7 @@ import ( "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/common/enum" "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/resources/purchase_resource" @@ -20,24 +21,11 @@ import ( * 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 { utils.AbstractObject OrderBy string `json:"order_by" bson:"order_by" 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"` 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 { return err } - o.Status = PENDING + o.Status = enum.PENDING _, code, err := o.GetAccessor(request).UpdateOne(o, o.GetID()) if code != 200 || err != nil { 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 { peerOrder := &PeerOrder{ - Status: DRAFT, + Status: enum.DRAFTED, PeerID: peerID, } peerOrder.GenerateID() @@ -231,13 +219,13 @@ func (d *Order) pay(request *tools.APIRequest) error { if res.Error != "" { errs += res.Error } - if res.Status != PAID { + if res.Status != enum.PAID { gotAnUnpaid = true } - d.Status = PARTIAL + d.Status = enum.PARTIAL d.SubOrders[res.GetID()] = res 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 { utils.AbstractObject - Error string `json:"error,omitempty" bson:"error,omitempty"` - PeerID string `json:"peer_id,omitempty" bson:"peer_id,omitempty"` - Status OrderStatus `json:"status" bson:"status" default:"0"` - BillingAddress string `json:"billing_address,omitempty" bson:"billing_address,omitempty"` - Items []*PeerItemOrder `json:"items,omitempty" bson:"items,omitempty"` - Total float64 `json:"total,omitempty" bson:"total,omitempty"` + Error string `json:"error,omitempty" bson:"error,omitempty"` + PeerID string `json:"peer_id,omitempty" bson:"peer_id,omitempty"` + Status enum.CompletionStatus `json:"status" bson:"status" default:"0"` + BillingAddress string `json:"billing_address,omitempty" bson:"billing_address,omitempty"` + Items []*PeerItemOrder `json:"items,omitempty" bson:"items,omitempty"` + Total float64 `json:"total,omitempty" bson:"total,omitempty"` } func (d *PeerOrder) Pay(request *tools.APIRequest, response chan *PeerOrder, wg *sync.WaitGroup) { - d.Status = PENDING + d.Status = enum.PENDING go func() { // DO SOMETHING TO PAY ON BLOCKCHAIN OR WHATEVER ON RETURN UPDATE STATUS - d.Status = PAID // TO REMOVE LATER IT'S A MOCK - if d.Status == PAID { + d.Status = enum.PAID // TO REMOVE LATER IT'S A MOCK + if d.Status == enum.PAID { for _, b := range d.Items { if !b.Item.IsPurchased() { 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 } wg.Done() diff --git a/models/resources/compute.go b/models/resources/compute.go index 9d0e20c..e2032ff 100644 --- a/models/resources/compute.go +++ b/models/resources/compute.go @@ -135,10 +135,10 @@ func (r *PricedComputeResource) GetType() tools.DataType { func (r *PricedComputeResource) GetPrice() (float64, error) { 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 { - return 0, errors.New("Selected pricing must be set") + return 0, errors.New("selected pricing must be set") } pricing := *r.SelectedPricing price := float64(0) diff --git a/models/resources/data.go b/models/resources/data.go index f392537..5ce7b87 100644 --- a/models/resources/data.go +++ b/models/resources/data.go @@ -95,7 +95,7 @@ func (t DataResourcePricingStrategy) GetQuantity(amountOfDataGB float64) (float6 case PER_KB_DOWNLOADED: return amountOfDataGB / 1000000, nil } - return 0, errors.New("Pricing strategy not found") + return 0, errors.New("pricing strategy not found") } type DataResourcePricingProfile struct { @@ -125,10 +125,10 @@ func (r *PricedDataResource) GetType() tools.DataType { func (r *PricedDataResource) GetPrice() (float64, error) { 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 { - return 0, errors.New("Selected pricing must be set") + return 0, errors.New("selected pricing must be set") } pricing := *r.SelectedPricing var err error diff --git a/models/resources/priced_resource.go b/models/resources/priced_resource.go index b0c5d0c..59ec77f 100644 --- a/models/resources/priced_resource.go +++ b/models/resources/priced_resource.go @@ -81,10 +81,10 @@ func (abs *PricedResource) GetExplicitDurationInS() float64 { func (r *PricedResource) GetPrice() (float64, error) { 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 { - 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) } diff --git a/models/resources/storage.go b/models/resources/storage.go index 3ce2e7a..9724645 100644 --- a/models/resources/storage.go +++ b/models/resources/storage.go @@ -105,7 +105,7 @@ func (t StorageResourcePricingStrategy) GetQuantity(amountOfDataGB float64) (flo case PER_KB_STORED: return amountOfDataGB * 1000000, nil } - return 0, errors.New("Pricing strategy not found") + return 0, errors.New("pricing strategy not found") } type StorageResourcePricingProfile struct { @@ -131,10 +131,10 @@ func (r *PricedStorageResource) GetType() tools.DataType { func (r *PricedStorageResource) GetPrice() (float64, error) { 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 { - return 0, errors.New("Selected pricing must be set") + return 0, errors.New("selected pricing must be set") } pricing := *r.SelectedPricing var err error diff --git a/models/workflow_execution/workflow_execution.go b/models/workflow_execution/workflow_execution.go index c8490f7..74efdee 100644 --- a/models/workflow_execution/workflow_execution.go +++ b/models/workflow_execution/workflow_execution.go @@ -23,7 +23,7 @@ type WorkflowExecutions struct { 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 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 }