Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ func (s Score) StackedGraphPerBand() []BandGraph {
DataPoints: make([]BandScore, len(graph.DataPoints)),
Max: graph.Max,
startTime: graph.startTime,
duration: graph.duration,
binSeconds: graph.binSeconds,
}

Expand All @@ -575,6 +576,7 @@ type BandGraph struct {
Max BandScore

startTime time.Time
duration time.Duration
binSeconds float64
}

Expand All @@ -583,7 +585,7 @@ func NewBandGraph(band Band, startTime time.Time, duration time.Duration) BandGr
if startTime.IsZero() || duration == 0 {
binCount = 1
} else {
binCount = 60
binCount = BinCountForDuration(duration)
}
return BandGraph{
Band: band,
Expand All @@ -592,6 +594,28 @@ func NewBandGraph(band Band, startTime time.Time, duration time.Duration) BandGr

binSeconds: duration.Seconds() / float64(binCount),
startTime: startTime,
duration: duration,
}
}

func BinCountForDuration(duration time.Duration) int {
switch {
case duration <= 0:
return 1
case duration <= time.Hour:
return int(duration / time.Minute)
case duration <= 6*time.Hour:
return int(duration / (5 * time.Minute))
case duration <= 12*time.Hour:
return int(duration / (10 * time.Minute))
case duration <= 24*time.Hour:
return int(duration / (15 * time.Minute))
case duration <= 36*time.Hour:
return int(duration / (30 * time.Minute))
case duration <= 48*time.Hour:
return int(duration / (60 * time.Minute))
default:
return 50
}
}

Expand All @@ -601,6 +625,7 @@ func (g BandGraph) Copy() BandGraph {
DataPoints: make([]BandScore, len(g.DataPoints)),
Max: g.Max,
startTime: g.startTime,
duration: g.duration,
binSeconds: g.binSeconds,
}

Expand Down Expand Up @@ -632,6 +657,7 @@ func (g *BandGraph) Add(timestamp time.Time, score QSOScore) {
g.Max = g.Max.Max(bandScore)
}

// Bindex returns the index of the bin corresponding to the given timestamp.
func (g *BandGraph) Bindex(timestamp time.Time) int {
if g.startTime.IsZero() {
return 0
Expand Down Expand Up @@ -668,6 +694,41 @@ func (g BandGraph) ScaleHourlyGoalToBin(goal int) float64 {
return (g.binSeconds / 3600.0) * float64(goal)
}

func (g BandGraph) ElapsedTime(timestamp time.Time) time.Duration {
if g.startTime.IsZero() {
return 0
}
if timestamp.IsZero() {
return 0
}
if timestamp.Before(g.startTime) {
return 0
}

return timestamp.Sub(g.startTime)
}

func (g BandGraph) ElapsedTimePercent(timestamp time.Time) float64 {
if g.startTime.IsZero() {
return 0
}
if g.duration == 0 {
return 0
}
if timestamp.IsZero() {
return 0
}
if timestamp.Before(g.startTime) {
return 0
}

return float64(g.ElapsedTime(timestamp)) / float64(g.duration)
}

func (g BandGraph) PercentAsDuration(percent float64) time.Duration {
return time.Duration(float64(g.duration) * percent)
}

type BandScore struct {
QSOs int
Duplicates int
Expand Down
8 changes: 4 additions & 4 deletions core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ func TestBandGraph_Bindex(t *testing.T) {
{2 * time.Hour, -1 * time.Second, -1},
{2 * time.Hour, 0, 0},
{2 * time.Hour, 1 * time.Second, 0},
{2 * time.Hour, 1*time.Hour - 1*time.Second, 29},
{2 * time.Hour, 1 * time.Hour, 30},
{2 * time.Hour, 1*time.Hour + 1*time.Second, 30},
{2 * time.Hour, 2*time.Hour - 1*time.Second, 59},
{2 * time.Hour, 1*time.Hour - 1*time.Second, 11},
{2 * time.Hour, 1 * time.Hour, 12},
{2 * time.Hour, 1*time.Hour + 1*time.Second, 12},
{2 * time.Hour, 2*time.Hour - 1*time.Second, 23},
{2 * time.Hour, 2 * time.Hour, -1},
{2 * time.Hour, 2*time.Hour + 1*time.Second, -1},
}
Expand Down
Loading
Loading