Skip to content

Commit 7e07fe2

Browse files
author
Gene Hynson
authored
feat(sub): notebooks integration (#5075)
1 parent 43ca802 commit 7e07fe2

File tree

5 files changed

+105
-1
lines changed

5 files changed

+105
-1
lines changed

src/flows/components/FromTemplatePage.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ const Template: FC = () => {
4646
})
4747
.then(data => {
4848
add(data).then(id => {
49+
if (TEMPLATES[params[0]].callback) {
50+
TEMPLATES[params[0]].callback.apply(this, [params.slice(1), id])
51+
}
4952
history.replace(
5053
`/orgs/${org.id}/${PROJECT_NAME_PLURAL.toLowerCase()}/${id}`
5154
)

src/flows/templates/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export interface Template {
22
type: string // a unique string that identifies a visualization
33
init: (...args: string[]) => any // returns a flow state that will pass the hydrate function in src/flows/context/flow.list.tsx
4+
callback?: (...args: string[]) => any // optional callback func to return the newly created notebook id
45
}
56

67
interface Templates {
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import {DEFAULT_TIME_RANGE} from 'src/shared/constants/timeRanges'
2+
import {AUTOREFRESH_DEFAULT} from 'src/shared/constants'
3+
import {PIPE_DEFINITIONS} from 'src/flows'
4+
import {getBuckets} from 'src/client'
5+
import {getByIDAPI, updateAPI} from 'src/writeData/subscriptions/context/api'
6+
import {Subscription} from 'src/types'
7+
8+
const getSubscriptionMeasurementValues = (sub: Subscription): string[] => {
9+
if (sub.dataFormat === 'json') {
10+
return sub.jsonMeasurementKey.name ? [sub.jsonMeasurementKey.name] : []
11+
} else if (sub.dataFormat === 'string') {
12+
return sub.stringMeasurement.name ? [sub.stringMeasurement.name] : []
13+
} else {
14+
return []
15+
}
16+
}
17+
18+
export default register =>
19+
register({
20+
type: 'subscription',
21+
callback: (subscriptionID: string, notebookID: string) =>
22+
updateAPI({id: subscriptionID, data: {notebookID}}),
23+
init: async (subscriptionID: string) => {
24+
let sub
25+
let buckets
26+
try {
27+
sub = await getByIDAPI({id: subscriptionID})
28+
const bucketsResp = await getBuckets({query: {orgID: sub.orgID}})
29+
buckets = bucketsResp.status === 200 ? bucketsResp.data.buckets : []
30+
} catch {
31+
sub = {}
32+
buckets = []
33+
}
34+
const name = `${sub.name} Subscription`
35+
return {
36+
name,
37+
spec: {
38+
readOnly: false,
39+
range: DEFAULT_TIME_RANGE,
40+
refresh: AUTOREFRESH_DEFAULT,
41+
pipes: [
42+
{
43+
type: 'queryBuilder',
44+
title: `Query ${sub.bucket} Bucket`,
45+
visible: true,
46+
...JSON.parse(
47+
JSON.stringify(PIPE_DEFINITIONS['queryBuilder'].initial)
48+
),
49+
buckets: buckets.filter(a => a.name === sub.bucket),
50+
tags: [
51+
{
52+
key: '_measurement',
53+
values: getSubscriptionMeasurementValues(sub),
54+
aggregateFunctionType: 'filter',
55+
},
56+
],
57+
},
58+
{
59+
title: `Subscription ${sub.name} Data`,
60+
visible: true,
61+
type: 'table',
62+
},
63+
{
64+
type: 'queryBuilder',
65+
title: 'Query _monitoring Bucket',
66+
visible: true,
67+
...JSON.parse(
68+
JSON.stringify(PIPE_DEFINITIONS['queryBuilder'].initial)
69+
),
70+
buckets: buckets.filter(a => a.name === '_monitoring'),
71+
tags: [
72+
{
73+
key: '_measurement',
74+
values: ['subscriptions'],
75+
aggregateFunctionType: 'filter',
76+
},
77+
],
78+
},
79+
{
80+
title: 'Subscriptions Error Data',
81+
visible: true,
82+
type: 'table',
83+
},
84+
],
85+
},
86+
}
87+
},
88+
})

src/types/subscriptions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export interface Subscription {
3333
isActive?: string
3434
flowVersion?: number
3535
timestampPrecision?: string
36+
notebookID?: string
3637
}
3738

3839
export interface JsonSpec {

src/writeData/subscriptions/components/BrokerDetails.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {getOrg} from 'src/organizations/selectors'
2929
import {event} from 'src/cloud/utils/reporting'
3030
import {AppSettingContext} from 'src/shared/contexts/app'
3131
import {checkRequiredFields} from 'src/writeData/subscriptions/utils/form'
32+
import {PROJECT_NAME, PROJECT_NAME_PLURAL} from 'src/flows'
3233

3334
// Types
3435
import {SUBSCRIPTIONS, LOAD_DATA} from 'src/shared/constants/routes'
@@ -143,7 +144,17 @@ const BrokerDetails: FC<Props> = ({
143144
{},
144145
{feature: 'subscriptions'}
145146
)
146-
history.push(`/orgs/${org.id}/notebooks`)
147+
history.push(
148+
currentSubscription.notebookID
149+
? `/orgs/${
150+
org.id
151+
}/${PROJECT_NAME_PLURAL.toLowerCase()}/${
152+
currentSubscription.notebookID
153+
}`
154+
: `/${PROJECT_NAME.toLowerCase()}/from/subscription/${
155+
currentSubscription.id
156+
}`
157+
)
147158
}}
148159
type={ButtonType.Button}
149160
testID="update-broker-form--view-data"

0 commit comments

Comments
 (0)