massive draft for payment process (UNCOMPLETE)
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
package resources
|
||||
|
||||
import (
|
||||
"cloud.o-forge.io/core/oc-lib/models/resources/resource_model"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
@@ -15,12 +18,15 @@ const (
|
||||
LICENCED
|
||||
)
|
||||
|
||||
/*
|
||||
* Struct of Usage Conditions
|
||||
*/
|
||||
type UsageConditions struct {
|
||||
Usage string `json:"usage,omitempty" bson:"usage,omitempty" description:"usage of the data"` // Usage is the usage of the data
|
||||
Actors []string `json:"actors,omitempty" bson:"actors,omitempty" description:"actors of the data"` // Actors is the actors of the data
|
||||
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
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -28,15 +34,105 @@ type UsageConditions struct {
|
||||
* it defines the resource data
|
||||
*/
|
||||
type DataResource struct {
|
||||
resource_model.AbstractResource // AbstractResource contains the basic fields of an object (id, name)
|
||||
resource_model.WebResource
|
||||
Type string `bson:"type,omitempty" json:"type,omitempty"` // Type is the type of the storage
|
||||
UsageConditions UsageConditions `json:"usage_conditions,omitempty" bson:"usage_conditions,omitempty" description:"usage conditions of the data"` // UsageConditions is the usage conditions of the data
|
||||
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
|
||||
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(username string, peerID string, groups []string, caller *tools.HTTPCaller) utils.Accessor {
|
||||
return New[*DataResource](tools.DATA_RESOURCE, username, peerID, groups, caller, func() utils.DBObject { return &DataResource{} }) // Create a new instance of the accessor
|
||||
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 {
|
||||
Pricing *pricing.PricingStrategy[DataResourcePricingStrategy] `json:"cpus_prices,omitempty" bson:"cpus_prices,omitempty"` // CPUsPrices is the prices of the CPUs
|
||||
pricing.AccessPricingProfile // AccessPricingProfile is the pricing profile of a data it means that we can access the data for an amount of time
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user