Skip to content

Commit

Permalink
Merge pull request #304 from v3io/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
gilad-shaham authored Dec 21, 2020
2 parents 395b375 + 7e827ff commit 00de3af
Show file tree
Hide file tree
Showing 3 changed files with 290 additions and 2 deletions.
21 changes: 19 additions & 2 deletions data-ingestion-and-preparation/README.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"- [Accessing Platform NoSQL and TSDB Data Using the Frames Library](#data-ingest-frames)\n",
"- [Getting Data from AWS S3 Using curl](data-ingest-s3-curl)\n",
"- [Running Distributed Python with Dask](#data-ingest-dask)\n",
"- [Running DataFrames on GPUs using NVIDIA cuDF](#data-ingest-gpu)"
"- [Running DataFrames on GPUs using NVIDIA cuDF](#data-ingest-gpu)\n",
"- [Creating Dashboards with Grafana](#data-grafana)"
]
},
{
Expand Down Expand Up @@ -553,6 +554,22 @@
"> **Note:** To use the cuDF library, you need to create a RAPIDS Conda environment.\n",
"> For more information, see the [**virtual-env**](../virtual-env.ipynb) tutorial."
]
},
{
"source": [
"<a id=\"data-grafana\"></a>"
],
"cell_type": "markdown",
"metadata": {}
},
{
"source": [
"## Creating Dashboards with Grafana\n",
"\n",
"You can create a Grafana dashboard programmatically using the Iguazio API. This allows you to define a dashboard that reads from the Iguazio data layer and display tables and charts of this data. The [**grafana-grafwiz example**](grafana-grafwiz.ipynb) demonstrates how to do that."
],
"cell_type": "markdown",
"metadata": {}
}
],
"metadata": {
Expand All @@ -576,4 +593,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
}
}
7 changes: 7 additions & 0 deletions data-ingestion-and-preparation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Learn about different methods for ingesting data into the Iguazio Data Science P
- [Getting Data from AWS S3 Using curl](data-ingest-s3-curl)
- [Running Distributed Python with Dask](#data-ingest-dask)
- [Running DataFrames on GPUs using NVIDIA cuDF](#data-ingest-gpu)
- [Creating Dashboards with Grafana](#data-grafana)

<a id="data-ingest-overview"></a>
## Overview
Expand Down Expand Up @@ -282,3 +283,9 @@ The [**gpu-cudf-vs-pd**](gpu-cudf-vs-pd.ipynb) tutorial demonstrates how to use

> **Note:** To use the cuDF library, you need to create a RAPIDS Conda environment.
> For more information, see the [**virtual-env**](../virtual-env.ipynb) tutorial.
<a id="data-grafana"></a>

## Creating Dashboards with Grafana

You can create a Grafana dashboard programmatically using the Iguazio API. This allows you to define a dashboard that reads from the Iguazio data layer and display tables and charts of this data. The [**grafana-grafwiz example**](grafana-grafwiz.ipynb) demonstrates how to do that.
264 changes: 264 additions & 0 deletions data-ingestion-and-preparation/grafana-grafwiz.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Make sure grafwiz is installed"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install git+https://github.com/v3io/grafwiz --upgrade"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Add environment configurations and import the necessary libraries\n",
"* Please make sure you have a Grafana service, in order to create one, please go to Iguazio dashboard -> Service, Create new service\n",
"* To retrieve the url of Grafana service please use \"API\" tab, in the services dashboard"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"grafana_url = 'http://grafana' \n",
"v3io_container = 'users'\n",
"stocks_kv_table = os.path.join(os.getenv(\"V3IO_USERNAME\"),'stocks_kv_table')\n",
"stocks_tsdb_table = os.path.join(os.getenv(\"V3IO_USERNAME\"),'stocks_tsdb_table')\n",
"sym = 'XYZ'\n",
"rows = 3450"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from grafwiz import *\n",
"import v3io_frames as v3f\n",
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Generate data for the dataframe"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Date that will serve as an index for the time series and additional parameters"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"import datetime\n",
"import numpy as np\n",
"\n",
"def generate_date(rows):\n",
" \n",
" datetimes = [datetime.datetime.today() - (random.random() * datetime.timedelta(minutes=15)) for i in range(rows)]\n",
" return datetimes\n",
"\n",
"time = sorted(generate_date(rows))\n",
"volume = np.random.randint(low=100, high=10000, size=rows)\n",
"price = np.cumsum([0.0001] * rows + np.random.random(rows))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Create a dataframe out of the generated index and add an index to the dataframe in order to make it accessible for tsdb"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"stocks_df = pd.DataFrame(\n",
" {'last_updated': time,\n",
" 'volume': volume,\n",
" 'price': price\n",
" })\n",
"stocks_df['symbol'] = sym\n",
"stocks_df = stocks_df.sort_values('last_updated')\n",
"stocks_df"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"stocks_df_tsdb = stocks_df\n",
"stocks_df_tsdb = stocks_df.reset_index()\n",
"stocks_df_tsdb = stocks_df.set_index(['last_updated'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Create a client for v3io_frames create table and persist the dataframe in tsdb"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"client = v3f.Client('framesd:8081',container=v3io_container)\n",
"client.create(backend='tsdb', table=stocks_tsdb_table, rate='1/m', if_exists=1)\n",
"client.write(backend='tsdb', table=stocks_tsdb_table, dfs=stocks_df_tsdb)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Update the kv table in order of rows arrival to simulate real time changing data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"expr_template = \"symbol='{symbol}';price='{price}';volume='{volume}';last_updated='{last_updated}'\"\n",
"# update NoSQL table with stock data\n",
"for idx, record in stocks_df.iterrows():\n",
" stock = {'symbol': sym, 'price': record['price'], 'volume': record['volume'], 'last_updated': record['last_updated']}\n",
" expr = expr_template.format(**stock)\n",
" client.execute('kv', stocks_kv_table, 'update', args={'key': sym, 'expression': expr})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Create a Datasource for grafana table and check that kv table exists and accessible"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create datasources\n",
"DataSource(name='Iguazio').deploy(grafana_url, use_auth=True)\n",
"\n",
"# Verify the KV table can be shown\n",
"client = v3f.Client('framesd:8081', container=v3io_container)\n",
"client.execute(backend='kv', table=stocks_kv_table, command='infer')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Create a Dashboard define its datasource"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create grafana dashboard\n",
"dash = Dashboard(\"stocks\", start='now-15m', dataSource='Iguazio', end='now')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Create a table and 2 graphs\n",
"Please note that you have to wait for couple of minutes to see the graphs being updated"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create a table and log viewer in one row\n",
"tbl = Table('Current Stocks Value', span=12).source(table=stocks_kv_table,fields=['symbol','volume', 'price', 'last_updated'],container=v3io_container)\n",
"dash.row([tbl])\n",
"\n",
"# Metrics row will\n",
"metrics_row = [Graph(metric).series(table=stocks_tsdb_table, fields=[metric], container=v3io_container) for metric in ['price','volume']]\n",
"\n",
"dashboard = dash.row(metrics_row)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Deploy to Grafana\n",
"dash.deploy(grafana_url)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

0 comments on commit 00de3af

Please sign in to comment.