Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to choose interpolation type in grapher #120

Merged
merged 2 commits into from
Mar 1, 2025
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
102 changes: 69 additions & 33 deletions pkg/grapher/grapher.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
// GraphType is used to define the type of graph you need.
type GraphType int

// InterpolationType is used to define the type of interpolation you need.
type InterpolationType int

// Numeric is a union of all numeric types.
type Numeric interface {
int | int64 | float64
Expand All @@ -26,27 +29,40 @@
CumulativeSameDate GraphType = iota
)

const (
// None InterpolationType provides a grapher that won't interpolate.
None InterpolationType = iota
// Zero InterpolationType provides a grapher that will interpolate using value zero.
Zero InterpolationType = iota
// PreviousValue InterpolationType provides a grapher
// that will interpolate using the previous value.
PreviousValue InterpolationType = iota
)

// Grapher is used to easily create graphs.
type Grapher[T Numeric] struct {
graphType GraphType
dateFormat string
dateGranularity time.Duration
dateStrings []string
values map[string][]T
graphType GraphType
interpolationType InterpolationType
dateFormat string
dateGranularity time.Duration
dateStrings []string
values map[string][]T
}

// New returns a new Grapher.
func New[T Numeric](
graphType GraphType,
interpolationType InterpolationType,
dateFormat string,
dateGranularity time.Duration,
) *Grapher[T] {
return &Grapher[T]{
graphType: graphType,
dateFormat: dateFormat,
dateGranularity: dateGranularity,
dateStrings: []string{},
values: make(map[string][]T),
graphType: graphType,
interpolationType: interpolationType,
dateFormat: dateFormat,
dateGranularity: dateGranularity,
dateStrings: []string{},
values: make(map[string][]T),
}
}

Expand Down Expand Up @@ -90,41 +106,61 @@
grapher.dateStrings[len(grapher.dateStrings)-1],
)

if grapher.interpolationType == None {
if dateDay.Before(smallestDate) {
grapher.addDateBefore(dateDay, *new(T))

Check warning on line 111 in pkg/grapher/grapher.go

View check run for this annotation

Codecov / codecov/patch

pkg/grapher/grapher.go#L111

Added line #L111 was not covered by tests
} else if dateDay.After(largestDate) {
grapher.addDateAfter(dateDay)
}
return
}

i := smallestDate
for i.After(dateDay) {
i = i.Add(-1 * grapher.dateGranularity)

grapher.dateStrings = append(
[]string{i.Format(grapher.dateFormat)},
grapher.dateStrings...)

for label := range grapher.values {
grapher.values[label] = append(
[]T{*new(T)},
grapher.values[label]...)
}
grapher.addDateBefore(i, *new(T))
}

i = largestDate
for i.Before(dateDay) {
i = i.Add(grapher.dateGranularity)
grapher.addDateAfter(i)
}
}

grapher.dateStrings = append(
grapher.dateStrings,
i.Format(grapher.dateFormat),
)
func (grapher *Grapher[T]) addDateBefore(date time.Time, value T) {
grapher.dateStrings = append(
[]string{date.Format(grapher.dateFormat)},
grapher.dateStrings...)

indexOfI := slices.Index(
grapher.dateStrings,
i.Format(grapher.dateFormat),
)
for label := range grapher.values {
grapher.values[label] = append(
[]T{value},
grapher.values[label]...)
}
}

func (grapher *Grapher[T]) addDateAfter(date time.Time) {
grapher.dateStrings = append(
grapher.dateStrings,
date.Format(grapher.dateFormat),
)

for label := range grapher.values {
grapher.values[label] = append(
grapher.values[label],
grapher.values[label][indexOfI-1],
)
indexOfI := slices.Index(
grapher.dateStrings,
date.Format(grapher.dateFormat),
)

for label := range grapher.values {
value := grapher.values[label][indexOfI-1]
if grapher.interpolationType == Zero || grapher.interpolationType == None {
value = *new(T)
}

grapher.values[label] = append(
grapher.values[label],
value,
)
}
}

Expand Down
16 changes: 13 additions & 3 deletions pkg/grapher/grapher_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import (
)

func TestGrapherCumulative(t *testing.T) {
grapher := grapher.New[int](grapher.Cumulative, "2006-01-02", 24*time.Hour)
grapher := grapher.New[int](
grapher.Cumulative,
grapher.PreviousValue,
"2006-01-02",
24*time.Hour,
)

dateNow := time.Now().UTC()
for i := 0; i < 10; i++ {
Expand All @@ -37,7 +42,12 @@ func TestGrapherCumulative(t *testing.T) {
}

func TestGrapherNormal(t *testing.T) {
grapher := grapher.New[int](grapher.Normal, "2006-01-02", 24*time.Hour)
grapher := grapher.New[int](
grapher.Normal,
grapher.PreviousValue,
"2006-01-02",
24*time.Hour,
)

dateNow := time.Now().UTC()
for i := 0; i < 10; i++ {
Expand All @@ -60,7 +70,7 @@ func TestGrapherNormal(t *testing.T) {
}

func TestGrapherNormalSeconds(t *testing.T) {
grapher := grapher.New[int](grapher.Normal, time.RFC3339, time.Second)
grapher := grapher.New[int](grapher.Normal, grapher.None, time.RFC3339, time.Second)

dateNow := time.Now().UTC()
for i := 0; i < 10; i++ {
Expand Down
Loading