github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/metrics/rules/view/changes/rollup_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 changes
    22  
    23  import (
    24  	"encoding/json"
    25  	"sort"
    26  	"testing"
    27  
    28  	"github.com/m3db/m3/src/metrics/aggregation"
    29  	"github.com/m3db/m3/src/metrics/pipeline"
    30  	"github.com/m3db/m3/src/metrics/policy"
    31  	"github.com/m3db/m3/src/metrics/rules/view"
    32  	"github.com/m3db/m3/src/metrics/transformation"
    33  
    34  	"github.com/stretchr/testify/require"
    35  )
    36  
    37  func TestSortRollupRuleChanges(t *testing.T) {
    38  	ruleChanges := []RollupRuleChange{
    39  		{
    40  			Op:     DeleteOp,
    41  			RuleID: ptr("rrID5"),
    42  		},
    43  		{
    44  			Op:     DeleteOp,
    45  			RuleID: ptr("rrID4"),
    46  		},
    47  		{
    48  			Op:     ChangeOp,
    49  			RuleID: ptr("rrID1"),
    50  			RuleData: &view.RollupRule{
    51  				Name: "change3",
    52  			},
    53  		},
    54  		{
    55  			Op: AddOp,
    56  			RuleData: &view.RollupRule{
    57  				Name: "Add2",
    58  			},
    59  		},
    60  		{
    61  			Op:     ChangeOp,
    62  			RuleID: ptr("rrID2"),
    63  			RuleData: &view.RollupRule{
    64  				Name: "change1",
    65  			},
    66  		},
    67  		{
    68  			Op:     DeleteOp,
    69  			RuleID: ptr("rrID5"),
    70  		},
    71  		{
    72  			Op:     DeleteOp,
    73  			RuleID: ptr("rrID4"),
    74  		},
    75  		{
    76  			Op:     ChangeOp,
    77  			RuleID: ptr("rrID3"),
    78  			RuleData: &view.RollupRule{
    79  				Name: "change2",
    80  			},
    81  		},
    82  		{
    83  			Op: AddOp,
    84  			RuleData: &view.RollupRule{
    85  				Name: "Add1",
    86  			},
    87  		},
    88  		{
    89  			Op:     ChangeOp,
    90  			RuleID: ptr("rrID2"),
    91  			RuleData: &view.RollupRule{
    92  				Name: "change1",
    93  			},
    94  		},
    95  	}
    96  	expected := []RollupRuleChange{
    97  		{
    98  			Op: AddOp,
    99  			RuleData: &view.RollupRule{
   100  				Name: "Add1",
   101  			},
   102  		},
   103  		{
   104  			Op: AddOp,
   105  			RuleData: &view.RollupRule{
   106  				Name: "Add2",
   107  			},
   108  		},
   109  		{
   110  			Op:     ChangeOp,
   111  			RuleID: ptr("rrID2"),
   112  			RuleData: &view.RollupRule{
   113  				Name: "change1",
   114  			},
   115  		},
   116  		{
   117  			Op:     ChangeOp,
   118  			RuleID: ptr("rrID2"),
   119  			RuleData: &view.RollupRule{
   120  				Name: "change1",
   121  			},
   122  		},
   123  		{
   124  			Op:     ChangeOp,
   125  			RuleID: ptr("rrID3"),
   126  			RuleData: &view.RollupRule{
   127  				Name: "change2",
   128  			},
   129  		},
   130  		{
   131  			Op:     ChangeOp,
   132  			RuleID: ptr("rrID1"),
   133  			RuleData: &view.RollupRule{
   134  				Name: "change3",
   135  			},
   136  		},
   137  		{
   138  			Op:     DeleteOp,
   139  			RuleID: ptr("rrID4"),
   140  		},
   141  		{
   142  			Op:     DeleteOp,
   143  			RuleID: ptr("rrID4"),
   144  		},
   145  		{
   146  			Op:     DeleteOp,
   147  			RuleID: ptr("rrID5"),
   148  		},
   149  		{
   150  			Op:     DeleteOp,
   151  			RuleID: ptr("rrID5"),
   152  		},
   153  	}
   154  
   155  	sort.Sort(rollupRuleChangesByOpAscNameAscIDAsc(ruleChanges))
   156  	require.Equal(t, expected, ruleChanges)
   157  }
   158  
   159  func TestRollupRuleJSONDeserialize(t *testing.T) {
   160  	jsonInput := []byte(`{
   161      "op": "change",
   162      "ruleData": {
   163          "id": "validID",
   164          "name": "valid Rule Name",
   165          "filter": "env:production clientname:client device:*  name:validMetricName regionname:global type:timer",
   166          "targets": [
   167              {
   168                  "pipeline": [
   169                    {
   170                      "aggregation":"Sum"
   171                    },
   172                    {
   173                      "transformation":"PerSecond"
   174                    },
   175                    {
   176                      "rollup": {
   177                        "newName":"testRollup",
   178                        "tags":["tag1","tag2"],
   179                        "aggregation":["Min","Max"]
   180                      }
   181                    }
   182                  ],
   183                  "storagePolicies": [
   184                      "10s:2d",
   185                      "1m:40d"
   186                  ]
   187              },
   188              {
   189                  "pipeline": [
   190                    {
   191                      "rollup": {
   192                        "newName":"testRollup",
   193                        "tags":["tag1","tag2"]
   194                      }
   195                    }
   196                  ],
   197                  "storagePolicies": [
   198                      "1m:2d"
   199                  ]
   200              }
   201          ],
   202          "cutoverMillis": 1519332893139,
   203          "lastUpdatedBy": "validUserName",
   204          "lastUpdatedAtMillis": 1519332833139
   205      },
   206      "ruleID": "validID"
   207    }`)
   208  
   209  	var ruleChange RollupRuleChange
   210  	err := json.Unmarshal(jsonInput, &ruleChange)
   211  	require.NoError(t, err)
   212  
   213  	rr1, err := pipeline.NewRollupOp(
   214  		pipeline.GroupByRollupType,
   215  		"testRollup",
   216  		[]string{"tag1", "tag2"},
   217  		aggregation.MustCompressTypes(aggregation.Min, aggregation.Max),
   218  	)
   219  	require.NoError(t, err)
   220  	rr2, err := pipeline.NewRollupOp(
   221  		pipeline.GroupByRollupType,
   222  		"testRollup",
   223  		[]string{"tag1", "tag2"},
   224  		aggregation.DefaultID,
   225  	)
   226  	require.NoError(t, err)
   227  
   228  	expected := RollupRuleChange{
   229  		Op:     ChangeOp,
   230  		RuleID: ptr("validID"),
   231  		RuleData: &view.RollupRule{
   232  			ID:     "validID",
   233  			Name:   "valid Rule Name",
   234  			Filter: "env:production clientname:client device:*  name:validMetricName regionname:global type:timer",
   235  			Targets: []view.RollupTarget{
   236  				{
   237  					Pipeline: pipeline.NewPipeline([]pipeline.OpUnion{
   238  						{
   239  							Type:        pipeline.AggregationOpType,
   240  							Aggregation: pipeline.AggregationOp{Type: aggregation.Sum},
   241  						},
   242  						{
   243  							Type:           pipeline.TransformationOpType,
   244  							Transformation: pipeline.TransformationOp{Type: transformation.PerSecond},
   245  						},
   246  						{
   247  							Type:   pipeline.RollupOpType,
   248  							Rollup: rr1,
   249  						},
   250  					}),
   251  					StoragePolicies: policy.StoragePolicies{
   252  						policy.MustParseStoragePolicy("10s:2d"),
   253  						policy.MustParseStoragePolicy("1m:40d"),
   254  					},
   255  				},
   256  				{
   257  					Pipeline: pipeline.NewPipeline([]pipeline.OpUnion{
   258  						{
   259  							Type:   pipeline.RollupOpType,
   260  							Rollup: rr2,
   261  						},
   262  					}),
   263  					StoragePolicies: policy.StoragePolicies{
   264  						policy.MustParseStoragePolicy("1m:2d"),
   265  					},
   266  				},
   267  			},
   268  			CutoverMillis:       1519332893139,
   269  			LastUpdatedBy:       "validUserName",
   270  			LastUpdatedAtMillis: 1519332833139,
   271  		},
   272  	}
   273  
   274  	require.Equal(t, expected, ruleChange)
   275  }