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
"errors"
"time"
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
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-11-08 11:44:23 +01:00
2024-11-07 11:05:24 +01:00
// enum of public private or licenced data
2024-11-08 11:44:23 +01:00
type DataLicense int
2024-11-07 11:05:24 +01:00
const (
2024-11-08 11:44:23 +01:00
PUBLIC DataLicense = iota
PRIVATE
LICENCED
2024-11-07 11:05:24 +01:00
)
2024-11-08 11:44:23 +01:00
2024-12-12 16:25:47 +01:00
type abstractDataResource struct {
Type string ` bson:"type,omitempty" json:"type,omitempty" `
Quality string ` bson:"quality,omitempty" json:"quality,omitempty" `
OpenData bool ` bson:"open_data" json:"open_data" default:"false" ` // Type is the type of the storage
Static bool ` bson:"static" json:"static" default:"false" `
UpdatePeriod time . Time ` bson:"update_period,omitempty" json:"update_period,omitempty" `
PersonalData bool ` bson:"personal_data,omitempty" json:"personal_data,omitempty" `
AnonymizedPersonalData bool ` bson:"anonymized_personal_data,omitempty" json:"anonymized_personal_data,omitempty" `
SizeGB float64 ` json:"size_gb,omitempty" bson:"size_gb,omitempty" ` // SizeGB is the size of the data
2024-11-07 11:05:24 +01:00
}
2024-11-08 11:44:23 +01:00
2024-08-30 14:50:48 +02:00
/ *
* DataResource is a struct that represents a data resource
* it defines the resource data
* /
2024-07-19 10:54:58 +02:00
type DataResource struct {
2024-12-12 16:25:47 +01:00
AbstractResource [ * ResourceInstance [ * DataResourcePartnership ] ]
abstractDataResource // AbstractResource contains the basic fields of an object (id, name)
License DataLicense ` json:"license" bson:"license" description:"license of the data" default:"0" ` // License is the license of the data
// ? Interest DataLicense `json:"interest" bson:"interest" description:"interest of the data" default:"0"` // Interest is the interest of the data
Example string ` json:"example,omitempty" bson:"example,omitempty" description:"base64 encoded data" ` // Example is an example of the data
}
func ( d * DataResource ) GetAccessor ( request * tools . APIRequest ) utils . Accessor {
return NewAccessor [ * DataResource ] ( tools . DATA_RESOURCE , request , func ( ) utils . DBObject { return & DataResource { } } ) // Create a new instance of the accessor
}
type DataResourcePartnership struct {
ResourcePartnerShip [ * DataResourcePricingProfile ]
MaxDownloadableGbAllowed float64 ` json:"allowed_gb,omitempty" bson:"allowed_gb,omitempty" `
PersonalDataAllowed bool ` json:"personal_data_allowed,omitempty" bson:"personal_data_allowed,omitempty" `
AnonymizedPersonalDataAllowed bool ` json:"anonymized_personal_data_allowed,omitempty" bson:"anonymized_personal_data_allowed,omitempty" `
}
type DataResourcePricingStrategy int
const (
PER_DOWNLOAD DataResourcePricingStrategy = iota
PER_TB_DOWNLOADED
PER_GB_DOWNLOADED
PER_MB_DOWNLOADED
PER_KB_DOWNLOADED
)
func ToDataResourcePricingStrategy ( i int ) DataResourcePricingStrategy {
return DataResourcePricingStrategy ( i )
}
func ( t DataResourcePricingStrategy ) GetStrategy ( ) string {
return [ ... ] string { "PER_DOWNLOAD" , "PER_GB" , "PER_MB" , "PER_KB" } [ t ]
}
func ( t DataResourcePricingStrategy ) GetStrategyValue ( ) int {
return int ( t )
}
func ( t DataResourcePricingStrategy ) GetQuantity ( amountOfDataGB float64 ) ( float64 , error ) {
switch t {
case PER_DOWNLOAD :
return 1 , nil
case PER_TB_DOWNLOADED :
return amountOfDataGB * 1000 , nil
case PER_GB_DOWNLOADED :
return amountOfDataGB , nil
case PER_MB_DOWNLOADED :
return amountOfDataGB / 1000 , nil
case PER_KB_DOWNLOADED :
return amountOfDataGB / 1000000 , nil
}
return 0 , errors . New ( "Pricing strategy not found" )
}
type DataResourcePricingProfile struct {
2024-12-16 12:17:20 +01:00
pricing . AccessPricingProfile [ DataResourcePricingStrategy ] // AccessPricingProfile is the pricing profile of a data it means that we can access the data for an amount of time
2024-12-12 16:25:47 +01:00
}
func ( p * DataResourcePricingProfile ) GetOverrideStrategyValue ( ) int {
return p . Pricing . OverrideStrategy . GetStrategyValue ( )
}
func ( p * DataResourcePricingProfile ) GetPrice ( amountOfData float64 , explicitDuration float64 , start time . Time , end time . Time , request * tools . APIRequest , params ... string ) ( float64 , error ) {
return p . Pricing . GetPrice ( amountOfData , explicitDuration , start , & end )
}
func ( p * DataResourcePricingProfile ) IsBuying ( ) bool {
return p . Pricing . BuyingStrategy != pricing . PAY_PER_USE
}
type CustomizedDataResource struct {
AbstractCustomizedResource [ * ResourceInstance [ * DataResourcePartnership ] ]
abstractDataResource
StorageGB float64 ` json:"storage_gb,omitempty" bson:"storage_gb,omitempty" `
}
func ( r * CustomizedDataResource ) GetType ( ) tools . DataType {
return tools . DATA_RESOURCE
2024-07-18 11:51:12 +02:00
}
2024-12-12 16:25:47 +01:00
func ( r * CustomizedDataResource ) GetPrice ( request * tools . APIRequest ) ( float64 , error ) {
if r . UsageStart == nil || r . UsageEnd == nil {
return 0 , errors . New ( "Usage start and end must be set" )
}
partner := r . GetPartnership ( request )
if partner != nil && partner . GetPricing ( r . SelectedPricing ) != nil {
return 0 , errors . New ( "Pricing strategy not found" )
}
pricing := partner . GetPricing ( r . SelectedPricing )
var err error
amountOfData := float64 ( 1 )
if pricing . GetOverrideStrategyValue ( ) >= 0 {
amountOfData , err = ToDataResourcePricingStrategy ( pricing . GetOverrideStrategyValue ( ) ) . GetQuantity ( r . StorageGB )
if err != nil {
return 0 , err
}
}
return pricing . GetPrice ( amountOfData , r . ExplicitBookingDurationS , * r . UsageStart , * r . UsageEnd , request )
2024-07-18 11:51:12 +02:00
}