2024-08-30 09:14:03 +02:00
package workflow
2024-07-19 10:54:58 +02:00
import (
2024-08-12 16:11:25 +02:00
"errors"
2024-12-12 16:25:47 +01:00
"time"
2024-07-19 10:54:58 +02:00
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"
2024-07-19 10:54:58 +02:00
)
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
}
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-01-15 10:56:44 +01:00
func ( w * Workflow ) GetPricedItem ( f func ( item graph . GraphItem ) bool , request * tools . APIRequest ) map [ string ] pricing . PricedItemITF {
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 ( )
ord := res . ConvertToPricedResource ( dt , request )
2024-12-12 16:25:47 +01:00
list_datas [ res . GetID ( ) ] = ord
2024-11-14 10:02:18 +01:00
}
}
2024-12-12 16:25:47 +01:00
return list_datas
2024-11-14 10:02:18 +01:00
}
2025-01-15 10:56:44 +01:00
func ( w * Workflow ) GetByRelatedProcessing ( processingID string , g func ( item graph . GraphItem ) bool ) [ ] resources . ResourceInterface {
2025-01-13 11:24:07 +01:00
storages := [ ] resources . ResourceInterface { }
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
storages = append ( storages , node )
}
}
return storages
}
2024-12-12 16:25:47 +01:00
func ( ao * Workflow ) VerifyAuth ( 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
}
2024-12-12 16:25:47 +01:00
isAuthorized = shared . VerifyAuth ( request )
2024-12-03 10:57:28 +01:00
}
}
2024-12-12 16:25:47 +01:00
return ao . AbstractObject . VerifyAuth ( 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
func ( wf * Workflow ) Planify ( start time . Time , end * time . Time , request * tools . APIRequest ) ( float64 , map [ tools . DataType ] [ ] pricing . PricedItemITF , * Workflow , error ) {
priceds := map [ tools . DataType ] [ ] pricing . PricedItemITF { }
2025-01-15 10:56:44 +01:00
ps , priceds , err := plan [ * resources . ProcessingResource ] ( tools . PROCESSING_RESOURCE , wf , priceds , request , wf . Graph . IsProcessing ,
func ( res resources . ResourceInterface , priced pricing . PricedItemITF ) ( time . Time , float64 ) {
return start . Add ( time . Duration ( wf . Graph . GetAverageTimeProcessingBeforeStart ( 0 , res . GetID ( ) , request ) ) * time . Second ) , priced . GetExplicitDurationInS ( )
} , func ( started time . Time , duration float64 ) time . Time {
return started . Add ( time . Duration ( duration ) )
} )
if err != nil {
return 0 , priceds , nil , err
2025-01-13 11:24:07 +01:00
}
2025-01-15 10:56:44 +01:00
if _ , priceds , err = plan [ resources . ResourceInterface ] ( tools . DATA_RESOURCE , wf , priceds , request , wf . Graph . IsData ,
func ( res resources . ResourceInterface , priced pricing . PricedItemITF ) ( time . Time , float64 ) {
return start , 0
} , func ( started time . Time , duration float64 ) time . Time {
return * end
} ) ; err != nil {
return 0 , priceds , nil , err
2025-01-13 11:24:07 +01:00
}
2025-01-15 10:56:44 +01:00
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 , wf , priceds , request , f ,
func ( res resources . ResourceInterface , priced pricing . PricedItemITF ) ( time . Time , float64 ) {
nearestStart , longestDuration := 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
2025-01-13 11:24:07 +01:00
} , request )
2025-01-15 10:56:44 +01:00
return start . Add ( time . Duration ( nearestStart ) * time . Second ) , longestDuration
} , func ( started time . Time , duration float64 ) time . Time {
return started . Add ( time . Duration ( duration ) )
} ) ; err != nil {
return 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 )
if _ , priceds , err = plan [ resources . ResourceInterface ] ( tools . WORKFLOW_RESOURCE , wf , priceds , request , wf . Graph . IsWorkflow ,
func ( res resources . ResourceInterface , priced pricing . PricedItemITF ) ( time . Time , float64 ) {
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 {
return start , longest
2025-01-13 11:24:07 +01:00
}
2025-01-15 10:56:44 +01:00
if neoLongest , _ , _ , err := r . ( * Workflow ) . Planify ( start , end , request ) ; err != nil {
return start , longest
} else if neoLongest > longest {
longest = neoLongest
2025-01-13 11:24:07 +01:00
}
2025-01-15 10:56:44 +01:00
return start . Add ( time . Duration ( common . GetPlannerNearestStart ( start , priceds , request ) ) * time . Second ) , longest
} , func ( start time . Time , longest float64 ) time . Time {
return start . Add ( time . Duration ( longest ) * time . Second )
} ) ; err != nil {
return 0 , priceds , nil , err
2025-01-13 11:24:07 +01:00
}
2025-01-15 10:56:44 +01:00
return longest , priceds , wf , nil
2025-01-13 11:24:07 +01:00
}
2025-01-15 10:56:44 +01:00
func plan [ T resources . ResourceInterface ] ( dt tools . DataType , wf * Workflow , priceds map [ tools . DataType ] [ ] pricing . PricedItemITF , request * tools . APIRequest ,
f func ( graph . GraphItem ) bool , start func ( resources . ResourceInterface , pricing . PricedItemITF ) ( time . Time , float64 ) , end func ( time . Time , float64 ) time . Time ) ( [ ] T , map [ tools . DataType ] [ ] pricing . PricedItemITF , error ) {
resources := [ ] T { }
for _ , item := range wf . GetGraphItems ( f ) {
if priceds [ dt ] == nil {
priceds [ dt ] = [ ] 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" )
}
priced := realItem . ConvertToPricedResource ( dt , request )
started , duration := start ( realItem , priced )
priced . SetLocationStart ( started )
if duration >= 0 {
priced . SetLocationEnd ( end ( started , duration ) )
2025-01-13 11:24:07 +01:00
}
2025-01-15 10:56:44 +01:00
priced . SetLocationEnd ( end ( started , priced . GetExplicitDurationInS ( ) ) )
resources = append ( resources , realItem . ( T ) )
priceds [ dt ] = append ( priceds [ dt ] , priced )
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
}