Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
149 changes: 18 additions & 131 deletions cmd/events/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import (
"encoding/json"
"fmt"
"net/http"
"os"
"time"

"github.com/dydx/vico-cli/pkg/auth"
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -45,11 +43,6 @@ type Event struct {

var hours int
var outputFormat string
var outputDestination string
var influxURL string
var influxOrg string
var influxBucket string
var influxToken string

// listCmd represents the command to list events from the Vicohome API.
// It allows users to fetch events from a specified number of hours in the past,
Expand Down Expand Up @@ -84,137 +77,38 @@ var listCmd = &cobra.Command{
return
}

// Display events based on output destination and format
// Display events
if len(events) == 0 {
fmt.Println("No events found in the specified time period.")
return
}

if outputDestination == "influxdb" {
// Write to InfluxDB
if err := writeToInfluxDB(events); err != nil {
fmt.Printf("Error writing to InfluxDB: %v\n", err)
// Write to stdout
if outputFormat == "json" {
// Output JSON format
prettyJSON, err := json.MarshalIndent(events, "", " ")
if err != nil {
fmt.Printf("Error formatting JSON: %v\n", err)
return
}
fmt.Println(string(prettyJSON))
} else {
// Write to stdout
if outputFormat == "json" {
// Output JSON format
prettyJSON, err := json.MarshalIndent(events, "", " ")
if err != nil {
fmt.Printf("Error formatting JSON: %v\n", err)
return
}
fmt.Println(string(prettyJSON))
} else {
// Output table format
// Output table format
fmt.Printf("%-36s %-20s %-25s %-25s %-25s\n",
"Trace ID", "Timestamp", "Device Name", "Bird Name", "Bird Latin")
fmt.Println("--------------------------------------------------------------------------------------------------")
for _, event := range events {
fmt.Printf("%-36s %-20s %-25s %-25s %-25s\n",
"Trace ID", "Timestamp", "Device Name", "Bird Name", "Bird Latin")
fmt.Println("--------------------------------------------------------------------------------------------------")
for _, event := range events {
fmt.Printf("%-36s %-20s %-25s %-25s %-25s\n",
event.TraceID,
event.Timestamp,
event.DeviceName,
event.BirdName,
event.BirdLatin)
}
event.TraceID,
event.Timestamp,
event.DeviceName,
event.BirdName,
event.BirdLatin)
}
}
},
}

// writeToInfluxDB writes events to InfluxDB
func writeToInfluxDB(events []Event) error {
// Validate required configuration
if influxURL == "" {
return fmt.Errorf("InfluxDB URL is required")
}
if influxOrg == "" {
return fmt.Errorf("InfluxDB organization is required")
}
if influxBucket == "" {
return fmt.Errorf("InfluxDB bucket is required")
}
if influxToken == "" {
return fmt.Errorf("InfluxDB token is required")
}

fmt.Printf("Writing %d events to InfluxDB...\n", len(events))

// Create client
client := influxdb2.NewClient(influxURL, influxToken)
defer client.Close()

// Get write API
writeAPI := client.WriteAPI(influxOrg, influxBucket)

// Create error channel to capture async errors
errorsCh := writeAPI.Errors()

// Create a channel to signal completion of error handling
done := make(chan bool)

// Track if we've encountered any errors
var writeError error

// Start a goroutine to handle errors
go func() {
for err := range errorsCh {
if writeError == nil {
writeError = err
} else {
writeError = fmt.Errorf("%w; additional error: %v", writeError, err)
}
}
done <- true
}()

// Write all points
for _, event := range events {
// Parse timestamp
timestamp, err := parseTimestamp(event.Timestamp)
if err != nil {
fmt.Printf("Warning: Invalid timestamp '%s' for event with TraceID '%s': %v\n",
event.Timestamp, event.TraceID, err)
continue
}

// Create a point with measurement "bird_sighting"
point := influxdb2.NewPoint(
"bird_sighting",
map[string]string{
"device": event.DeviceName,
"serial": event.SerialNumber,
"bird_name": event.BirdName,
"bird_latin": event.BirdLatin,
"trace_id": event.TraceID,
},
map[string]interface{}{
"confidence": event.BirdConfidence,
},
timestamp,
)

// Write the point
writeAPI.WritePoint(point)
}

// Force writing of buffered points
writeAPI.Flush()

// Wait for any errors to be processed
<-done

// If no errors occurred, we're done
if writeError == nil {
fmt.Printf("Successfully wrote %d events to InfluxDB\n", len(events))
return nil
}

return fmt.Errorf("error writing to InfluxDB: %w", writeError)
}

// supportedTimeFormats contains the timestamp formats that the handler can parse
var supportedTimeFormats = []string{
"2006-01-02 15:04:05", // Standard format
Expand All @@ -241,13 +135,6 @@ func parseTimestamp(timestamp string) (time.Time, error) {
func init() {
listCmd.Flags().IntVar(&hours, "hours", 24, "Number of hours to fetch events for")
listCmd.Flags().StringVar(&outputFormat, "format", "table", "Output format (table or json)")

// Add flags for output destination and InfluxDB configuration
listCmd.Flags().StringVar(&outputDestination, "output", "stdout", "Output destination (stdout or influxdb)")
listCmd.Flags().StringVar(&influxURL, "influx-url", os.Getenv("INFLUX_URL"), "InfluxDB server URL")
listCmd.Flags().StringVar(&influxOrg, "influx-org", os.Getenv("INFLUX_ORG"), "InfluxDB organization")
listCmd.Flags().StringVar(&influxBucket, "influx-bucket", os.Getenv("INFLUX_BUCKET"), "InfluxDB bucket name")
listCmd.Flags().StringVar(&influxToken, "influx-token", os.Getenv("INFLUX_TOKEN"), "InfluxDB authentication token")
}

// fetchEvents retrieves events from the Vicohome API within the specified time range.
Expand Down
8 changes: 0 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@ go 1.23.6
require github.com/spf13/cobra v1.8.0

require (
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/influxdata/influxdb-client-go/v2 v2.14.0 // indirect
github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839 // indirect
github.com/oapi-codegen/runtime v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/lint v0.0.0-20241112194109-818c5a804067 // indirect
golang.org/x/net v0.39.0 // indirect
golang.org/x/tools v0.32.0 // indirect
)
37 changes: 0 additions & 37 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,47 +1,10 @@
github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk=
github.com/apapsch/go-jsonmerge/v2 v2.0.0 h1:axGnT1gRIfimI7gJifB699GoE/oq+F2MU7Dml6nw9rQ=
github.com/apapsch/go-jsonmerge/v2 v2.0.0/go.mod h1:lvDnEdqiQrp0O42VQGgmlKpxL1AP2+08jFMw88y4klk=
github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/influxdata/influxdb-client-go/v2 v2.14.0 h1:AjbBfJuq+QoaXNcrova8smSjwJdUHnwvfjMF71M1iI4=
github.com/influxdata/influxdb-client-go/v2 v2.14.0/go.mod h1:Ahpm3QXKMJslpXl3IftVLVezreAUtBOTZssDrjZEFHI=
github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839 h1:W9WBk7wlPfJLvMCdtV4zPulc4uCPrlywQOmbFOhgQNU=
github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839/go.mod h1:xaLFMmpvUxqXtVkUJfg9QmT88cDaCJ3ZKgdZ78oO8Qo=
github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE=
github.com/oapi-codegen/runtime v1.0.0 h1:P4rqFX5fMFWqRzY9M/3YF9+aPSPPB06IzP2P7oOxrWo=
github.com/oapi-codegen/runtime v1.0.0/go.mod h1:LmCUMQuPB4M/nLXilQXhHw+BLZdDb18B34OO356yJ/A=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKkMo8ZTx3f+BZEkzsRUY10Xsm2mwU0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/lint v0.0.0-20241112194109-818c5a804067 h1:adDmSQyFTCiv19j015EGKJBoaa7ElV0Q1Wovb/4G7NA=
golang.org/x/lint v0.0.0-20241112194109-818c5a804067/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/net v0.39.0 h1:ZCu7HMWDxpXpaiKdhzIfaltL9Lp31x/3fCP11bc6/fY=
golang.org/x/net v0.39.0/go.mod h1:X7NRbYVEA+ewNkCNyJ513WmMdQ3BineSwVtN2zD/d+E=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.32.0 h1:Q7N1vhpkQv7ybVzLFtTjvQya2ewbwNDZzUgfXGqtMWU=
golang.org/x/tools v0.32.0/go.mod h1:ZxrU41P/wAbZD8EDa6dDCa6XfpkhJ7HFMjHJXfBDu8s=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
33 changes: 0 additions & 33 deletions pkg/output/config/config.go

This file was deleted.

Loading