massive draft for payment process (UNCOMPLETE)
This commit is contained in:
		@@ -1,8 +1,10 @@
 | 
			
		||||
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/models/utils"
 | 
			
		||||
	"cloud.o-forge.io/core/oc-lib/tools"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@@ -13,7 +15,84 @@ 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) GetResource(id string) (string, utils.DBObject) {
 | 
			
		||||
func (g *Graph) GetAverageTimeRelatedToProcessingActivity(start time.Time, processings []*resources.CustomizedProcessingResource, resource resources.ShallowResourceInterface, f func(GraphItem) resources.ShallowResourceInterface) (float64, float64) {
 | 
			
		||||
	nearestStart := float64(10000000000)
 | 
			
		||||
	oneIsInfinite := false
 | 
			
		||||
	longestDuration := float64(0)
 | 
			
		||||
	for _, link := range g.Links {
 | 
			
		||||
		for _, processing := range processings {
 | 
			
		||||
			var source string                                                                                                                             // source is the source of the link
 | 
			
		||||
			if link.Destination.ID == processing.GetID() && f(g.Items[link.Source.ID]) != nil && f(g.Items[link.Source.ID]).GetID() == resource.GetID() { // if the destination is the processing and the source is not a compute
 | 
			
		||||
				source = link.Source.ID
 | 
			
		||||
			} 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
 | 
			
		||||
			}
 | 
			
		||||
			if source != "" {
 | 
			
		||||
				if processing.UsageStart != nil {
 | 
			
		||||
					near := float64(processing.UsageStart.Sub(start).Seconds())
 | 
			
		||||
					if near < nearestStart {
 | 
			
		||||
						nearestStart = near
 | 
			
		||||
					}
 | 
			
		||||
 | 
			
		||||
				}
 | 
			
		||||
				if processing.UsageEnd != nil {
 | 
			
		||||
					duration := float64(processing.UsageEnd.Sub(*processing.UsageStart).Seconds())
 | 
			
		||||
					if longestDuration < duration {
 | 
			
		||||
						longestDuration = duration
 | 
			
		||||
					}
 | 
			
		||||
				} else {
 | 
			
		||||
					oneIsInfinite = true
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	if oneIsInfinite {
 | 
			
		||||
		return nearestStart, -1
 | 
			
		||||
	}
 | 
			
		||||
	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 {
 | 
			
		||||
	currents := []float64{}        // list of current time
 | 
			
		||||
	for _, link := range g.Links { // for each link
 | 
			
		||||
		var source string                                                                     // source is the source of the link
 | 
			
		||||
		if link.Destination.ID == processingID && g.Items[link.Source.ID].Processing == nil { // if the destination is the processing and the source is not a compute
 | 
			
		||||
			source = link.Source.ID
 | 
			
		||||
		} else if link.Source.ID == processingID && g.Items[link.Source.ID].Processing == nil { // if the source is the processing and the destination is not a compute
 | 
			
		||||
			source = link.Destination.ID
 | 
			
		||||
		}
 | 
			
		||||
		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
 | 
			
		||||
			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
 | 
			
		||||
	}
 | 
			
		||||
	var max float64 // get the max time to wait dependancies to finish
 | 
			
		||||
	for _, current := range currents {
 | 
			
		||||
		if current > max {
 | 
			
		||||
			max = current
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return max
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (g *Graph) GetResource(id string) (string, resources.ShallowResourceInterface) {
 | 
			
		||||
	if item, ok := g.Items[id]; ok {
 | 
			
		||||
		if item.Data != nil {
 | 
			
		||||
			return tools.DATA_RESOURCE.String(), item.Data
 | 
			
		||||
@@ -32,11 +111,41 @@ func (g *Graph) GetResource(id string) (string, utils.DBObject) {
 | 
			
		||||
 | 
			
		||||
// 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.ItemResource          // 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.ItemExploitedResource          // ItemResource is the resource of the item affected to the item
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (g *GraphItem) GetResource() resources.ShallowResourceInterface {
 | 
			
		||||
	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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GraphLink is a struct that represents a link between two items in a graph
 | 
			
		||||
@@ -46,6 +155,20 @@ type GraphLink struct {
 | 
			
		||||
	Style       *GraphLinkStyle `bson:"style,omitempty" json:"style,omitempty"`             // Style is the graphical style of the link
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// tool function to check if a link is a link between a compute and a resource
 | 
			
		||||
func (l *GraphLink) IsComputeLink(g Graph) (bool, string) {
 | 
			
		||||
	if g.Items == nil {
 | 
			
		||||
		return false, ""
 | 
			
		||||
	}
 | 
			
		||||
	if d, ok := g.Items[l.Source.ID]; ok && d.Compute != nil {
 | 
			
		||||
		return true, d.Compute.UUID
 | 
			
		||||
	}
 | 
			
		||||
	if d, ok := g.Items[l.Destination.ID]; ok && d.Compute != nil {
 | 
			
		||||
		return true, d.Compute.UUID
 | 
			
		||||
	}
 | 
			
		||||
	return false, ""
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GraphLinkStyle is a struct that represents the style of a link in a graph
 | 
			
		||||
type GraphLinkStyle struct {
 | 
			
		||||
	Color           int64    `bson:"color" json:"color"`                         // Color is the graphical color of the link (int description of a color, can be transpose as hex)
 | 
			
		||||
@@ -64,7 +187,7 @@ type GraphLinkStyle struct {
 | 
			
		||||
 | 
			
		||||
// Position is a struct that represents a graphical position
 | 
			
		||||
type Position struct {
 | 
			
		||||
	ID string  `json:"id" bson:"id"`                   // ID reprents ItemID (optionnal), TODO: rename to ItemID
 | 
			
		||||
	ID string  `json:"id" bson:"id"`                   // ID reprents ItemID (optionnal)
 | 
			
		||||
	X  float64 `json:"x" bson:"x" validate:"required"` // X is the graphical x position
 | 
			
		||||
	Y  float64 `json:"y" bson:"y" validate:"required"` // Y is the graphical y position
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user