From 29bc21735d6e8427de49f62d1223f0b4b613f165 Mon Sep 17 00:00:00 2001 From: mr Date: Wed, 18 Jun 2025 07:58:40 +0200 Subject: [PATCH] setup --- models/booking/booking.go | 48 ++++++++++++++++++++++++++++------ models/common/models/metric.go | 17 ++++++++++++ models/common/planner.go | 0 3 files changed, 57 insertions(+), 8 deletions(-) create mode 100644 models/common/models/metric.go mode change 100644 => 100755 models/common/planner.go diff --git a/models/booking/booking.go b/models/booking/booking.go index 4d6b20a..74e9ffe 100644 --- a/models/booking/booking.go +++ b/models/booking/booking.go @@ -5,6 +5,7 @@ import ( "cloud.o-forge.io/core/oc-lib/dbs" "cloud.o-forge.io/core/oc-lib/models/common/enum" + "cloud.o-forge.io/core/oc-lib/models/common/models" "cloud.o-forge.io/core/oc-lib/models/utils" "cloud.o-forge.io/core/oc-lib/tools" "go.mongodb.org/mongo-driver/bson/primitive" @@ -14,14 +15,18 @@ import ( * Booking is a struct that represents a booking */ type Booking struct { - utils.AbstractObject // AbstractObject contains the basic fields of an object (id, name) - ExecutionsID string `json:"executions_id,omitempty" bson:"executions_id,omitempty" validate:"required"` // ExecutionsID is the ID of the executions - DestPeerID string `json:"dest_peer_id,omitempty"` // DestPeerID is the ID of the destination peer - WorkflowID string `json:"workflow_id,omitempty" bson:"workflow_id,omitempty"` // WorkflowID is the ID of the workflow - ExecutionID string `json:"execution_id,omitempty" bson:"execution_id,omitempty" validate:"required"` - State enum.BookingStatus `json:"state,omitempty" bson:"state,omitempty" validate:"required"` // State is the state of the booking - ExpectedStartDate time.Time `json:"expected_start_date,omitempty" bson:"expected_start_date,omitempty" validate:"required"` // ExpectedStartDate is the expected start date of the booking - ExpectedEndDate *time.Time `json:"expected_end_date,omitempty" bson:"expected_end_date,omitempty" validate:"required"` // ExpectedEndDate is the expected end date of the booking + utils.AbstractObject // AbstractObject contains the basic fields of an object (id, name) + + ResumeMetrics map[string]map[string]models.MetricResume `json:"resume_metrics,omitempty" bson:"resume_metrics,omitempty"` + ExecutionMetrics map[string][]models.MetricsSnapshot `json:"metrics,omitempty" bson:"metrics,omitempty"` + + ExecutionsID string `json:"executions_id,omitempty" bson:"executions_id,omitempty" validate:"required"` // ExecutionsID is the ID of the executions + DestPeerID string `json:"dest_peer_id,omitempty"` // DestPeerID is the ID of the destination peer + WorkflowID string `json:"workflow_id,omitempty" bson:"workflow_id,omitempty"` // WorkflowID is the ID of the workflow + ExecutionID string `json:"execution_id,omitempty" bson:"execution_id,omitempty" validate:"required"` + State enum.BookingStatus `json:"state,omitempty" bson:"state,omitempty" validate:"required"` // State is the state of the booking + ExpectedStartDate time.Time `json:"expected_start_date,omitempty" bson:"expected_start_date,omitempty" validate:"required"` // ExpectedStartDate is the expected start date of the booking + ExpectedEndDate *time.Time `json:"expected_end_date,omitempty" bson:"expected_end_date,omitempty" validate:"required"` // ExpectedEndDate is the expected end date of the booking RealStartDate *time.Time `json:"real_start_date,omitempty" bson:"real_start_date,omitempty"` // RealStartDate is the real start date of the booking RealEndDate *time.Time `json:"real_end_date,omitempty" bson:"real_end_date,omitempty"` // RealEndDate is the real end date of the booking @@ -30,6 +35,33 @@ type Booking struct { ResourceID string `json:"resource_id,omitempty" bson:"resource_id,omitempty" validate:"required"` // could be a Compute or a Storage } +func (b *Booking) CalcDeltaOfExecution() map[string]map[string]models.MetricResume { + m := map[string]map[string]models.MetricResume{} + for instance, snapshot := range b.ExecutionMetrics { + m[instance] = map[string]models.MetricResume{} + for _, metric := range snapshot { + for _, mm := range metric.Metrics { + if resume, ok := m[instance][mm.Name]; !ok { + m[instance][mm.Name] = models.MetricResume{ + Delta: 0, + LastValue: mm.Value, + } + } else { + delta := resume.LastValue - mm.Value + if delta == 0 { + resume.Delta = delta + } else { + resume.Delta = (resume.Delta + delta) / 2 + } + resume.LastValue = mm.Value + m[instance][mm.Name] = resume + } + } + } + } + return m +} + // CheckBooking checks if a booking is possible on a specific compute resource func (wfa *Booking) Check(id string, start time.Time, end *time.Time, parrallelAllowed int) (bool, error) { // check if diff --git a/models/common/models/metric.go b/models/common/models/metric.go new file mode 100644 index 0000000..0b4c429 --- /dev/null +++ b/models/common/models/metric.go @@ -0,0 +1,17 @@ +package models + +type MetricsSnapshot struct { + From string `json:"origin"` + Metrics []Metric `json:"metrics"` +} + +type Metric struct { + Name string `json:"name"` + Value float64 `json:"value"` + Error error `json:"error"` +} + +type MetricResume struct { + Delta float64 `json:"delta"` + LastValue float64 `json:"last_value"` +} diff --git a/models/common/planner.go b/models/common/planner.go old mode 100644 new mode 100755