|
| 1 | +package query |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + "github.com/duneanalytics/cli/cmdutil" |
| 9 | + "github.com/duneanalytics/cli/output" |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +// alertChannels maps each channel flag to the channel type setQueryAlert takes. |
| 14 | +var alertChannels = map[string]string{ |
| 15 | + "slack-webhook": "slack", |
| 16 | + "discord-webhook": "discord", |
| 17 | + "webhook": "webhook", |
| 18 | +} |
| 19 | + |
| 20 | +func newAlertCmd() *cobra.Command { |
| 21 | + cmd := &cobra.Command{ |
| 22 | + Use: "alert <query-id>", |
| 23 | + Short: "Notify you each time a saved query's scheduled run returns rows", |
| 24 | + Long: `Put a saved query on a schedule and send a notification each time a run |
| 25 | +returns rows. This sets the query's schedule (creating it or replacing its |
| 26 | +frequency and tier) and creates or replaces the alert for the chosen channel. |
| 27 | +
|
| 28 | +Write the query so it returns rows only when the event happened in the latest |
| 29 | +period, such as the last hour for an hourly schedule. |
| 30 | +
|
| 31 | +Choose one channel: |
| 32 | + --email a@x.com --email b@x.com up to 5 addresses |
| 33 | + --slack-webhook <url> Slack incoming webhook |
| 34 | + --discord-webhook <url> Discord webhook |
| 35 | + --webhook <url> HTTPS URL that receives a JSON POST per fire |
| 36 | +
|
| 37 | +- A query has one alert per channel; this replaces that channel's alert. |
| 38 | +- Every scheduled run is charged credits at --performance, so use the smallest |
| 39 | + tier and the lowest frequency that catch the event in time. |
| 40 | +- You need to own the query or be an editor or admin of the team that owns it. |
| 41 | + The owner's plan limits how many queries can have alerts and schedules. |
| 42 | +- Recipients receive the query name, a link to it and its charts. |
| 43 | +
|
| 44 | +Supported cron expressions (5-part, UTC): |
| 45 | + "*/15 * * * *", "*/20 * * * *", "*/30 * * * *" every 15, 20 or 30 minutes |
| 46 | + "0 * * * *" hourly |
| 47 | + "0 */N * * *" with N in 2, 3, 4, 6, 8, 12 every N hours |
| 48 | + "M H * * *" daily at H:M |
| 49 | + "M H * * D" weekly on day D (0 = Sunday) |
| 50 | +where M is 0 or 30 and H is 0-23. |
| 51 | +
|
| 52 | +To stop the schedule, use 'dune schedule delete --query-id <id>'. |
| 53 | +
|
| 54 | +Examples: |
| 55 | + dune query alert 12345 --cron "0 * * * *" --performance small --email me@example.com |
| 56 | + dune query alert 12345 --cron "*/30 * * * *" --performance small --slack-webhook "$SLACK_URL"`, |
| 57 | + Args: cobra.ExactArgs(1), |
| 58 | + RunE: runAlert, |
| 59 | + } |
| 60 | + |
| 61 | + cmd.Flags().String("cron", "", "when to run, as a supported 5-part UTC cron expression (required)") |
| 62 | + cmd.Flags().String("performance", "", `engine tier for each run: "small", "medium", or "large" (required)`) |
| 63 | + cmd.Flags().StringArray("email", nil, "address to notify; repeat for up to 5") |
| 64 | + cmd.Flags().String("slack-webhook", "", "Slack incoming webhook URL") |
| 65 | + cmd.Flags().String("discord-webhook", "", "Discord webhook URL") |
| 66 | + cmd.Flags().String("webhook", "", "HTTPS URL that receives a JSON POST for each fire") |
| 67 | + _ = cmd.MarkFlagRequired("cron") |
| 68 | + _ = cmd.MarkFlagRequired("performance") |
| 69 | + cmd.MarkFlagsOneRequired("email", "slack-webhook", "discord-webhook", "webhook") |
| 70 | + cmd.MarkFlagsMutuallyExclusive("email", "slack-webhook", "discord-webhook", "webhook") |
| 71 | + output.AddFormatFlag(cmd, "text") |
| 72 | + |
| 73 | + return cmd |
| 74 | +} |
| 75 | + |
| 76 | +func runAlert(cmd *cobra.Command, args []string) error { |
| 77 | + queryID, err := strconv.Atoi(args[0]) |
| 78 | + if err != nil || queryID <= 0 { |
| 79 | + return fmt.Errorf("invalid query ID %q: must be a positive integer", args[0]) |
| 80 | + } |
| 81 | + |
| 82 | + var channel map[string]any |
| 83 | + if cmd.Flags().Changed("email") { |
| 84 | + emails, _ := cmd.Flags().GetStringArray("email") |
| 85 | + channel = map[string]any{"type": "email", "emails": emails} |
| 86 | + } |
| 87 | + for flag, typ := range alertChannels { |
| 88 | + if cmd.Flags().Changed(flag) { |
| 89 | + url, _ := cmd.Flags().GetString(flag) |
| 90 | + if url == "" { |
| 91 | + return fmt.Errorf("--%s must not be empty", flag) |
| 92 | + } |
| 93 | + channel = map[string]any{"type": typ, "url": url} |
| 94 | + } |
| 95 | + } |
| 96 | + if channel == nil { |
| 97 | + return errors.New("choose a channel: --email, --slack-webhook, --discord-webhook, or --webhook") |
| 98 | + } |
| 99 | + |
| 100 | + toolArgs := cmdutil.ToolArgs(cmd, map[string]string{"cron": "cron_expression", "performance": "performance"}) |
| 101 | + toolArgs["query_id"] = queryID |
| 102 | + toolArgs["channel"] = channel |
| 103 | + |
| 104 | + // The result has no webhook URL, so it is safe to print as it is. |
| 105 | + var res struct { |
| 106 | + AlertID string `json:"alert_id"` |
| 107 | + Channel string `json:"channel"` |
| 108 | + CronExpression string `json:"cron_expression"` |
| 109 | + NextExecutionTime string `json:"next_execution_time"` |
| 110 | + } |
| 111 | + raw, err := cmdutil.CallTool(cmd, "setQueryAlert", toolArgs, &res) |
| 112 | + if err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + w := cmd.OutOrStdout() |
| 116 | + if output.FormatFromCmd(cmd) == output.FormatJSON { |
| 117 | + return output.PrintRawJSON(w, raw) |
| 118 | + } |
| 119 | + fmt.Fprintf(w, "Set %s alert %s on query %d\n", res.Channel, res.AlertID, queryID) |
| 120 | + fmt.Fprintf(w, "Schedule: %s (UTC), next run %s\n", res.CronExpression, res.NextExecutionTime) |
| 121 | + return nil |
| 122 | +} |
0 commit comments