Skip to main content

3.2 Take action from a dashboard

Features: Actions

The investigation is complete: GB mobile customers are hitting payment failures. The VP wants to act immediately -- cancel the affected orders immediately, so that the stock is released for new customers during the sale -- without waiting for a developer to write a script.

An Action lets you attach an interactive button to a panel that fires an HTTP request when clicked. You'll build a table of affected orders from MySQL and wire a one-click cancel button directly into it. When you click the button, it sends a POST to an API with the order number.

Task 1: Add a failed orders panel

We'll add a new panel to the dashboard that shows a table of failed orders.

Add the panel

  1. Add a new panel and name the panel Incomplete Orders.

  2. Set its type to Table.

  3. Set the datasource to Orders MySQL.

  4. Switch to Code mode and paste this SQL:

    SELECT
    o.order_date AS "Order Date",
    o.order_id AS "Order ID",
    c.country AS "Country",
    o.device AS "Device",
    FORMAT(o.total_amount, 2) AS "Amount",
    o.status AS "Status",
    NULL AS "Actions" -- we'll add a button here later
    FROM orders o
    JOIN customers c ON o.customer_id = c.customer_id
    WHERE $__timeFilter(o.order_date)
    AND c.country IN (${geography:singlequote}) -- references the 'geography' variable
    AND o.status != 'completed'
    ORDER BY o.order_date DESC
    LIMIT 20
    Grot teaching a class

    Notice how we're baking in the geography dashboard variable into the query, so that we show results that are relevant to the current dashboard context.

Make it look good

Add a couple of field overrides to make the table look nice. Use this Assistant prompt to help you:

Suggested prompt
In the Incomplete Orders table, format the Amount column as dollars, and change the Status column to pill view, with payment_failure in red, cancelled in blue, and completed in green

Incomplete orders table with formatting

Curious how to do this without Grafana Assistant?
  1. Add a field override for the Amount column:

    • Choose Fields with name then select Amount.
    • Add override property:
      • Type: Standard options > Unit
      • Value: Currency / Dollars ($)
  2. Add a second field override for the Status column:

    • Matcher: byNameStatus
    • Cell type: Color text
    • Value mappings:
      • payment_failure → red, display text Payment Failure
      • cancelled → blue, display text Cancelled
      • completed → green, display text Completed

Task 2: Cancel failed orders with an Action button

Set up the Actions column

  1. Edit the Incomplete Orders panel.

  2. In the panel settings sidebar, under Data links and actions, under the Actions heading, click Add action.

    • Title: Cancel order

    • Confirmation message: Do you really want to cancel order ${__data.fields['Order ID']}?

    • Connection: Direct from browser

    • Method: POST

    • URL: https://<ID>.aws.work-shop.grafana.net/api/cancel (replace <ID> with your workshop ID)

    • Headers: Content-Type: application/json

    • Body:

      {
      "order_id": "${__data.fields['Order ID']}",
      "admin_password": "astronomix-admin"
      }
      Grot teaching a class

      Notice how we reference data fields from the panel using the __data.fields structure.

  3. Scroll to the bottom and click the Add field override button.

    • Use the Fields with name option
    • Set the field name to Actions
    • Click Add override property
    • Set the property to Cell type and choose Actions (this renders the column as buttons).
  4. Click Back to dashboard, then click the blue Save button.

Verify it works

Now let's verify the action works, by cancelling a couple of orders:

  1. Filter to $geography = GB -- you should see payment failed orders
  2. Click the Cancel Order button on a row
  3. Confirm the dialog, then the request should fire.
  4. Refresh the dashboard and you should see the order status change to canceled.
  5. Try performing this for another couple of orders and see what happens.

Incomplete orders table with actions

Grot teaching a class

You just took a targeted, customer-facing action without opening a terminal, writing a script, or waiting for an engineer. Observe and operate -- from the same screen.

Wrapping up

In this section you've learned how to take action from a dashboard. This is an extremely powerful capability that turns a plain old dashboard into a power tool. Now any dashboard user can be an operator, too! You can take action without writing a script, and most importantly without switching context.

Actions can be added to any panel, and can be configured to fire HTTP requests with dynamic payloads based on the context of the click.

You could use Actions to:

  • Send notifications to your team
  • Trigger a script to run on a server
  • Perform a rollback or restart of a service
  • Send a message to a Slack channel
  • Send an email to a list of recipients

Click Next to continue to the final section where we'll polish up the dashboard and make it more user-friendly.