github.com/cs3org/reva/v2@v2.27.7/pkg/mentix/meshdata/downtime.go (about)

     1  // Copyright 2018-2021 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package meshdata
    20  
    21  import (
    22  	"time"
    23  
    24  	"github.com/pkg/errors"
    25  )
    26  
    27  // Downtimes represents all scheduled downtimes of a site.
    28  type Downtimes struct {
    29  	Downtimes []*Downtime
    30  }
    31  
    32  // ScheduleDowntime schedules a new downtime.
    33  func (dts *Downtimes) ScheduleDowntime(start time.Time, end time.Time, affectedServices []string) (*Downtime, error) {
    34  	// Create a new downtime and verify it
    35  	dt := &Downtime{
    36  		StartDate:        start,
    37  		EndDate:          end,
    38  		AffectedServices: affectedServices,
    39  	}
    40  	dt.InferMissingData()
    41  	if err := dt.Verify(); err != nil {
    42  		return nil, err
    43  	}
    44  
    45  	// Only schedule the downtime if it hasn't expired yet
    46  	if dt.IsExpired() {
    47  		return nil, nil
    48  	}
    49  
    50  	dts.Downtimes = append(dts.Downtimes, dt)
    51  	return dt, nil
    52  }
    53  
    54  // Clear clears all downtimes.
    55  func (dts *Downtimes) Clear() {
    56  	dts.Downtimes = make([]*Downtime, 0, 10)
    57  }
    58  
    59  // IsAnyActive returns true if any downtime is currently active.
    60  func (dts *Downtimes) IsAnyActive() bool {
    61  	for _, dt := range dts.Downtimes {
    62  		if dt.IsActive() {
    63  			return true
    64  		}
    65  	}
    66  	return false
    67  }
    68  
    69  // InferMissingData infers missing data from other data where possible.
    70  func (dts *Downtimes) InferMissingData() {
    71  	for _, dt := range dts.Downtimes {
    72  		dt.InferMissingData()
    73  	}
    74  }
    75  
    76  // Verify checks if the downtimes data is valid.
    77  func (dts *Downtimes) Verify() error {
    78  	for _, dt := range dts.Downtimes {
    79  		if err := dt.Verify(); err != nil {
    80  			return err
    81  		}
    82  	}
    83  
    84  	return nil
    85  }
    86  
    87  // Downtime represents a single scheduled downtime.
    88  type Downtime struct {
    89  	StartDate        time.Time
    90  	EndDate          time.Time
    91  	AffectedServices []string
    92  }
    93  
    94  // IsActive returns true if the downtime is currently active.
    95  func (dt *Downtime) IsActive() bool {
    96  	now := time.Now()
    97  	return dt.StartDate.Before(now) && dt.EndDate.After(now)
    98  }
    99  
   100  // IsPending returns true if the downtime is yet to come.
   101  func (dt *Downtime) IsPending() bool {
   102  	return dt.StartDate.After(time.Now())
   103  }
   104  
   105  // IsExpired returns true of the downtime has expired (i.e., lies in the past).
   106  func (dt *Downtime) IsExpired() bool {
   107  	return dt.EndDate.Before(time.Now())
   108  }
   109  
   110  // InferMissingData infers missing data from other data where possible.
   111  func (dt *Downtime) InferMissingData() {
   112  }
   113  
   114  // Verify checks if the downtime data is valid.
   115  func (dt *Downtime) Verify() error {
   116  	if dt.EndDate.Before(dt.StartDate) {
   117  		return errors.Errorf("downtime end is before its start")
   118  	}
   119  	if len(dt.AffectedServices) == 0 {
   120  		return errors.Errorf("no services affected by downtime")
   121  	}
   122  
   123  	return nil
   124  }