Blog

Collecting Time Series Data with Predix (a Golang API Wrapper Included)

Stas Turlo

predix-time-series-demo

In this post, we show how to work with the Predix Time Series API using our Go library. We’ve created the wrapper to provide a more convenient way to interact with the Time Series service from Go: successful API queries return native Go structs that can be used immediately, with no need for type assertions.

A demo application that we will deploy to Predix consists of two parts:

  • a “sensor” pushing Time Series data
  • a web application reading the data from Time Series and visualizing it

The source code for the application is available in this GitHub repository.

 

Prerequisites

To get started with the Time Series service, make sure you have the following:

  • a Predix account
  • the Cloud Foundry CLI
  • the Cloud Foundry UAA CLI

 

Getting code and creating an application in Predix

Clone the following repository and go to the demo application directory:

git clone https://github.com/Altoros/go-predix-timeseries.git
cd go-predix-timeseries/examples/go-predix-timeseries-demo/
cf push APP_NAME # This will create an application in Predix.

 

Setting up services

Then, create a UAA service instance and bind it to your application:

cf create-service predix-uaa Tiered go-predix-timeseries-demo-uaa -c '{"adminClientSecret":""}'
cf bind-service APP_NAME go-predix-timeseries-demo-uaa

Create and bind the Time Series service:

cf create-service predix-timeseries Bronze go-predix-timeseries-demo-ts -c '{"trustedIssuerIds":[""]}'
cf bind-service APP_NAME go-predix-timeseries-demo-ts

You can find issuerId in the output of the cf env APP_NAME command:

predix-time-series-credentials
 

Creating ingest and query clients

Now, we need to create ingest and query clients in our UAA service with appropriate authorities. To find the required authorities, type the cf env APP_NAME command and look for the zone-token-scopes fields in credentials of the predix-timeseries service:

go-predix-time-series-demo

To create the clients, we will use uaac:

uaac target UAA_uri
uaac token client get admin
uaac client add ingest --authorized_grant_types client_credentials --authorities "timeseries.zones.bfdd9bb0-0b0f-4667-8338-18b048c8ce31.user,timeseries.zones.bfdd9bb0-0b0f-4667-8338-18b048c8ce31.ingest"
uaac client add query --authorized_grant_types client_credentials --authorities "timeseries.zones.bfdd9bb0-0b0f-4667-8338-18b048c8ce31.user,timeseries.zones.bfdd9bb0-0b0f-4667-8338-18b048c8ce31.query"

Now, set the CLIENT_ID and CLIENT_SECRET environment variables and restage the application to ensure the environment variable changes take effect:

cf set-env go-predix-timeseries-demo CLIENT_ID query
cf set-env go-predix-timeseries-demo CLIENT_SECRET 
cf restage APP_NAME

 

Pushing Time Series data

Run the “sensor” to start pushing Time Series data:

cd sensor
go run sensor.go -clientId=ingest -clientSecret= -ingestUrl="ingest_url" -uaaIssuerId="uaa_IssuerId" -zoneId="zone_id"

Look for the ingest URL and zone ID (the uri and zone-http-header-value fields) in the Time Series credentials:

"ingest": {
    "uri": "wss://gateway-predix-data-services.run.aws-usw02-pr.ice.predix.io/v1/stream/messages",
    "zone-http-header-name": "Predix-Zone-Id",
    "zone-http-header-value": "bfdd9bb0-0b0f-4667-8338-18b048c8ce31",
    "zone-token-scopes": [
      "timeseries.zones.bfdd9bb0-0b0f-4667-8338-18b048c8ce31.user",
      "timeseries.zones.bfdd9bb0-0b0f-4667-8338-18b048c8ce31.ingest"
    ]
  }

Now you can open the web application in a browser and see it populating data.

The Time Series service enables you to quickly and efficiently ingest, store, and analyze time series data. Our library, in its turn, provides an easy way to work with Time Series from Go.

See also:


Related tutorials:


About the author: Stas Turlo is a software engineer at Altoros. He specializes in development using Golang and Ruby as his primary tools. Stas also has professional interests and expertise in many other areas related to software engineering, including programming languages and cloud technologies. You can find him on GitHub.


For the next parts of this series, subscribe to our blog or follow @altoros.

Get new posts right in your inbox!

No Comments

Benchmarks and Research

Subscribe to new posts

Get new posts right in your inbox!