2024-08-12 14:18:13 +02:00
package booking
import (
"time"
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/models/workflow_execution"
2024-08-12 16:11:25 +02:00
"cloud.o-forge.io/core/oc-lib/tools"
2024-08-12 14:18:13 +02:00
"go.mongodb.org/mongo-driver/bson/primitive"
)
2024-08-30 14:50:48 +02:00
/ *
* Booking is a struct that represents a booking
* /
2024-08-12 14:18:13 +02:00
type Booking struct {
2024-08-30 14:50:48 +02:00
workflow_execution . WorkflowExecution // WorkflowExecution contains the workflow execution data
2024-11-14 11:39:36 +01:00
ComputeResourceID string ` json:"compute_resource_id,omitempty" bson:"compute_resource_id,omitempty" validate:"required" ` // ComputeResourceID is the ID of the compute resource specified in the booking
2024-08-12 14:18:13 +02:00
}
2024-11-07 11:05:24 +01:00
// CheckBooking checks if a booking is possible on a specific compute resource
2024-08-22 08:45:26 +02:00
func ( wfa * Booking ) CheckBooking ( id string , start time . Time , end * time . Time ) ( bool , error ) {
2024-08-12 14:18:13 +02:00
// check if
if end == nil {
// if no end... then Book like a savage
return true , nil
}
e := * end
2024-11-28 11:05:54 +01:00
accessor := New ( )
2024-08-12 14:18:13 +02:00
res , code , err := accessor . Search ( & dbs . Filters {
2024-11-07 11:05:24 +01:00
And : map [ string ] [ ] dbs . Filter { // check if there is a booking on the same compute resource by filtering on the compute_resource_id, the state and the execution date
2024-11-14 11:39:36 +01:00
"compute_resource_id" : { { Operator : dbs . EQUAL . String ( ) , Value : id } } ,
2024-08-12 14:18:13 +02:00
"workflowexecution.state" : { { Operator : dbs . EQUAL . String ( ) , Value : workflow_execution . SCHEDULED . EnumIndex ( ) } } ,
"workflowexecution.execution_date" : {
{ Operator : dbs . LTE . String ( ) , Value : primitive . NewDateTimeFromTime ( e ) } ,
{ Operator : dbs . GTE . String ( ) , Value : primitive . NewDateTimeFromTime ( start ) } ,
} ,
} ,
} , "" )
if code != 200 {
return false , err
}
return len ( res ) == 0 , nil
}
2024-08-30 14:50:48 +02:00
// tool to convert the argo status to a state
2024-08-12 14:18:13 +02:00
func ( wfa * Booking ) ArgoStatusToState ( status string ) * Booking {
wfa . WorkflowExecution . ArgoStatusToState ( status )
return wfa
}
func ( d * Booking ) GetName ( ) string {
return d . UUID + "_" + d . ExecDate . String ( )
}
2024-11-28 11:05:54 +01:00
func ( d * Booking ) GetAccessor ( peerID string , groups [ ] string , caller * tools . HTTPCaller ) utils . Accessor {
data := New ( ) // Create a new instance of the accessor
data . Init ( tools . BOOKING , peerID , groups , caller ) // Initialize the accessor with the BOOKING model type
2024-08-12 14:18:13 +02:00
return data
}