Skip to content

Commit

Permalink
[ABAP] update ConvertTime (#4919)
Browse files Browse the repository at this point in the history
* update ConvertTime

* clean up

* fix typo

---------

Co-authored-by: Daniel Mieg <[email protected]>
  • Loading branch information
doldsimo and DanielMieg authored May 8, 2024
1 parent 6456f61 commit ff9b3d4
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 40 deletions.
12 changes: 0 additions & 12 deletions pkg/abaputils/abaputils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -244,17 +243,6 @@ func GetErrorDetailsFromResponse(resp *http.Response) (errorString string, error

}

// ConvertTime formats an ABAP timestamp string from format /Date(1585576807000+0000)/ into a UNIX timestamp and returns it
func ConvertTime(logTimeStamp string) time.Time {
seconds := strings.TrimPrefix(strings.TrimSuffix(logTimeStamp, "000+0000)/"), "/Date(")
n, error := strconv.ParseInt(seconds, 10, 64)
if error != nil {
return time.Unix(0, 0).UTC()
}
t := time.Unix(n, 0).UTC()
return t
}

// AddDefaultDashedLine adds 25 dashes
func AddDefaultDashedLine(j int) {
for i := 1; i <= j; i++ {
Expand Down
21 changes: 0 additions & 21 deletions pkg/abaputils/abaputils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,27 +271,6 @@ func TestReadServiceKeyAbapEnvironment(t *testing.T) {
})
}

func TestTimeConverter(t *testing.T) {
t.Run("Test example time", func(t *testing.T) {
inputDate := "/Date(1585576809000+0000)/"
expectedDate := "2020-03-30 14:00:09 +0000 UTC"
result := ConvertTime(inputDate)
assert.Equal(t, expectedDate, result.String(), "Dates do not match after conversion")
})
t.Run("Test Unix time", func(t *testing.T) {
inputDate := "/Date(0000000000000+0000)/"
expectedDate := "1970-01-01 00:00:00 +0000 UTC"
result := ConvertTime(inputDate)
assert.Equal(t, expectedDate, result.String(), "Dates do not match after conversion")
})
t.Run("Test unexpected format", func(t *testing.T) {
inputDate := "/Date(0012300000001+0000)/"
expectedDate := "1970-01-01 00:00:00 +0000 UTC"
result := ConvertTime(inputDate)
assert.Equal(t, expectedDate, result.String(), "Dates do not match after conversion")
})
}

func TestHandleHTTPError(t *testing.T) {
t.Run("Test", func(t *testing.T) {

Expand Down
14 changes: 7 additions & 7 deletions pkg/abaputils/manageGitRepositoryUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func PrintLogs(api SoftwareComponentApiInterface) {
return results[i].Index < results[j].Index
})

printOverview(results)
printOverview(results, api)

// Print Details
for _, logEntryForDetails := range results {
Expand All @@ -80,7 +80,7 @@ func printExecutionLogs(executionLogs ExecutionLog) {
AddDefaultDashedLine(1)
}

func printOverview(results []LogResultsV2) {
func printOverview(results []LogResultsV2, api SoftwareComponentApiInterface) {

logOutputPhaseLength, logOutputLineLength := calculateLenghts(results)

Expand All @@ -93,7 +93,7 @@ func printOverview(results []LogResultsV2) {
printDashedLine(logOutputLineLength)

for _, logEntry := range results {
log.Entry().Infof("| %-"+fmt.Sprint(logOutputPhaseLength)+"s | %"+fmt.Sprint(logOutputStatusLength)+"s | %-"+fmt.Sprint(logOutputTimestampLength)+"s |", logEntry.Name, logEntry.Status, ConvertTime(logEntry.Timestamp))
log.Entry().Infof("| %-"+fmt.Sprint(logOutputPhaseLength)+"s | %"+fmt.Sprint(logOutputStatusLength)+"s | %-"+fmt.Sprint(logOutputTimestampLength)+"s |", logEntry.Name, logEntry.Status, api.ConvertTime(logEntry.Timestamp))
}
printDashedLine(logOutputLineLength)
}
Expand All @@ -117,7 +117,7 @@ func printDashedLine(i int) {
func printLog(logOverviewEntry LogResultsV2, api SoftwareComponentApiInterface) {

page := 0
printHeader(logOverviewEntry)
printHeader(logOverviewEntry, api)
for {
logProtocols, count, err := api.GetLogProtocol(logOverviewEntry, page)
printLogProtocolEntries(logOverviewEntry, logProtocols)
Expand Down Expand Up @@ -149,16 +149,16 @@ func allLogsHaveBeenPrinted(protocols []LogProtocol, page int, count int, err er
return (err != nil || allPagesHaveBeenRead || reflect.DeepEqual(protocols, []LogProtocol{}))
}

func printHeader(logEntry LogResultsV2) {
func printHeader(logEntry LogResultsV2, api SoftwareComponentApiInterface) {
if logEntry.Status != `Success` {
log.Entry().Infof("\n")
AddDefaultDashedLine(1)
log.Entry().Infof("%s (%v)", logEntry.Name, ConvertTime(logEntry.Timestamp))
log.Entry().Infof("%s (%v)", logEntry.Name, api.ConvertTime(logEntry.Timestamp))
AddDefaultDashedLine(1)
} else {
log.Entry().Debugf("\n")
AddDebugDashedLine()
log.Entry().Debugf("%s (%v)", logEntry.Name, ConvertTime(logEntry.Timestamp))
log.Entry().Debugf("%s (%v)", logEntry.Name, api.ConvertTime(logEntry.Timestamp))
AddDebugDashedLine()
}
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/abaputils/sap_com_0510.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,14 @@ func (api *SAP_COM_0510) getLogProtocolQuery(page int) string {

return fmt.Sprintf("?$skip=%s&$top=%s&$inlinecount=allpages", fmt.Sprint(skip), fmt.Sprint(top))
}

// ConvertTime formats an ABAP timestamp string from format /Date(1585576807000+0000)/ into a UNIX timestamp and returns it
func (api *SAP_COM_0510) ConvertTime(logTimeStamp string) time.Time {
seconds := strings.TrimPrefix(strings.TrimSuffix(logTimeStamp, "000+0000)/"), "/Date(")
n, error := strconv.ParseInt(seconds, 10, 64)
if error != nil {
return time.Unix(0, 0).UTC()
}
t := time.Unix(n, 0).UTC()
return t
}
24 changes: 24 additions & 0 deletions pkg/abaputils/sap_com_0510_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,3 +481,27 @@ func TestSleepTime(t *testing.T) {
assert.ErrorContains(t, err, "Exceeded max sleep time")
})
}

func TestTimeConverter(t *testing.T) {

api := SAP_COM_0510{}

t.Run("Test example time", func(t *testing.T) {
inputDate := "/Date(1585576809000+0000)/"
expectedDate := "2020-03-30 14:00:09 +0000 UTC"
result := api.ConvertTime(inputDate)
assert.Equal(t, expectedDate, result.String(), "Dates do not match after conversion")
})
t.Run("Test Unix time", func(t *testing.T) {
inputDate := "/Date(0000000000000+0000)/"
expectedDate := "1970-01-01 00:00:00 +0000 UTC"
result := api.ConvertTime(inputDate)
assert.Equal(t, expectedDate, result.String(), "Dates do not match after conversion")
})
t.Run("Test unexpected format", func(t *testing.T) {
inputDate := "/Date(0012300000001+0000)/"
expectedDate := "1970-01-01 00:00:00 +0000 UTC"
result := api.ConvertTime(inputDate)
assert.Equal(t, expectedDate, result.String(), "Dates do not match after conversion")
})
}
9 changes: 9 additions & 0 deletions pkg/abaputils/sap_com_0948.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,12 @@ func (api *SAP_COM_0948) getLogProtocolQuery(page int) string {

return fmt.Sprintf("?$skip=%s&$top=%s&$count=true", fmt.Sprint(skip), fmt.Sprint(top))
}

// ConvertTime formats an ISO 8601 timestamp string from format 2024-05-02T09:25:40Z into a UNIX timestamp and returns it
func (api *SAP_COM_0948) ConvertTime(logTimeStamp string) time.Time {
t, error := time.Parse(time.RFC3339, logTimeStamp)
if error != nil {
return time.Unix(0, 0).UTC()
}
return t
}
24 changes: 24 additions & 0 deletions pkg/abaputils/sap_com_0948_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,30 @@ func TestSleepTime0948(t *testing.T) {
})
}

func TestTimeConverter0948(t *testing.T) {

api := SAP_COM_0948{}

t.Run("Test example time", func(t *testing.T) {
inputDate := "2024-05-02T09:25:40Z"
expectedDate := "2024-05-02 09:25:40 +0000 UTC"
result := api.ConvertTime(inputDate)
assert.Equal(t, expectedDate, result.String(), "Dates do not match after conversion")
})
t.Run("Test Unix time", func(t *testing.T) {
inputDate := "2023-12-24T16:19:29.000Z"
expectedDate := "2023-12-24 16:19:29 +0000 UTC"
result := api.ConvertTime(inputDate)
assert.Equal(t, expectedDate, result.String(), "Dates do not match after conversion")
})
t.Run("Test unexpected format", func(t *testing.T) {
inputDate := "2024-05-02T09:254:40Z"
expectedDate := "1970-01-01 00:00:00 +0000 UTC"
result := api.ConvertTime(inputDate)
assert.Equal(t, expectedDate, result.String(), "Dates do not match after conversion")
})
}

func TestGetExecutionLog(t *testing.T) {
t.Run("Test Get Executionlog Success", func(t *testing.T) {

Expand Down
1 change: 1 addition & 0 deletions pkg/abaputils/softwareComponentApiManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type SoftwareComponentApiInterface interface {
CreateTag(tag Tag) error
GetLogOverview() ([]LogResultsV2, error)
GetLogProtocol(LogResultsV2, int) (result []LogProtocol, count int, err error)
ConvertTime(logTimeStamp string) time.Time
GetExecutionLog() (ExecutionLog, error)
}

Expand Down

0 comments on commit ff9b3d4

Please sign in to comment.