Skip to content

Commit f6f9c41

Browse files
committed
feat: add dune viz create command
1 parent 39b2126 commit f6f9c41

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

cli/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/duneanalytics/cli/cmd/query"
2323
"github.com/duneanalytics/cli/cmd/sim"
2424
"github.com/duneanalytics/cli/cmd/usage"
25+
"github.com/duneanalytics/cli/cmd/visualization"
2526
"github.com/duneanalytics/cli/cmd/whoami"
2627
"github.com/duneanalytics/cli/cmdutil"
2728
"github.com/duneanalytics/cli/tracking"
@@ -121,6 +122,7 @@ func init() {
121122
rootCmd.AddCommand(usage.NewUsageCmd())
122123
rootCmd.AddCommand(whoami.NewWhoAmICmd())
123124
rootCmd.AddCommand(sim.NewSimCmd())
125+
rootCmd.AddCommand(visualization.NewVisualizationCmd())
124126
}
125127

126128
// Execute runs the root command via Fang.

cmd/visualization/create.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package visualization
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
"github.com/duneanalytics/cli/cmdutil"
8+
"github.com/duneanalytics/cli/output"
9+
"github.com/duneanalytics/duneapi-client-go/models"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
func newCreateCmd() *cobra.Command {
14+
cmd := &cobra.Command{
15+
Use: "create",
16+
Short: "Create a new visualization on an existing query",
17+
Long: "Create a visualization attached to an existing saved query.\n\n" +
18+
"The visualization type must be one of: chart, table, counter, pivot,\n" +
19+
"cohort, funnel, choropleth, sankey, sunburst_sequence, word_cloud.\n\n" +
20+
"The --options flag accepts a JSON string of visualization-specific\n" +
21+
"configuration (axes, series, formatting, etc.).\n\n" +
22+
"Examples:\n" +
23+
" dune viz create --query-id 12345 --name \"Token Volume\" --type chart\n" +
24+
" dune viz create --query-id 12345 --name \"Summary\" --type counter --options '{\"column\":\"total\",\"row_num\":1}'\n" +
25+
" dune viz create --query-id 12345 --name \"Results\" --type table -o json",
26+
RunE: runCreate,
27+
}
28+
29+
cmd.Flags().Int("query-id", 0, "ID of the query to attach the visualization to (required)")
30+
cmd.Flags().String("name", "", "visualization name, max 300 characters (required)")
31+
cmd.Flags().String("type", "table", "visualization type: chart, table, counter, pivot, cohort, funnel, choropleth, sankey, sunburst_sequence, word_cloud")
32+
cmd.Flags().String("description", "", "visualization description, max 1000 characters")
33+
cmd.Flags().String("options", "{}", "JSON string of visualization options")
34+
_ = cmd.MarkFlagRequired("query-id")
35+
_ = cmd.MarkFlagRequired("name")
36+
output.AddFormatFlag(cmd, "text")
37+
38+
return cmd
39+
}
40+
41+
func runCreate(cmd *cobra.Command, _ []string) error {
42+
client := cmdutil.ClientFromCmd(cmd)
43+
44+
queryID, _ := cmd.Flags().GetInt("query-id")
45+
name, _ := cmd.Flags().GetString("name")
46+
vizType, _ := cmd.Flags().GetString("type")
47+
description, _ := cmd.Flags().GetString("description")
48+
optionsStr, _ := cmd.Flags().GetString("options")
49+
50+
var options map[string]any
51+
if err := json.Unmarshal([]byte(optionsStr), &options); err != nil {
52+
return fmt.Errorf("invalid --options JSON: %w", err)
53+
}
54+
55+
resp, err := client.CreateVisualization(models.CreateVisualizationRequest{
56+
QueryID: queryID,
57+
Name: name,
58+
Type: vizType,
59+
Description: description,
60+
Options: options,
61+
})
62+
if err != nil {
63+
return err
64+
}
65+
66+
w := cmd.OutOrStdout()
67+
switch output.FormatFromCmd(cmd) {
68+
case output.FormatJSON:
69+
return output.PrintJSON(w, resp)
70+
default:
71+
fmt.Fprintf(w, "Created visualization %d on query %d\n", resp.ID, queryID)
72+
return nil
73+
}
74+
}

cmd/visualization/visualization.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package visualization
2+
3+
import "github.com/spf13/cobra"
4+
5+
func NewVisualizationCmd() *cobra.Command {
6+
cmd := &cobra.Command{
7+
Use: "visualization",
8+
Aliases: []string{"viz"},
9+
Short: "Create and manage Dune visualizations",
10+
Long: "Create and manage visualizations on Dune queries.\n\n" +
11+
"Visualizations are charts, tables, counters, and other visual representations\n" +
12+
"of query results. Each visualization is attached to a saved query.",
13+
}
14+
15+
cmd.AddCommand(newCreateCmd())
16+
17+
return cmd
18+
}

0 commit comments

Comments
 (0)