Skip to content

Commit

Permalink
Add announced timestamp to devInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
nocturnalastro committed Jun 21, 2023
1 parent 1476f18 commit 0432e86
Showing 1 changed file with 39 additions and 13 deletions.
52 changes: 39 additions & 13 deletions pkg/collectors/devices/ptp_devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,24 @@ import (
)

type PTPDeviceInfo struct {
Timestamp string `json:"date" fetcherKey:"date"`
VendorID string `json:"vendorId" fetcherKey:"vendorID"`
DeviceID string `json:"deviceInfo" fetcherKey:"devID"`
GNSSDev string `json:"GNSSDev" fetcherKey:"gnss"`
Timestamp string `json:"date" fetcherKey:"date"`
VendorID string `json:"vendorId" fetcherKey:"vendorID"`
DeviceID string `json:"deviceInfo" fetcherKey:"devID"`
GNSSDev string `json:"GNSSDev" fetcherKey:"gnss"`
Timeoffset string `fetcherKey:"timeOffset"`
}

// AnalyserJSON returns the json expected by the analysers
func (ptpDevInfo *PTPDeviceInfo) GetAnalyserFormat() (*callbacks.AnalyserFormatType, error) {
offset, err := time.ParseDuration(ptpDevInfo.Timeoffset)
if err != nil {
return &callbacks.AnalyserFormatType{}, fmt.Errorf("failed to parse offset %s %w", ptpDevInfo.Timeoffset, err)
}

formatted := callbacks.AnalyserFormatType{
ID: "devInfo",
Data: []string{
time.Now().Add(offset).UTC().Format(time.RFC3339Nano),
ptpDevInfo.Timestamp,
ptpDevInfo.VendorID,
ptpDevInfo.DeviceID,
Expand Down Expand Up @@ -69,34 +76,54 @@ const (
var (
devFetcher map[string]*fetcher
dpllFetcher map[string]*fetcher
dateCmd *clients.Cmd
dpplDateCmd *clients.Cmd
devDateCmd *clients.Cmd
)

func init() {
devFetcher = make(map[string]*fetcher)
devDateCmdInst, err := clients.NewCmd("date", "date +%s.%N")
if err != nil {
panic(err)
}

devDateCmd = devDateCmdInst
devDateCmd.SetCleanupFunc(TrimSpace)

dpllFetcher = make(map[string]*fetcher)
dateCmdInst, err := clients.NewCmd("date", "date +%s.%N")
dpplDateCmdInst, err := clients.NewCmd("date", "date +%s.%N")
if err != nil {
panic(err)
}
dateCmd = dateCmdInst
dateCmd.SetCleanupFunc(FormatTimestampAsRFC3339Nano)
dpplDateCmd = dpplDateCmdInst
dpplDateCmd.SetCleanupFunc(formatTimestampAsRFC3339Nano)
}
func FormatTimestampAsRFC3339Nano(s string) (string, error) {

func formatTimestampAsRFC3339Nano(s string) (string, error) {
timestamp, err := utils.ParseTimestamp(strings.TrimSpace(s))
if err != nil {
return "", fmt.Errorf("failed to parse timestamp %w", err)
}
return timestamp.Format(time.RFC3339Nano), nil
}

func extractOffsetFromTimestamp(result map[string]string) (map[string]string, error) {
timestamp, err := utils.ParseTimestamp(result["date"])
if err != nil {
return result, fmt.Errorf("failed to parse timestamp %w", err)
}
result["date"] = timestamp.Format(time.RFC3339Nano)
result["timeOffset"] = fmt.Sprintf("%fs", time.Since(timestamp).Seconds())
return result, nil
}

// BuildPTPDeviceInfo popluates the fetcher required for
// collecting the PTPDeviceInfo
func BuildPTPDeviceInfo(interfaceName string) error {
fetcherInst := NewFetcher()
devFetcher[interfaceName] = fetcherInst

fetcherInst.AddCommand(dateCmd)
fetcherInst.SetPostProcesser(extractOffsetFromTimestamp)
fetcherInst.AddCommand(devDateCmd)

err := fetcherInst.AddNewCommand(
"gnss",
Expand Down Expand Up @@ -158,8 +185,7 @@ func GetPTPDeviceInfo(interfaceName string, ctx clients.ContainerContext) (PTPDe
func BuildDPLLInfoFetcher(interfaceName string) error {
fetcherInst := NewFetcher()
dpllFetcher[interfaceName] = fetcherInst

fetcherInst.AddCommand(dateCmd)
fetcherInst.AddCommand(dpplDateCmd)

err := fetcherInst.AddNewCommand(
"dpll_0_state",
Expand Down

0 comments on commit 0432e86

Please sign in to comment.