feat(ui): Create dashboards from templates for telegraf plugins

pull/12533/head
Iris Scholten 2019-03-11 17:03:17 -07:00
parent 98915875cf
commit 3df959c8a4
3 changed files with 75 additions and 26 deletions

View File

@ -1,6 +1,7 @@
// Libraries // Libraries
import React, {PureComponent, ChangeEvent} from 'react' import React, {PureComponent, ChangeEvent} from 'react'
import {connect} from 'react-redux' import {connect} from 'react-redux'
import {includes} from 'lodash'
// Components // Components
import {Form, Input, InputType, ComponentSize} from 'src/clockface' import {Form, Input, InputType, ComponentSize} from 'src/clockface'
@ -23,12 +24,18 @@ import {
import {createDashboardsForPlugins as createDashboardsForPluginsAction} from 'src/protos/actions' import {createDashboardsForPlugins as createDashboardsForPluginsAction} from 'src/protos/actions'
import {notify as notifyAction} from 'src/shared/actions/notifications' import {notify as notifyAction} from 'src/shared/actions/notifications'
// Constants
import {
TelegrafConfigCreationSuccess,
TelegrafDashboardCreated,
TelegrafDashboardFailed,
} from 'src/shared/copy/notifications'
// Types // Types
import {AppState} from 'src/types/v2/index' import {AppState} from 'src/types/v2/index'
import {TelegrafPlugin} from 'src/types/v2/dataLoaders' import {TelegrafPlugin, ConfigurationState} from 'src/types/v2/dataLoaders'
import {client} from 'src/utils/api'
// Constants import {IDashboardTemplate} from '@influxdata/influx'
import {TelegrafConfigCreationSuccess} from 'src/shared/copy/notifications'
interface DispatchProps { interface DispatchProps {
onSetTelegrafConfigName: typeof setTelegrafConfigName onSetTelegrafConfigName: typeof setTelegrafConfigName
@ -47,6 +54,8 @@ interface StateProps {
telegrafConfigDescription: string telegrafConfigDescription: string
telegrafPlugins: TelegrafPlugin[] telegrafPlugins: TelegrafPlugin[]
telegrafConfigID: string telegrafConfigID: string
org: string
orgID: string
} }
type Props = DispatchProps & StateProps type Props = DispatchProps & StateProps
@ -118,23 +127,60 @@ export class TelegrafPluginInstructions extends PureComponent<Props> {
} }
private handleFormSubmit = async () => { private handleFormSubmit = async () => {
const { const {onSaveTelegrafConfig, telegrafConfigID, notify} = this.props
onSaveTelegrafConfig,
createDashboardsForPlugins,
telegrafConfigID,
notify,
} = this.props
await onSaveTelegrafConfig() await onSaveTelegrafConfig()
notify(TelegrafConfigCreationSuccess) notify(TelegrafConfigCreationSuccess)
if (!telegrafConfigID) { if (!telegrafConfigID) {
await createDashboardsForPlugins() this.handleCreateDashboardsForPlugins()
} }
this.props.onIncrementStep() this.props.onIncrementStep()
} }
private async handleCreateDashboardsForPlugins() {
const {notify, telegrafPlugins, org, orgID} = this.props
try {
const templatesEntries = await client.templates.getAll(org)
const configuredPlugins = telegrafPlugins.filter(
tp => tp.configured === ConfigurationState.Configured
)
const configuredPluginNames = configuredPlugins.map(t =>
`${t.name}-Template`.toLowerCase()
)
const templatesToGet = templatesEntries.filter(t => {
return includes(configuredPluginNames, t.meta.name.toLowerCase())
})
const pluginsWithTemplates = templatesToGet.map(t => {
return t.meta.name.replace('-Template', '')
})
const pendingTemplates = templatesToGet.map(t =>
client.templates.get(t.id)
)
const templates = await Promise.all(pendingTemplates)
const pendingDashboards = templates.map(t =>
client.dashboards.createFromTemplate(t as IDashboardTemplate, orgID)
)
const dashboards = await Promise.all(pendingDashboards)
if (dashboards.length) {
notify(TelegrafDashboardCreated(pluginsWithTemplates))
}
} catch (err) {
notify(TelegrafDashboardFailed())
}
}
private get sideBarVisible() { private get sideBarVisible() {
const {telegrafPlugins} = this.props const {telegrafPlugins} = this.props
@ -173,6 +219,7 @@ const mstp = ({
telegrafPlugins, telegrafPlugins,
telegrafConfigID, telegrafConfigID,
}, },
steps: {org, orgID},
}, },
}: AppState): StateProps => { }: AppState): StateProps => {
return { return {
@ -180,6 +227,8 @@ const mstp = ({
telegrafConfigDescription, telegrafConfigDescription,
telegrafPlugins, telegrafPlugins,
telegrafConfigID, telegrafConfigID,
org,
orgID,
} }
} }

View File

@ -18,8 +18,8 @@ import {ConfigurationState} from 'src/types/v2/dataLoaders'
// Const // Const
import { import {
ProtoDashboardFailed, TelegrafDashboardFailed,
ProtoDashboardCreated, TelegrafDashboardCreated,
} from 'src/shared/copy/notifications' } from 'src/shared/copy/notifications'
export enum ActionTypes { export enum ActionTypes {
@ -98,10 +98,10 @@ export const createDashboardsForPlugins = () => async (
}) })
if (plugins.length) { if (plugins.length) {
dispatch(notify(ProtoDashboardCreated(plugins))) dispatch(notify(TelegrafDashboardCreated(plugins)))
} }
} catch (err) { } catch (err) {
console.error(err) console.error(err)
dispatch(notify(ProtoDashboardFailed())) dispatch(notify(TelegrafDashboardFailed()))
} }
} }

View File

@ -769,17 +769,6 @@ export const fluxTimeSeriesError = (message: string): Notification => ({
}) })
// Protos // Protos
export const ProtoDashboardCreated = (configs: string[]): Notification => ({
...defaultSuccessNotification,
message: `Successfully created dashboards for telegraf plugin${
configs.length > 1 ? 's' : ''
}: ${configs.join(', ')}.`,
})
export const ProtoDashboardFailed = (): Notification => ({
...defaultErrorNotification,
message: `Could not create dashboards for one or more plugins`,
})
export const importSucceeded = (): Notification => ({ export const importSucceeded = (): Notification => ({
...defaultSuccessNotification, ...defaultSuccessNotification,
@ -792,6 +781,17 @@ export const importFailed = (): Notification => ({
}) })
// Templates // Templates
export const TelegrafDashboardCreated = (configs: string[]): Notification => ({
...defaultSuccessNotification,
message: `Successfully created dashboards for telegraf plugin${
configs.length > 1 ? 's' : ''
}: ${configs.join(', ')}.`,
})
export const TelegrafDashboardFailed = (): Notification => ({
...defaultErrorNotification,
message: `Could not create dashboards for one or more plugins`,
})
export const importTaskSucceeded = (): Notification => ({ export const importTaskSucceeded = (): Notification => ({
...defaultSuccessNotification, ...defaultSuccessNotification,