Skip to content

Commit

Permalink
add function to get timestamp from gzip file name
Browse files Browse the repository at this point in the history
Since the functionality is used across pillar, newlog and edgeview code,
it is better to have it in a separate file imported by all of them.

Signed-off-by: Paul Gaiduk <[email protected]>
  • Loading branch information
europaul committed Nov 26, 2024
1 parent 047e07d commit 64218a3
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pkg/pillar/types/newlogtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
package types

import (
"fmt"
"strconv"
"strings"
"time"
)

Expand Down Expand Up @@ -81,3 +84,22 @@ type NewlogMetrics struct {
DevMetrics logfileMetrics // Device metrics
AppMetrics logfileMetrics // App metrics
}

// GetTimestampFromGzipName - get timestamp from gzip file name
func GetTimestampFromGzipName(fName string) (time.Time, error) {
// here are example file names:
// app.6656f860-7563-4bbf-8bba-051f5942982b.log.1730464687367.gz
// dev.log.keep.1730404601953.gz
// dev.log.upload.1730404601953.gz
// the timestamp is the number between the last two dots
nameParts := strings.Split(fName, ".")
if len(nameParts) < 2 {
return time.Time{}, fmt.Errorf("getTimestampFromGzipName: invalid log file name %s", fName)
}
timeStr := nameParts[len(nameParts)-2]
fTime, err := strconv.Atoi(timeStr)
if err != nil {
return time.Time{}, fmt.Errorf("getTimestampFromGzipName: %w", err)
}
return time.Unix(0, int64(fTime)*int64(time.Millisecond)), nil
}

0 comments on commit 64218a3

Please sign in to comment.