Skip to content

Commit aae8dfc

Browse files
feat: add schema injection (#4910)
* feat: add injection for bucket and measurement * test: skip the search bar test * feat: add injection for fields * feat: add injection for tag keys and values * feat: add events for schema injection * feat: add injection for bucket and time range * chore: add condition check for bucket.id vs bucket.name * chore: remove injection code for tag key * chore: remove the front-end injection for bucket and time range * chore: remove the unused variable * refactor: remove the intermediate handle function * chore: remove unused import variables * feat: add injection for bucket and measurement * feat: add injection for fields * feat: add injection for tag keys and values * feat: add events for schema injection * feat: add injection for bucket and time range * chore: add condition check for bucket.id vs bucket.name * chore: remove injection code for tag key * chore: remove the front-end injection for bucket and time range * chore: remove the unused variable * refactor: remove the intermediate handle function * chore: remove unused import variables
1 parent c62d804 commit aae8dfc

File tree

3 files changed

+75
-9
lines changed

3 files changed

+75
-9
lines changed

src/dataExplorer/components/FieldSelector.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import WaitingText from 'src/shared/components/WaitingText'
77

88
// Contexts
99
import {FieldsContext} from 'src/dataExplorer/context/fields'
10+
import {FluxQueryBuilderContext} from 'src/dataExplorer/context/fluxQueryBuilder'
1011

1112
// Types
1213
import {RemoteDataState} from 'src/types'
@@ -26,6 +27,7 @@ conceptually similar to a non-indexed column and value.`
2627

2728
const FieldSelector: FC = () => {
2829
const {fields, loading} = useContext(FieldsContext)
30+
const {selectField} = useContext(FluxQueryBuilderContext)
2931
const [fieldsToShow, setFieldsToShow] = useState([])
3032

3133
useEffect(() => {
@@ -48,7 +50,11 @@ const FieldSelector: FC = () => {
4850
list = <WaitingText text="Loading fields" />
4951
} else if (loading === RemoteDataState.Done && fieldsToShow.length) {
5052
list = fieldsToShow.map(field => (
51-
<div key={field} className="field-selector--list-item--selectable">
53+
<div
54+
key={field}
55+
className="field-selector--list-item--selectable"
56+
onClick={() => selectField(field)}
57+
>
5258
<TextBlock text={field} size={ComponentSize.ExtraSmall} />
5359
</div>
5460
))

src/dataExplorer/components/TagSelector.tsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ interface Prop {
2929
}
3030

3131
const TagValues: FC<Prop> = ({loading, tagKey, tagValues}) => {
32-
const {selectedBucket, selectedMeasurement} = useContext(
32+
const {selectedBucket, selectedMeasurement, selectTagValue} = useContext(
3333
FluxQueryBuilderContext
3434
)
3535
const {getTagValues} = useContext(TagsContext)
@@ -40,13 +40,9 @@ const TagValues: FC<Prop> = ({loading, tagKey, tagValues}) => {
4040
setValuesToShow(tagValues.slice(0, LOAD_MORE_LIMIT_INITIAL))
4141
}, [tagValues])
4242

43-
const handleSelectTagValue = (value: string) => {
44-
// TODO: potentially inject tag value into the flux script editor
45-
// still TBD on product
46-
47-
/* eslint-disable no-console */
48-
console.log(value)
49-
/* eslint-disable no-console */
43+
const handleSelectTagValue = (tagValue: string) => {
44+
// Inject tag key and value into editor
45+
selectTagValue(tagKey, tagValue)
5046
}
5147

5248
const handleSelectTagKey = (key: string) => {

src/dataExplorer/context/fluxQueryBuilder.tsx

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,20 @@ import {createLocalStorageStateHook} from 'use-local-storage-state'
1212
import {MeasurementsContext} from 'src/dataExplorer/context/measurements'
1313
import {FieldsContext} from 'src/dataExplorer/context/fields'
1414
import {TagsContext} from 'src/dataExplorer/context/tags'
15+
import {EditorContext} from 'src/shared/contexts/editor'
1516

1617
// Types
1718
import {Bucket} from 'src/types'
1819

20+
// Utils
21+
import {ExecuteCommand} from 'src/languageSupport/languages/flux/lsp/utils'
22+
import {
23+
ExecuteCommandInjectMeasurement,
24+
ExecuteCommandInjectTagValue,
25+
ExecuteCommandInjectField,
26+
} from 'src/languageSupport/languages/flux/lsp/utils'
27+
import {event} from 'src/cloud/utils/reporting'
28+
1929
const useLocalStorageState = createLocalStorageStateHook(
2030
'dataExplorer.schema',
2131
{
@@ -42,6 +52,8 @@ interface FluxQueryBuilderContextType {
4252
searchTerm: string // for searching fields and tags
4353
selectBucket: (bucket: Bucket) => void
4454
selectMeasurement: (measurement: string) => void
55+
selectField: (field: string) => void
56+
selectTagValue: (tagKey: string, tagValue: string) => void
4557
setSearchTerm: (str: string) => void
4658
}
4759

@@ -52,6 +64,8 @@ const DEFAULT_CONTEXT: FluxQueryBuilderContextType = {
5264
searchTerm: '',
5365
selectBucket: (_b: Bucket) => {},
5466
selectMeasurement: (_m: string) => {},
67+
selectField: (_f: string) => {},
68+
selectTagValue: (_k: string, _v: string) => {},
5569
setSearchTerm: (_s: string) => {},
5670
}
5771

@@ -64,6 +78,7 @@ export const FluxQueryBuilderProvider: FC = ({children}) => {
6478
const {getMeasurements} = useContext(MeasurementsContext)
6579
const {getFields, resetFields} = useContext(FieldsContext)
6680
const {getTagKeys, resetTags} = useContext(TagsContext)
81+
const {injectViaLsp} = useContext(EditorContext)
6782

6883
// States
6984
const [selection, setSelection] = useLocalStorageState()
@@ -86,6 +101,20 @@ export const FluxQueryBuilderProvider: FC = ({children}) => {
86101
selection.measurement = measurement
87102
setSelection({...selection})
88103

104+
// Inject measurement
105+
injectViaLsp(ExecuteCommand.InjectionMeasurement, {
106+
bucket:
107+
selection.bucket.type === 'sample'
108+
? selection.bucket.id
109+
: selection.bucket.name,
110+
name: measurement,
111+
} as ExecuteCommandInjectMeasurement)
112+
113+
event('flux.schema.injected', {
114+
command: ExecuteCommand.InjectionMeasurement,
115+
context: 'flux query builder',
116+
})
117+
89118
// Reset fields and tags
90119
resetFields()
91120
resetTags()
@@ -95,6 +124,39 @@ export const FluxQueryBuilderProvider: FC = ({children}) => {
95124
getTagKeys(selection.bucket, measurement)
96125
}
97126

127+
const handleSelectField = (field: string): void => {
128+
// Inject field
129+
injectViaLsp(ExecuteCommand.InjectField, {
130+
bucket:
131+
selection.bucket.type === 'sample'
132+
? selection.bucket.id
133+
: selection.bucket.name,
134+
name: field,
135+
} as ExecuteCommandInjectField)
136+
137+
event('flux.schema.injected', {
138+
command: ExecuteCommand.InjectField,
139+
context: 'flux query builder',
140+
})
141+
}
142+
143+
const handleSelectTagValue = (tagKey: string, tagValue: string): void => {
144+
// Inject tag value
145+
injectViaLsp(ExecuteCommand.InjectTagValue, {
146+
bucket:
147+
selection.bucket.type === 'sample'
148+
? selection.bucket.id
149+
: selection.bucket.name,
150+
name: tagKey,
151+
value: tagValue,
152+
} as ExecuteCommandInjectTagValue)
153+
154+
event('flux.schema.injected', {
155+
command: ExecuteCommand.InjectTagValue,
156+
context: 'flux query builder',
157+
})
158+
}
159+
98160
const handleSearchTerm = useCallback(
99161
(searchTerm: string): void => {
100162
setSearchTerm(searchTerm)
@@ -116,6 +178,8 @@ export const FluxQueryBuilderProvider: FC = ({children}) => {
116178
searchTerm,
117179
selectBucket: handleSelectBucket,
118180
selectMeasurement: handleSelectMeasurement,
181+
selectField: handleSelectField,
182+
selectTagValue: handleSelectTagValue,
119183
setSearchTerm: handleSearchTerm,
120184
}}
121185
>

0 commit comments

Comments
 (0)