github.com/m3db/m3@v1.5.0/src/metrics/rules/view/rollup.go (about)

     1  // Copyright (c) 2020 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 view
    22  
    23  import (
    24  	"github.com/m3db/m3/src/metrics/pipeline"
    25  	"github.com/m3db/m3/src/metrics/policy"
    26  	"github.com/m3db/m3/src/query/models"
    27  )
    28  
    29  // RollupTarget is a rollup target model.
    30  type RollupTarget struct {
    31  	Pipeline        pipeline.Pipeline      `json:"pipeline" validate:"required"`
    32  	StoragePolicies policy.StoragePolicies `json:"storagePolicies" validate:"required"`
    33  	ResendEnabled   bool                   `json:"resendEnabled"`
    34  }
    35  
    36  // Equal determines whether two rollup targets are equal.
    37  func (t *RollupTarget) Equal(other *RollupTarget) bool {
    38  	if t == nil && other == nil {
    39  		return true
    40  	}
    41  	if t == nil || other == nil {
    42  		return false
    43  	}
    44  	return t.Pipeline.Equal(other.Pipeline) && t.StoragePolicies.Equal(other.StoragePolicies) &&
    45  		t.ResendEnabled == other.ResendEnabled
    46  }
    47  
    48  // RollupRule is rollup rule model.
    49  type RollupRule struct {
    50  	ID                  string         `json:"id,omitempty"`
    51  	Name                string         `json:"name" validate:"required"`
    52  	Tombstoned          bool           `json:"tombstoned"`
    53  	CutoverMillis       int64          `json:"cutoverMillis,omitempty"`
    54  	Filter              string         `json:"filter" validate:"required"`
    55  	Targets             []RollupTarget `json:"targets" validate:"required,dive,required"`
    56  	LastUpdatedBy       string         `json:"lastUpdatedBy"`
    57  	LastUpdatedAtMillis int64          `json:"lastUpdatedAtMillis"`
    58  	KeepOriginal        bool           `json:"keepOriginal"`
    59  	Tags                []models.Tag   `json:"tags"`
    60  }
    61  
    62  // Equal determines whether two rollup rules are equal.
    63  func (r *RollupRule) Equal(other *RollupRule) bool {
    64  	if r == nil && other == nil {
    65  		return true
    66  	}
    67  	if r == nil || other == nil {
    68  		return false
    69  	}
    70  	if len(r.Tags) != len(other.Tags) {
    71  		return false
    72  	}
    73  	for i := 0; i < len(r.Tags); i++ {
    74  		if !r.Tags[i].Equal(other.Tags[i]) {
    75  			return false
    76  		}
    77  	}
    78  	return r.ID == other.ID &&
    79  		r.Name == other.Name &&
    80  		r.Filter == other.Filter &&
    81  		r.KeepOriginal == other.KeepOriginal &&
    82  		rollupTargets(r.Targets).Equal(other.Targets)
    83  }
    84  
    85  // RollupRules belong to a ruleset indexed by uuid.
    86  // Each value contains the entire snapshot history of the rule.
    87  type RollupRules map[string][]RollupRule
    88  
    89  type rollupTargets []RollupTarget
    90  
    91  func (t rollupTargets) Equal(other rollupTargets) bool {
    92  	if len(t) != len(other) {
    93  		return false
    94  	}
    95  	for i := 0; i < len(t); i++ {
    96  		if !t[i].Equal(&other[i]) {
    97  			return false
    98  		}
    99  	}
   100  	return true
   101  }
   102  
   103  // RollupRulesByNameAsc sorts rollup rules by name in ascending order.
   104  type RollupRulesByNameAsc []RollupRule
   105  
   106  func (a RollupRulesByNameAsc) Len() int           { return len(a) }
   107  func (a RollupRulesByNameAsc) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
   108  func (a RollupRulesByNameAsc) Less(i, j int) bool { return a[i].Name < a[j].Name }
   109  
   110  // RollupRuleSnapshots contains a list of rollup rule snapshots.
   111  type RollupRuleSnapshots struct {
   112  	RollupRules []RollupRule `json:"rollupRules"`
   113  }