Files

64 lines
1.4 KiB
Go

package utils
import (
"math"
"os"
"time"
"github.com/fiskerinc/cloud-services/pkg/logger"
"github.com/intel-go/fastjson"
"github.com/sony/gobreaker"
)
// Converts float64 (as decimal seconds in unix epoch) to Time.Time struct
func FloatToTime(float float64) time.Time {
s := int64(math.Floor(float))
ns := int64((float - math.Floor(float)) * 1e9)
return time.Unix(
s,
ns,
)
}
func TimeToFloat(timestamp time.Time) float64 {
return float64(timestamp.UnixNano()) / 1e9
}
func FixFloatTimestampScale(float float64) float64 {
for float > 9999999999 {
float /= 1000
}
return float
}
// marchTimer increments the value at timer until newTime has been reached.
// This is used to try to maintain consistent downsample and insertion periods.
func MarchTimer(timer *time.Time, newTime *time.Time, delay time.Duration) {
for timer.Before(*newTime) {
*timer = timer.Add(delay)
}
}
func ReadVarListFromFile(varsFile string) []string {
data, err := os.ReadFile(varsFile)
if err != nil {
// when running tests, pwd is in the wrong directory to find the default json files.
os.Chdir("..")
data, err = os.ReadFile(varsFile)
if err != nil {
panic(err)
}
}
var result []string
err = fastjson.Unmarshal(data, &result)
if err != nil {
panic(err)
}
return result
}
func BreakerStateChange(name string, from gobreaker.State, to gobreaker.State) {
logger.Warn().Stack().Msgf("%s breaker change from %d to %d", name, from, to)
}