|
| 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. |
| 18 | +
|
| 19 | +IMPORTANT: Visualizations can only be attached to saved (non-temporary) queries. |
| 20 | +If you created the query with --temp, you must recreate it without --temp first. |
| 21 | +
|
| 22 | +The --options flag is required for a working visualization. Without proper options, |
| 23 | +the visualization will fail to render. The options format depends on the |
| 24 | +visualization type. |
| 25 | +
|
| 26 | +Visualization types: chart, table, counter, pivot, cohort, funnel, choropleth, |
| 27 | +sankey, sunburst_sequence, word_cloud. |
| 28 | +
|
| 29 | +For chart type, set globalSeriesType to: column, line, area, scatter, or pie. |
| 30 | +
|
| 31 | +COUNTER (simplest — shows a single number): |
| 32 | + --type counter --options '{ |
| 33 | + "counterColName": "<column>", |
| 34 | + "rowNumber": 1, |
| 35 | + "stringDecimal": 0, |
| 36 | + "stringPrefix": "", |
| 37 | + "stringSuffix": "", |
| 38 | + "counterLabel": "My Label", |
| 39 | + "coloredPositiveValues": false, |
| 40 | + "coloredNegativeValues": false |
| 41 | + }' |
| 42 | +
|
| 43 | +TABLE (displays query results as a table): |
| 44 | + --type table --options '{ |
| 45 | + "itemsPerPage": 25, |
| 46 | + "columns": [ |
| 47 | + {"name": "<column>", "title": "Display Name", "type": "normal", |
| 48 | + "alignContent": "left", "isHidden": false} |
| 49 | + ] |
| 50 | + }' |
| 51 | +
|
| 52 | +COLUMN/LINE/AREA/SCATTER CHART: |
| 53 | + --type chart --options '{ |
| 54 | + "globalSeriesType": "line", |
| 55 | + "sortX": true, |
| 56 | + "legend": {"enabled": true}, |
| 57 | + "series": {"stacking": null}, |
| 58 | + "xAxis": {"title": {"text": "Date"}}, |
| 59 | + "yAxis": [{"title": {"text": "Value"}}], |
| 60 | + "columnMapping": {"<x_column>": "x", "<y_column>": "y"}, |
| 61 | + "seriesOptions": { |
| 62 | + "<y_column>": {"type": "line", "yAxis": 0, "zIndex": 0} |
| 63 | + } |
| 64 | + }' |
| 65 | +
|
| 66 | +PIE CHART: |
| 67 | + --type chart --options '{ |
| 68 | + "globalSeriesType": "pie", |
| 69 | + "sortX": true, |
| 70 | + "showDataLabels": true, |
| 71 | + "columnMapping": {"<category_column>": "x", "<value_column>": "y"}, |
| 72 | + "seriesOptions": { |
| 73 | + "<value_column>": {"type": "pie", "yAxis": 0, "zIndex": 0} |
| 74 | + } |
| 75 | + }' |
| 76 | +
|
| 77 | +Column names in options must match actual query result columns exactly. |
| 78 | +
|
| 79 | +Examples: |
| 80 | + # Counter showing row count |
| 81 | + dune viz create --query-id 12345 --name "Total Count" --type counter \ |
| 82 | + --options '{"counterColName":"count","rowNumber":1,"stringDecimal":0}' |
| 83 | +
|
| 84 | + # Line chart of daily volume |
| 85 | + dune viz create --query-id 12345 --name "Daily Volume" --type chart \ |
| 86 | + --options '{"globalSeriesType":"line","sortX":true,"columnMapping":{"day":"x","volume":"y"},"seriesOptions":{"volume":{"type":"line","yAxis":0,"zIndex":0}},"xAxis":{"title":{"text":"Day"}},"yAxis":[{"title":{"text":"Volume"}}],"legend":{"enabled":true},"series":{"stacking":null}}' |
| 87 | +
|
| 88 | + # Simple table |
| 89 | + dune viz create --query-id 12345 --name "Results" --type table \ |
| 90 | + --options '{"itemsPerPage":25,"columns":[{"name":"address","title":"Address","type":"normal","alignContent":"left","isHidden":false},{"name":"balance","title":"Balance","type":"normal","alignContent":"right","isHidden":false}]}'`, |
| 91 | + RunE: runCreate, |
| 92 | + } |
| 93 | + |
| 94 | + cmd.Flags().Int("query-id", 0, "ID of the query to attach the visualization to (required)") |
| 95 | + cmd.Flags().String("name", "", "visualization name, max 300 characters (required)") |
| 96 | + cmd.Flags().String("type", "table", "visualization type: chart, table, counter, pivot, cohort, funnel, choropleth, sankey, sunburst_sequence, word_cloud") |
| 97 | + cmd.Flags().String("description", "", "visualization description, max 1000 characters") |
| 98 | + cmd.Flags().String("options", "", `visualization options JSON (required for working visualizations, see --help for format per type)`) |
| 99 | + _ = cmd.MarkFlagRequired("query-id") |
| 100 | + _ = cmd.MarkFlagRequired("name") |
| 101 | + _ = cmd.MarkFlagRequired("options") |
| 102 | + output.AddFormatFlag(cmd, "text") |
| 103 | + |
| 104 | + return cmd |
| 105 | +} |
| 106 | + |
| 107 | +func runCreate(cmd *cobra.Command, _ []string) error { |
| 108 | + client := cmdutil.ClientFromCmd(cmd) |
| 109 | + |
| 110 | + queryID, _ := cmd.Flags().GetInt("query-id") |
| 111 | + name, _ := cmd.Flags().GetString("name") |
| 112 | + vizType, _ := cmd.Flags().GetString("type") |
| 113 | + description, _ := cmd.Flags().GetString("description") |
| 114 | + optionsStr, _ := cmd.Flags().GetString("options") |
| 115 | + |
| 116 | + var options map[string]any |
| 117 | + if err := json.Unmarshal([]byte(optionsStr), &options); err != nil { |
| 118 | + return fmt.Errorf("invalid --options JSON: %w", err) |
| 119 | + } |
| 120 | + |
| 121 | + resp, err := client.CreateVisualization(models.CreateVisualizationRequest{ |
| 122 | + QueryID: queryID, |
| 123 | + Name: name, |
| 124 | + Type: vizType, |
| 125 | + Description: description, |
| 126 | + Options: options, |
| 127 | + }) |
| 128 | + if err != nil { |
| 129 | + return err |
| 130 | + } |
| 131 | + |
| 132 | + url := fmt.Sprintf("https://dune.com/embeds/%d/%d", queryID, resp.ID) |
| 133 | + |
| 134 | + w := cmd.OutOrStdout() |
| 135 | + switch output.FormatFromCmd(cmd) { |
| 136 | + case output.FormatJSON: |
| 137 | + return output.PrintJSON(w, map[string]any{ |
| 138 | + "id": resp.ID, |
| 139 | + "url": url, |
| 140 | + }) |
| 141 | + default: |
| 142 | + fmt.Fprintf(w, "Created visualization %d on query %d\n%s\n", resp.ID, queryID, url) |
| 143 | + return nil |
| 144 | + } |
| 145 | +} |
0 commit comments