Order Flow Payment Draft
This commit is contained in:
@@ -3,7 +3,6 @@ package graph
|
||||
import (
|
||||
"time"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
|
||||
"cloud.o-forge.io/core/oc-lib/models/resources"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
@@ -15,7 +14,8 @@ type Graph struct {
|
||||
Links []GraphLink `bson:"links" json:"links" default:"{}" validate:"required"` // Links is the list of links between elements in the graph
|
||||
}
|
||||
|
||||
func (g *Graph) GetAverageTimeRelatedToProcessingActivity(start time.Time, processings []*resources.CustomizedProcessingResource, resource resources.ShallowResourceInterface, f func(GraphItem) resources.ShallowResourceInterface) (float64, float64) {
|
||||
func (g *Graph) GetAverageTimeRelatedToProcessingActivity(start time.Time, processings []*resources.ProcessingResource, resource resources.ResourceInterface,
|
||||
f func(GraphItem) resources.ResourceInterface, request *tools.APIRequest) (float64, float64) {
|
||||
nearestStart := float64(10000000000)
|
||||
oneIsInfinite := false
|
||||
longestDuration := float64(0)
|
||||
@@ -27,16 +27,17 @@ func (g *Graph) GetAverageTimeRelatedToProcessingActivity(start time.Time, proce
|
||||
} else if link.Source.ID == processing.GetID() && f(g.Items[link.Source.ID]) != nil && f(g.Items[link.Source.ID]).GetID() == resource.GetID() { // if the source is the processing and the destination is not a compute
|
||||
source = link.Destination.ID
|
||||
}
|
||||
priced := processing.ConvertToPricedResource(tools.PROCESSING_RESOURCE, request)
|
||||
if source != "" {
|
||||
if processing.UsageStart != nil {
|
||||
near := float64(processing.UsageStart.Sub(start).Seconds())
|
||||
if priced.GetLocationStart() != nil {
|
||||
near := float64(priced.GetLocationStart().Sub(start).Seconds())
|
||||
if near < nearestStart {
|
||||
nearestStart = near
|
||||
}
|
||||
|
||||
}
|
||||
if processing.UsageEnd != nil {
|
||||
duration := float64(processing.UsageEnd.Sub(*processing.UsageStart).Seconds())
|
||||
if priced.GetLocationEnd() != nil {
|
||||
duration := float64(priced.GetLocationEnd().Sub(*priced.GetLocationStart()).Seconds())
|
||||
if longestDuration < duration {
|
||||
longestDuration = duration
|
||||
}
|
||||
@@ -52,18 +53,10 @@ func (g *Graph) GetAverageTimeRelatedToProcessingActivity(start time.Time, proce
|
||||
return nearestStart, longestDuration
|
||||
}
|
||||
|
||||
func (g *Graph) SetItemStartUsage(graphItemID string, start time.Time) {
|
||||
g.Items[graphItemID].SetItemStartUsage(start)
|
||||
}
|
||||
|
||||
func (g *Graph) SetItemEndUsage(graphItemID string, end time.Time) {
|
||||
g.Items[graphItemID].SetItemEndUsage(end)
|
||||
}
|
||||
|
||||
/*
|
||||
* GetAverageTimeBeforeStart is a function that returns the average time before the start of a processing
|
||||
*/
|
||||
func (g *Graph) GetAverageTimeProcessingBeforeStart(average float64, processingID string) float64 {
|
||||
func (g *Graph) GetAverageTimeProcessingBeforeStart(average float64, processingID string, request *tools.APIRequest) float64 {
|
||||
currents := []float64{} // list of current time
|
||||
for _, link := range g.Links { // for each link
|
||||
var source string // source is the source of the link
|
||||
@@ -75,13 +68,17 @@ func (g *Graph) GetAverageTimeProcessingBeforeStart(average float64, processingI
|
||||
if source == "" { // if source is empty, continue
|
||||
continue
|
||||
}
|
||||
_, item := g.GetResource(source) // get the resource of the source
|
||||
current := item.GetExplicitDurationInS() // get the explicit duration of the item
|
||||
if current < 0 { // if current is negative, its means that duration of a before could be infinite continue
|
||||
dt, r := g.GetResource(source) // get the resource of the source
|
||||
if r == nil { // if item is nil, continue
|
||||
continue
|
||||
}
|
||||
priced := r.ConvertToPricedResource(dt, request)
|
||||
current := priced.GetExplicitDurationInS() // get the explicit duration of the item
|
||||
if current < 0 { // if current is negative, its means that duration of a before could be infinite continue
|
||||
return current
|
||||
}
|
||||
current += g.GetAverageTimeProcessingBeforeStart(current, source) // get the average time before start of the source
|
||||
currents = append(currents, current) // append the current to the currents
|
||||
current += g.GetAverageTimeProcessingBeforeStart(current, source, request) // get the average time before start of the source
|
||||
currents = append(currents, current) // append the current to the currents
|
||||
}
|
||||
var max float64 // get the max time to wait dependancies to finish
|
||||
for _, current := range currents {
|
||||
@@ -92,60 +89,45 @@ func (g *Graph) GetAverageTimeProcessingBeforeStart(average float64, processingI
|
||||
return max
|
||||
}
|
||||
|
||||
func (g *Graph) GetResource(id string) (string, resources.ShallowResourceInterface) {
|
||||
func (g *Graph) GetResource(id string) (tools.DataType, resources.ResourceInterface) {
|
||||
if item, ok := g.Items[id]; ok {
|
||||
if item.Data != nil {
|
||||
return tools.DATA_RESOURCE.String(), item.Data
|
||||
return tools.DATA_RESOURCE, item.Data
|
||||
} else if item.Compute != nil {
|
||||
return tools.COMPUTE_RESOURCE.String(), item.Compute
|
||||
return tools.COMPUTE_RESOURCE, item.Compute
|
||||
} else if item.Workflow != nil {
|
||||
return tools.WORKFLOW_RESOURCE.String(), item.Workflow
|
||||
return tools.WORKFLOW_RESOURCE, item.Workflow
|
||||
} else if item.Processing != nil {
|
||||
return tools.PROCESSING_RESOURCE.String(), item.Processing
|
||||
return tools.PROCESSING_RESOURCE, item.Processing
|
||||
} else if item.Storage != nil {
|
||||
return tools.STORAGE_RESOURCE.String(), item.Storage
|
||||
return tools.STORAGE_RESOURCE, item.Storage
|
||||
}
|
||||
}
|
||||
return "", nil
|
||||
return tools.INVALID, nil
|
||||
}
|
||||
|
||||
// GraphItem is a struct that represents an item in a graph
|
||||
type GraphItem struct {
|
||||
ID string `bson:"id" json:"id" validate:"required"` // ID is the unique identifier of the item
|
||||
Width float64 `bson:"width" json:"width" validate:"required"` // Width is the graphical width of the item
|
||||
Height float64 `bson:"height" json:"height" validate:"required"` // Height is the graphical height of the item
|
||||
Position Position `bson:"position" json:"position" validate:"required"` // Position is the graphical position of the item
|
||||
*resources.ItemExploitedResource // ItemResource is the resource of the item affected to the item
|
||||
ID string `bson:"id" json:"id" validate:"required"` // ID is the unique identifier of the item
|
||||
Width float64 `bson:"width" json:"width" validate:"required"` // Width is the graphical width of the item
|
||||
Height float64 `bson:"height" json:"height" validate:"required"` // Height is the graphical height of the item
|
||||
Position Position `bson:"position" json:"position" validate:"required"` // Position is the graphical position of the item
|
||||
*resources.ItemResource // ItemResource is the resource of the item affected to the item
|
||||
}
|
||||
|
||||
func (g *GraphItem) GetResource() resources.ShallowResourceInterface {
|
||||
func (g *GraphItem) GetResource() (tools.DataType, resources.ResourceInterface) {
|
||||
if g.Data != nil {
|
||||
return g.Data
|
||||
return tools.DATA_RESOURCE, g.Data
|
||||
} else if g.Compute != nil {
|
||||
return g.Compute
|
||||
return tools.COMPUTE_RESOURCE, g.Compute
|
||||
} else if g.Workflow != nil {
|
||||
return g.Workflow
|
||||
return tools.WORKFLOW_RESOURCE, g.Workflow
|
||||
} else if g.Processing != nil {
|
||||
return g.Processing
|
||||
return tools.PROCESSING_RESOURCE, g.Processing
|
||||
} else if g.Storage != nil {
|
||||
return g.Storage
|
||||
return tools.STORAGE_RESOURCE, g.Storage
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *GraphItem) GetPricedItem() pricing.PricedItemITF {
|
||||
if g.Data != nil {
|
||||
return g.Data
|
||||
} else if g.Compute != nil {
|
||||
return g.Compute
|
||||
} else if g.Workflow != nil {
|
||||
return g.Workflow
|
||||
} else if g.Processing != nil {
|
||||
return g.Processing
|
||||
} else if g.Storage != nil {
|
||||
return g.Storage
|
||||
}
|
||||
return nil
|
||||
return tools.INVALID, nil
|
||||
}
|
||||
|
||||
// GraphLink is a struct that represents a link between two items in a graph
|
||||
|
@@ -2,6 +2,7 @@ package workflow
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/collaborative_area/shallow_collaborative_area"
|
||||
@@ -40,42 +41,42 @@ func (w *AbstractWorkflow) GetGraphItems(f func(item graph.GraphItem) bool) (lis
|
||||
return
|
||||
}
|
||||
|
||||
func (w *AbstractWorkflow) GetResources(f func(item graph.GraphItem) bool) map[string]resources.ShallowResourceInterface {
|
||||
list_datas := map[string]resources.ShallowResourceInterface{}
|
||||
func (w *AbstractWorkflow) GetResources(f func(item graph.GraphItem) bool) map[string]resources.ResourceInterface {
|
||||
list_datas := map[string]resources.ResourceInterface{}
|
||||
for _, item := range w.Graph.Items {
|
||||
if f(item) {
|
||||
res := item.GetResource()
|
||||
_, res := item.GetResource()
|
||||
list_datas[res.GetID()] = res
|
||||
}
|
||||
}
|
||||
return list_datas
|
||||
}
|
||||
|
||||
func (w *AbstractWorkflow) GetPricedItem(f func(item graph.GraphItem) bool) map[string]pricing.PricedItemITF {
|
||||
func (w *AbstractWorkflow) GetPricedItem(f func(item graph.GraphItem) bool, request *tools.APIRequest) map[string]pricing.PricedItemITF {
|
||||
list_datas := map[string]pricing.PricedItemITF{}
|
||||
for _, item := range w.Graph.Items {
|
||||
if f(item) {
|
||||
res := item.GetResource()
|
||||
ord := item.GetPricedItem()
|
||||
dt, res := item.GetResource()
|
||||
ord := res.ConvertToPricedResource(dt, request)
|
||||
list_datas[res.GetID()] = ord
|
||||
}
|
||||
}
|
||||
return list_datas
|
||||
}
|
||||
|
||||
func (w *AbstractWorkflow) GetByRelatedProcessing(processingID string, g func(item graph.GraphItem) bool) []resources.ShallowResourceInterface {
|
||||
storages := []resources.ShallowResourceInterface{}
|
||||
func (w *AbstractWorkflow) GetByRelatedProcessing(processingID string, g func(item graph.GraphItem) bool) []resources.ResourceInterface {
|
||||
storages := []resources.ResourceInterface{}
|
||||
for _, link := range w.Graph.Links {
|
||||
nodeID := link.Destination.ID
|
||||
var node resources.ShallowResourceInterface
|
||||
var node resources.ResourceInterface
|
||||
if g(w.Graph.Items[link.Source.ID]) {
|
||||
item := w.Graph.Items[link.Source.ID]
|
||||
node = item.GetResource()
|
||||
_, node = item.GetResource()
|
||||
}
|
||||
if node == nil && g(w.Graph.Items[link.Destination.ID]) { // if the source is not a storage, we consider that the destination is the storage
|
||||
nodeID = link.Source.ID
|
||||
item := w.Graph.Items[link.Destination.ID] // and the processing is the source
|
||||
node = item.GetResource() // we are looking for the storage as destination
|
||||
_, node = item.GetResource() // we are looking for the storage as destination
|
||||
}
|
||||
if processingID == nodeID && node != nil { // if the storage is linked to the processing
|
||||
storages = append(storages, node)
|
||||
@@ -113,37 +114,12 @@ type Workflow struct {
|
||||
AbstractWorkflow // AbstractWorkflow contains the basic fields of a workflow
|
||||
}
|
||||
|
||||
func (w *Workflow) GetNearestStart(start time.Time) float64 {
|
||||
near := float64(10000000000)
|
||||
for _, item := range w.Graph.Items {
|
||||
if item.GetResource().GetLocationStart() == nil {
|
||||
continue
|
||||
}
|
||||
newS := item.GetResource().GetLocationStart()
|
||||
if newS.Sub(start).Seconds() < near {
|
||||
near = newS.Sub(start).Seconds()
|
||||
}
|
||||
// get the nearest start from start var
|
||||
func (w *Workflow) getPricedItem(item graph.GraphItem, request *tools.APIRequest) pricing.PricedItemITF {
|
||||
dt, res := item.GetResource()
|
||||
if dt == tools.INVALID {
|
||||
return nil
|
||||
}
|
||||
return near
|
||||
}
|
||||
|
||||
func (w *Workflow) GetLongestTime(end *time.Time) float64 {
|
||||
if end == nil {
|
||||
return -1
|
||||
}
|
||||
longestTime := float64(0)
|
||||
for _, item := range w.GetGraphItems(w.IsProcessing) {
|
||||
if item.GetResource().GetLocationEnd() == nil {
|
||||
continue
|
||||
}
|
||||
newS := item.GetResource().GetLocationEnd()
|
||||
if longestTime < newS.Sub(*end).Seconds() {
|
||||
longestTime = newS.Sub(*end).Seconds()
|
||||
}
|
||||
// get the nearest start from start var
|
||||
}
|
||||
return longestTime
|
||||
return res.ConvertToPricedResource(dt, request)
|
||||
}
|
||||
|
||||
func (ao *Workflow) VerifyAuth(request *tools.APIRequest) bool {
|
||||
@@ -188,3 +164,125 @@ func (wfa *Workflow) CheckBooking(caller *tools.HTTPCaller) (bool, error) {
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (wf *Workflow) Planify(start time.Time, end *time.Time, request *tools.APIRequest) (float64, map[tools.DataType][]pricing.PricedItemITF, *Workflow, error) {
|
||||
processings := []*resources.ProcessingResource{}
|
||||
priceds := map[tools.DataType][]pricing.PricedItemITF{}
|
||||
priceds[tools.PROCESSING_RESOURCE] = []pricing.PricedItemITF{}
|
||||
for _, item := range wf.GetGraphItems(wf.IsProcessing) {
|
||||
dt, realItem := item.GetResource()
|
||||
if realItem == nil {
|
||||
return 0, priceds, nil, errors.New("could not load the processing resource")
|
||||
}
|
||||
priced := realItem.ConvertToPricedResource(dt, request)
|
||||
timeFromStartS := wf.Graph.GetAverageTimeProcessingBeforeStart(0, realItem.GetID(), request)
|
||||
started := start.Add(time.Duration(timeFromStartS) * time.Second)
|
||||
priced.SetLocationStart(started)
|
||||
priced.SetLocationEnd(started.Add(time.Duration(priced.GetExplicitDurationInS())))
|
||||
processings = append(processings, realItem.(*resources.ProcessingResource))
|
||||
priceds[tools.PROCESSING_RESOURCE] = append(priceds[tools.PROCESSING_RESOURCE], priced)
|
||||
}
|
||||
priceds[tools.DATA_RESOURCE] = []pricing.PricedItemITF{}
|
||||
for _, item := range wf.GetGraphItems(wf.IsData) {
|
||||
dt, realItem := item.GetResource()
|
||||
if realItem == nil {
|
||||
continue
|
||||
}
|
||||
priced := realItem.ConvertToPricedResource(dt, request)
|
||||
priced.SetLocationStart(start)
|
||||
priced.SetLocationEnd(*end)
|
||||
priceds[tools.PROCESSING_RESOURCE] = append(priceds[tools.PROCESSING_RESOURCE], priced)
|
||||
}
|
||||
for _, f := range []func(graph.GraphItem) bool{wf.IsStorage, wf.IsCompute} {
|
||||
for _, item := range wf.GetGraphItems(f) {
|
||||
dt, r := item.GetResource()
|
||||
if r == nil {
|
||||
continue
|
||||
}
|
||||
if priceds[dt] == nil {
|
||||
priceds[dt] = []pricing.PricedItemITF{}
|
||||
}
|
||||
priced := r.ConvertToPricedResource(dt, request)
|
||||
nearestStart, longestDuration := wf.Graph.GetAverageTimeRelatedToProcessingActivity(start, processings, r,
|
||||
func(i graph.GraphItem) resources.ResourceInterface {
|
||||
if f(i) {
|
||||
_, r := i.GetResource()
|
||||
return r
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}, request)
|
||||
started := start.Add(time.Duration(nearestStart) * time.Second)
|
||||
priced.SetLocationStart(started)
|
||||
if longestDuration >= 0 {
|
||||
priced.SetLocationEnd(started.Add(time.Duration(longestDuration)))
|
||||
}
|
||||
priceds[dt] = append(priceds[dt], priced)
|
||||
}
|
||||
}
|
||||
longest := wf.getLongestTime(end, priceds, request)
|
||||
priceds[tools.WORKFLOW_RESOURCE] = []pricing.PricedItemITF{}
|
||||
for _, item := range wf.GetGraphItems(wf.IsWorkflow) {
|
||||
access := NewAccessor(nil)
|
||||
_, r := item.GetResource()
|
||||
if r == nil {
|
||||
return 0, priceds, nil, errors.New("could not load the workflow")
|
||||
}
|
||||
priced := r.ConvertToPricedResource(tools.WORKFLOW_RESOURCE, request)
|
||||
res, code, err := access.LoadOne(r.GetID())
|
||||
if code != 200 || err != nil {
|
||||
return 0, priceds, nil, errors.New("could not load the workflow with id: " + fmt.Sprintf("%v", err.Error()))
|
||||
}
|
||||
neoLongest := float64(0)
|
||||
innerWF := res.(*Workflow)
|
||||
neoLongest, _, innerWF, err = innerWF.Planify(start, end, request)
|
||||
if neoLongest > longest {
|
||||
longest = neoLongest
|
||||
}
|
||||
started := start.Add(time.Duration(wf.getNearestStart(start, priceds, request)) * time.Second)
|
||||
priced.SetLocationStart(started)
|
||||
durationE := time.Duration(longest)
|
||||
if durationE < 0 {
|
||||
continue
|
||||
}
|
||||
ended := start.Add(durationE * time.Second)
|
||||
priced.SetLocationEnd(ended)
|
||||
priceds[tools.WORKFLOW_RESOURCE] = append(priceds[tools.WORKFLOW_RESOURCE], priced)
|
||||
}
|
||||
return longest, priceds, wf, nil
|
||||
}
|
||||
|
||||
func (wf *Workflow) getNearestStart(start time.Time, priceds map[tools.DataType][]pricing.PricedItemITF, request *tools.APIRequest) float64 {
|
||||
near := float64(10000000000)
|
||||
for _, items := range priceds {
|
||||
for _, priced := range items {
|
||||
if priced.GetLocationStart() == nil {
|
||||
continue
|
||||
}
|
||||
newS := priced.GetLocationStart()
|
||||
if newS.Sub(start).Seconds() < near {
|
||||
near = newS.Sub(start).Seconds()
|
||||
}
|
||||
}
|
||||
// get the nearest start from start var
|
||||
}
|
||||
return near
|
||||
}
|
||||
|
||||
func (wf *Workflow) getLongestTime(end *time.Time, priceds map[tools.DataType][]pricing.PricedItemITF, request *tools.APIRequest) float64 {
|
||||
if end == nil {
|
||||
return -1
|
||||
}
|
||||
longestTime := float64(0)
|
||||
for _, priced := range priceds[tools.PROCESSING_RESOURCE] {
|
||||
if priced.GetLocationEnd() == nil {
|
||||
continue
|
||||
}
|
||||
newS := priced.GetLocationEnd()
|
||||
if longestTime < newS.Sub(*end).Seconds() {
|
||||
longestTime = newS.Sub(*end).Seconds()
|
||||
}
|
||||
// get the nearest start from start var
|
||||
}
|
||||
return longestTime
|
||||
}
|
||||
|
@@ -8,7 +8,7 @@ import (
|
||||
|
||||
type WorkflowHistory struct{ Workflow }
|
||||
|
||||
func (d *WorkflowHistory) GetAccessor(request tools.APIRequest) utils.Accessor {
|
||||
func (d *WorkflowHistory) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||
return NewAccessorHistory(request) // Create a new instance of the accessor
|
||||
}
|
||||
func (r *WorkflowHistory) GenerateID() {
|
||||
|
Reference in New Issue
Block a user