Created
June 7, 2017 11:45
-
-
Save indaco/0c94b42032f96c6fa9d4a036c4828693 to your computer and use it in GitHub Desktop.
Call the Predix Time Series REST APIs to retrieve data and show on a chart
This file contains hidden or 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
""" | |
Call the Predix Time Series REST APIs to retrieve data and show on a chart | |
""" | |
import pandas as pd | |
import requests | |
import json | |
import base64 | |
from bokeh.io import output_notebook | |
from bokeh.charts import TimeSeries, output_file, show | |
output_notebook() | |
uaaUrl = "<uaa-issuer-id-url-here>" | |
tsUrl = "<predix-time-series-query-url-here>" | |
payload_last = "{\n \"start\": \"50y-ago\",\n \"tags\": [\n {\n \"name\": \"TAG NAME\",\n \"order\": \"desc\",\n \"limit\": 1\n }\n]\n}" | |
payload_first = "{\n \"start\": \"50y-ago\",\n \"tags\": [\n {\n \"name\": \"TAG NAME\",\n \"order\": \"asc\",\n \"limit\": 1\n }\n]\n}" | |
zoneId = "<your-time-series-zone-id-here>" | |
token = base64.b64encode('<your-client-id>:<your-client-secret>') | |
def doQuery(payload, tsUrl, uaaUrl, token, zoneId): | |
headers = { | |
'authorization': "Basic " + token, | |
'cache-control': "no-cache", | |
'content-type': "application/x-www-form-urlencoded" | |
} | |
response = requests.request('POST', uaaUrl, data="grant_type=client_credentials", headers=headers) | |
token = json.loads(response.text)['access_token'] | |
headers = { | |
'authorization': "Bearer " + token, | |
'predix-zone-id': zoneId, | |
'content-type': "application/json", | |
'cache-control': "no-cache" | |
} | |
response = requests.request("POST", tsUrl, data=payload, headers=headers) | |
data = json.loads(response.text)['tags'][0]['results'][0]['values'] | |
column_labels = ['timestamp', 'values', 'quality'] | |
series = pd.DataFrame(data, columns=column_labels) | |
series['timestamp'] = pd.to_datetime(series['timestamp'], unit='ms') | |
return series | |
firstPoint = doQuery(payload_first, tsUrl, uaaUrl, token, zoneId) | |
startDate = pd.Timestamp(firstPoint['timestamp'][0]) | |
startDateOrigin = startDate = int(startDate.strftime("%s")) * 1000 | |
lastPoint = doQuery(payload_last, tsUrl, uaaUrl, token, zoneId) | |
endDate = pd.Timestamp(lastPoint['timestamp'][0]) | |
endDate = int(endDate.strftime("%s")) * 1000 | |
pdArray = [] | |
while (startDate < endDate ): | |
payload = { 'cache_time': 0, 'tags': [{'name': '<TAG-NAME>', 'order': 'asc'}], 'start': startDate, 'end': startDate + 10000000} | |
startDate = startDate + 100000000 | |
series = doQuery(json.dumps(payload), tsUrl, uaaUrl, token, zoneId) | |
pdArray.append(series) | |
fullseries = pd.concat(pdArray) | |
data = dict(values = fullseries['values'], Date=fullseries['timestamp']) | |
p = TimeSeries(data, x='Date', y='values') | |
show(p) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment