github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/query/graphite/ts/sortable_series.go (about)

     1  // Copyright (c) 2019 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 ts
    22  
    23  import (
    24  	"math"
    25  	"sort"
    26  )
    27  
    28  // Direction signifies ascending or descending order
    29  type Direction int
    30  
    31  const (
    32  	// Ascending order
    33  	Ascending Direction = iota
    34  	// Descending order
    35  	Descending
    36  )
    37  
    38  type sortableSeries struct {
    39  	series *Series
    40  	value  float64
    41  }
    42  
    43  type sortableSeriesList []sortableSeries
    44  
    45  func (s sortableSeriesList) Len() int { return len(s) }
    46  
    47  func (s sortableSeriesList) Less(i, j int) bool {
    48  	if math.IsNaN(s[i].value) && !math.IsNaN(s[j].value) {
    49  		return true
    50  	}
    51  	return s[i].value < s[j].value
    52  }
    53  
    54  func (s sortableSeriesList) Swap(i, j int) {
    55  	s[i], s[j] = s[j], s[i]
    56  }
    57  
    58  // SortSeries applies a given SeriesReducer to each series in the input
    59  // list and sorts based on the assigned value
    60  func SortSeries(input SeriesList, sr SeriesReducer, dir Direction) (SeriesList, error) {
    61  	var (
    62  		in           = input.Values
    63  		sortableList = make(sortableSeriesList, 0, len(in))
    64  		results      = make([]*Series, 0, len(in))
    65  	)
    66  
    67  	for _, series := range in {
    68  		results = append(results, series)
    69  	}
    70  
    71  	if !input.SortApplied {
    72  		// If no sort was applied to these series then sort by name
    73  		// before we do the sort.Stable so that if two series are equal
    74  		// (for instance if comparing by min and they all share same min)
    75  		// that you get the same minimum series each time for same set of
    76  		// series.
    77  		sort.Stable(SeriesByName(results))
    78  	}
    79  
    80  	for _, series := range results {
    81  		sortableList = append(sortableList, sortableSeries{series: series, value: sr(series)})
    82  	}
    83  
    84  	// Use sort.Stable for deterministic output.
    85  	if dir == Ascending {
    86  		sort.Stable(sortableList)
    87  	} else {
    88  		sort.Stable(sort.Reverse(sortableList))
    89  	}
    90  
    91  	// Set results in the sort order of the sortable series list.
    92  	results = results[:0]
    93  	for _, sortable := range sortableList {
    94  		results = append(results, sortable.series)
    95  	}
    96  
    97  	// Preserve metadata and ensure that sort applied is set to true.
    98  	input.Values = results
    99  	input.SortApplied = true
   100  	return input, nil
   101  }