2024-11-28 16:49:41 +01:00
package resources
2024-07-18 11:51:12 +02:00
import (
2024-12-12 16:25:47 +01:00
"time"
2024-07-18 13:35:14 +02:00
"cloud.o-forge.io/core/oc-lib/models/utils"
2024-08-12 16:11:25 +02:00
"cloud.o-forge.io/core/oc-lib/tools"
2024-07-18 11:51:12 +02:00
)
2024-12-12 16:25:47 +01:00
// 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
}
2024-08-30 14:50:48 +02:00
// WorkflowResource is a struct that represents a workflow resource
// it defines the resource workflow
2024-07-19 10:54:58 +02:00
type WorkflowResource struct {
2024-12-12 16:25:47 +01:00
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 {
2024-12-16 12:17:20 +01:00
ID string ` json:"id,omitempty" bson:"id,omitempty" `
2024-12-12 16:25:47 +01:00
ExploitedResourceSet
2024-12-16 12:17:20 +01:00
}
func ( p * WorkflowResourcePricingProfile ) GetOverrideStrategyValue ( ) int {
return - 1
}
func ( p * WorkflowResourcePricingProfile ) GetID ( ) string {
return p . ID
2024-12-12 16:25:47 +01:00
}
2024-12-17 10:42:00 +01:00
func ( p * WorkflowResourcePricingProfile ) IsPurchased ( ) bool {
2024-12-12 16:25:47 +01:00
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
2024-07-18 11:51:12 +02:00
}
2024-12-12 16:25:47 +01:00
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
2024-07-18 11:51:12 +02:00
}