65 lines
1.7 KiB
Go
65 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"git.gammaspectra.live/P2Pool/consensus/v3/p2pool/sidechain"
|
|
"git.gammaspectra.live/P2Pool/consensus/v3/types"
|
|
cmdutils "git.gammaspectra.live/P2Pool/observer-cmd-utils/utils"
|
|
"math"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func EffortColor(effort float64) string {
|
|
effortColor := FormatColorRed
|
|
if effort < 200 {
|
|
effortColor = FormatColorYellow
|
|
}
|
|
if effort < 100 {
|
|
effortColor = FormatColorGreen
|
|
}
|
|
return effortColor
|
|
}
|
|
|
|
func TimeElapsed(timestamp uint64) string {
|
|
diff := time.Since(time.Unix(int64(timestamp), 0).UTC())
|
|
|
|
days := int64(diff.Hours() / 24)
|
|
hours := int64(diff.Hours()) % 24
|
|
minutes := int64(diff.Minutes()) % 60
|
|
seconds := int64(diff.Seconds()) % 60
|
|
|
|
var result []string
|
|
if days > 0 {
|
|
result = append(result, strconv.FormatInt(days, 10)+"d")
|
|
}
|
|
if hours > 0 {
|
|
result = append(result, strconv.FormatInt(hours, 10)+"h")
|
|
}
|
|
if minutes > 0 {
|
|
result = append(result, strconv.FormatInt(minutes, 10)+"m")
|
|
}
|
|
if seconds > 0 {
|
|
result = append(result, strconv.FormatInt(seconds, 10)+"s")
|
|
}
|
|
|
|
if timestamp == 0 {
|
|
return "never"
|
|
} else if len(result) == 0 {
|
|
return "just now"
|
|
} else {
|
|
return strings.Join(result, " ") + " ago"
|
|
}
|
|
}
|
|
|
|
func GetShareLink(host string, sideHeight uint64, mainId types.Hash) string {
|
|
uHeight := (sideHeight << 16) | (uint64(mainId[0]) << 8) | uint64(mainId[1])
|
|
return fmt.Sprintf("%s/s/%s", host, cmdutils.EncodeBinaryNumber(uHeight))
|
|
}
|
|
|
|
func GetPayoutLink[T uint64 | uint32 | int](host string, sideHeight uint64, index T, consensus *sidechain.Consensus) string {
|
|
payoutIndex := (sideHeight << uint64(math.Ceil(math.Log2(float64(consensus.ChainWindowSize*4))))) | uint64(index)
|
|
return fmt.Sprintf("%s/p/%s", host, cmdutils.EncodeBinaryNumber(payoutIndex))
|
|
}
|