@@ -17,6 +17,7 @@ import {
1717 THRESHOLD_TYPES ,
1818} from 'src/flows/pipes/Visualization/threshold'
1919import { RemoteDataState } from 'src/types'
20+ import { ImportDeclaration } from 'src/types/ast'
2021
2122// Utils
2223import { 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