Skip to content

Commit

Permalink
feat(clientLibs): add Swift library to UI (#20621)
Browse files Browse the repository at this point in the history

Signed-off-by: Jakub Bednar <[email protected]>
Co-authored-by: Russ Savage <[email protected]>
  • Loading branch information
bednar and Russ Savage authored Feb 9, 2021
1 parent fae5c2c commit 262cdba
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Features

1. [19811](https://github.com/influxdata/influxdb/pull/19811): Add Geo graph type to be able to store in Dashboard cells.
1. [20621](https://github.com/influxdata/influxdb/pull/20621): Add Swift client library to the data loading section of the UI

### Bug Fixes

Expand Down
120 changes: 120 additions & 0 deletions ui/src/writeData/components/clientLibraries/Swift.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
For more detailed and up to date information check out the [GitHub Repository](https://github.com/influxdata/influxdb-client-swift/)

##### Install via Swift Package Manager

Add this line to your `Package.swift`:

```swift
// swift-tools-version:5.3
import PackageDescription

let package = Package(
name: "MyPackage",
dependencies: [
.package(name: "influxdb-client-swift", url: "https://github.com/influxdata/influxdb-client-swift", from: "0.1.0"),
],
targets: [
.target(name: "MyModule", dependencies: [
.product(name: "InfluxDBSwift", package: "influxdb-client-swift"),
// or InfluxDBSwiftApis for management API
.product(name: "InfluxDBSwiftApis", package: "influxdb-client-swift")
])
]
)
```

##### Creating a client

```swift
import Foundation
import InfluxDBSwift

let url = "<%= server %>"
let token = "<%= token %>"
let bucket = "<%= bucket %>"
let org = "<%= org %>"

let client = InfluxDBClient(url: url, token: token)

// always close client at the end
client.close()
```

##### Write Data

```swift
//
// Record defined as String
//
let recordString = "demo,type=string value=1i"
//
// Record defined as Data Point
//
let recordPoint = InfluxDBClient
.Point("demo")
.addTag(key: "type", value: "point")
.addField(key: "value", value: 2)
//
// Record defined as Data Point with Timestamp
//
let recordPointDate = InfluxDBClient
.Point("demo")
.addTag(key: "type", value: "point-timestamp")
.addField(key: "value", value: 2)
.time(time: Date())
//
// Record defined as Tuple
//
let recordTuple = (measurement: "demo", tags: ["type": "tuple"], fields: ["value": 3])

let records: [Any] = [recordString, recordPoint, recordPointDate, recordTuple]

client.getWriteAPI().writeRecords(records: records) { result, error in
// For handle error
if let error = error {
print("Error:\n\n\(error)")
}

// For Success write
if result != nil {
print("Successfully written data:\n\n\(records)")
}
}
```

##### Execute a Flux query

```swift
// Flux query
let query = """
from(bucket: "\(self.bucket)")
|> range(start: -10m)
|> filter(fn: (r) => r["_measurement"] == "cpu")
|> filter(fn: (r) => r["cpu"] == "cpu-total")
|> filter(fn: (r) => r["_field"] == "usage_user" or r["_field"] == "usage_system")
|> last()
"""

print("\nQuery to execute:\n\n\(query)")

client.getQueryAPI().query(query: query) { response, error in
// For handle error
if let error = error {
print("Error:\n\n\(error)")
}

// For Success response
if let response = response {

print("\nSuccess response...\n")
print("CPU usage:")
do {
try response.forEach { record in
print("\t\(record.values["_field"]!): \(record.values["_value"]!)")
}
} catch {
print("Error:\n\n\(error)")
}
}
}
```
9 changes: 9 additions & 0 deletions ui/src/writeData/constants/contentClientLibraries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import RubyMarkdown from 'src/writeData/components/clientLibraries/Ruby.md'
import PHPMarkdown from 'src/writeData/components/clientLibraries/PHP.md'
import KotlinMarkdown from 'src/writeData/components/clientLibraries/Kotlin.md'
import ScalaMarkdown from 'src/writeData/components/clientLibraries/Scala.md'
import SwiftMarkdown from 'src/writeData/components/clientLibraries/Swift.md'

// Graphics
import arduinoLogo from 'src/writeData/graphics/arduinoLogo.svg'
Expand All @@ -27,6 +28,7 @@ import rubyLogo from 'src/writeData/graphics/rubyLogo.svg'
import phpLogo from 'src/writeData/graphics/phpLogo.svg'
import kotlinLogo from 'src/writeData/graphics/kotlinLogo.svg'
import scalaLogo from 'src/writeData/graphics/scalaLogo.svg'
import swiftLogo from 'src/writeData/graphics/swiftLogo.svg'

export const WRITE_DATA_CLIENT_LIBRARIES: WriteDataItem[] = [
{
Expand Down Expand Up @@ -99,6 +101,13 @@ export const WRITE_DATA_CLIENT_LIBRARIES: WriteDataItem[] = [
image: scalaLogo,
markdown: ScalaMarkdown,
},
{
id: 'swift',
name: 'Swift',
url: `${CLIENT_LIBS}/swift`,
image: swiftLogo,
markdown: SwiftMarkdown,
},
]

const WRITE_DATA_CLIENT_LIBRARIES_SECTION: WriteDataSection = {
Expand Down
1 change: 1 addition & 0 deletions ui/src/writeData/graphics/swiftLogo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 262cdba

Please sign in to comment.