2024-08-30 09:14:03 +02:00
package workflow
2024-07-19 10:54:58 +02:00
import (
2026-01-12 11:59:05 +01:00
"bufio"
"encoding/json"
2024-08-12 16:11:25 +02:00
"errors"
2026-01-12 11:59:05 +01:00
"fmt"
2026-01-12 14:26:29 +01:00
"mime/multipart"
2026-01-12 11:59:05 +01:00
"strings"
2024-12-12 16:25:47 +01:00
"time"
2024-07-19 10:54:58 +02:00
2026-01-13 16:04:31 +01:00
"cloud.o-forge.io/core/oc-lib/models/booking"
2024-12-03 10:57:28 +01:00
"cloud.o-forge.io/core/oc-lib/models/collaborative_area/shallow_collaborative_area"
2025-01-15 10:56:44 +01:00
"cloud.o-forge.io/core/oc-lib/models/common"
2024-12-12 16:25:47 +01:00
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
2024-08-13 09:49:42 +02:00
"cloud.o-forge.io/core/oc-lib/models/peer"
2024-07-26 10:36:23 +02:00
"cloud.o-forge.io/core/oc-lib/models/resources"
2024-07-19 10:54:58 +02:00
"cloud.o-forge.io/core/oc-lib/models/utils"
2024-11-28 16:49:41 +01:00
"cloud.o-forge.io/core/oc-lib/models/workflow/graph"
2024-08-12 16:11:25 +02:00
"cloud.o-forge.io/core/oc-lib/tools"
2026-01-12 11:59:05 +01:00
"github.com/google/uuid"
2024-07-19 10:54:58 +02:00
)
2026-01-13 16:04:31 +01:00
type ConfigItem map [ string ] int
func ( c ConfigItem ) Get ( key string ) * int {
i := 0
if ins , ok := c [ key ] ; ok {
i = ins
}
return & i
}
2024-08-30 14:50:48 +02:00
/ *
2025-01-15 10:56:44 +01:00
* Workflow is a struct that represents a workflow
* it defines the native workflow
2024-08-30 14:50:48 +02:00
* /
2025-01-15 10:56:44 +01:00
type Workflow struct {
utils . AbstractObject // AbstractObject contains the basic fields of an object (id, name)
2024-07-26 10:36:23 +02:00
resources . ResourceSet
2024-12-12 16:25:47 +01:00
Graph * graph . Graph ` bson:"graph,omitempty" json:"graph,omitempty" ` // Graph UI & logic representation of the workflow
ScheduleActive bool ` json:"schedule_active" bson:"schedule_active" ` // ScheduleActive is a flag that indicates if the schedule is active, if not the workflow is not scheduled and no execution or booking will be set
// Schedule *WorkflowSchedule `bson:"schedule,omitempty" json:"schedule,omitempty"` // Schedule is the schedule of the workflow
2025-01-15 10:56:44 +01:00
Shared [ ] string ` json:"shared,omitempty" bson:"shared,omitempty" ` // Shared is the ID of the shared workflow // AbstractWorkflow contains the basic fields of a workflow
2024-07-26 10:36:23 +02:00
}
2024-12-12 16:25:47 +01:00
func ( d * Workflow ) GetAccessor ( request * tools . APIRequest ) utils . Accessor {
return NewAccessor ( request ) // Create a new instance of the accessor
}
2026-01-12 11:59:05 +01:00
func ( d * Workflow ) GetResources ( dt tools . DataType ) [ ] resources . ResourceInterface {
itf := [ ] resources . ResourceInterface { }
switch dt {
2026-01-13 16:04:31 +01:00
case tools . NATIVE_TOOL :
for _ , d := range d . NativeTools {
itf = append ( itf , d )
}
return itf
2026-01-12 11:59:05 +01:00
case tools . DATA_RESOURCE :
for _ , d := range d . DataResources {
itf = append ( itf , d )
}
return itf
case tools . PROCESSING_RESOURCE :
for _ , d := range d . ProcessingResources {
itf = append ( itf , d )
}
return itf
case tools . COMPUTE_RESOURCE :
for _ , d := range d . ComputeResources {
itf = append ( itf , d )
}
return itf
case tools . WORKFLOW_RESOURCE :
for _ , d := range d . WorkflowResources {
itf = append ( itf , d )
}
return itf
case tools . STORAGE_RESOURCE :
for _ , d := range d . StorageResources {
itf = append ( itf , d )
}
return itf
}
return itf
}
2026-01-12 14:26:29 +01:00
func ( d * Workflow ) ExtractFromPlantUML ( plantUML multipart . File , request * tools . APIRequest ) ( * Workflow , error ) {
2026-01-12 11:59:05 +01:00
if plantUML == nil {
return d , errors . New ( "no file available to export" )
}
2026-01-12 14:26:29 +01:00
2026-01-12 11:59:05 +01:00
defer plantUML . Close ( )
d . Datas = [ ] string { }
d . Storages = [ ] string { }
d . Processings = [ ] string { }
d . Computes = [ ] string { }
d . Workflows = [ ] string { }
d . DataResources = [ ] * resources . DataResource { }
d . StorageResources = [ ] * resources . StorageResource { }
d . ProcessingResources = [ ] * resources . ProcessingResource { }
d . ComputeResources = [ ] * resources . ComputeResource { }
d . WorkflowResources = [ ] * resources . WorkflowResource { }
d . Graph = graph . NewGraph ( )
resourceCatalog := map [ string ] func ( ) resources . ResourceInterface {
"Processing" : func ( ) resources . ResourceInterface {
return & resources . ProcessingResource {
AbstractInstanciatedResource : resources . AbstractInstanciatedResource [ * resources . ProcessingInstance ] {
Instances : [ ] * resources . ProcessingInstance { } ,
} ,
}
} ,
"Storage" : func ( ) resources . ResourceInterface {
return & resources . StorageResource {
AbstractInstanciatedResource : resources . AbstractInstanciatedResource [ * resources . StorageResourceInstance ] {
Instances : [ ] * resources . StorageResourceInstance { } ,
} ,
}
} ,
"Data" : func ( ) resources . ResourceInterface {
return & resources . DataResource {
AbstractInstanciatedResource : resources . AbstractInstanciatedResource [ * resources . DataInstance ] {
Instances : [ ] * resources . DataInstance { } ,
} ,
}
} ,
"ComputeUnit" : func ( ) resources . ResourceInterface {
return & resources . ComputeResource {
AbstractInstanciatedResource : resources . AbstractInstanciatedResource [ * resources . ComputeResourceInstance ] {
Instances : [ ] * resources . ComputeResourceInstance { } ,
} ,
}
} ,
}
graphVarName := map [ string ] * graph . GraphItem { }
scanner := bufio . NewScanner ( plantUML )
for scanner . Scan ( ) {
line := scanner . Text ( )
for n , new := range resourceCatalog {
if strings . Contains ( line , n + "(" ) && ! strings . Contains ( line , "!procedure" ) { // should exclude declaration of type.
newRes := new ( )
varName , graphItem , err := d . extractResourcePlantUML ( line , newRes , n , request . PeerID )
if err != nil {
return d , err
}
graphVarName [ varName ] = graphItem
continue
} else if strings . Contains ( line , n + "-->" ) {
2026-01-13 16:04:31 +01:00
err := d . extractLink ( line , graphVarName , "-->" , false )
if err != nil {
fmt . Println ( err )
continue
}
} else if strings . Contains ( line , n + "<--" ) {
err := d . extractLink ( line , graphVarName , "<--" , true )
2026-01-12 11:59:05 +01:00
if err != nil {
fmt . Println ( err )
continue
}
} else if strings . Contains ( line , n + "--" ) {
2026-01-13 16:04:31 +01:00
err := d . extractLink ( line , graphVarName , "--" , false )
2026-01-12 11:59:05 +01:00
if err != nil {
fmt . Println ( err )
continue
}
} else if strings . Contains ( line , n + "-" ) {
2026-01-13 16:04:31 +01:00
err := d . extractLink ( line , graphVarName , "-" , false )
2026-01-12 11:59:05 +01:00
if err != nil {
fmt . Println ( err )
continue
}
}
}
}
if err := scanner . Err ( ) ; err != nil {
return d , err
}
d . generateResource ( d . GetResources ( tools . DATA_RESOURCE ) , request )
d . generateResource ( d . GetResources ( tools . PROCESSING_RESOURCE ) , request )
d . generateResource ( d . GetResources ( tools . STORAGE_RESOURCE ) , request )
d . generateResource ( d . GetResources ( tools . COMPUTE_RESOURCE ) , request )
d . generateResource ( d . GetResources ( tools . WORKFLOW_RESOURCE ) , request )
return d , nil
}
func ( d * Workflow ) generateResource ( datas [ ] resources . ResourceInterface , request * tools . APIRequest ) error {
for _ , d := range datas {
access := d . GetAccessor ( request )
if _ , code , err := access . LoadOne ( d . GetID ( ) ) ; err != nil && code == 200 {
continue
}
access . StoreOne ( d )
}
return nil
}
2026-01-13 16:04:31 +01:00
func ( d * Workflow ) extractLink ( line string , graphVarName map [ string ] * graph . GraphItem , pattern string , reverse bool ) error {
2026-01-12 11:59:05 +01:00
splitted := strings . Split ( line , pattern )
if len ( splitted ) < 2 {
return errors . New ( "links elements not found" )
}
if graphVarName [ splitted [ 0 ] ] != nil {
return errors . New ( "links elements not found -> " + strings . Trim ( splitted [ 0 ] , " " ) )
}
if graphVarName [ splitted [ 1 ] ] != nil {
return errors . New ( "links elements not found -> " + strings . Trim ( splitted [ 1 ] , " " ) )
}
link := & graph . GraphLink {
Source : graph . Position {
ID : graphVarName [ splitted [ 0 ] ] . ID ,
X : 0 ,
Y : 0 ,
} ,
Destination : graph . Position {
ID : graphVarName [ splitted [ 1 ] ] . ID ,
X : 0 ,
Y : 0 ,
} ,
}
2026-01-13 16:04:31 +01:00
if reverse {
tmp := link . Destination
link . Destination = link . Source
link . Source = tmp
}
2026-01-12 11:59:05 +01:00
splittedComments := strings . Split ( line , "'" )
if len ( splittedComments ) <= 1 {
return errors . New ( "Can't deserialize Object, there's no commentary" )
}
comment := strings . ReplaceAll ( splittedComments [ 1 ] , "'" , "" ) // for now it's a json.
json . Unmarshal ( [ ] byte ( comment ) , link )
d . Graph . Links = append ( d . Graph . Links , * link )
return nil
}
func ( d * Workflow ) extractResourcePlantUML ( line string , resource resources . ResourceInterface , dataName string , peerID string ) ( string , * graph . GraphItem , error ) {
splittedFunc := strings . Split ( line , "(" )
if len ( splittedFunc ) <= 1 {
return "" , nil , errors . New ( "Can't deserialize Object, there's no func" )
}
splittedParams := strings . Split ( splittedFunc [ 1 ] , "," )
if len ( splittedFunc ) <= 1 {
return "" , nil , errors . New ( "Can't deserialize Object, there's no params" )
}
varName := splittedParams [ 0 ]
splitted := strings . Split ( splittedParams [ 1 ] , "\"" )
if len ( splitted ) <= 1 {
return "" , nil , errors . New ( "Can't deserialize Object, there's no name" )
}
resource . SetName ( splitted [ 1 ] )
splittedComments := strings . Split ( line , "'" )
if len ( splittedComments ) <= 1 {
return "" , nil , errors . New ( "Can't deserialize Object, there's no commentary" )
}
comment := strings . ReplaceAll ( splittedComments [ 1 ] , "'" , "" ) // for now it's a json.
instance := d . getNewInstance ( dataName , splitted [ 1 ] , peerID )
if instance == nil {
return "" , nil , errors . New ( "No instance found." )
}
resource . AddInstances ( instance )
json . Unmarshal ( [ ] byte ( comment ) , instance )
// deserializer les instances... une instance doit par défaut avoir certaines valeurs d'accès.
graphID := uuid . New ( )
graphItem := & graph . GraphItem {
ID : graphID . String ( ) ,
}
graphItem = d . getNewGraphItem ( dataName , graphItem , resource )
d . Graph . Items [ graphID . String ( ) ] = * graphItem
return varName , graphItem , nil
}
func ( d * Workflow ) getNewGraphItem ( dataName string , graphItem * graph . GraphItem , resource resources . ResourceInterface ) * graph . GraphItem {
switch dataName {
case "Data" :
d . Datas = append ( d . Datas , resource . GetID ( ) )
d . DataResources = append ( d . DataResources , resource . ( * resources . DataResource ) )
graphItem . Data = resource . ( * resources . DataResource )
2026-01-13 16:04:31 +01:00
case "Processing" :
2026-01-12 11:59:05 +01:00
d . Processings = append ( d . Processings , resource . GetID ( ) )
d . ProcessingResources = append ( d . ProcessingResources , resource . ( * resources . ProcessingResource ) )
graphItem . Processing = resource . ( * resources . ProcessingResource )
2026-01-13 16:04:31 +01:00
case "Event" :
access := resources . NewAccessor [ * resources . NativeTool ] ( tools . NATIVE_TOOL , & tools . APIRequest {
Admin : true ,
} , func ( ) utils . DBObject { return & resources . NativeTool { } } )
t , _ , err := access . Search ( nil , "WORKFLOW_EVENT" , false )
if err == nil && len ( t ) > 0 {
d . NativeTool = append ( d . NativeTool , t [ 0 ] . GetID ( ) )
graphItem . NativeTool = t [ 0 ] . ( * resources . NativeTool )
}
2026-01-12 11:59:05 +01:00
case "Storage" :
d . Storages = append ( d . Storages , resource . GetID ( ) )
d . StorageResources = append ( d . StorageResources , resource . ( * resources . StorageResource ) )
graphItem . Storage = resource . ( * resources . StorageResource )
case "ComputeUnit" :
d . Computes = append ( d . Computes , resource . GetID ( ) )
d . ComputeResources = append ( d . ComputeResources , resource . ( * resources . ComputeResource ) )
graphItem . Compute = resource . ( * resources . ComputeResource )
default :
return graphItem
}
return graphItem
}
func ( d * Workflow ) getNewInstance ( dataName string , name string , peerID string ) resources . ResourceInstanceITF {
switch dataName {
case "Data" :
return resources . NewDataInstance ( name , peerID )
case "Processing" :
return resources . NewProcessingInstance ( name , peerID )
case "Storage" :
return resources . NewStorageResourceInstance ( name , peerID )
case "ComputeUnit" :
return resources . NewComputeResourceInstance ( name , peerID )
default :
return nil
}
}
2025-06-12 10:42:05 +02:00
type Deps struct {
Source string
Dest string
}
2025-06-12 10:47:38 +02:00
func ( w * Workflow ) IsDependancy ( id string ) [ ] Deps {
2025-06-12 10:42:05 +02:00
dependancyOfIDs := [ ] Deps { }
for _ , link := range w . Graph . Links {
if _ , ok := w . Graph . Items [ link . Destination . ID ] ; ! ok {
continue
}
source := w . Graph . Items [ link . Destination . ID ] . Processing
if id == link . Source . ID && source != nil {
dependancyOfIDs = append ( dependancyOfIDs , Deps { Source : source . GetName ( ) , Dest : link . Destination . ID } )
}
sourceWF := w . Graph . Items [ link . Destination . ID ] . Workflow
if id == link . Source . ID && sourceWF != nil {
dependancyOfIDs = append ( dependancyOfIDs , Deps { Source : sourceWF . GetName ( ) , Dest : link . Destination . ID } )
}
}
return dependancyOfIDs
}
2026-01-13 16:04:31 +01:00
func ( w * Workflow ) GetFirstItems ( ) [ ] graph . GraphItem {
return w . GetGraphItems ( func ( item graph . GraphItem ) bool {
return len ( w . GetDependencies ( w . GetID ( ) ) ) == 0
} )
}
2025-06-12 10:42:05 +02:00
func ( w * Workflow ) GetDependencies ( id string ) ( dependencies [ ] Deps ) {
for _ , link := range w . Graph . Links {
if _ , ok := w . Graph . Items [ link . Source . ID ] ; ! ok {
continue
}
source := w . Graph . Items [ link . Source . ID ] . Processing
if id == link . Destination . ID && source != nil {
dependencies = append ( dependencies , Deps { Source : source . GetName ( ) , Dest : link . Source . ID } )
continue
}
}
return
}
2025-01-15 10:56:44 +01:00
func ( w * Workflow ) GetGraphItems ( f func ( item graph . GraphItem ) bool ) ( list_datas [ ] graph . GraphItem ) {
2024-10-03 17:25:54 +02:00
for _ , item := range w . Graph . Items {
2024-12-12 16:25:47 +01:00
if f ( item ) {
list_datas = append ( list_datas , item )
2024-10-03 17:25:54 +02:00
}
}
return
}
2025-06-19 08:11:11 +02:00
func ( w * Workflow ) GetPricedItem (
2026-01-13 16:04:31 +01:00
f func ( item graph . GraphItem ) bool , request * tools . APIRequest ,
instance int ,
partnership int ,
buying int ,
strategy int ,
bookingMode int ,
buyingStrategy int ,
pricingStrategy int ) ( map [ string ] pricing . PricedItemITF , error ) {
2024-12-12 16:25:47 +01:00
list_datas := map [ string ] pricing . PricedItemITF { }
for _ , item := range w . Graph . Items {
if f ( item ) {
2025-01-13 11:24:07 +01:00
dt , res := item . GetResource ( )
2026-01-13 16:04:31 +01:00
ord , err := res . ConvertToPricedResource ( dt , & instance , & partnership , & buying , & strategy , & bookingMode , request )
if err != nil {
return list_datas , err
}
2024-12-12 16:25:47 +01:00
list_datas [ res . GetID ( ) ] = ord
2024-11-14 10:02:18 +01:00
}
}
2026-01-13 16:04:31 +01:00
return list_datas , nil
2024-11-14 10:02:18 +01:00
}
2025-01-30 09:45:13 +01:00
type Related struct {
2025-01-30 11:11:34 +01:00
Node resources . ResourceInterface
2025-01-30 09:45:13 +01:00
Links [ ] graph . GraphLink
}
func ( w * Workflow ) GetByRelatedProcessing ( processingID string , g func ( item graph . GraphItem ) bool ) map [ string ] Related {
related := map [ string ] Related { }
2024-10-10 09:38:27 +02:00
for _ , link := range w . Graph . Links {
2024-12-12 16:25:47 +01:00
nodeID := link . Destination . ID
2025-01-13 11:24:07 +01:00
var node resources . ResourceInterface
2024-12-12 16:25:47 +01:00
if g ( w . Graph . Items [ link . Source . ID ] ) {
item := w . Graph . Items [ link . Source . ID ]
2025-01-13 11:24:07 +01:00
_ , node = item . GetResource ( )
2024-12-12 16:25:47 +01:00
}
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
2025-01-13 11:24:07 +01:00
_ , node = item . GetResource ( ) // we are looking for the storage as destination
2024-10-10 09:38:27 +02:00
}
if processingID == nodeID && node != nil { // if the storage is linked to the processing
2025-08-08 16:15:53 +02:00
relID := node . GetID ( )
rel := Related { }
2025-01-30 11:11:34 +01:00
rel . Node = node
2025-01-30 09:45:13 +01:00
rel . Links = append ( rel . Links , link )
2025-08-08 16:15:53 +02:00
related [ relID ] = rel
2024-10-10 09:38:27 +02:00
}
}
2025-01-30 09:45:13 +01:00
return related
2024-10-10 09:38:27 +02:00
}
2026-01-13 16:04:31 +01:00
func ( ao * Workflow ) VerifyAuth ( callName string , request * tools . APIRequest ) bool {
2024-12-03 10:57:28 +01:00
isAuthorized := false
if len ( ao . Shared ) > 0 {
for _ , shared := range ao . Shared {
2024-12-12 16:25:47 +01:00
shared , code , _ := shallow_collaborative_area . NewAccessor ( request ) . LoadOne ( shared )
2024-12-03 10:57:28 +01:00
if code != 200 || shared == nil {
isAuthorized = false
2025-02-03 12:21:50 +01:00
} else {
2026-01-13 16:04:31 +01:00
isAuthorized = shared . VerifyAuth ( callName , request )
2024-12-03 10:57:28 +01:00
}
}
}
2026-01-13 16:04:31 +01:00
return ao . AbstractObject . VerifyAuth ( callName , request ) || isAuthorized
2024-12-03 10:57:28 +01:00
}
2024-08-30 14:50:48 +02:00
/ *
* CheckBooking is a function that checks the booking of the workflow on peers ( even ourselves )
* /
2024-08-23 09:53:37 +02:00
func ( wfa * Workflow ) CheckBooking ( caller * tools . HTTPCaller ) ( bool , error ) {
2024-08-12 14:18:13 +02:00
// check if
2024-08-30 14:50:48 +02:00
if wfa . Graph == nil { // no graph no booking
2024-08-12 14:18:13 +02:00
return false , nil
}
2024-12-12 16:25:47 +01:00
accessor := ( & resources . ComputeResource { } ) . GetAccessor ( & tools . APIRequest { Caller : caller } )
2024-08-12 16:11:25 +02:00
for _ , link := range wfa . Graph . Links {
2024-12-12 16:25:47 +01:00
if ok , compute_id := link . IsComputeLink ( * wfa . Graph ) ; ok { // check if the link is a link between a compute and a resource
compute , code , _ := accessor . LoadOne ( compute_id )
2024-08-12 16:11:25 +02:00
if code != 200 {
continue
}
2024-11-07 11:05:24 +01:00
// CHECK BOOKING ON PEER, compute could be a remote one
2024-12-12 16:25:47 +01:00
peerID := compute . ( * resources . ComputeResource ) . CreatorID
2024-08-13 09:49:42 +02:00
if peerID == "" {
return false , errors . New ( "no peer id" )
2024-08-30 14:50:48 +02:00
} // no peer id no booking, we need to know where to book
2024-12-12 16:25:47 +01:00
_ , err := ( & peer . Peer { } ) . LaunchPeerExecution ( peerID , compute_id , tools . BOOKING , tools . GET , nil , caller )
2024-08-12 16:11:25 +02:00
if err != nil {
return false , err
}
}
2024-08-12 14:18:13 +02:00
}
2024-08-12 16:11:25 +02:00
return true , nil
}
2025-01-13 11:24:07 +01:00
2026-01-13 16:04:31 +01:00
func ( wf * Workflow ) Planify ( start time . Time , end * time . Time , instances ConfigItem , partnerships ConfigItem , buyings ConfigItem , strategies ConfigItem , bookingMode int , request * tools . APIRequest ) ( bool , float64 , map [ tools . DataType ] map [ string ] pricing . PricedItemITF , * Workflow , error ) {
2025-02-13 09:10:24 +01:00
priceds := map [ tools . DataType ] map [ string ] pricing . PricedItemITF { }
2026-01-13 16:04:31 +01:00
ps , priceds , err := plan [ * resources . ProcessingResource ] ( tools . PROCESSING_RESOURCE , instances , partnerships , buyings , strategies , bookingMode , wf , priceds , request , wf . Graph . IsProcessing ,
func ( res resources . ResourceInterface , priced pricing . PricedItemITF ) ( time . Time , float64 , error ) {
d , err := wf . Graph . GetAverageTimeProcessingBeforeStart ( 0 , res . GetID ( ) ,
* instances . Get ( res . GetID ( ) ) , * partnerships . Get ( res . GetID ( ) ) , * buyings . Get ( res . GetID ( ) ) , * strategies . Get ( res . GetID ( ) ) ,
bookingMode , request )
if err != nil {
return start , 0 , err
}
return start . Add ( time . Duration ( d ) * time . Second ) , priced . GetExplicitDurationInS ( ) , nil
} , func ( started time . Time , duration float64 ) ( * time . Time , error ) {
2025-01-22 11:11:04 +01:00
s := started . Add ( time . Duration ( duration ) )
2026-01-13 16:04:31 +01:00
return & s , nil
2025-01-15 10:56:44 +01:00
} )
if err != nil {
2026-01-13 16:04:31 +01:00
return false , 0 , priceds , nil , err
2025-01-13 11:24:07 +01:00
}
2026-01-13 16:04:31 +01:00
if _ , priceds , err = plan [ resources . ResourceInterface ] ( tools . NATIVE_TOOL , instances , partnerships , buyings , strategies , bookingMode , wf , priceds , request ,
wf . Graph . IsNativeTool , func ( res resources . ResourceInterface , priced pricing . PricedItemITF ) ( time . Time , float64 , error ) {
return start , 0 , nil
} , func ( started time . Time , duration float64 ) ( * time . Time , error ) {
return end , nil
2025-01-15 10:56:44 +01:00
} ) ; err != nil {
2026-01-13 16:04:31 +01:00
return false , 0 , priceds , nil , err
2025-01-13 11:24:07 +01:00
}
2026-01-13 16:04:31 +01:00
if _ , priceds , err = plan [ resources . ResourceInterface ] ( tools . DATA_RESOURCE , instances , partnerships , buyings , strategies , bookingMode , wf , priceds , request ,
wf . Graph . IsData , func ( res resources . ResourceInterface , priced pricing . PricedItemITF ) ( time . Time , float64 , error ) {
return start , 0 , nil
} , func ( started time . Time , duration float64 ) ( * time . Time , error ) {
return end , nil
} ) ; err != nil {
return false , 0 , priceds , nil , err
}
for k , f := range map [ tools . DataType ] func ( graph . GraphItem ) bool { tools . STORAGE_RESOURCE : wf . Graph . IsStorage ,
tools . COMPUTE_RESOURCE : wf . Graph . IsCompute } {
if _ , priceds , err = plan [ resources . ResourceInterface ] ( k , instances , partnerships , buyings , strategies , bookingMode , wf , priceds , request ,
f , func ( res resources . ResourceInterface , priced pricing . PricedItemITF ) ( time . Time , float64 , error ) {
nearestStart , longestDuration , err := wf . Graph . GetAverageTimeRelatedToProcessingActivity ( start , ps , res , func ( i graph . GraphItem ) ( r resources . ResourceInterface ) {
2025-01-13 11:24:07 +01:00
if f ( i ) {
2025-01-15 10:56:44 +01:00
_ , r = i . GetResource ( )
2025-01-13 11:24:07 +01:00
}
2025-01-15 10:56:44 +01:00
return r
2026-01-13 16:04:31 +01:00
} , * instances . Get ( res . GetID ( ) ) , * partnerships . Get ( res . GetID ( ) ) ,
* buyings . Get ( res . GetID ( ) ) , * strategies . Get ( res . GetID ( ) ) , bookingMode , request )
if err != nil {
return start , longestDuration , err
}
return start . Add ( time . Duration ( nearestStart ) * time . Second ) , longestDuration , nil
} , func ( started time . Time , duration float64 ) ( * time . Time , error ) {
2025-01-22 11:11:04 +01:00
s := started . Add ( time . Duration ( duration ) )
2026-01-13 16:04:31 +01:00
return & s , nil
2025-01-15 10:56:44 +01:00
} ) ; err != nil {
2026-01-13 16:04:31 +01:00
return false , 0 , priceds , nil , err
2025-01-13 11:24:07 +01:00
}
}
2025-01-15 10:56:44 +01:00
longest := common . GetPlannerLongestTime ( end , priceds , request )
2026-01-13 16:04:31 +01:00
if _ , priceds , err = plan [ resources . ResourceInterface ] ( tools . WORKFLOW_RESOURCE , instances , partnerships , buyings , strategies ,
bookingMode , wf , priceds , request , wf . Graph . IsWorkflow ,
func ( res resources . ResourceInterface , priced pricing . PricedItemITF ) ( time . Time , float64 , error ) {
2025-01-15 10:56:44 +01:00
start := start . Add ( time . Duration ( common . GetPlannerNearestStart ( start , priceds , request ) ) * time . Second )
longest := float64 ( - 1 )
r , code , err := res . GetAccessor ( request ) . LoadOne ( res . GetID ( ) )
if code != 200 || err != nil {
2026-01-13 16:04:31 +01:00
return start , longest , err
2025-01-13 11:24:07 +01:00
}
2026-01-13 16:04:31 +01:00
_ , neoLongest , priceds2 , _ , err := r . ( * Workflow ) . Planify ( start , end , instances , partnerships , buyings , strategies , bookingMode , request )
// should ... import priced
if err != nil {
return start , longest , err
2025-01-15 10:56:44 +01:00
} else if neoLongest > longest {
longest = neoLongest
2025-01-13 11:24:07 +01:00
}
2026-01-13 16:04:31 +01:00
for k , v := range priceds2 {
if priceds [ k ] == nil {
priceds [ k ] = map [ string ] pricing . PricedItemITF { }
}
for k2 , v2 := range v {
if priceds [ k ] [ k2 ] != nil {
v2 . AddQuantity ( priceds [ k ] [ k2 ] . GetQuantity ( ) )
}
}
}
return start . Add ( time . Duration ( common . GetPlannerNearestStart ( start , priceds , request ) ) * time . Second ) , longest , nil
} , func ( start time . Time , longest float64 ) ( * time . Time , error ) {
2025-01-22 11:11:04 +01:00
s := start . Add ( time . Duration ( longest ) * time . Second )
2026-01-13 16:04:31 +01:00
return & s , nil
2025-01-15 10:56:44 +01:00
} ) ; err != nil {
2026-01-13 16:04:31 +01:00
return false , 0 , priceds , nil , err
}
isPreemptible := true
for _ , first := range wf . GetFirstItems ( ) {
_ , res := first . GetResource ( )
if res . GetBookingModes ( ) [ booking . PREEMPTED ] == nil {
isPreemptible = false
break
}
2025-01-13 11:24:07 +01:00
}
2026-01-13 16:04:31 +01:00
return isPreemptible , longest , priceds , wf , nil
2025-01-13 11:24:07 +01:00
}
2026-01-12 14:26:29 +01:00
// Returns a map of DataType (processing,computing,data,storage,worfklow) where each resource (identified by its UUID)
2025-07-30 18:28:19 +02:00
// is mapped to the list of its items (different appearance) in the graph
// ex: if the same Minio storage is represented by several nodes in the graph, in [tools.STORAGE_RESSOURCE] its UUID will be mapped to
2026-01-12 14:26:29 +01:00
// the list of GraphItem ID that correspond to the ID of each node
func ( w * Workflow ) GetItemsByResources ( ) map [ tools . DataType ] map [ string ] [ ] string {
2025-07-30 18:21:09 +02:00
res := make ( map [ tools . DataType ] map [ string ] [ ] string )
2025-07-30 18:01:23 +02:00
dtMethodMap := map [ tools . DataType ] func ( ) [ ] graph . GraphItem {
2026-01-12 14:26:29 +01:00
tools . STORAGE_RESOURCE : func ( ) [ ] graph . GraphItem { return w . GetGraphItems ( w . Graph . IsStorage ) } ,
tools . DATA_RESOURCE : func ( ) [ ] graph . GraphItem { return w . GetGraphItems ( w . Graph . IsData ) } ,
tools . COMPUTE_RESOURCE : func ( ) [ ] graph . GraphItem { return w . GetGraphItems ( w . Graph . IsCompute ) } ,
tools . PROCESSING_RESOURCE : func ( ) [ ] graph . GraphItem { return w . GetGraphItems ( w . Graph . IsProcessing ) } ,
tools . WORKFLOW_RESOURCE : func ( ) [ ] graph . GraphItem { return w . GetGraphItems ( w . Graph . IsWorkflow ) } ,
2025-07-30 18:01:23 +02:00
}
for dt , meth := range dtMethodMap {
2025-07-30 18:15:55 +02:00
res [ dt ] = make ( map [ string ] [ ] string )
2025-07-30 18:01:23 +02:00
items := meth ( )
for _ , i := range items {
_ , r := i . GetResource ( )
rId := r . GetID ( )
2026-01-12 14:26:29 +01:00
res [ dt ] [ rId ] = append ( res [ dt ] [ rId ] , i . ID )
2025-07-30 18:01:23 +02:00
}
}
2025-07-30 18:21:09 +02:00
return res
2025-07-30 18:01:23 +02:00
}
2025-06-19 08:11:11 +02:00
func plan [ T resources . ResourceInterface ] (
2026-01-13 16:04:31 +01:00
dt tools . DataType , instances ConfigItem , partnerships ConfigItem , buyings ConfigItem , strategies ConfigItem , bookingMode int , wf * Workflow , priceds map [ tools . DataType ] map [ string ] pricing . PricedItemITF , request * tools . APIRequest ,
f func ( graph . GraphItem ) bool ,
start func ( resources . ResourceInterface , pricing . PricedItemITF ) ( time . Time , float64 , error ) ,
end func ( time . Time , float64 ) ( * time . Time , error ) ) ( [ ] T , map [ tools . DataType ] map [ string ] pricing . PricedItemITF , error ) {
2025-01-15 10:56:44 +01:00
resources := [ ] T { }
for _ , item := range wf . GetGraphItems ( f ) {
if priceds [ dt ] == nil {
2025-02-13 09:10:24 +01:00
priceds [ dt ] = map [ string ] pricing . PricedItemITF { }
2025-01-13 11:24:07 +01:00
}
2025-01-15 10:56:44 +01:00
dt , realItem := item . GetResource ( )
if realItem == nil {
return resources , priceds , errors . New ( "could not load the processing resource" )
}
2026-01-13 16:04:31 +01:00
priced , err := realItem . ConvertToPricedResource ( dt , instances . Get ( realItem . GetID ( ) ) ,
partnerships . Get ( realItem . GetID ( ) ) , buyings . Get ( realItem . GetID ( ) ) , strategies . Get ( realItem . GetID ( ) ) , & bookingMode , request )
if err != nil {
return resources , priceds , err
}
2025-06-27 17:10:32 +02:00
// Should be commented once the Pricing selection feature has been implemented, related to the commit d35ad440fa77763ec7f49ab34a85e47e75581b61
// if priced.SelectPricing() == nil {
// return resources, priceds, errors.New("no pricings are selected... can't proceed")
// }
2026-01-13 16:04:31 +01:00
started , duration , err := start ( realItem , priced )
if err != nil {
return resources , priceds , err
}
2025-01-15 10:56:44 +01:00
priced . SetLocationStart ( started )
if duration >= 0 {
2026-01-13 16:04:31 +01:00
if e , err := end ( started , duration ) ; err == nil && e != nil {
2025-01-22 11:11:04 +01:00
priced . SetLocationEnd ( * e )
}
}
2026-01-13 16:04:31 +01:00
if e , err := end ( started , priced . GetExplicitDurationInS ( ) ) ; err != nil && e != nil {
2025-01-22 11:11:04 +01:00
priced . SetLocationEnd ( * e )
2025-01-13 11:24:07 +01:00
}
2025-01-15 10:56:44 +01:00
resources = append ( resources , realItem . ( T ) )
2026-01-13 16:04:31 +01:00
if priceds [ dt ] [ item . ID ] != nil {
priced . AddQuantity ( priceds [ dt ] [ item . ID ] . GetQuantity ( ) )
}
2025-02-13 09:10:24 +01:00
priceds [ dt ] [ item . ID ] = priced
2026-01-13 16:04:31 +01:00
2025-01-13 11:24:07 +01:00
}
2025-01-15 10:56:44 +01:00
return resources , priceds , nil
2025-01-13 11:24:07 +01:00
}