Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RSDK-8819: Finish FTDC #4579

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 33 additions & 9 deletions ftdc/cmd/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ package main

import (
"bufio"
"cmp"
"errors"
"fmt"
"io"
"math"
"os"
"os/exec"
"slices"
"strings"
"time"

"go.viam.com/utils"

"go.viam.com/rdk/ftdc"
"go.viam.com/rdk/logging"
)

// gnuplotWriter organizes all of the output for `gnuplot` to create a graph from FTDC
Expand All @@ -36,6 +39,31 @@ type gnuplotWriter struct {
options graphOptions
}

type KVPair[K, V any] struct {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expect you'll enjoy this @benjirewis

Key K
Val V
}

func sorted[K cmp.Ordered, V any](mp map[K]V) []KVPair[K, V] {
ret := make([]KVPair[K, V], 0, len(mp))
for key, val := range mp {
ret = append(ret, KVPair[K, V]{key, val})
}

slices.SortFunc(ret, func(left, right KVPair[K, V]) int {
if left.Key < right.Key {
return -1
}
if right.Key < left.Key {
return 1
}

return 0
})

return ret
}

type graphOptions struct {
// minTimeSeconds and maxTimeSeconds control which datapoints should render based on their
// timestamp. The default is all datapoints (minTimeSeconds: 0, maxTimeSeconds: MaxInt64).
Expand Down Expand Up @@ -155,7 +183,8 @@ func (gpw *gnuplotWriter) CompileAndClose() string {
// per-graph setting rather than a global.
writeln(gnuFile, "set yrange [0:*]")

for metricName, file := range gpw.metricFiles {
for _, nameFilePair := range sorted(gpw.metricFiles) {
metricName, file := nameFilePair.Key, nameFilePair.Val
writelnf(gnuFile, "plot '%v' using 1:2 with lines linestyle 7 lw 4 title '%v'", file.Name(), strings.ReplaceAll(metricName, "_", "\\_"))
utils.UncheckedErrorFunc(file.Close)
}
Expand All @@ -165,25 +194,20 @@ func (gpw *gnuplotWriter) CompileAndClose() string {

func main() {
if len(os.Args) < 2 {
// We are a CLI, it's appropriate to write to stdout.
//

nolintPrintln("Expected an FTDC filename. E.g: go run parser.go <path-to>/viam-server.ftdc")
return
}

ftdcFile, err := os.Open(os.Args[1])
if err != nil {
// We are a CLI, it's appropriate to write to stdout.
//

nolintPrintln("Error opening file. File:", os.Args[1], "Err:", err)

nolintPrintln("Expected an FTDC filename. E.g: go run parser.go <path-to>/viam-server.ftdc")
return
}

data, err := ftdc.Parse(ftdcFile)
logger := logging.NewDebugLogger("parser")
logger.SetLevel(logging.WARN)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced with a simple NewLogger and removed the SetLevel -- using the INFO default.

data, err := ftdc.ParseWithLogger(ftdcFile, logger)
if err != nil {
panic(err)
}
Expand Down
13 changes: 12 additions & 1 deletion ftdc/custom_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,9 @@ func ParseWithLogger(rawReader io.Reader, logger logging.Logger) ([]FlatDatum, e
// "packed byte" where the first bit is not a diff bit. `readDiffBits` must account for
// that.
diffedFieldsIndexes := readDiffBits(reader, schema)
logger.Debugw("Diff bits", "changedFields", diffedFieldsIndexes)
logger.Debugw("Diff bits",
"changedFieldIndexes", diffedFieldsIndexes,
"changedFieldNames", schema.FieldNamesForIndexes(diffedFieldsIndexes))

// The next eight bytes after the diff bits is the time in nanoseconds since the 1970 epoch.
var dataTime int64
Expand Down Expand Up @@ -653,3 +655,12 @@ func (schema *schema) Zip(data []float32) []Reading {

return ret
}

func (schema *schema) FieldNamesForIndexes(fieldIdxs []int) []string {
ret := make([]string, len(fieldIdxs))
for idx, fieldIdx := range fieldIdxs {
ret[idx] = schema.fieldOrder[fieldIdx]
}

return ret
}