|
| 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 | +} |
0 commit comments