Skip to content

Commit

Permalink
Merge pull request #196 from matt-mazzucato/fix-get-samples-and-data-…
Browse files Browse the repository at this point in the history
…snapshot

Fix get-samples and data-snapshot
  • Loading branch information
Annopaolo authored May 22, 2023
2 parents 47f9d65 + 1b65daa commit 5a55c89
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 45 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Fixed
- `appengine data-snapshot` properly gathers and shows data snapshots from a device.
- context: do not warn when config is missing. Users will have to provide parameters by hand.
- `appengine get-samples` properly gathers and show samples from a device.

## [22.11.01] - 2023-03-15
### Added
Expand Down
155 changes: 115 additions & 40 deletions cmd/appengine/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -825,13 +825,14 @@ func devicesGetSamplesF(command *cobra.Command, args []string) error {
isAggregate = forceAggregate
}

// prepare some helper variables, they will come handy for data visualization
sliceAcc := []any{}
mapAcc := map[string]any{}

// We are good to go.
t := tableWriterForOutputType(outputType)
if !isAggregate {
// Go with the table header
t.AppendHeader(table.Row{"Timestamp", "Value"})
printedValues := 0
jsonOutput := []client.DatastreamIndividualValue{}
datastreamPaginator, err := astarteAPIClient.GetDatastreamIndividualTimeWindowPaginator(realm, deviceID,
deviceIdentifierType, interfaceName, interfacePath, sinceTime, toTime, resultSetOrder, 100)
if err != nil {
Expand All @@ -849,32 +850,58 @@ func devicesGetSamplesF(command *cobra.Command, args []string) error {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

rawPage, err := nextPageRes.Parse()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
page, _ := rawPage.([]client.DatastreamIndividualValue)

if outputType == "json" {
jsonOutput = append(jsonOutput, page...)
} else {
switch rawPage.(type) {
case []client.DatastreamIndividualValue:
page, _ := rawPage.([]client.DatastreamIndividualValue)

// Go with the table header regardless of the requested output type
t.AppendHeader(table.Row{"Timestamp", "Value"})

// and start appending values
for _, v := range page {
t.AppendRow([]interface{}{timestampForOutput(v.Timestamp, outputType), v.Value})
if outputType != "json" {
t.AppendRow([]interface{}{timestampForOutput(v.Timestamp, outputType), v.Value})
} else {
sliceAcc = append(sliceAcc, v)
}
printedValues++
if printedValues >= limit && limit > 0 {
renderOutput(t, jsonOutput, outputType)
renderOutput(t, sliceAcc, outputType)
return nil
}
}
renderOutput(t, sliceAcc, outputType)

case map[string]client.DatastreamIndividualValue:
page, _ := rawPage.(map[string]client.DatastreamIndividualValue)

// Go with the table header regardless of the requested output type
t.AppendHeader(table.Row{"Path", "Timestamp", "Value"})

// and start appending values
for k, v := range page {
if outputType != "json" {
t.AppendRow([]interface{}{k, timestampForOutput(v.Timestamp, outputType), v.Value})
} else {
mapAcc[k] = v
}
printedValues++
if printedValues >= limit && limit > 0 {
renderOutput(t, mapAcc, outputType)
return nil
}
}
renderOutput(t, mapAcc, outputType)
}
}
renderOutput(t, jsonOutput, outputType)
} else {
headerRow := table.Row{"Timestamp"}
headerPrinted := false

jsonOutput := []client.DatastreamObjectValue{}
printedValues := 0
datastreamPaginator, err := astarteAPIClient.GetDatastreamObjectTimeWindowPaginator(realm, deviceID, deviceIdentifierType, interfaceName, interfacePath,
sinceTime, toTime, resultSetOrder, 100)
Expand All @@ -898,42 +925,90 @@ func devicesGetSamplesF(command *cobra.Command, args []string) error {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
page, _ := rawPage.([]client.DatastreamObjectValue)

if outputType == "json" {
jsonOutput = append(jsonOutput, page...)
} else {
switch rawPage.(type) {
case []client.DatastreamObjectValue:
headerRow := table.Row{"Timestamp"}
headerPrinted := false

page, _ := rawPage.([]client.DatastreamObjectValue)

for _, v := range page {
// Iterate the aggregate
line := []interface{}{}
line = append(line, timestampForOutput(v.Timestamp, outputType))
for _, path := range v.Values.Keys() {
value, _ := v.Values.Get(path)
if !headerPrinted {
headerRow = append(headerRow, path)
if outputType != "json" {
// Iterate the aggregate
line := []interface{}{}
line = append(line, timestampForOutput(v.Timestamp, outputType))
for _, path := range v.Values.Keys() {
value, _ := v.Values.Get(path)
if !headerPrinted {
headerRow = append(headerRow, path)
}
if value != nil {
line = append(line, value)
} else {
line = append(line, "(null)")
}
}
if value != nil {
line = append(line, value)
} else {
line = append(line, "(null)")
if !headerPrinted {
t.AppendHeader(headerRow)
headerPrinted = true
}
t.AppendRow(line)
} else {
sliceAcc = append(sliceAcc, v)
}
if !headerPrinted {
t.AppendHeader(headerRow)
headerPrinted = true
}
t.AppendRow(line)
printedValues++
if printedValues >= limit && limit > 0 {
renderOutput(t, jsonOutput, outputType)
renderOutput(t, sliceAcc, outputType)
return nil
}
}
renderOutput(t, sliceAcc, outputType)

case map[string][]client.DatastreamObjectValue:
headerRow := table.Row{"Base path", "Timestamp"}
headerPrinted := false

page, _ := rawPage.(map[string][]client.DatastreamObjectValue)

keys := []string{}
for k, v := range page {
for _, item := range v {
if outputType != "json" {
line := []interface{}{}
if !headerPrinted {
for _, path := range item.Values.Keys() {
keys = append(keys, path)
headerRow = append(headerRow, path)
}
t.AppendHeader(headerRow)
headerPrinted = true
}
line = append(line, k)
line = append(line, timestampForOutput(item.Timestamp, outputType))
for _, key := range keys {
value, _ := item.Values.Get(key)
if value != nil {
line = append(line, value)
} else {
line = append(line, "(null)")
}
}
t.AppendRow(line)
} else {
mapAcc[k] = v
}
printedValues++
if printedValues >= limit && limit > 0 {
renderOutput(t, mapAcc, outputType)
return nil
}
}
}
renderOutput(t, mapAcc, outputType)
}
}
renderOutput(t, jsonOutput, outputType)
}

return nil
}

Expand Down Expand Up @@ -1183,15 +1258,15 @@ func timestampForOutput(timestamp time.Time, outputType string) string {
return ""
}

func renderOutput(t table.Writer, jsonOutput interface{}, outputType string) {
func renderOutput(t table.Writer, accumulator interface{}, outputType string) {
switch outputType {
case "default":
t.Render()
case "csv":
t.RenderCSV()
case "json":
respJSON, _ := json.MarshalIndent(jsonOutput, "", " ")
fmt.Println(string(respJSON))
marshaledOutput, _ := json.MarshalIndent(accumulator, "", " ")
fmt.Println(string(marshaledOutput))
}
}

Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
code.cloudfoundry.org/bytefmt v0.0.0-20211005130812-5bb3c17173e5
github.com/Masterminds/semver/v3 v3.1.1
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de
github.com/astarte-platform/astarte-go v0.90.5-0.20230323110518-62761dbc87f2
github.com/astarte-platform/astarte-go v0.90.5-0.20230522073615-9c590d8a9ff6
github.com/go-openapi/strfmt v0.21.1 // indirect
github.com/google/go-cmp v0.5.8
github.com/google/go-github/v30 v30.1.0
Expand Down Expand Up @@ -47,14 +47,15 @@ require (
github.com/googleapis/gnostic v0.5.5 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/iancoleman/orderedmap v0.2.0 // indirect
github.com/imdario/mergo v0.3.5 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mattn/go-runewidth v0.0.10 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/nqd/flat v0.2.0 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/rivo/uniseg v0.1.0 // indirect
Expand Down
9 changes: 6 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgI
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef h1:46PFijGLmAjMPwCCCo7Jf0W6f9slllCkkv7vyc1yOSg=
github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
github.com/astarte-platform/astarte-go v0.90.5-0.20230323110518-62761dbc87f2 h1:wXwbDzGktjvdtaUIG/yC9VmL35SIIiwNCKfak/idy1w=
github.com/astarte-platform/astarte-go v0.90.5-0.20230323110518-62761dbc87f2/go.mod h1:h5Kmuogn6yDrclFQmbbau+rX6pkOblF9gnyhIFTGw1w=
github.com/astarte-platform/astarte-go v0.90.5-0.20230522073615-9c590d8a9ff6 h1:7zEWr8mTVoA31rjBj5dACag1YCccDyD1yzao9UjXrp8=
github.com/astarte-platform/astarte-go v0.90.5-0.20230522073615-9c590d8a9ff6/go.mod h1:6e/IkwjAS7fXdCerA/xr/Fcv6OseNRhDFytuhCGfjvM=
github.com/benbjohnson/clock v1.0.3/go.mod h1:bGMdMPoPVvcYyt1gHDf4J2KE153Yf9BuiUKYMaxlTDM=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
Expand Down Expand Up @@ -358,8 +358,9 @@ github.com/iancoleman/orderedmap v0.2.0/go.mod h1:N0Wam8K1arqPXNWjMo21EXnBPOPp36
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/imdario/mergo v0.3.5 h1:JboBksRwiiAJWvIYJVo46AfV+IAIKZpfrSzVKj42R4Q=
github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jedib0t/go-pretty v4.3.0+incompatible h1:CGs8AVhEKg/n9YbUenWmNStRW2PHJzaeDodcfvRAbIo=
Expand Down Expand Up @@ -451,6 +452,8 @@ github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRW
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nqd/flat v0.2.0 h1:g6lXtMxsxrz6PZOO+rNnAJUn/GGRrK4FgVEhy/v+cHI=
github.com/nqd/flat v0.2.0/go.mod h1:FOuslZmNY082wVfVUUb7qAGWKl8z8Nor9FMg+Xj2Nss=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
Expand Down

0 comments on commit 5a55c89

Please sign in to comment.