|
| 1 | +import React, {useContext, useEffect, useState} from 'react' |
| 2 | +import {useDispatch, useSelector} from 'react-redux' |
| 3 | +import { |
| 4 | + Columns, |
| 5 | + ComponentSize, |
| 6 | + Grid, |
| 7 | + InfluxColors, |
| 8 | + Panel, |
| 9 | +} from '@influxdata/clockface' |
| 10 | + |
| 11 | +import {SafeBlankLink} from 'src/utils/SafeBlankLink' |
| 12 | +import WriteDataHelperBuckets from 'src/writeData/components/WriteDataHelperBuckets' |
| 13 | +import CodeSnippet from 'src/shared/components/CodeSnippet' |
| 14 | +import WriteDataDetailsContextProvider, { |
| 15 | + WriteDataDetailsContext, |
| 16 | +} from 'src/writeData/components/WriteDataDetailsContext' |
| 17 | + |
| 18 | +import {getOrg} from 'src/organizations/selectors' |
| 19 | +import DataListening from 'src/homepageExperience/components/DataListening' |
| 20 | +import {getBuckets} from 'src/buckets/actions/thunks' |
| 21 | +import {event} from 'src/cloud/utils/reporting' |
| 22 | + |
| 23 | +const logCopyCodeSnippet = () => { |
| 24 | + event('firstMile.nodejsWizard.writeData.code.copied') |
| 25 | +} |
| 26 | + |
| 27 | +const logDocsOpened = () => { |
| 28 | + event('firstMile.nodejsWizard.writeData.docs.opened') |
| 29 | +} |
| 30 | + |
| 31 | +type WriteDataProps = { |
| 32 | + onSelectBucket: (bucketName: string) => void |
| 33 | +} |
| 34 | + |
| 35 | +export const WriteDataComponent = (props: WriteDataProps) => { |
| 36 | + const org = useSelector(getOrg) |
| 37 | + const dispatch = useDispatch() |
| 38 | + const {onSelectBucket} = props |
| 39 | + |
| 40 | + useEffect(() => { |
| 41 | + dispatch(getBuckets()) |
| 42 | + }, []) |
| 43 | + |
| 44 | + const {bucket} = useContext(WriteDataDetailsContext) |
| 45 | + |
| 46 | + const [selectedBucket, setSelectedBucket] = useState(bucket) |
| 47 | + |
| 48 | + useEffect(() => { |
| 49 | + setSelectedBucket(bucket) |
| 50 | + onSelectBucket(bucket.name) |
| 51 | + }, [bucket]) |
| 52 | + |
| 53 | + const codeSnippet = ` |
| 54 | +const org = '${org.name}' |
| 55 | +const bucket = '${bucket.name}' |
| 56 | +
|
| 57 | +const writeClient = influxDBClient.getWriteApi(org, bucket, 'ns') |
| 58 | +
|
| 59 | +for (let i = 0; i < 5; i++) { |
| 60 | + const point = new Point('measurement1') |
| 61 | + .tag('tagname1', 'tagvalue1') |
| 62 | + .floatField('field1', i) |
| 63 | +
|
| 64 | + setTimeout(() => { |
| 65 | + writeClient.writePoint(point) |
| 66 | + }, i * 1000) // separate points by 1 second |
| 67 | +}` |
| 68 | + |
| 69 | + return ( |
| 70 | + <> |
| 71 | + <h1>Write Data</h1> |
| 72 | + <p> |
| 73 | + To start writing data, we need a place to our time-series store data. We |
| 74 | + call these{' '} |
| 75 | + <SafeBlankLink |
| 76 | + href={`orgs/${org.id}/load-data/buckets`} |
| 77 | + onClick={logDocsOpened} |
| 78 | + > |
| 79 | + buckets. |
| 80 | + </SafeBlankLink> |
| 81 | + </p> |
| 82 | + |
| 83 | + <Panel backgroundColor={InfluxColors.Grey15}> |
| 84 | + <Panel.Body size={ComponentSize.ExtraSmall}> |
| 85 | + <Grid> |
| 86 | + <Grid.Row> |
| 87 | + <Grid.Column widthSM={Columns.Twelve}> |
| 88 | + <WriteDataHelperBuckets useSimplifiedBucketForm={true} /> |
| 89 | + </Grid.Column> |
| 90 | + </Grid.Row> |
| 91 | + </Grid> |
| 92 | + </Panel.Body> |
| 93 | + </Panel> |
| 94 | + <p> |
| 95 | + In this code, we define five data points and write each one for |
| 96 | + InfluxDB. Run the following code in your Nodejs shell: |
| 97 | + </p> |
| 98 | + <CodeSnippet text={codeSnippet} onCopy={logCopyCodeSnippet} /> |
| 99 | + <p style={{marginTop: '20px'}}> |
| 100 | + In the above code snippet, we define five data points and write each on |
| 101 | + the InfluxDB. Each of the 5 points we write has a{' '} |
| 102 | + <SafeBlankLink |
| 103 | + href="https://docs.influxdata.com/influxdb/v1.8/concepts/glossary/#field-key" |
| 104 | + onClick={logDocsOpened} |
| 105 | + > |
| 106 | + field |
| 107 | + </SafeBlankLink>{' '} |
| 108 | + and a{' '} |
| 109 | + <SafeBlankLink |
| 110 | + href="https://docs.influxdata.com/influxdb/v1.8/concepts/glossary/#tag-key" |
| 111 | + onClick={logDocsOpened} |
| 112 | + > |
| 113 | + tag |
| 114 | + </SafeBlankLink> |
| 115 | + . |
| 116 | + </p> |
| 117 | + <p style={{marginTop: '40px'}}> |
| 118 | + Once you write this data, you’ll begin to see the confirmation below |
| 119 | + </p> |
| 120 | + |
| 121 | + <Panel backgroundColor={InfluxColors.Grey15}> |
| 122 | + <Panel.Body> |
| 123 | + <DataListening bucket={selectedBucket.name} /> |
| 124 | + </Panel.Body> |
| 125 | + </Panel> |
| 126 | + </> |
| 127 | + ) |
| 128 | +} |
| 129 | + |
| 130 | +export const WriteData = props => { |
| 131 | + return ( |
| 132 | + <WriteDataDetailsContextProvider> |
| 133 | + <WriteDataComponent onSelectBucket={props.onSelectBucket} /> |
| 134 | + </WriteDataDetailsContextProvider> |
| 135 | + ) |
| 136 | +} |
0 commit comments