github.com/m3db/m3@v1.5.0/src/metrics/policy/retention.go (about)

     1  // Copyright (c) 2016 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package policy
    22  
    23  import (
    24  	"errors"
    25  	"fmt"
    26  	"time"
    27  
    28  	"github.com/m3db/m3/src/metrics/generated/proto/policypb"
    29  	xtime "github.com/m3db/m3/src/x/time"
    30  )
    31  
    32  var (
    33  	errNilRetentionProto = errors.New("empty retention proto message")
    34  	emptyRetentionProto  = policypb.Retention{}
    35  )
    36  
    37  // Retention is the retention period for datapoints.
    38  type Retention time.Duration
    39  
    40  // String is the string representation of a retention period.
    41  func (r Retention) String() string {
    42  	return xtime.ToExtendedString(r.Duration())
    43  }
    44  
    45  // Duration returns the duration of the retention period.
    46  func (r Retention) Duration() time.Duration {
    47  	return time.Duration(r)
    48  }
    49  
    50  // ToProto converts the retention to a protobuf message in place.
    51  func (r Retention) ToProto(pb *policypb.Retention) {
    52  	pb.Period = r.Duration().Nanoseconds()
    53  }
    54  
    55  // FromProto converts the protobuf message to a retention in place.
    56  func (r *Retention) FromProto(pb policypb.Retention) error {
    57  	if pb == emptyRetentionProto {
    58  		return errNilRetentionProto
    59  	}
    60  	*r = Retention(pb.Period)
    61  	return nil
    62  }
    63  
    64  // ParseRetention parses a retention.
    65  func ParseRetention(str string) (Retention, error) {
    66  	d, err := xtime.ParseExtendedDuration(str)
    67  	if err != nil {
    68  		return 0, err
    69  	}
    70  	return Retention(d), nil
    71  }
    72  
    73  // MustParseRetention parses a retention, and panics if the input is invalid.
    74  func MustParseRetention(str string) Retention {
    75  	retention, err := ParseRetention(str)
    76  	if err != nil {
    77  		panic(fmt.Errorf("invalid retention string %s: %v", str, err))
    78  	}
    79  	return retention
    80  }
    81  
    82  // RetentionValue is the retention value.
    83  type RetentionValue int
    84  
    85  // List of known retention values.
    86  const (
    87  	UnknownRetentionValue RetentionValue = iota
    88  	OneHour
    89  	SixHours
    90  	TwelveHours
    91  	OneDay
    92  	TwoDays
    93  	SevenDays
    94  	FourteenDays
    95  	ThirtyDays
    96  	FourtyFiveDays
    97  )
    98  
    99  var (
   100  	errUnknownRetention      = errors.New("unknown retention")
   101  	errUnknownRetentionValue = errors.New("unknown retention value")
   102  
   103  	// EmptyRetention is an empty retention.
   104  	EmptyRetention Retention
   105  )
   106  
   107  // Retention returns the retention associated with a value.
   108  func (v RetentionValue) Retention() (Retention, error) {
   109  	retention, exists := valuesToRetention[v]
   110  	if !exists {
   111  		return EmptyRetention, errUnknownRetentionValue
   112  	}
   113  	return retention, nil
   114  }
   115  
   116  // IsValid returns whether the retention value is valid.
   117  func (v RetentionValue) IsValid() bool {
   118  	_, valid := valuesToRetention[v]
   119  	return valid
   120  }
   121  
   122  // ValueFromRetention returns the value given a retention.
   123  func ValueFromRetention(retention Retention) (RetentionValue, error) {
   124  	value, exists := retentionToValues[retention]
   125  	if exists {
   126  		return value, nil
   127  	}
   128  	return UnknownRetentionValue, errUnknownRetention
   129  }
   130  
   131  var (
   132  	valuesToRetention = map[RetentionValue]Retention{
   133  		OneHour:        Retention(time.Hour),
   134  		SixHours:       Retention(6 * time.Hour),
   135  		TwelveHours:    Retention(12 * time.Hour),
   136  		OneDay:         Retention(24 * time.Hour),
   137  		TwoDays:        Retention(2 * 24 * time.Hour),
   138  		SevenDays:      Retention(7 * 24 * time.Hour),
   139  		FourteenDays:   Retention(14 * 24 * time.Hour),
   140  		ThirtyDays:     Retention(30 * 24 * time.Hour),
   141  		FourtyFiveDays: Retention(45 * 24 * time.Hour),
   142  	}
   143  
   144  	retentionToValues = make(map[Retention]RetentionValue)
   145  )
   146  
   147  func init() {
   148  	for value, retention := range valuesToRetention {
   149  		retentionToValues[retention] = value
   150  	}
   151  }