github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/metrics/rules/view/ruleset_test.go (about)

     1  // Copyright (c) 2018 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  	"testing"
    25  	"time"
    26  
    27  	"github.com/m3db/m3/src/metrics/aggregation"
    28  	"github.com/m3db/m3/src/metrics/pipeline"
    29  	"github.com/m3db/m3/src/metrics/policy"
    30  	"github.com/m3db/m3/src/metrics/transformation"
    31  	xtime "github.com/m3db/m3/src/x/time"
    32  
    33  	"github.com/stretchr/testify/require"
    34  )
    35  
    36  func TestRuleSetSort(t *testing.T) {
    37  	rr1, err := pipeline.NewRollupOp(
    38  		pipeline.GroupByRollupType,
    39  		"name",
    40  		[]string{"tag2", "tag1"},
    41  		aggregation.DefaultID,
    42  	)
    43  	require.NoError(t, err)
    44  	rr2, err := pipeline.NewRollupOp(
    45  		pipeline.GroupByRollupType,
    46  		"name",
    47  		[]string{"tag2", "tag1"},
    48  		aggregation.DefaultID,
    49  	)
    50  	require.NoError(t, err)
    51  
    52  	ruleset := RuleSet{
    53  		Namespace:     "testNamespace",
    54  		Version:       1,
    55  		CutoverMillis: 1234,
    56  		MappingRules: []MappingRule{
    57  			{
    58  				ID:            "uuid1",
    59  				Name:          "foo",
    60  				Filter:        "filter1",
    61  				AggregationID: aggregation.MustCompressTypes(aggregation.Sum),
    62  				StoragePolicies: policy.StoragePolicies{
    63  					policy.NewStoragePolicy(10*time.Second, xtime.Second, time.Hour),
    64  				},
    65  			},
    66  			{
    67  				ID:            "uuid2",
    68  				Name:          "bar",
    69  				Filter:        "filter2",
    70  				AggregationID: aggregation.DefaultID,
    71  				StoragePolicies: policy.StoragePolicies{
    72  					policy.NewStoragePolicy(time.Minute, xtime.Minute, time.Hour),
    73  				},
    74  			},
    75  		},
    76  		RollupRules: []RollupRule{
    77  			{
    78  				ID:            "uuid3",
    79  				Name:          "car",
    80  				CutoverMillis: 1234,
    81  				Filter:        "filter3",
    82  				Targets: []RollupTarget{
    83  					{
    84  						Pipeline: pipeline.NewPipeline([]pipeline.OpUnion{
    85  							{
    86  								Type:   pipeline.RollupOpType,
    87  								Rollup: rr1,
    88  							},
    89  						}),
    90  						StoragePolicies: policy.StoragePolicies{
    91  							policy.NewStoragePolicy(10*time.Second, xtime.Second, 12*time.Hour),
    92  						},
    93  					},
    94  				},
    95  				LastUpdatedAtMillis: 1234,
    96  				LastUpdatedBy:       "john",
    97  			},
    98  			{
    99  				ID:            "uuid4",
   100  				Name:          "baz",
   101  				CutoverMillis: 4567,
   102  				Filter:        "filter4",
   103  				Targets: []RollupTarget{
   104  					{
   105  						Pipeline: pipeline.NewPipeline([]pipeline.OpUnion{
   106  							{
   107  								Type:        pipeline.AggregationOpType,
   108  								Aggregation: pipeline.AggregationOp{Type: aggregation.Sum},
   109  							},
   110  							{
   111  								Type:           pipeline.TransformationOpType,
   112  								Transformation: pipeline.TransformationOp{Type: transformation.PerSecond},
   113  							},
   114  							{
   115  								Type:   pipeline.RollupOpType,
   116  								Rollup: rr2,
   117  							},
   118  						}),
   119  						StoragePolicies: policy.StoragePolicies{
   120  							policy.NewStoragePolicy(10*time.Second, xtime.Second, 12*time.Hour),
   121  							policy.NewStoragePolicy(time.Minute, xtime.Minute, 24*time.Hour),
   122  							policy.NewStoragePolicy(5*time.Minute, xtime.Minute, 48*time.Hour),
   123  						},
   124  					},
   125  				},
   126  				LastUpdatedAtMillis: 4567,
   127  				LastUpdatedBy:       "jane",
   128  			},
   129  		},
   130  	}
   131  	ruleset.Sort()
   132  
   133  	expected := RuleSet{
   134  		Namespace:     "testNamespace",
   135  		Version:       1,
   136  		CutoverMillis: 1234,
   137  		MappingRules: []MappingRule{
   138  			{
   139  				ID:            "uuid2",
   140  				Name:          "bar",
   141  				Filter:        "filter2",
   142  				AggregationID: aggregation.DefaultID,
   143  				StoragePolicies: policy.StoragePolicies{
   144  					policy.NewStoragePolicy(time.Minute, xtime.Minute, time.Hour),
   145  				},
   146  			},
   147  			{
   148  				ID:            "uuid1",
   149  				Name:          "foo",
   150  				Filter:        "filter1",
   151  				AggregationID: aggregation.MustCompressTypes(aggregation.Sum),
   152  				StoragePolicies: policy.StoragePolicies{
   153  					policy.NewStoragePolicy(10*time.Second, xtime.Second, time.Hour),
   154  				},
   155  			},
   156  		},
   157  		RollupRules: []RollupRule{
   158  			{
   159  				ID:            "uuid4",
   160  				Name:          "baz",
   161  				CutoverMillis: 4567,
   162  				Filter:        "filter4",
   163  				Targets: []RollupTarget{
   164  					{
   165  						Pipeline: pipeline.NewPipeline([]pipeline.OpUnion{
   166  							{
   167  								Type:        pipeline.AggregationOpType,
   168  								Aggregation: pipeline.AggregationOp{Type: aggregation.Sum},
   169  							},
   170  							{
   171  								Type:           pipeline.TransformationOpType,
   172  								Transformation: pipeline.TransformationOp{Type: transformation.PerSecond},
   173  							},
   174  							{
   175  								Type:   pipeline.RollupOpType,
   176  								Rollup: rr1,
   177  							},
   178  						}),
   179  						StoragePolicies: policy.StoragePolicies{
   180  							policy.NewStoragePolicy(10*time.Second, xtime.Second, 12*time.Hour),
   181  							policy.NewStoragePolicy(time.Minute, xtime.Minute, 24*time.Hour),
   182  							policy.NewStoragePolicy(5*time.Minute, xtime.Minute, 48*time.Hour),
   183  						},
   184  					},
   185  				},
   186  				LastUpdatedAtMillis: 4567,
   187  				LastUpdatedBy:       "jane",
   188  			},
   189  			{
   190  				ID:            "uuid3",
   191  				Name:          "car",
   192  				CutoverMillis: 1234,
   193  				Filter:        "filter3",
   194  				Targets: []RollupTarget{
   195  					{
   196  						Pipeline: pipeline.NewPipeline([]pipeline.OpUnion{
   197  							{
   198  								Type:   pipeline.RollupOpType,
   199  								Rollup: rr2,
   200  							},
   201  						}),
   202  						StoragePolicies: policy.StoragePolicies{
   203  							policy.NewStoragePolicy(10*time.Second, xtime.Second, 12*time.Hour),
   204  						},
   205  					},
   206  				},
   207  				LastUpdatedAtMillis: 1234,
   208  				LastUpdatedBy:       "john",
   209  			},
   210  		},
   211  	}
   212  	require.Equal(t, expected, ruleset)
   213  }
   214  
   215  func TestRuleSetsSort(t *testing.T) {
   216  	rr1, err := pipeline.NewRollupOp(
   217  		pipeline.GroupByRollupType,
   218  		"name",
   219  		[]string{"tag2", "tag1"},
   220  		aggregation.DefaultID,
   221  	)
   222  	require.NoError(t, err)
   223  	rr2, err := pipeline.NewRollupOp(
   224  		pipeline.GroupByRollupType,
   225  		"name",
   226  		[]string{"tag2", "tag1"},
   227  		aggregation.DefaultID,
   228  	)
   229  	require.NoError(t, err)
   230  
   231  	rulesets := RuleSets{
   232  		"ns1": &RuleSet{
   233  			Namespace:     "ns1",
   234  			Version:       1,
   235  			CutoverMillis: 1234,
   236  			MappingRules: []MappingRule{
   237  				{
   238  					ID:            "uuid2",
   239  					Name:          "ƒoo",
   240  					Filter:        "filter2",
   241  					AggregationID: aggregation.DefaultID,
   242  					StoragePolicies: policy.StoragePolicies{
   243  						policy.NewStoragePolicy(time.Minute, xtime.Minute, time.Hour),
   244  					},
   245  				},
   246  				{
   247  					ID:            "uuid1",
   248  					Name:          "bar",
   249  					Filter:        "filter1",
   250  					AggregationID: aggregation.MustCompressTypes(aggregation.Sum),
   251  					StoragePolicies: policy.StoragePolicies{
   252  						policy.NewStoragePolicy(10*time.Second, xtime.Second, time.Hour),
   253  					},
   254  				},
   255  			},
   256  		},
   257  		"ns2": &RuleSet{
   258  			Namespace:     "ns2",
   259  			Version:       1,
   260  			CutoverMillis: 1234,
   261  			RollupRules: []RollupRule{
   262  				{
   263  					ID:            "uuid4",
   264  					Name:          "cat",
   265  					CutoverMillis: 4567,
   266  					Filter:        "filter4",
   267  					Targets: []RollupTarget{
   268  						{
   269  							Pipeline: pipeline.NewPipeline([]pipeline.OpUnion{
   270  								{
   271  									Type:        pipeline.AggregationOpType,
   272  									Aggregation: pipeline.AggregationOp{Type: aggregation.Sum},
   273  								},
   274  								{
   275  									Type:           pipeline.TransformationOpType,
   276  									Transformation: pipeline.TransformationOp{Type: transformation.PerSecond},
   277  								},
   278  								{
   279  									Type:   pipeline.RollupOpType,
   280  									Rollup: rr1,
   281  								},
   282  							}),
   283  							StoragePolicies: policy.StoragePolicies{
   284  								policy.NewStoragePolicy(10*time.Second, xtime.Second, 12*time.Hour),
   285  								policy.NewStoragePolicy(time.Minute, xtime.Minute, 24*time.Hour),
   286  								policy.NewStoragePolicy(5*time.Minute, xtime.Minute, 48*time.Hour),
   287  							},
   288  						},
   289  					},
   290  					LastUpdatedAtMillis: 4567,
   291  					LastUpdatedBy:       "jane",
   292  				},
   293  				{
   294  					ID:            "uuid3",
   295  					Name:          "baz",
   296  					CutoverMillis: 1234,
   297  					Filter:        "filter3",
   298  					Targets: []RollupTarget{
   299  						{
   300  							Pipeline: pipeline.NewPipeline([]pipeline.OpUnion{
   301  								{
   302  									Type:   pipeline.RollupOpType,
   303  									Rollup: rr2,
   304  								},
   305  							}),
   306  							StoragePolicies: policy.StoragePolicies{
   307  								policy.NewStoragePolicy(10*time.Second, xtime.Second, 12*time.Hour),
   308  							},
   309  						},
   310  					},
   311  					LastUpdatedAtMillis: 1234,
   312  					LastUpdatedBy:       "john",
   313  				},
   314  			},
   315  		},
   316  	}
   317  	rulesets.Sort()
   318  
   319  	expected := RuleSets{
   320  		"ns1": &RuleSet{
   321  			Namespace:     "ns1",
   322  			Version:       1,
   323  			CutoverMillis: 1234,
   324  			MappingRules: []MappingRule{
   325  				{
   326  					ID:            "uuid1",
   327  					Name:          "bar",
   328  					Filter:        "filter1",
   329  					AggregationID: aggregation.MustCompressTypes(aggregation.Sum),
   330  					StoragePolicies: policy.StoragePolicies{
   331  						policy.NewStoragePolicy(10*time.Second, xtime.Second, time.Hour),
   332  					},
   333  				},
   334  				{
   335  					ID:            "uuid2",
   336  					Name:          "ƒoo",
   337  					Filter:        "filter2",
   338  					AggregationID: aggregation.DefaultID,
   339  					StoragePolicies: policy.StoragePolicies{
   340  						policy.NewStoragePolicy(time.Minute, xtime.Minute, time.Hour),
   341  					},
   342  				},
   343  			},
   344  		},
   345  		"ns2": &RuleSet{
   346  			Namespace:     "ns2",
   347  			Version:       1,
   348  			CutoverMillis: 1234,
   349  			RollupRules: []RollupRule{
   350  				{
   351  					ID:            "uuid3",
   352  					Name:          "baz",
   353  					CutoverMillis: 1234,
   354  					Filter:        "filter3",
   355  					Targets: []RollupTarget{
   356  						{
   357  							Pipeline: pipeline.NewPipeline([]pipeline.OpUnion{
   358  								{
   359  									Type:   pipeline.RollupOpType,
   360  									Rollup: rr1,
   361  								},
   362  							}),
   363  							StoragePolicies: policy.StoragePolicies{
   364  								policy.NewStoragePolicy(10*time.Second, xtime.Second, 12*time.Hour),
   365  							},
   366  						},
   367  					},
   368  					LastUpdatedAtMillis: 1234,
   369  					LastUpdatedBy:       "john",
   370  				},
   371  				{
   372  					ID:            "uuid4",
   373  					Name:          "cat",
   374  					CutoverMillis: 4567,
   375  					Filter:        "filter4",
   376  					Targets: []RollupTarget{
   377  						{
   378  							Pipeline: pipeline.NewPipeline([]pipeline.OpUnion{
   379  								{
   380  									Type:        pipeline.AggregationOpType,
   381  									Aggregation: pipeline.AggregationOp{Type: aggregation.Sum},
   382  								},
   383  								{
   384  									Type:           pipeline.TransformationOpType,
   385  									Transformation: pipeline.TransformationOp{Type: transformation.PerSecond},
   386  								},
   387  								{
   388  									Type:   pipeline.RollupOpType,
   389  									Rollup: rr2,
   390  								},
   391  							}),
   392  							StoragePolicies: policy.StoragePolicies{
   393  								policy.NewStoragePolicy(10*time.Second, xtime.Second, 12*time.Hour),
   394  								policy.NewStoragePolicy(time.Minute, xtime.Minute, 24*time.Hour),
   395  								policy.NewStoragePolicy(5*time.Minute, xtime.Minute, 48*time.Hour),
   396  							},
   397  						},
   398  					},
   399  					LastUpdatedAtMillis: 4567,
   400  					LastUpdatedBy:       "jane",
   401  				},
   402  			},
   403  		},
   404  	}
   405  	require.Equal(t, expected, rulesets)
   406  }