Skip to content

Using the Time Series API to Fetch Time Series Data

jeremyosterhoudt edited this page Mar 15, 2018 · 9 revisions

The goal of this guide is to show you how to use the Time Series API provided by the Predix SDK For iOS to retrieve time series data from the Predix Time Series service.

Note - This guide does not cover setting up a Time Series service, for information on Time Series service, see the Predix Time Series service documentation

Prerequisites

You must have:

  • Predix SDK for iOS version 1.1 or higher
  • Completed all tasks from our Getting Started Guide
  • Completed all tasks from our User Authentication Using Native UI
  • Some familiarity with the Predix Time Series service
  • Knowledge of Xcode's Interface Builder (IB)
  • Experience in connecting the Xcode IB elements (IBOutlets and IBActions) to a Swift UIViewController

Before you begin

To allow a user token to access Time Series data you will need to make sure the user that is being used to authenticate with UAA has the Time Series zone.user and zone.query scopes added to the user's groups in UAA. For more information please see our Time Series FAQ, Time Series Documentation and UAA Documentation.

Creating a View Interface

Create a simple view that allows you to send the Time Series request and view the Time Series response.

  1. Open Xcode and create a Single View App

    File > New > Project > iOS > Single View App

  2. Open the main StoryBoard and add a way to authenticate a user. If you need help with this please reference our User Authentication Using Native UI guide.

  3. Open the main StoryBoard and modify the existing view to add some buttons and labels. Below is a simple example

StoryBoard

  1. Open your Time Series UIViewController class to wire up the IBOutlets and IBActions`
import UIKit

class ViewController: UIViewController {
        
    @IBOutlet weak var tagNamesTextView: UITextView!
    @IBOutlet weak var tagNamesLabel: UITextField!
    @IBOutlet weak var dataPointsTextView: UITextView!
}

Outlets

Fetching Time Series Data

You need to add some additional code snippets to fetch the Time Series data.

  1. Open the ViewController class and import PredixSDK
import UIKit
import PredixSDK

class ViewController: UIViewController {
        
    @IBOutlet weak var tagNamesTextView: UITextView!
    @IBOutlet weak var tagNamesLabel: UITextField!
    @IBOutlet weak var dataPointsTextView: UITextView!
}
  1. Create a variable to hold the TimeSeriesManager.
import UIKit
import PredixSDK

class ViewController: UIViewController {
        
    @IBOutlet weak var tagNamesTextView: UITextView!
    @IBOutlet weak var tagNamesLabel: UITextField!
    @IBOutlet weak var dataPointsTextView: UITextView!
    private var timeSeriesManager: TimeSeriesManager?
}
  1. Create TimeSeriesManagerConfiguration and the TimeSeriesManager in the ViewDidLoad method as shown below:
class ViewController: UIViewController {
...
override func viewDidLoad() {
    super.viewDidLoad()
        
    let config: TimeSeriesManagerConfiguration = TimeSeriesManagerConfiguration(predixZoneID: "6a1487bd-afb6-40b8-929c-ca872c958f74")

    self.timeSeriesManager = TimeSeriesManager(configuration: config)
}
...

}

NOTE - To allow a user token to access Time Series data you will need to make sure the user that is being used to authenticate with UAA has the Time Series zone.user and zone.query groups added to the user's groups in UAA. For more information please see our Time Series FAQ, Time Series Documentation and UAA Documentation.

  1. Add the following code to the fetchTags IBOutlet that you created earlier, to fetch the tags for your Time Series service.
@IBAction func fetchTags(_ sender: Any) {
    self.timeSeriesManager?.fetchTagNames { (tags, error) in
        if let tagNames = tags {
            tagNamesTextView.text = tagNames.joined(separator: ",")
        } else if let anError = error {
            tagNamesTextView.text = error?.localizedDescription
        } else {
            tagNamesTextView.text = "No Tags available"
        }
    }
}
  1. Add the following code to fetch the Time Series data points for the tags typed in by the user.
@IBAction func fetchDataPoints(_ sender: Any) {
   guard let tagNames = tagNamesLabel.text?.split(separator: ",") else {
       self.dataPointsTextView.text = "can't fetch tags unless the tag names are in the proper format.  The tag names input format should look like: tag1,tag2"
       return
   }
   
   let dataPointsRequest = DataPointRequest(tagNames: tagNames)
   self.timeSeriesManager?.fetchLatestDataPoints(request: dataPointsRequest) { (results, error) in
       if let anError = error {
           dataPointsTextView.text = error?.localizedDescription
       } else if let dataPoints = results?.dataPoints {
           var responseString = ""
           for dataPoint in dataPoints {
               responseString = responseString + "------------------------------------------\n"
               responseString = responseString + "\(dataPoint.tagName): \(dataPoint.results)\n"
               responseString = responseString + "------------------------------------------\n"
           }
           dataPointsTextView.text = responseString
       } else {
           dataPointsTextView.text = "No data points found for the given Tag(s)"
       }
   }
}

The above block of code is adding some simple data about the tag data points into the UITextField, however, you can access many other properties from the TagDataPointResponse model.

Next Steps