oc-lib/models/resources/workflow.go

104 lines
3.1 KiB
Go

package resources
import (
"time"
"cloud.o-forge.io/core/oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/tools"
)
// COMPLEX SHOULD BE REFACTORED
// we don't have any information about the accessor
type abstractWorkflowResource struct {
ExploitedResourceSet
WorkflowID string `bson:"workflow_id,omitempty" json:"workflow_id,omitempty"` // WorkflowID is the ID of the native workflow
}
// WorkflowResource is a struct that represents a workflow resource
// it defines the resource workflow
type WorkflowResource struct {
AbstractResource[*ResourceInstance[*ResourcePartnerShip[*WorkflowResourcePricingProfile]]]
abstractWorkflowResource
}
type CustomizedWorkflowResource struct {
AbstractCustomizedResource[*ResourceInstance[*ResourcePartnerShip[*WorkflowResourcePricingProfile]]]
abstractWorkflowResource
}
func (r *CustomizedWorkflowResource) GetType() tools.DataType {
return tools.WORKFLOW_RESOURCE
}
func (d *WorkflowResource) GetAccessor(request *tools.APIRequest) utils.Accessor {
return NewAccessor[*WorkflowResource](tools.WORKFLOW_RESOURCE, request, func() utils.DBObject { return &WorkflowResource{} }) // Create a new instance of the accessor
}
type WorkflowResourcePricingProfile struct {
ID string `json:"id,omitempty" bson:"id,omitempty"`
ExploitedResourceSet
}
func (p *WorkflowResourcePricingProfile) GetOverrideStrategyValue() int {
return -1
}
func (p *WorkflowResourcePricingProfile) GetID() string {
return p.ID
}
func (p *WorkflowResourcePricingProfile) IsPurchased() bool {
return false
}
/*
* Missing wich Instance is selected
*
*/
func (p *WorkflowResourcePricingProfile) GetPrice(amountOfData float64, val float64, start time.Time, end time.Time, request *tools.APIRequest, params ...string) (float64, error) {
// load workflow
price := float64(0)
pp, err := getPrice[*CustomizedDataResource](p.DataResources, amountOfData, val, start, end, request, params...)
if err != nil {
return 0, err
}
price += pp
pp, err = getPrice[*CustomizedStorageResource](p.StorageResources, amountOfData, val, start, end, request, params...)
if err != nil {
return 0, err
}
price += pp
pp, err = getPrice[*CustomizedProcessingResource](p.ProcessingResources, amountOfData, val, start, end, request, params...)
if err != nil {
return 0, err
}
price += pp
pp, err = getPrice[*CustomizedComputeResource](p.ComputeResources, amountOfData, val, start, end, request, params...)
if err != nil {
return 0, err
}
price += pp
pp, err = getPrice[*CustomizedWorkflowResource](p.WorkflowResources, amountOfData, val, start, end, request, params...)
if err != nil {
return 0, err
}
price += pp
return price, nil
}
func getPrice[T ShallowResourceInterface](arr []T, amountOfData float64, val float64, start time.Time, end time.Time, request *tools.APIRequest, params ...string) (float64, error) {
// load workflow
price := float64(0)
for _, data := range arr {
partner := data.GetPartnership(request)
pricing := partner.GetPricing(data.GetPricingID())
pp, err := pricing.GetPrice(amountOfData, val, start, end, request)
if err != nil {
return 0, err
}
price += pp
}
return price, nil
}