Skip to content

Commit

Permalink
fix time formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
minj131 committed Feb 28, 2024
1 parent 90eb9e3 commit f45e938
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 39 deletions.
18 changes: 6 additions & 12 deletions pkg/fileutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,17 @@ func StartLoadDynamicFile(filename string, callBack FileChangeCallBack, stopCh <
}, time.Second, stopCh)
}

func CalculateTimeDeltaFromUnixInSeconds(from, to string) (float64, error) {
parsedFrom, err := strconv.ParseInt(from, 10, 64)
func CalculateTimeDeltaFromUnixInSeconds(from string) (int64, error) {
startTime, err := strconv.ParseInt(from, 10, 64)
if err != nil {
return 0, fmt.Errorf("failed to parse 'from' string: %v", err)
return 0, fmt.Errorf("failed to parse 'startTime' string: %v", err)
}

parsedTo, err := strconv.ParseInt(to, 10, 64)
if err != nil {
return 0, fmt.Errorf("failed to parse 'to' string: %v", err)
}

timeFrom := time.Unix(parsedFrom, 0).UTC()
timeTo := time.Unix(parsedTo, 0).UTC()
endTime := time.Now().Unix()

if timeFrom.After(timeTo) {
if startTime > endTime {
return 0, fmt.Errorf("start timestamp is after end timestamp")
}

return timeTo.Sub(timeFrom).Seconds(), nil
return endTime - startTime, nil
}
38 changes: 20 additions & 18 deletions pkg/fileutil/util_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package fileutil

import (
"fmt"
"os"
"strconv"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -126,52 +126,54 @@ func TestDeleteDynamicFile(t *testing.T) {

func TestCalculateTimeDeltaFromUnixInSeconds(t *testing.T) {
type args struct {
from string
to string
startTime string
}
cases := []struct {
input args
want float64
errexp bool
sleep bool
}{
{
args{"1706648530", "1706648539"},
9.0,
args{"1706648530"},
false,
false,
},
{
args{"1706648520", "1706648539"},
19.0,
args{"1706648520"},
false,
false,
},
{
args{"1906648520", "1806648539"},
0,
args{"foo"},
true,
false,
},
{
args{"foo", "1806648539"},
0,
args{"2706648520"},
true,
false,
},
{
args{"1706648520", "bar"},
0,
args{strconv.FormatInt(time.Now().Unix(), 10)},
false,
true,
},
}

for _, c := range cases {
fmt.Println(c.input.from, c.input.to)
out, err := CalculateTimeDeltaFromUnixInSeconds(c.input.from, c.input.to)
if c.sleep {
time.Sleep(1 * time.Second)
}

out, err := CalculateTimeDeltaFromUnixInSeconds(c.input.startTime)
if !c.errexp && err != nil {
t.Errorf("Did not expect error but got err: %v", err)
} else if c.errexp && err == nil {
t.Error("Expected error but got nil")
}

if out != c.want {
t.Errorf("unexpected result: got %v but expected %v", out, c.want)
if !c.errexp && out < 1 {
t.Errorf("Returned an invalid value: %d", out)
}
}
}
6 changes: 2 additions & 4 deletions pkg/mapper/dynamicfile/dynamicfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package dynamicfile
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"sync"
"time"

"github.com/sirupsen/logrus"
"sigs.k8s.io/aws-iam-authenticator/pkg/arn"
Expand Down Expand Up @@ -181,11 +179,11 @@ func (ms *DynamicFileMapStore) CallBackForFileLoad(dynamicContent []byte) error
// so a workaround is to skip the first time the metric is calculated, and only emit metris after
// as we know any subsequent calculations are from a valid change upstream
if ms.dynamicFileInitDone {
latency, err := fileutil.CalculateTimeDeltaFromUnixInSeconds(dynamicFileData.LastUpdatedDateTime, strconv.FormatInt(time.Now().Unix(), 10))
latency, err := fileutil.CalculateTimeDeltaFromUnixInSeconds(dynamicFileData.LastUpdatedDateTime)
if err != nil {
logrus.Errorf("error parsing latency for dynamic file: %v", err)
} else {
metrics.Get().E2ELatency.WithLabelValues("dynamic_file").Observe(latency)
metrics.Get().E2ELatency.WithLabelValues("dynamic_file").Observe(float64(latency))
logrus.WithFields(logrus.Fields{
"Version": dynamicFileData.Version,
"Type": "dynamic_file",
Expand Down
2 changes: 0 additions & 2 deletions pkg/mapper/dynamicfile/dynamicfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ func TestAWSAccount(t *testing.T) {

var origFileContent = `
{
"ApiVersion": "1",
"Version": "1",
"LastUpdatedDateTime": "12345678",
"ClusterId": "000000000098",
Expand Down Expand Up @@ -144,7 +143,6 @@ var origFileContent = `

var updatedFileContent = `
{
"ApiVersion": "1",
"Version": "1",
"LastUpdatedDateTime": "12345678",
"ClusterId": "000000000098",
Expand Down
5 changes: 2 additions & 3 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"log"
"net/http"
"regexp"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -508,11 +507,11 @@ func (h *handler) CallBackForFileLoad(dynamicContent []byte) error {
// so a workaround is to skip the first time the metric is calculated, and only emit metris after
// as we know any subsequent calculations are from a valid change upstream
if h.backendModeConfigInitDone {
latency, err := fileutil.CalculateTimeDeltaFromUnixInSeconds(backendModes.LastUpdatedDateTime, strconv.FormatInt(time.Now().Unix(), 10))
latency, err := fileutil.CalculateTimeDeltaFromUnixInSeconds(backendModes.LastUpdatedDateTime)
if err != nil {
logrus.Errorf("error parsing latency for dynamic backend mode file: %v", err)
} else {
metrics.Get().E2ELatency.WithLabelValues("dynamic_backend_mode").Observe(latency)
metrics.Get().E2ELatency.WithLabelValues("dynamic_backend_mode").Observe(float64(latency))
logrus.WithFields(logrus.Fields{
"Version": backendModes.Version,
"Type": "dynamic_backend_mode",
Expand Down

0 comments on commit f45e938

Please sign in to comment.