oc-lib/models/resources/priced_resource.go

94 lines
3.0 KiB
Go
Raw Normal View History

2025-01-13 11:24:07 +01:00
package resources
import (
"errors"
2025-02-10 11:32:55 +01:00
"fmt"
2025-01-13 11:24:07 +01:00
"time"
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
"cloud.o-forge.io/core/oc-lib/tools"
)
type PricedResource struct {
2025-01-14 11:28:16 +01:00
Name string `json:"name,omitempty" bson:"name,omitempty"`
Logo string `json:"logo,omitempty" bson:"logo,omitempty"`
InstancesRefs map[string]string `json:"instances_refs,omitempty" bson:"instances_refs,omitempty"`
PricingProfiles []pricing.PricingProfileITF `json:"pricing_profiles,omitempty" bson:"pricing_profiles,omitempty"`
SelectedPricing *pricing.PricingProfileITF `json:"selected_pricing,omitempty" bson:"selected_pricing,omitempty"`
ExplicitBookingDurationS float64 `json:"explicit_location_duration_s,omitempty" bson:"explicit_location_duration_s,omitempty"`
UsageStart *time.Time `json:"start,omitempty" bson:"start,omitempty"`
UsageEnd *time.Time `json:"end,omitempty" bson:"end,omitempty"`
CreatorID string `json:"peer_id,omitempty" bson:"peer_id,omitempty"`
ResourceID string `json:"resource_id,omitempty" bson:"resource_id,omitempty"`
ResourceType tools.DataType `json:"resource_type,omitempty" bson:"resource_type,omitempty"`
2025-01-13 11:24:07 +01:00
}
func (abs *PricedResource) GetID() string {
return abs.ResourceID
}
func (abs *PricedResource) GetType() tools.DataType {
return abs.ResourceType
}
func (abs *PricedResource) GetCreatorID() string {
return abs.CreatorID
}
func (abs *PricedResource) IsPurchased() bool {
if abs.SelectedPricing == nil {
return false
}
return (*abs.SelectedPricing).IsPurchased()
}
func (abs *PricedResource) GetLocationEnd() *time.Time {
return abs.UsageEnd
}
func (abs *PricedResource) GetLocationStart() *time.Time {
return abs.UsageStart
}
func (abs *PricedResource) SetLocationStart(start time.Time) {
abs.UsageStart = &start
}
func (abs *PricedResource) SetLocationEnd(end time.Time) {
abs.UsageEnd = &end
}
func (abs *PricedResource) GetExplicitDurationInS() float64 {
if abs.ExplicitBookingDurationS == 0 {
2025-02-10 10:42:37 +01:00
if abs.UsageEnd == nil && abs.UsageStart == nil {
2025-01-13 11:24:07 +01:00
return time.Duration(1 * time.Hour).Seconds()
}
2025-02-10 10:42:37 +01:00
if abs.UsageEnd == nil {
add := abs.UsageStart.Add(time.Duration(1 * time.Hour))
abs.UsageEnd = &add
}
2025-01-13 11:24:07 +01:00
return abs.UsageEnd.Sub(*abs.UsageStart).Seconds()
}
return abs.ExplicitBookingDurationS
}
func (r *PricedResource) GetPrice() (float64, error) {
2025-02-10 11:32:55 +01:00
fmt.Println("GetPrice", r.UsageStart, r.UsageEnd)
2025-02-10 13:04:13 +01:00
now := time.Now()
if r.UsageStart == nil {
r.UsageStart = &now
2025-01-13 11:24:07 +01:00
}
2025-02-10 13:04:13 +01:00
if r.UsageEnd == nil {
add := r.UsageStart.Add(time.Duration(1 * time.Hour))
r.UsageEnd = &add
}
2025-02-10 13:10:42 +01:00
if r.SelectedPricing == nil {
2025-02-10 13:04:13 +01:00
if len(r.PricingProfiles) == 0 {
return 0, errors.New("pricing profile must be set")
}
2025-02-10 13:10:42 +01:00
*r.SelectedPricing = r.PricingProfiles[0]
2025-01-13 11:24:07 +01:00
}
2025-02-10 13:10:42 +01:00
pricing := *r.SelectedPricing
2025-02-10 13:04:13 +01:00
return pricing.GetPrice(1, 0, *r.UsageStart, *r.UsageEnd)
2025-01-13 11:24:07 +01:00
}