-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from lsst-sqre/u/afausti/influxdb-requests
Add example notebook to query the EFD using Python requests
- Loading branch information
Showing
1 changed file
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "c82fdf8e-5979-4e7c-bb74-a4cde915384c", | ||
"metadata": {}, | ||
"source": [ | ||
"# Querying the EFD using the InfluxDB API and the Python requests module\n", | ||
"\n", | ||
"See the InfluxDB API documentation at https://docs.influxdata.com/influxdb/v1/tools/api/#query-http-endpoint\n", | ||
"\n", | ||
"InfluxDB uses the InfluxQL query language. The same queries you use in Chronograf you can use here. See InfluxQL documentation at https://docs.influxdata.com/influxdb/v1/query_language/" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "884ff032-2538-4728-a087-e3e1cfb455b0", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import requests\n", | ||
"\n", | ||
"class InfluxDBClient:\n", | ||
" def __init__(self, url, database_name, username=None, password=None):\n", | ||
" self.url = url\n", | ||
" self.database_name = database_name\n", | ||
" self.auth = (username, password) if username and password else None\n", | ||
"\n", | ||
" def query(self, query):\n", | ||
" \"\"\"Send a query to the InfluxDB API.\"\"\"\n", | ||
" params = {\n", | ||
" 'db': self.database_name,\n", | ||
" 'q': query\n", | ||
" }\n", | ||
" try:\n", | ||
" response = requests.get(f\"{self.url}/query\", params=params, auth=self.auth)\n", | ||
" response.raise_for_status() # Will raise an exception for HTTP error codes\n", | ||
" return response.json()\n", | ||
" except requests.exceptions.RequestException as e:\n", | ||
" print(f\"An error occurred: {e}\")\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "07e77a6b-d743-4253-879c-a672dd9583e7", | ||
"metadata": {}, | ||
"source": [ | ||
"Here, we assume you have set the EFD reader password for InlfuxDB at USDF in an environment variable called USDF_EFD_PASSWORD, available at 1Password under LSST IT/SQuaRE/EFD reader at USDF." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "c43abca9-3f33-409b-b723-99c90fd96be1", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import os\n", | ||
"\n", | ||
"url = \"https://usdf-rsp.slac.stanford.edu/influxdb-enterprise-data\"\n", | ||
"database_name = \"efd\"\n", | ||
"username = \"efdreader\"\n", | ||
"password = os.getenv(\"USDF_EFD_PASSWORD\")\n", | ||
"\n", | ||
"client = InfluxDBClient(url=url, database_name=database_name, username=username, password=password)\n", | ||
"\n", | ||
"# Query the lsst.sal.ATMonochromator.logevent_wavelength topic \n", | ||
"result = client.query('SELECT * FROM \"lsst.sal.ATMonochromator.logevent_wavelength\"')\n", | ||
"print(result)\n" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "LSST", | ||
"language": "python", | ||
"name": "lsst" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.11.7" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |