From real-time monitoring to historical analysis
While InfluxDB is a champion of real-time data, its power extends far beyond live dashboards. It provides a highly optimized engine for storing and querying massive time-series datasets, making it a perfect middle-ground between the local analytics of DuckDB and the enterprise scale of ClickHouse.Looking for other analytics solutions? Check out our full list of API Tutorials for more step-by-step guides.
- Ingest 1-hour OHLCV data for the top 500 Uniswap v3 pools over a 14-day period.
- Run complex time-series analysis using Python and the Flux language.
- Visualize the data in a live-updating Grafana dashboard.
Step 1: Docker Setup
Get InfluxDB and Grafana running in seconds with Docker.
Step 2: ETL Pipeline
Create a robust data pipeline for ingesting large time-series datasets.
Step 3: Programmatic Analysis
Analyze your time-series data with InfluxDB’s Python client.
Step 4: Live Dashboard
Build a real-time dashboard to monitor crypto pools.
Step 1: Setting Up InfluxDB and Grafana
We’ll use Docker Compose to spin up both services. First, create a new directory namedINFLUXDB
in your project root. Inside that directory, create a file named docker-compose.yml
with the following content.
INFLUXDB/docker-compose.yml
- InfluxDB UI:
http://localhost:8087
- Grafana UI:
http://localhost:3000
(login withadmin
/admin
)
my-super-secret-token
to connect to InfluxDB.
We use port
8087
for InfluxDB to avoid potential conflicts with other services that might be using the default port 8086
.Step 2: Build the Python ETL Pipeline
Create a new file namedINFLUXDB/build_influxdb_db.py
. This script is built to be robust and efficient, capable of ingesting large volumes of time-series data from the DexPaprika API into your InfluxDB instance. It leverages two key endpoints: the Top Pools on a DEX endpoint to discover pools, and the Pool OHLCV Data endpoint to fetch historical price data.
INFLUXDB/build_influxdb_db.py
This script is built for performance and reliability, using several best practices common in data pipelines:
- Asynchronous operations: By using
asyncio
andaiohttp
, the script can make many API requests concurrently instead of one by one. - Dynamic windowing: The
fetch_pool_ohlcv_paginated
function calculates how much data to request per API call to ensure complete history is fetched efficiently. - Concurrency control & throttling: An
asyncio.Semaphore
, combined with carefully tunedBATCH_SIZE
andasyncio.sleep()
calls, prevents API rate-limiting. - Resiliency: The
fetch_with_retry
function automatically retries failed requests with an exponential backoff delay. - Data Integrity: The script automatically clears old data from the bucket before each run to ensure a clean, consistent dataset.
Setup Python Environment
Before running the ETL script, it’s a critical best practice to create an isolated Python environment to manage dependencies.-
Create a virtual environment:
Open your terminal in the project root and run:
-
Activate the environment:
- On macOS and Linux:
- On Windows:
(venv)
, indicating that the virtual environment is active. - On macOS and Linux:
-
Install required libraries:
Now, install the necessary Python packages inside the activated environment:
Step 3: Programmatic Analysis with Python
While the InfluxDB UI is great for exploration, the real power comes from programmatic access. Theinfluxdb-client
library for Python allows you to run complex Flux queries and integrate the data into other tools or scripts.
We’ve created a query_influxdb.py
script to demonstrate how to connect to your database and perform analysis on the hourly data.
INFLUXDB/query_influxdb.py
Step 4: Visualizing Data in Grafana
-
Open Grafana at
http://localhost:3000
. - Go to Connections > Add new connection > InfluxDB.
-
Configure the connection:
- Name: InfluxDB_Crypto
- Query Language: Flux
- URL:
http://influxdb:8086
(use the Docker service name) - Under Auth, toggle Basic auth off.
- In the Custom HTTP Headers section, add a header:
- Header:
Authorization
- Value:
Token my-super-secret-token
- Header:
- Enter your InfluxDB Organization (
my-org
) and default Bucket (crypto-data
).
- Click Save & Test. You should see a “Bucket found” confirmation.
- Now, let’s create a dashboard. In the left-hand menu, click the + icon and select Dashboard.
- Click on the Add new panel button.
-
In the new panel view, ensure your
InfluxDB_Crypto
data source is selected at the top. - Below the graph, you’ll see a query editor. You can switch to the Script editor by clicking the pencil icon on the right.
-
Paste the following query into the editor. This query will plot the raw closing price for the
AAVE/USDC
trading pair.Troubleshooting Tip: If you initially see “No data,” there are two common reasons:- Time Range: Ensure the time picker at the top right is set to “Last 7 days” or wider, not a shorter period like “Last 6 hours.”
- Trading Pair: The default
WETH/USDC
pair used in the original tutorial may not have been in the top 500 pools fetched by the script. The query above usesAAVE/USDC
, which is more likely to be present. You can find other available pairs by running thequery_influxdb.py
script.
- At the top right of the dashboard page, set the time range to Last 7 days to ensure you see all the historical data you ingested.
- You should now see the data appear in the panel. You can give the panel a title (e.g., “AAVE/USDC Close Price”) in the settings on the right.
- Click Apply to save the panel to your dashboard. You can now add more panels for other queries.
What You’ve Built
You now have a powerful, scalable analytics pipeline for time-series crypto data. You’ve combined a production-grade Python ETL script with the industry-standard tools for time-series data storage (InfluxDB) and visualization (Grafana). Key achievements:- Built a production-ready ETL pipeline: You have a reusable, high-performance Python script that can populate a time-series database from any supported DEX.
- Unlocked programmatic time-series analysis: You can now perform complex analytical queries on large time-series datasets using Python and Flux.
- Mastered a scalable analytics workflow: This pipeline provides a solid foundation for building live dashboards, conducting in-depth market research, and developing sophisticated monitoring or trading algorithms.
- Enabled live data visualization: You’ve connected your database to Grafana, the leading open-source tool for observability and data visualization.