Skip to content

Commit

Permalink
refactor(changetracking): move timestamp millisecond function to help…
Browse files Browse the repository at this point in the history
…ers/utils
  • Loading branch information
pranav-new-relic committed Jun 5, 2024
1 parent afea6a0 commit b6f4019
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 14 deletions.
21 changes: 21 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package utils
import (
"strconv"
"strings"
"time"

"github.com/newrelic/newrelic-client-go/v2/pkg/nrtime"
)

// IntArrayToString converts an array of integers
Expand All @@ -17,3 +20,21 @@ func IntArrayToString(integers []int) string {

return strings.Join(sArray, ",")
}

// a helper function that is used to populate a timestamp with non-zero milliseconds
// if the millisecond count has been found zero, usually when generated with time.Time()
// in Golang which does not have a nanoseconds field; which helps mutations such as those
// in changetracking, the API of which requires a timestamp with non-zero milliseconds.
func GetSafeTimestampWithMilliseconds(inputTimestamp nrtime.EpochMilliseconds) nrtime.EpochMilliseconds {
timestamp := time.Time(inputTimestamp)

// since time.Time in Go does not have a milliseconds field, which is why the implementation
// of unmarshaling time.Time into a Unix timestamp in the serialization package relies on
// nanoseconds to produce a value of milliseconds, we try employing a similar logic below

if timestamp.Nanosecond() < 100000000 {
timestamp = timestamp.Add(time.Nanosecond * 100000000)
}

return nrtime.EpochMilliseconds(timestamp)
}
77 changes: 77 additions & 0 deletions internal/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ package utils

import (
"testing"
"time"

"github.com/newrelic/newrelic-client-go/v2/pkg/nrtime"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestIntArrayToString(t *testing.T) {
Expand All @@ -25,3 +28,77 @@ func TestIntArrayToString(t *testing.T) {
result = IntArrayToString([]int{1, 2, 3, 4})
assert.Equal(t, "1,2,3,4", result)
}

func TestGetSafeTimestampWithMilliseconds_ZeroNanoseconds(t *testing.T) {
t.Parallel()

now := time.Now()
actualNanoseconds := 0
expectedNanoseconds := actualNanoseconds + 100000000

actualResult := GetSafeTimestampWithMilliseconds(nrtime.EpochMilliseconds(
time.Date(
now.Year(),
now.Month(),
now.Day(),
now.Hour()-3,
now.Minute()-30,
0,
actualNanoseconds,
time.Local,
),
))

expectedResult := nrtime.EpochMilliseconds(
time.Date(
now.Year(),
now.Month(),
now.Day(),
now.Hour()-3,
now.Minute()-30,
0,
expectedNanoseconds,
time.Local,
),
)
require.Equal(t, actualResult, expectedResult)
}

func TestGetSafeTimestampWithMilliseconds_NonZeroNanoseconds(t *testing.T) {
t.Parallel()

now := time.Now()
actualNanoseconds := 123000000

// equal, since actualNanoSeconds > 100000000
expectedNanoseconds := actualNanoseconds

actualResult := GetSafeTimestampWithMilliseconds(nrtime.EpochMilliseconds(
time.Date(
now.Year(),
now.Month(),
now.Day(),
now.Hour()-3,
now.Minute()-30,
0,
actualNanoseconds,
time.Local,
),
))

expectedResult := nrtime.EpochMilliseconds(
time.Date(
now.Year(),
now.Month(),
now.Day(),
now.Hour()-3,
now.Minute()-30,
0,
expectedNanoseconds,
time.Local,
),
)

require.Equal(t, actualResult, expectedResult)

}
17 changes: 3 additions & 14 deletions pkg/changetracking/changetracking_api_utilities.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
package changetracking

import (
"time"

"github.com/newrelic/newrelic-client-go/v2/pkg/nrtime"
"github.com/newrelic/newrelic-client-go/v2/internal/utils"
)

// DO NOT DELETE the following function - this is not covered by Tutone
// but is needed to ensure proper conversion of timestamps
func (input *ChangeTrackingDeploymentInput) CorrectTimestampMilliseconds() {
inputTimestamp := input.Timestamp
timestamp := time.Time(inputTimestamp)

// since time.Time in Go does not have a milliseconds field, which is why the implementation
// of unmarshaling time.Time into a Unix timestamp in the serialization package relies on
// nanoseconds to produce a value of milliseconds, we try employing a similar logic below

if timestamp.Nanosecond() < 100000000 {
timestamp = timestamp.Add(time.Nanosecond * 100000000)
}

input.Timestamp = nrtime.EpochMilliseconds(timestamp)
timestamp := utils.GetSafeTimestampWithMilliseconds(inputTimestamp)
input.Timestamp = timestamp
}

0 comments on commit b6f4019

Please sign in to comment.