diff --git a/src/homepageExperience/components/steps/go/ExecuteQuerySql.tsx b/src/homepageExperience/components/steps/go/ExecuteQuerySql.tsx index 023f510fd3..fb6097f11c 100644 --- a/src/homepageExperience/components/steps/go/ExecuteQuerySql.tsx +++ b/src/homepageExperience/components/steps/go/ExecuteQuerySql.tsx @@ -21,70 +21,48 @@ FROM 'census' WHERE time >= now() - interval '1 hour' AND ('bees' IS NOT NULL OR 'ants' IS NOT NULL)` - const querySnippet = `// Execute query -query := \`SELECT * - FROM 'census' - WHERE time >= now() - interval '1 hour' - AND ('bees' IS NOT NULL OR 'ants' IS NOT NULL)\` + const querySnippet = ` // Execute query with parameters + query := \`SELECT * + FROM 'census' + WHERE time >= now() - interval '1 hour' + AND ($species1 IS NOT NULL OR $species2 IS NOT NULL)\` -queryOptions := influxdb3.QueryOptions{ - Database: database, -} -iterator, err := client.QueryWithOptions(context.Background(), &queryOptions, query) + iterator, err := client.QueryWithParameters(context.Background(), + query, influxdb3.QueryParameters{ + "species1": "bees", + "species2": "ants", + }, + influxdb3.WithDatabase(database)) -if err != nil { - panic(err) -} + if err != nil { + panic(err) + } -for iterator.Next() { - value := iterator.Value() + val2int := func(v interface{}) int64 { + if v == nil { + return 0 + } + return v.(int64) + } + + for iterator.Next() { + value := iterator.Value() - location := value["location"] - ants := value["ants"] - bees := value["bees"] - fmt.Printf("in %s are %d ants and %d bees\\n", location, ants, bees) -} + location := value["location"] + ants := val2int(value["ants"]) + bees := val2int(value["bees"]) + ts := value["time"].(time.Time) + fmt.Printf("At %s in %s there were %d ants and %d bees\\n", + ts.Format(time.RFC3339), location, ants, bees) + } ` - const resultPreviewSnippet = `RECORD BATCH -[ - { - "ants": null, - "bees": 23, - "location": "Klamath", - "time": "2023-02-08 00:50:22.729692187" - }, - { - "ants": null, - "bees": 28, - "location": "Klamath", - "time": "2023-02-08 00:50:25.22003343" - }, - { - "ants": null, - "bees": 29, - "location": "Klamath", - "time": "2023-02-08 00:50:27.5276304" - }, - { - "ants": 40, - "bees": null, - "location": "Portland", - "time": "2023-02-08 00:50:21.465572125" - }, - { - "ants": 30, - "bees": null, - "location": "Portland", - "time": "2023-02-08 00:50:23.866994344" - }, - { - "ants": 32, - "bees": null, - "location": "Portland", - "time": "2023-02-08 00:50:26.42849497" - } -]` + const resultPreviewSnippet = `At 2024-12-13T13:33:13Z in Klamath there were 0 ants and 28 bees +At 2024-12-13T13:33:15Z in Klamath there were 0 ants and 29 bees +At 2024-12-13T13:33:17Z in Klamath there were 0 ants and 23 bees +At 2024-12-13T13:33:14Z in Portland there were 32 ants and 0 bees +At 2024-12-13T13:33:16Z in Portland there were 40 ants and 0 bees +At 2024-12-13T13:33:18Z in Portland there were 30 ants and 0 bees` return ( <> @@ -104,7 +82,9 @@ for iterator.Next() { with a "census" measurement and either "bees" or "ants" fields.
- Add the following function to the end of your{' '}
+ The following snippet introduces query parameters for the model query
+ above. The fixed values "bees" and "ants" are replaced with "species1"
+ and "species2". Add this to the end of your{' '}
main
function:
You'll need to have{' '}
diff --git a/src/homepageExperience/components/steps/go/WriteData.tsx b/src/homepageExperience/components/steps/go/WriteData.tsx
index e0b03a6db9..b40d379610 100644
--- a/src/homepageExperience/components/steps/go/WriteData.tsx
+++ b/src/homepageExperience/components/steps/go/WriteData.tsx
@@ -48,7 +48,8 @@ export const WriteDataComponent = (props: OwnProps) => {
onSelectBucket(bucket.name)
}, [bucket, onSelectBucket])
- const codeSnippet = `org := "${org.name}"
+ const codeSnippet = `// FOO
+org := "${org.name}"
bucket := "${bucket.name}"
writeAPI := client.WriteAPIBlocking(org, bucket)
for value := 0; value < 5; value++ {
diff --git a/src/homepageExperience/components/steps/go/WriteDataSql.tsx b/src/homepageExperience/components/steps/go/WriteDataSql.tsx
index d5a3492754..c88348ba0c 100644
--- a/src/homepageExperience/components/steps/go/WriteDataSql.tsx
+++ b/src/homepageExperience/components/steps/go/WriteDataSql.tsx
@@ -61,13 +61,14 @@ export const WriteDataSqlComponent = (props: OwnProps) => {
}, [bucket, onSelectBucket])
const initializeCodeSnippet = `package main
-
+
import (
"context"
"fmt"
"time"
+ "os"
- "github.com/InfluxCommunity/influxdb3-go/influxdb3"
+ "github.com/InfluxCommunity/influxdb3-go/v2/influxdb3"
)
func main() {
@@ -95,55 +96,61 @@ func main() {
database := "${bucket.name}"
}`
- const writeCodeSnippet = `data := map[string]map[string]interface{}{
- "point1": {
- "location": "Klamath",
- "species": "bees",
- "count": 23,
- },
- "point2": {
- "location": "Portland",
- "species": "ants",
- "count": 30,
- },
- "point3": {
- "location": "Klamath",
- "species": "bees",
- "count": 28,
- },
- "point4": {
- "location": "Portland",
- "species": "ants",
- "count": 32,
- },
- "point5": {
- "location": "Klamath",
- "species": "bees",
- "count": 29,
- },
- "point6": {
- "location": "Portland",
- "species": "ants",
- "count": 40,
- },
-}
+ const writeCodeSnippet = ` data := map[string]map[string]interface{}{
+ "point1": {
+ "location": "Klamath",
+ "species": "bees",
+ "count": 23,
+ },
+ "point2": {
+ "location": "Portland",
+ "species": "ants",
+ "count": 30,
+ },
+ "point3": {
+ "location": "Klamath",
+ "species": "bees",
+ "count": 28,
+ },
+ "point4": {
+ "location": "Portland",
+ "species": "ants",
+ "count": 32,
+ },
+ "point5": {
+ "location": "Klamath",
+ "species": "bees",
+ "count": 29,
+ },
+ "point6": {
+ "location": "Portland",
+ "species": "ants",
+ "count": 40,
+ },
+ }
-// Write data
-options := influxdb3.WriteOptions{
- Database: database,
-}
-for key := range data {
- point := influxdb3.NewPointWithMeasurement("census").
- AddTag("location", data[key]["location"].(string)).
- AddField(data[key]["species"].(string), data[key]["count"])
+ // convert data to Points
+ points := []*influxdb3.Point{}
+ // start time stamp
+ stamp := time.Now().Add(time.Duration(len(data)) * time.Second * -1)
- if err := client.WritePointsWithOptions(context.Background(), &options, point); err != nil {
- panic(err)
+ for key := range data {
+ points = append(points,
+ influxdb3.NewPointWithMeasurement("census").
+ SetTag("location", data[key]["location"].(string)).
+ SetIntegerField(data[key]["species"].(string),
+ int64(data[key]["count"].(int))).
+ SetTimestamp(stamp))
+ stamp = stamp.Add(time.Second) // Add a second to the stamp
}
- time.Sleep(1 * time.Second) // separate points by 1 second
-}
-
+ // Write data
+ fmt.Printf("Writing %d points\\n", len(points))
+ if err := client.WritePoints(context.Background(),
+ points,
+ influxdb3.WithDatabase(database)); err != nil {
+ panic(err)
+ }
`
return (
@@ -328,7 +335,7 @@ for key := range data {
The program should write data once you run it. After the data is