Skip to main content

Export to CSV

info

Huge thanks to neshorg (Grafana profile) for the original post.

Example: export to CSV

Check out examples based on specific data sources

note

Most data sources should work with some tweaks

This works by extracting the targets using the data object, making it possible to use most data sources provided by Grafana with some tweaks.

tip

The query can be "hidden" by pressing the "Enable/disable query" button (small eye icon). Which will disable the query (for better performance).

Remember to add a query!!!

An example query using the influx database: Example: export to CSV query

Below is a basic example of how to approach implementing the code.

The code requires the query to be sent, where to send the query, converting the retrieved data to CSV, and a way to execute it.

Query to be sent

The query and most of the additional data can be obtained from DataQueryRequest interface data.request.targets.

const [{ query, refId, datasource }] = data.request.targets;
const { from, to } = data.timeRange;

Where to send the query

Most data sources use /api/datasources/proxy/:datasourceId/*, /api/ds/query and the deprecated /api/tsdb/query.

The approach is usually the same for the APIs, but data sources require different data.

Using the /api/ds/query

const { from, to } = data.timeRange;

const body = { stuff: 'Some random data based on the data source' };

fetch('/api/ds/query', {
headers: {
'cache-control': 'no-cache',
'content-type': 'application/json',
},
body: JSON.stringify(body),
method: 'POST',
});