11// Libraries
22import React , { useEffect } from 'react'
33
4+ // Components
5+ import CodeSnippet from 'src/shared/components/CodeSnippet'
6+ import { DEFAULT_BUCKET } from 'src/writeData/components/WriteDataDetailsContext'
7+ import { SafeBlankLink } from 'src/utils/SafeBlankLink'
8+
49// Utils
510import { event } from 'src/cloud/utils/reporting'
611import { keyboardCopyTriggered , userSelection } from 'src/utils/crossPlatform'
@@ -9,12 +14,77 @@ const logCopyCodeSnippet = () => {
914 event ( 'firstMile.arduinoWizard.executeAggregateQuery.code.copied' )
1015}
1116
17+ const logDocsOpened = ( ) => {
18+ event ( 'firstMile.arduinoWizard.executeAggregateQuery.docs.opened' )
19+ }
20+
1221type OwnProps = {
1322 bucket : string
1423}
1524
1625export const ExecuteAggregateQuery = ( props : OwnProps ) => {
1726 const { bucket} = props
27+ const bucketName = bucket === DEFAULT_BUCKET ? 'sample-bucket' : bucket
28+
29+ const fromBucketSnippet = `from(bucket: "weather-data")
30+ |> range(start: -10m)
31+ |> filter(fn: (r) => r.measurement == "temperature")
32+ |> mean()`
33+
34+ const codeSnippet = `void loop() {
35+ // ... code from Write Data step
36+
37+ // Query will find the min RSSI value for last minute for each connected WiFi network with this device
38+ String query = "from(bucket: \"${ bucketName } \")\n\
39+ |> range(start: -1m)\n\
40+ |> filter(fn: (r) => r._measurement == \"wifi_status\")\n\
41+ |> min()";
42+
43+ // Print composed query
44+ Serial.println("Querying for the mean RSSI value written to the \"${ bucketName } \" bucket in the last 1 min... ");
45+ Serial.println(query);
46+
47+ // Send query to the server and get result
48+ FluxQueryResult result = client.query(query);
49+
50+ Serial.println("Result : ");
51+ // Iterate over rows.
52+ while (result.next()) {
53+ // Get converted value for flux result column 'SSID'
54+ String ssid = result.getValueByName("SSID").getString();
55+ Serial.print("SSID '");
56+ Serial.print(ssid);
57+
58+ Serial.print("' with RSSI ");
59+ // Get value of column named '_value'
60+ long value = result.getValueByName("_value").getLong();
61+ Serial.print(value);
62+
63+ // Get value for the _time column
64+ FluxDateTime time = result.getValueByName("_time").getDateTime();
65+
66+ String timeStr = time.format("%F %T");
67+
68+ Serial.print(" at ");
69+ Serial.print(timeStr);
70+
71+ Serial.println();
72+ }
73+
74+ // Report any error
75+ if (result.getError() != "") {
76+ Serial.print("Query result error: ");
77+ Serial.println(result.getError());
78+ }
79+
80+ // Close the result
81+ result.close();
82+
83+ Serial.println("==========");
84+
85+ delay(5000);
86+
87+ }`
1888
1989 useEffect ( ( ) => {
2090 const fireKeyboardCopyEvent = event => {
@@ -29,5 +99,40 @@ export const ExecuteAggregateQuery = (props: OwnProps) => {
2999 return ( ) => document . removeEventListener ( 'keydown' , fireKeyboardCopyEvent )
30100 } , [ ] )
31101
32- return < h1 > Execute a Flux Aggregate Query on { bucket } </ h1 >
102+ return (
103+ < >
104+ < h1 > Execute a Flux Aggregate Query</ h1 >
105+ < p >
106+ < SafeBlankLink
107+ href = "https://docs.influxdata.com/flux/v0.x/function-types/#aggregates"
108+ onClick = { logDocsOpened }
109+ >
110+ Aggregate functions
111+ </ SafeBlankLink > { ' ' }
112+ take the values of all rows in a table and use them to perform an
113+ aggregate operation. The result is output as a new value in a single-row
114+ table.
115+ </ p >
116+ < p >
117+ An aggregation is applied after the time range and filters, as seen in
118+ the example below.
119+ </ p >
120+ < CodeSnippet
121+ text = { fromBucketSnippet }
122+ showCopyControl = { false }
123+ onCopy = { logCopyCodeSnippet }
124+ language = "properties"
125+ />
126+ < p className = "small-margins" >
127+ In this example, we use the{ ' ' }
128+ < code className = "homepage-wizard--code-highlight" > mean()</ code > function
129+ to calculate the average value of data points in the last 1 minute.
130+ </ p >
131+ < CodeSnippet
132+ text = { codeSnippet }
133+ onCopy = { logCopyCodeSnippet }
134+ language = "arduino"
135+ />
136+ </ >
137+ )
33138}
0 commit comments