diff --git a/controllers/booking.go b/controllers/booking.go index f3c89ad..3a8bbdd 100644 --- a/controllers/booking.go +++ b/controllers/booking.go @@ -281,6 +281,12 @@ func (o *BookingController) Post() { o.ServeJSON() return } + if b.Data.(*booking.Booking).ResourceType == tools.COMPUTE_RESOURCE { + go func() { + time.Sleep(time.Until(b.Data.(*booking.Booking).ExpectedStartDate)) + infrastructure.NewPrometheusService().Stream(b.Data.GetID(), b.Data.(*booking.Booking).ExpectedEndDate, 1*time.Second, nil, nil) + }() + } if err := o.createNamespace(resp.ExecutionsID); err != nil { fmt.Println(err.Error()) diff --git a/infrastructure/prometheus.go b/infrastructure/prometheus.go index 9c154b8..4f8e671 100644 --- a/infrastructure/prometheus.go +++ b/infrastructure/prometheus.go @@ -131,46 +131,50 @@ func (p *PrometheusService) Call(bookingID string) (*booking.Booking, map[string return book.Data.(*booking.Booking), metrics } -func (p *PrometheusService) Stream(bookingID string, end *time.Time, interval time.Duration, flusher http.Flusher, encoder *json.Encoder) { +func (p *PrometheusService) Stream(bookingID string, end *time.Time, interval time.Duration, flusher *http.Flusher, encoder *json.Encoder) { + e := time.Now().UTC().Add(time.Hour * 1) if end != nil { - max := 100 - bookIDS := []string{} - mets := map[string][]models.MetricsSnapshot{} - for time.Now().Before(*end) { - go func() { - book, metrics := p.Call(bookingID) - for k, v := range metrics { - if me, ok := mets[k]; !ok { - mets[k] = []models.MetricsSnapshot{v} - } else { - me = append(me, v) - mets[k] = me - } + e = (*end).UTC() + } + max := 100 + bookIDS := []string{} + mets := map[string][]models.MetricsSnapshot{} + for time.Now().Before(e) { + go func() { + book, metrics := p.Call(bookingID) + for k, v := range metrics { + if me, ok := mets[k]; !ok { + mets[k] = []models.MetricsSnapshot{v} + } else { + me = append(me, v) + mets[k] = me } - bookIDS = append(bookIDS, bookingID) + } + bookIDS = append(bookIDS, bookingID) + if flusher != nil { encoder.Encode(metrics) - flusher.Flush() - if len(bookIDS) == max { - if book.ExecutionMetrics == nil { - book.ExecutionMetrics = mets + (*flusher).Flush() + } + if len(bookIDS) != max { + return + } + if book.ExecutionMetrics == nil { + book.ExecutionMetrics = mets + } else { + for kk, vv := range mets { + if em, ok := book.ExecutionMetrics[kk]; !ok { + book.ExecutionMetrics[kk] = vv } else { - for kk, vv := range mets { - if em, ok := book.ExecutionMetrics[kk]; !ok { - book.ExecutionMetrics[kk] = vv - } else { - em = append(em, vv...) - book.ExecutionMetrics[kk] = em - } - } + em = append(em, vv...) + book.ExecutionMetrics[kk] = em } - book.GetAccessor(nil).UpdateOne(book, bookingID) - bookIDS = []string{} } - }() - time.Sleep(interval) - } - } else { - // todo an anchor... detecting the end of a task. + } + book.GetAccessor(nil).UpdateOne(book, bookingID) + bookIDS = []string{} + + }() + time.Sleep(interval) } }