|
| 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 | + }) |
0 commit comments