github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/utils/dateutils.go (about)

     1  /*
     2  Copyright 2023.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package utils
    18  
    19  import (
    20  	"errors"
    21  
    22  	log "github.com/sirupsen/logrus"
    23  )
    24  
    25  var HT_STEPS = []uint64{60_000, // 1m
    26  	300_000,       // 5m
    27  	600_000,       // 10m
    28  	1800_000,      // 30m
    29  	3600_000,      // 1h
    30  	10800_000,     // 3h
    31  	43200_000,     // 12h
    32  	86400_000,     // 1d
    33  	604800_000,    // 7d
    34  	2592000_000,   // 30d
    35  	7776000_000,   // 90d
    36  	31536000_000,  // 1y
    37  	315360000_000, // 10y
    38  }
    39  
    40  const MAX_HT_BUCKETS = 90
    41  
    42  // Returns the size for histogram IntervalMillis.
    43  func SanitizeHistogramInterval(startEpochMs uint64, endEpochMs uint64,
    44  	intervalMs uint64) (uint64, error) {
    45  
    46  	var retVal uint64
    47  
    48  	if startEpochMs > endEpochMs {
    49  		return retVal, errors.New("startEpochMs was higher than endEpochMs")
    50  	}
    51  
    52  	trange := endEpochMs - startEpochMs
    53  
    54  	numBuckets := trange / intervalMs
    55  	if numBuckets <= MAX_HT_BUCKETS {
    56  		return intervalMs, nil
    57  	}
    58  
    59  	for _, cand := range HT_STEPS {
    60  		numBuckets = trange / cand
    61  		if numBuckets <= MAX_HT_BUCKETS {
    62  			return cand, nil
    63  		}
    64  	}
    65  
    66  	log.Infof("SanitizeHistogramInterval: returning really long 20y HT interval, should not have happened")
    67  	return HT_STEPS[len(HT_STEPS)-1], nil
    68  }