booking start intelligency

This commit is contained in:
mr
2024-08-12 14:18:13 +02:00
parent eea7f25379
commit 93b10de61d
6 changed files with 272 additions and 35 deletions

83
models/booking/booking.go Normal file
View File

@@ -0,0 +1,83 @@
package booking
import (
"encoding/json"
"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"
"github.com/google/uuid"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type Booking struct {
workflow_execution.WorkflowExecution
PeerID string `json:"peer_id,omitempty" bson:"peer_id,omitempty" validate:"required"`
DatacenterResourceID string `json:"datacenter_resource_id,omitempty" bson:"datacenter_resource_id,omitempty" validate:"required"`
}
func (wfa *Booking) CheckBooking(start time.Time, end *time.Time) (bool, error) {
// check if
if end == nil {
// if no end... then Book like a savage
return true, nil
}
e := *end
accessor := wfa.GetAccessor()
res, code, err := accessor.Search(&dbs.Filters{
And: map[string][]dbs.Filter{
"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
}
func (wfa *Booking) ArgoStatusToState(status string) *Booking {
wfa.WorkflowExecution.ArgoStatusToState(status)
return wfa
}
func (ao *Booking) GetID() string {
return ao.UUID
}
func (r *Booking) GenerateID() {
r.UUID = uuid.New().String()
}
func (d *Booking) GetName() string {
return d.UUID + "_" + d.ExecDate.String()
}
func (d *Booking) GetAccessor() utils.Accessor {
data := New()
data.SetLogger(utils.BOOKING)
return data
}
func (dma *Booking) Deserialize(j map[string]interface{}) utils.DBObject {
b, err := json.Marshal(j)
if err != nil {
return nil
}
json.Unmarshal(b, dma)
return dma
}
func (dma *Booking) Serialize() map[string]interface{} {
var m map[string]interface{}
b, err := json.Marshal(dma)
if err != nil {
return nil
}
json.Unmarshal(b, &m)
return m
}

View File

@@ -0,0 +1,83 @@
package booking
import (
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type bookingExecutionMongoAccessor struct {
utils.AbstractAccessor
}
func New() *bookingExecutionMongoAccessor {
return &bookingExecutionMongoAccessor{}
}
func (wfa *bookingExecutionMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
return wfa.GenericDeleteOne(id, wfa)
}
func (wfa *bookingExecutionMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
return wfa.GenericUpdateOne(set, id, wfa, &Booking{})
}
func (wfa *bookingExecutionMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
return wfa.GenericStoreOne(data, wfa)
}
func (wfa *bookingExecutionMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
return wfa.GenericStoreOne(data, wfa)
}
func (wfa *bookingExecutionMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
var workflow Booking
res_mongo, code, err := mongo.MONGOService.LoadOne(id, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, code, err
}
res_mongo.Decode(&workflow)
return &workflow, 200, nil
}
func (wfa bookingExecutionMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
return nil, code, err
}
var results []Booking
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, &r.AbstractObject)
}
return objs, 200, nil
}
func (wfa *bookingExecutionMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
filters = &dbs.Filters{
Or: map[string][]dbs.Filter{
"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
},
}
}
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
return nil, code, err
}
var results []Booking
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, &r)
}
return objs, 200, nil
}