Skip to content

Commit 31403f2

Browse files
authored
fix: allow alerts with imports (#4010)
1 parent 140debb commit 31403f2

File tree

3 files changed

+92
-56
lines changed

3 files changed

+92
-56
lines changed

src/flows/components/header/AutoRefreshButton.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, {FC, useCallback, useContext, useEffect} from 'react'
22
import {useDispatch, useSelector} from 'react-redux'
3-
import {Button, ComponentColor} from '@influxdata/clockface'
3+
import {Button, ComponentColor, ComponentStatus} from '@influxdata/clockface'
44
import {PROJECT_NAME} from 'src/flows'
55
import {AppState, AutoRefreshStatus} from 'src/types'
66
import {resetAutoRefresh} from 'src/shared/actions/autoRefresh'
@@ -15,7 +15,7 @@ import {FlowQueryContext} from 'src/flows/context/flow.query'
1515

1616
const AutoRefreshButton: FC = () => {
1717
const {flow} = useContext(FlowContext)
18-
const {queryAll} = useContext(FlowQueryContext)
18+
const {queryAll, generateMap} = useContext(FlowQueryContext)
1919
const dispatch = useDispatch()
2020

2121
const autoRefresh = useSelector(
@@ -85,12 +85,18 @@ const AutoRefreshButton: FC = () => {
8585
}, [queryAll])
8686

8787
const isActive = autoRefresh?.status === AutoRefreshStatus.Active
88+
const hasQueries = generateMap().filter(s => !!s.source).length > 0
8889

8990
return (
9091
<Button
9192
text={
9293
isActive ? `Refreshing Every ${autoRefresh?.label}` : 'Set Auto Refresh'
9394
}
95+
status={
96+
hasQueries === false
97+
? ComponentStatus.Disabled
98+
: ComponentStatus.Default
99+
}
94100
color={isActive ? ComponentColor.Secondary : ComponentColor.Default}
95101
onClick={
96102
isActive

src/flows/components/header/Submit.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Libraries
2-
import React, {FC, useContext, useMemo} from 'react'
2+
import React, {FC, useContext} from 'react'
33
import {IconFont} from '@influxdata/clockface'
44

55
import {SubmitQueryButton} from 'src/timeMachine/components/SubmitQueryButton'
@@ -19,7 +19,7 @@ export const Submit: FC = () => {
1919
const {cancel} = useContext(QueryContext)
2020
const {generateMap, queryAll, status} = useContext(FlowQueryContext)
2121

22-
const hasQueries = useMemo(() => generateMap().length > 0, [generateMap])
22+
const hasQueries = generateMap().filter(s => !!s.source).length > 0
2323

2424
const handleSubmit = () => {
2525
event('Notebook Submit Button Clicked')

src/flows/pipes/Notification/ExportTask.tsx

Lines changed: 82 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
THRESHOLD_TYPES,
1818
} from 'src/flows/pipes/Visualization/threshold'
1919
import {RemoteDataState} from 'src/types'
20+
import {ImportDeclaration} from 'src/types/ast'
2021

2122
// Utils
2223
import {event} from 'src/cloud/utils/reporting'
@@ -78,35 +79,49 @@ const ExportTask: FC = () => {
7879
}, {})
7980

8081
const conditions = THRESHOLD_TYPES[deadmanType].condition(deadman)
82+
const imports = parse(`
83+
import "strings"
84+
import "regexp"
85+
import "influxdata/influxdb/monitor"
86+
import "influxdata/influxdb/schema"
87+
import "influxdata/influxdb/secrets"
88+
import "experimental"
89+
${ENDPOINT_DEFINITIONS[data.endpoint]?.generateImports()}`)
90+
91+
imports.imports = Object.values(
92+
imports.imports.concat(ast.imports || []).reduce((acc, curr) => {
93+
acc[curr.path.value] = curr
94+
return acc
95+
}, {})
96+
).sort((a: ImportDeclaration, b: ImportDeclaration) =>
97+
b.path.value.toLowerCase().localeCompare(a.path.value.toLowerCase())
98+
)
99+
100+
ast.imports = []
101+
102+
const newQuery = `
103+
${format_from_js_file(imports)}
104+
105+
check = {
106+
_check_id: "${id}",
107+
_check_name: "Notebook Generated Deadman Check",
108+
_type: "deadman",
109+
tags: {},
110+
}
81111
82-
const newQuery = `import "strings"
83-
import "regexp"
84-
import "influxdata/influxdb/monitor"
85-
import "influxdata/influxdb/schema"
86-
import "influxdata/influxdb/secrets"
87-
import "experimental"
88-
${ENDPOINT_DEFINITIONS[data.endpoint]?.generateImports()}
89-
90-
check = {
91-
_check_id: "${id}",
92-
_check_name: "Notebook Generated Deadman Check",
93-
_type: "deadman",
94-
tags: {},
95-
}
96-
97-
notification = {
98-
_notification_rule_id: "${id}",
99-
_notification_rule_name: "Notebook Generated Rule",
100-
_notification_endpoint_id: "${id}",
101-
_notification_endpoint_name: "Notebook Generated Endpoint",
102-
}
103-
104-
task_data = ${format_from_js_file(ast)}
105-
trigger = ${conditions}
106-
messageFn = (r) => ("${data.message}")
107-
108-
${ENDPOINT_DEFINITIONS[data.endpoint]?.generateQuery(data.endpointData)}
109-
|> monitor["deadman"](t: experimental["subDuration"](from: now(), d: ${
112+
notification = {
113+
_notification_rule_id: "${id}",
114+
_notification_rule_name: "Notebook Generated Rule",
115+
_notification_endpoint_id: "${id}",
116+
_notification_endpoint_name: "Notebook Generated Endpoint",
117+
}
118+
119+
task_data = ${format_from_js_file(ast)}
120+
trigger = ${conditions}
121+
messageFn = (r) => ("${data.message}")
122+
123+
${ENDPOINT_DEFINITIONS[data.endpoint]?.generateQuery(data.endpointData)}
124+
|> monitor["deadman"](t: experimental["subDuration"](from: now(), d: ${
110125
deadman.deadmanCheckValue
111126
}))`
112127

@@ -190,32 +205,47 @@ const ExportTask: FC = () => {
190205
.map(threshold => THRESHOLD_TYPES[threshold.type].condition(threshold))
191206
.join(' and ')
192207

193-
const newQuery = `import "strings"
194-
import "regexp"
195-
import "influxdata/influxdb/monitor"
196-
import "influxdata/influxdb/schema"
197-
import "influxdata/influxdb/secrets"
198-
import "experimental"
199-
${ENDPOINT_DEFINITIONS[data.endpoint]?.generateImports()}
200-
201-
check = {
202-
_check_id: "${id}",
203-
_check_name: "Notebook Generated Check",
204-
_type: "custom",
205-
tags: {},
206-
}
207-
notification = {
208-
_notification_rule_id: "${id}",
209-
_notification_rule_name: "Notebook Generated Rule",
210-
_notification_endpoint_id: "${id}",
211-
_notification_endpoint_name: "Notebook Generated Endpoint",
212-
}
208+
const imports = parse(`
209+
import "strings"
210+
import "regexp"
211+
import "influxdata/influxdb/monitor"
212+
import "influxdata/influxdb/schema"
213+
import "influxdata/influxdb/secrets"
214+
import "experimental"
215+
${ENDPOINT_DEFINITIONS[data.endpoint]?.generateImports()}`)
216+
217+
imports.imports = Object.values(
218+
imports.imports.concat(ast.imports || []).reduce((acc, curr) => {
219+
acc[curr.path.value] = curr
220+
return acc
221+
}, {})
222+
).sort((a: ImportDeclaration, b: ImportDeclaration) =>
223+
b.path.value.toLowerCase().localeCompare(a.path.value.toLowerCase())
224+
)
225+
226+
ast.imports = []
227+
228+
const newQuery = `
229+
${format_from_js_file(imports)}
230+
231+
check = {
232+
_check_id: "${id}",
233+
_check_name: "Notebook Generated Check",
234+
_type: "custom",
235+
tags: {},
236+
}
237+
notification = {
238+
_notification_rule_id: "${id}",
239+
_notification_rule_name: "Notebook Generated Rule",
240+
_notification_endpoint_id: "${id}",
241+
_notification_endpoint_name: "Notebook Generated Endpoint",
242+
}
213243
214-
task_data = ${format_from_js_file(ast)}
215-
trigger = ${conditions}
216-
messageFn = (r) => ("${data.message}")
244+
task_data = ${format_from_js_file(ast)}
245+
trigger = ${conditions}
246+
messageFn = (r) => ("${data.message}")
217247
218-
${ENDPOINT_DEFINITIONS[data.endpoint]?.generateQuery(data.endpointData)}`
248+
${ENDPOINT_DEFINITIONS[data.endpoint]?.generateQuery(data.endpointData)}`
219249

220250
const newAST = parse(newQuery)
221251

0 commit comments

Comments
 (0)