diff --git a/ui/src/dashboards/actions/thunks.ts b/ui/src/dashboards/actions/thunks.ts index 8542721e055..0ee123e215f 100644 --- a/ui/src/dashboards/actions/thunks.ts +++ b/ui/src/dashboards/actions/thunks.ts @@ -6,7 +6,6 @@ import {push} from 'connected-react-router' // APIs import * as dashAPI from 'src/dashboards/apis' import * as api from 'src/client' -import * as tempAPI from 'src/templates/api' import {createCellWithView} from 'src/cells/actions/thunks' // Schemas @@ -29,7 +28,6 @@ import { updateTimeRangeFromQueryParams, } from 'src/dashboards/actions/ranges' import {getVariables, hydrateVariables} from 'src/variables/actions/thunks' -import {setExportTemplate} from 'src/templates/actions/creators' import {checkDashboardLimits} from 'src/cloud/actions/limits' import {setCells, Action as CellAction} from 'src/cells/actions/creators' import { @@ -42,9 +40,6 @@ import {setLabelOnResource} from 'src/labels/actions/creators' import * as creators from 'src/dashboards/actions/creators' // Utils -import {filterUnusedVars} from 'src/shared/utils/filterUnusedVars' -import {dashboardToTemplate} from 'src/shared/utils/resourceToTemplate' -import {exportVariables} from 'src/variables/utils/exportVariables' import {getSaveableView} from 'src/timeMachine/selectors' import {incrementCloneName} from 'src/utils/naming' import {isLimitError} from 'src/cloud/utils/limits' @@ -61,18 +56,14 @@ import { GetState, View, Cell, - DashboardTemplate, Label, RemoteDataState, DashboardEntities, ViewEntities, ResourceType, - VariableEntities, - Variable, LabelEntities, } from 'src/types' import {CellsWithViewProperties} from 'src/client' -import {arrayOfVariables} from 'src/schemas/variables' type Action = creators.Action @@ -285,37 +276,6 @@ export const getDashboards = () => async ( } } -export const createDashboardFromTemplate = ( - template: DashboardTemplate -) => async (dispatch, getState: GetState) => { - try { - const org = getOrg(getState()) - - await tempAPI.createDashboardFromTemplate(template, org.id) - - const resp = await api.getDashboards({query: {orgID: org.id}}) - - if (resp.status !== 200) { - throw new Error(resp.data.message) - } - - const dashboards = normalize( - resp.data.dashboards, - arrayOfDashboards - ) - - dispatch(creators.setDashboards(RemoteDataState.Done, dashboards)) - dispatch(notify(copy.importDashboardSucceeded())) - dispatch(checkDashboardLimits()) - } catch (error) { - if (isLimitError(error)) { - dispatch(notify(copy.resourceLimitReached('dashboards'))) - } else { - dispatch(notify(copy.importDashboardFailed(error))) - } - } -} - export const deleteDashboard = (dashboardID: string, name: string) => async ( dispatch ): Promise => { @@ -489,70 +449,6 @@ export const removeDashboardLabel = ( } } -export const convertToTemplate = (dashboardID: string) => async ( - dispatch, - getState: GetState -): Promise => { - try { - dispatch(setExportTemplate(RemoteDataState.Loading)) - const state = getState() - const org = getOrg(state) - - const dashResp = await api.getDashboard({dashboardID}) - - if (dashResp.status !== 200) { - throw new Error(dashResp.data.message) - } - - const {entities, result} = normalize( - dashResp.data, - dashboardSchema - ) - - const dashboard = entities.dashboards[result] - const cells = dashboard.cells.map(cellID => entities.cells[cellID]) - - const pendingViews = dashboard.cells.map(cellID => - dashAPI.getView(dashboardID, cellID) - ) - - const views = await Promise.all(pendingViews) - const resp = await api.getVariables({query: {orgID: org.id}}) - if (resp.status !== 200) { - throw new Error(resp.data.message) - } - - let vars = [] - - // dumb bug - // https://github.com/paularmstrong/normalizr/issues/290 - if (resp.data.variables.length) { - const normVars = normalize( - resp.data.variables, - arrayOfVariables - ) - - vars = Object.values(normVars.entities.variables) - } - - const variables = filterUnusedVars(vars, views) - const exportedVariables = exportVariables(variables, vars) - const dashboardTemplate = dashboardToTemplate( - state, - dashboard, - cells, - views, - exportedVariables - ) - - dispatch(setExportTemplate(RemoteDataState.Done, dashboardTemplate)) - } catch (error) { - console.error(error) - dispatch(setExportTemplate(RemoteDataState.Error)) - dispatch(notify(copy.createTemplateFailed(error))) - } -} - export const saveVEOView = (dashboardID: string) => async ( dispatch, getState: GetState diff --git a/ui/src/shared/components/resource_sort_dropdown/generateSortItems.ts b/ui/src/shared/components/resource_sort_dropdown/generateSortItems.ts index a09d9021fd7..f753e1ab825 100644 --- a/ui/src/shared/components/resource_sort_dropdown/generateSortItems.ts +++ b/ui/src/shared/components/resource_sort_dropdown/generateSortItems.ts @@ -6,7 +6,6 @@ import { Task, Variable, Label, - Template, Bucket, Telegraf, Scraper, @@ -17,7 +16,6 @@ export type DashboardSortKey = keyof Dashboard | 'meta.updatedAt' export type TaskSortKey = keyof Task export type VariableSortKey = keyof Variable | 'arguments.type' export type LabelSortKey = keyof Label | 'properties.description' -export type TemplateSortKey = keyof Template | 'meta.name' | 'meta.description' export type BucketSortKey = keyof Bucket | 'retentionRules[0].everySeconds' export type TelegrafSortKey = keyof Telegraf export type ScraperSortKey = keyof Scraper @@ -28,7 +26,6 @@ export type SortKey = | TaskSortKey | VariableSortKey | LabelSortKey - | TemplateSortKey | BucketSortKey | TelegrafSortKey | ScraperSortKey @@ -177,33 +174,6 @@ export const generateSortItems = ( sortDirection: Sort.Descending, }, ] - case ResourceType.Templates: - return [ - { - label: 'Name (A → Z)', - sortKey: 'meta.name', - sortType: SortTypes.String, - sortDirection: Sort.Ascending, - }, - { - label: 'Name (Z → A)', - sortKey: 'meta.name', - sortType: SortTypes.String, - sortDirection: Sort.Descending, - }, - { - label: 'Description (A → Z)', - sortKey: 'meta.description', - sortType: SortTypes.String, - sortDirection: Sort.Ascending, - }, - { - label: 'Description (Z → A)', - sortKey: 'meta.description', - sortType: SortTypes.String, - sortDirection: Sort.Descending, - }, - ] case ResourceType.Buckets: return [ { diff --git a/ui/src/shared/copy/notifications.ts b/ui/src/shared/copy/notifications.ts index 0d0dfe921d7..b8cfe1db1c4 100644 --- a/ui/src/shared/copy/notifications.ts +++ b/ui/src/shared/copy/notifications.ts @@ -375,36 +375,6 @@ export const createResourceFromTemplateFailed = ( message: `Failed to create from template: ${error}`, }) -export const updateTemplateSucceeded = (): Notification => ({ - ...defaultSuccessNotification, - message: `Successfully updated template.`, -}) - -export const updateTemplateFailed = (error: string): Notification => ({ - ...defaultErrorNotification, - message: `Failed to update template: ${error}`, -}) - -export const deleteTemplateFailed = (error: string): Notification => ({ - ...defaultErrorNotification, - message: `Failed to delete template: ${error}`, -}) - -export const deleteTemplateSuccess = (): Notification => ({ - ...defaultSuccessNotification, - message: 'Template was deleted successfully', -}) - -export const cloneTemplateFailed = (error: string): Notification => ({ - ...defaultErrorNotification, - message: `Failed to clone template: ${error}`, -}) - -export const cloneTemplateSuccess = (): Notification => ({ - ...defaultSuccessNotification, - message: 'Template cloned successfully', -}) - export const resourceSavedAsTemplate = ( resourceName: string ): Notification => ({ diff --git a/ui/src/shared/utils/resourceToTemplate.test.ts b/ui/src/shared/utils/resourceToTemplate.test.ts deleted file mode 100644 index 296ffe84c17..00000000000 --- a/ui/src/shared/utils/resourceToTemplate.test.ts +++ /dev/null @@ -1,438 +0,0 @@ -import { - labelToRelationship, - labelToIncluded, - taskToTemplate, - variableToTemplate, - dashboardToTemplate, -} from 'src/shared/utils/resourceToTemplate' -import {TemplateType} from '@influxdata/influx' -import {createVariable} from 'src/variables/mocks' -import { - myDashboard, - myView, - myVariable, - myfavelabel, - myfavetask, - myCell, -} from 'src/shared/utils/mocks/resourceToTemplate' - -// Libraries -import {RemoteDataState, AppState} from 'src/types' - -describe('resourceToTemplate', () => { - const appState = { - resources: { - labels: { - byID: { - [myfavelabel.id]: myfavelabel, - allIDs: [myfavelabel.id], - status: RemoteDataState.Done, - }, - }, - }, - } - - describe('labelToRelationship', () => { - it('converts a label to a relationship struct', () => { - const actual = labelToRelationship(myfavelabel) - const expected = {type: TemplateType.Label, id: myfavelabel.id} - - expect(actual).toEqual(expected) - }) - }) - - describe('labelToIncluded', () => { - it('converts a label to a data structure in included', () => { - const actual = labelToIncluded(myfavelabel) - const expected = { - type: TemplateType.Label, - id: myfavelabel.id, - attributes: { - name: myfavelabel.name, - properties: { - color: myfavelabel.properties.color, - description: myfavelabel.properties.description, - }, - }, - } - - expect(actual).toEqual(expected) - }) - }) - - describe('variableToTemplate', () => { - it('converts a variable with dependencies to a template', () => { - const a = { - ...createVariable('a', 'x.b + 1'), - labels: [myfavelabel.id], - } - - const b = createVariable('b', '9000') - - const dependencies: any = [a, b] - - const actual = variableToTemplate( - (appState as unknown) as AppState, - myVariable, - dependencies - ) - - const expected = { - meta: { - version: '1', - name: 'beep-Template', - type: 'variable', - description: 'template created from variable: beep', - }, - content: { - data: { - type: 'variable', - id: '039ae3b3b74b0000', - attributes: { - name: 'beep', - arguments: { - type: 'query', - values: { - query: 'f(x: v.a)', - language: 'flux', - }, - }, - selected: null, - }, - relationships: { - variable: { - data: [ - { - id: 'a', - type: 'variable', - }, - { - id: 'b', - type: 'variable', - }, - ], - }, - label: { - data: [], - }, - }, - }, - included: [ - { - type: 'variable', - id: 'a', - attributes: { - name: 'a', - arguments: { - type: 'query', - values: { - query: 'x.b + 1', - language: 'flux', - }, - }, - selected: [], - }, - relationships: { - label: { - data: [ - { - type: 'label', - id: 'myfavelabel1', - }, - ], - }, - }, - }, - { - type: 'variable', - id: 'b', - attributes: { - name: 'b', - arguments: { - type: 'query', - values: { - query: '9000', - language: 'flux', - }, - }, - selected: [], - }, - relationships: { - label: { - data: [], - }, - }, - }, - { - id: 'myfavelabel1', - type: 'label', - attributes: { - name: '1label', - properties: {color: 'fffff', description: 'omg'}, - }, - }, - ], - }, - labels: [], - } - - expect(actual).toEqual(expected) - }) - }) - - describe('taskToTemplate', () => { - it('converts a task to a template', () => { - const label = { - id: '037b0c86a92a2000', - name: 'yum', - properties: { - color: '#FF8564', - description: '', - }, - } - - const state = { - resources: { - labels: { - byID: { - [label.id]: label, - allIDs: [label.id], - status: RemoteDataState.Done, - }, - }, - }, - } - - const actual = taskToTemplate((state as unknown) as AppState, { - ...myfavetask, - labels: [label.id], - }) - - const expected = { - content: { - data: { - type: 'task', - attributes: { - every: '24h0m0s', - flux: - 'option task = {name: "lala", every: 24h0m0s, offset: 1m0s}\n\nfrom(bucket: "defnuck")\n\t|> range(start: -task.every)', - name: 'lala', - offset: '1m0s', - status: 'active', - }, - relationships: { - label: { - data: [ - { - id: '037b0c86a92a2000', - type: 'label', - }, - ], - }, - }, - }, - included: [ - { - attributes: { - name: 'yum', - properties: { - color: '#FF8564', - description: '', - }, - }, - id: '037b0c86a92a2000', - type: TemplateType.Label, - }, - ], - }, - labels: [], - meta: { - description: 'template created from task: lala', - name: 'lala-Template', - type: 'task', - version: '1', - }, - } - - expect(actual).toEqual(expected) - }) - }) - - describe('dashboardToTemplate', () => { - it('can convert a dashboard to template', () => { - const myLabeledVar = { - ...createVariable('var_1', 'labeled var!'), - labels: [myfavelabel.id], - } - - const dashboardWithDupeLabel = { - ...myDashboard, - labels: [myfavelabel.id], - } - - const actual = dashboardToTemplate( - (appState as unknown) as AppState, - dashboardWithDupeLabel, - [myCell], - [myView], - [myLabeledVar] - ) - - const expected = { - meta: { - version: '1', - name: 'MyDashboard-Template', - type: 'dashboard', - description: 'template created from dashboard: MyDashboard', - }, - content: { - data: { - type: 'dashboard', - attributes: { - name: 'MyDashboard', - description: '', - }, - relationships: { - label: { - data: [ - { - id: 'myfavelabel1', - type: 'label', - }, - ], - }, - cell: { - data: [ - { - type: 'cell', - id: 'cell_view_1', - }, - ], - }, - variable: { - data: [ - { - type: 'variable', - id: 'var_1', - }, - ], - }, - }, - }, - included: [ - { - id: 'myfavelabel1', - type: 'label', - attributes: { - name: '1label', - properties: {color: 'fffff', description: 'omg'}, - }, - }, - { - id: 'cell_view_1', - type: 'cell', - attributes: { - x: 0, - y: 0, - w: 4, - h: 4, - }, - relationships: { - view: { - data: { - type: 'view', - id: 'cell_view_1', - }, - }, - }, - }, - { - type: 'view', - id: 'cell_view_1', - attributes: { - name: 'My Cell', - properties: { - shape: 'chronograf-v2', - queries: [ - { - text: 'v.bucket', - editMode: 'advanced', - name: 'View Query', - builderConfig: { - buckets: [], - tags: [ - { - key: '_measurement', - values: [], - aggregateFunctionType: 'filter', - }, - ], - functions: [{name: 'mean'}], - aggregateWindow: {period: 'auto', fillValues: false}, - }, - }, - ], - axes: { - x: { - bounds: ['', ''], - label: '', - prefix: '', - suffix: '', - base: '10', - scale: 'linear', - }, - y: { - bounds: ['', ''], - label: '', - prefix: '', - suffix: '', - base: '10', - scale: 'linear', - }, - }, - type: 'xy', - legend: {}, - geom: 'line', - colors: [], - note: '', - showNoteWhenEmpty: false, - xColumn: null, - yColumn: null, - position: 'overlaid', - }, - }, - }, - { - type: 'variable', - id: 'var_1', - attributes: { - name: 'var_1', - arguments: { - type: 'query', - values: { - query: 'labeled var!', - language: 'flux', - }, - }, - selected: [], - }, - relationships: { - label: { - data: [ - { - type: 'label', - id: 'myfavelabel1', - }, - ], - }, - }, - }, - ], - }, - labels: [], - } - - expect(actual).toEqual(expected) - }) - }) -}) diff --git a/ui/src/shared/utils/resourceToTemplate.ts b/ui/src/shared/utils/resourceToTemplate.ts deleted file mode 100644 index ec08df3dc70..00000000000 --- a/ui/src/shared/utils/resourceToTemplate.ts +++ /dev/null @@ -1,350 +0,0 @@ -import {get, pick, flatMap, uniqBy} from 'lodash' - -import {defaultBuilderConfig} from 'src/views/helpers' -import {getLabels} from 'src/resources/selectors' - -import { - AppState, - Task, - Label, - Dashboard, - DashboardQuery, - Cell, - View, - Variable, - LabelRelationship, - LabelIncluded, -} from 'src/types' -import {TemplateType, DocumentCreate, ITemplate} from '@influxdata/influx' - -const CURRENT_TEMPLATE_VERSION = '1' - -const blankTemplate = () => ({ - meta: {version: CURRENT_TEMPLATE_VERSION}, - content: {data: {}, included: []}, - labels: [], -}) - -const blankTaskTemplate = () => { - const baseTemplate = blankTemplate() - return { - ...baseTemplate, - meta: {...baseTemplate.meta, type: TemplateType.Task}, - content: { - ...baseTemplate.content, - data: {...baseTemplate.content.data, type: TemplateType.Task}, - }, - } -} - -const blankVariableTemplate = () => { - const baseTemplate = blankTemplate() - return { - ...baseTemplate, - meta: {...baseTemplate.meta, type: TemplateType.Variable}, - content: { - ...baseTemplate.content, - data: {...baseTemplate.content.data, type: TemplateType.Variable}, - }, - } -} - -const blankDashboardTemplate = () => { - const baseTemplate = blankTemplate() - return { - ...baseTemplate, - meta: {...baseTemplate.meta, type: TemplateType.Dashboard}, - content: { - ...baseTemplate.content, - data: {...baseTemplate.content.data, type: TemplateType.Dashboard}, - }, - } -} - -export const labelToRelationship = (l: Label): LabelRelationship => { - return {type: TemplateType.Label, id: l.id} -} - -export const labelToIncluded = (l: Label): LabelIncluded => { - return { - type: TemplateType.Label, - id: l.id, - attributes: { - name: l.name, - properties: l.properties, - }, - } -} - -export const taskToTemplate = ( - state: AppState, - task: Task, - baseTemplate = blankTaskTemplate() -): DocumentCreate => { - const taskName = get(task, 'name', '') - const templateName = `${taskName}-Template` - - const taskAttributes = pick(task, [ - 'status', - 'name', - 'flux', - 'every', - 'cron', - 'offset', - ]) - - const taskLabels = getLabels(state, task.labels) - const includedLabels = taskLabels.map(label => labelToIncluded(label)) - const relationshipsLabels = taskLabels.map(label => - labelToRelationship(label) - ) - - const template = { - ...baseTemplate, - meta: { - ...baseTemplate.meta, - name: templateName, - description: `template created from task: ${taskName}`, - }, - content: { - ...baseTemplate.content, - data: { - ...baseTemplate.content.data, - type: TemplateType.Task, - attributes: taskAttributes, - relationships: { - [TemplateType.Label]: {data: relationshipsLabels}, - }, - }, - included: [...baseTemplate.content.included, ...includedLabels], - }, - } - - return template -} - -const viewToIncluded = (view: View) => { - let properties = view.properties - - if ('queries' in properties) { - const sanitizedQueries = properties.queries.map((q: DashboardQuery) => { - return { - ...q, - editMode: 'advanced' as 'advanced', - builderConfig: defaultBuilderConfig(), - } - }) - - properties = { - ...properties, - queries: sanitizedQueries, - } - } - - return { - type: TemplateType.View, - id: view.id, - attributes: {name: view.name, properties}, - } -} - -const viewToRelationship = (view: View) => ({ - type: TemplateType.View, - id: view.id, -}) - -const cellToIncluded = (cell: Cell, views: View[]) => { - const cellView = views.find(v => v.id === cell.id) - const viewRelationship = viewToRelationship(cellView) - - const cellAttributes = pick(cell, ['x', 'y', 'w', 'h']) - - return { - id: cell.id, - type: TemplateType.Cell, - attributes: cellAttributes, - relationships: { - [TemplateType.View]: { - data: viewRelationship, - }, - }, - } -} - -const cellToRelationship = (cell: Cell) => ({ - type: TemplateType.Cell, - id: cell.id, -}) - -export const variableToTemplate = ( - state: AppState, - v: Variable, - dependencies: Variable[], - baseTemplate = blankVariableTemplate() -) => { - const labelsByID = state.resources.labels.byID - const variableName = get(v, 'name', '') - const templateName = `${variableName}-Template` - const variableData = variableToIncluded(v, labelsByID) - const variableRelationships = dependencies.map(d => variableToRelationship(d)) - const includedDependencies = dependencies.map(d => - variableToIncluded(d, labelsByID) - ) - - const vLabels = getLabels(state, v.labels) - - const includedLabels = vLabels.map(label => labelToIncluded(label)) - const labelRelationships = vLabels.map(label => labelToRelationship(label)) - const includedDependentLabels = flatMap(dependencies, d => { - const dLabels = getLabels(state, d.labels) - return dLabels.map(label => labelToIncluded(label)) - }) - - return { - ...baseTemplate, - meta: { - ...baseTemplate.meta, - name: templateName, - description: `template created from variable: ${variableName}`, - }, - content: { - ...baseTemplate.content, - data: { - ...baseTemplate.content.data, - ...variableData, - relationships: { - [TemplateType.Variable]: { - data: [...variableRelationships], - }, - [TemplateType.Label]: { - data: [...labelRelationships], - }, - }, - }, - included: [ - ...includedDependencies, - ...includedLabels, - ...includedDependentLabels, - ], - }, - } -} - -type LabelsByID = AppState['resources']['labels']['byID'] - -const variableToIncluded = (v: Variable, labelsByID: LabelsByID) => { - const variableAttributes = pick(v, ['name', 'arguments', 'selected']) - const labelRelationships = v.labels - .map(labelID => { - const label = labelsByID[labelID] - if (!label) { - return null - } - - return labelToRelationship(label) - }) - .filter(label => !!label) - - return { - id: v.id, - type: TemplateType.Variable, - attributes: variableAttributes, - relationships: { - [TemplateType.Label]: { - data: [...labelRelationships], - }, - }, - } -} - -const variableToRelationship = (v: Variable) => ({ - type: TemplateType.Variable, - id: v.id, -}) - -export const dashboardToTemplate = ( - state: AppState, - dashboard: Dashboard, - cells: Cell[], - views: View[], - variables: Variable[], - baseTemplate = blankDashboardTemplate() -): DocumentCreate => { - const labelsByID = state.resources.labels.byID - const dashboardName = get(dashboard, 'name', '') - const templateName = `${dashboardName}-Template` - - const dashboardAttributes = pick(dashboard, ['name', 'description']) - - const dashboardLabels = getLabels(state, dashboard.labels) - const dashboardIncludedLabels = dashboardLabels.map(label => - labelToIncluded(label) - ) - const relationshipsLabels = dashboardLabels.map(label => - labelToRelationship(label) - ) - - const includedCells = cells.map(c => cellToIncluded(c, views)) - const relationshipsCells = cells.map(c => cellToRelationship(c)) - - const includedVariables = variables.map(v => - variableToIncluded(v, labelsByID) - ) - - const variableIncludedLabels = flatMap(variables, v => - getLabels(state, v.labels).map(label => labelToIncluded(label)) - ) - - const relationshipsVariables = variables.map(v => variableToRelationship(v)) - - const includedViews = views.map(v => viewToIncluded(v)) - const includedLabels = uniqBy( - [...dashboardIncludedLabels, ...variableIncludedLabels], - 'id' - ) - - const template = { - ...baseTemplate, - meta: { - ...baseTemplate.meta, - name: templateName, - description: `template created from dashboard: ${dashboardName}`, - }, - content: { - ...baseTemplate.content, - data: { - ...baseTemplate.content.data, - type: TemplateType.Dashboard, - attributes: dashboardAttributes, - relationships: { - [TemplateType.Label]: {data: relationshipsLabels}, - [TemplateType.Cell]: {data: relationshipsCells}, - [TemplateType.Variable]: {data: relationshipsVariables}, - }, - }, - included: [ - ...baseTemplate.content.included, - ...includedLabels, - ...includedCells, - ...includedViews, - ...includedVariables, - ], - }, - } - - return template -} - -export const templateToExport = (template: ITemplate): DocumentCreate => { - const pickedTemplate = pick(template, ['meta', 'content']) - const labelsArray = template.labels.map(l => l.name) - const templateWithLabels = {...pickedTemplate, labels: labelsArray} - return templateWithLabels -} - -export const addOrgIDToTemplate = ( - template: DocumentCreate, - orgID: string -): DocumentCreate => { - return {...template, orgID} -} diff --git a/ui/src/tasks/actions/thunks.ts b/ui/src/tasks/actions/thunks.ts index c808d4c8e3f..925464173b0 100644 --- a/ui/src/tasks/actions/thunks.ts +++ b/ui/src/tasks/actions/thunks.ts @@ -5,13 +5,11 @@ import {normalize} from 'normalizr' // APIs import * as api from 'src/client' -import {createTaskFromTemplate as createTaskFromTemplateAJAX} from 'src/templates/api' // Schemas import {taskSchema, arrayOfTasks} from 'src/schemas/tasks' // Actions -import {setExportTemplate} from 'src/templates/actions/creators' import {notify, Action as NotifyAction} from 'src/shared/actions/notifications' import { addTask, @@ -34,7 +32,6 @@ import * as copy from 'src/shared/copy/notifications' // Types import { Label, - TaskTemplate, Task, GetState, TaskSchedule, @@ -46,7 +43,6 @@ import { // Utils import {getErrorMessage} from 'src/utils/api' import {insertPreambleInScript} from 'src/shared/utils/insertPreambleInScript' -import {taskToTemplate} from 'src/shared/utils/resourceToTemplate' import {isLimitError} from 'src/cloud/utils/limits' import {checkTaskLimits} from 'src/cloud/actions/limits' import {getOrg} from 'src/organizations/selectors' @@ -432,52 +428,6 @@ export const getLogs = (taskID: string, runID: string) => async ( } } -export const convertToTemplate = (taskID: string) => async ( - dispatch, - getState: GetState -): Promise => { - try { - dispatch(setExportTemplate(RemoteDataState.Loading)) - const resp = await api.getTask({taskID}) - if (resp.status !== 200) { - throw new Error(resp.data.message) - } - - const {entities, result} = normalize( - resp.data, - taskSchema - ) - - const taskTemplate = taskToTemplate(getState(), entities.tasks[result]) - - dispatch(setExportTemplate(RemoteDataState.Done, taskTemplate)) - } catch (error) { - dispatch(setExportTemplate(RemoteDataState.Error)) - dispatch(notify(copy.createTemplateFailed(error))) - } -} - -export const createTaskFromTemplate = (template: TaskTemplate) => async ( - dispatch: Dispatch, - getState: GetState -): Promise => { - try { - const org = getOrg(getState()) - - await createTaskFromTemplateAJAX(template, org.id) - - dispatch(getTasks()) - dispatch(notify(copy.importTaskSucceeded())) - dispatch(checkTaskLimits()) - } catch (error) { - if (isLimitError(error)) { - dispatch(notify(copy.resourceLimitReached('tasks'))) - } else { - dispatch(notify(copy.importTaskFailed(error))) - } - } -} - export const runDuration = (finishedAt: Date, startedAt: Date): string => { let timeTag = 'seconds' diff --git a/ui/src/templates/actions/creators.ts b/ui/src/templates/actions/creators.ts index b8aefe4b4b1..962c8fe66c7 100644 --- a/ui/src/templates/actions/creators.ts +++ b/ui/src/templates/actions/creators.ts @@ -1,23 +1,9 @@ // Types -import { - CommunityTemplate, - RemoteDataState, - TemplateSummaryEntities, -} from 'src/types' -import {DocumentCreate} from '@influxdata/influx' -import {NormalizedSchema} from 'normalizr' - +import {CommunityTemplate} from 'src/types' import {InstalledStack} from 'src/types' -export const ADD_TEMPLATE_SUMMARY = 'ADD_TEMPLATE_SUMMARY' -export const GET_TEMPLATE_SUMMARIES_FOR_ORG = 'GET_TEMPLATE_SUMMARIES_FOR_ORG' -export const POPULATE_TEMPLATE_SUMMARIES = 'POPULATE_TEMPLATE_SUMMARIES' -export const REMOVE_TEMPLATE_SUMMARY = 'REMOVE_TEMPLATE_SUMMARY' export const SET_COMMUNITY_TEMPLATE_TO_INSTALL = 'SET_COMMUNITY_TEMPLATE_TO_INSTALL' -export const SET_EXPORT_TEMPLATE = 'SET_EXPORT_TEMPLATE' -export const SET_TEMPLATE_SUMMARY = 'SET_TEMPLATE_SUMMARY' -export const SET_TEMPLATES_STATUS = 'SET_TEMPLATES_STATUS' export const TOGGLE_TEMPLATE_RESOURCE_INSTALL = 'TOGGLE_TEMPLATE_RESOURCE_INSTALL' @@ -25,72 +11,12 @@ export const SET_STACKS = 'SET_STACKS' export const DELETE_STACKS = 'DELETE_STACKS' export type Action = - | ReturnType - | ReturnType - | ReturnType - | ReturnType - | ReturnType - | ReturnType | ReturnType | ReturnType | ReturnType | ReturnType -type TemplateSummarySchema = NormalizedSchema< - TemplateSummaryEntities, - R -> - // Action Creators -export const addTemplateSummary = (schema: TemplateSummarySchema) => - ({ - type: ADD_TEMPLATE_SUMMARY, - schema, - } as const) - -export const populateTemplateSummaries = ( - schema: TemplateSummarySchema -) => - ({ - type: POPULATE_TEMPLATE_SUMMARIES, - status: RemoteDataState.Done, - schema, - } as const) - -export const setExportTemplate = ( - status: RemoteDataState, - item?: DocumentCreate -) => - ({ - type: SET_EXPORT_TEMPLATE, - status, - item, - } as const) - -export const setTemplatesStatus = (status: RemoteDataState) => - ({ - type: SET_TEMPLATES_STATUS, - status, - } as const) - -export const removeTemplateSummary = (id: string) => - ({ - type: REMOVE_TEMPLATE_SUMMARY, - id, - } as const) - -export const setTemplateSummary = ( - id: string, - status: RemoteDataState, - schema?: TemplateSummarySchema -) => - ({ - type: SET_TEMPLATE_SUMMARY, - id, - status, - schema, - } as const) - export const setCommunityTemplateToInstall = (template: CommunityTemplate) => ({ type: SET_COMMUNITY_TEMPLATE_TO_INSTALL, diff --git a/ui/src/templates/actions/thunks.ts b/ui/src/templates/actions/thunks.ts index 8cf4bca47a1..1122de799de 100644 --- a/ui/src/templates/actions/thunks.ts +++ b/ui/src/templates/actions/thunks.ts @@ -1,284 +1,18 @@ -// Libraries -import {normalize} from 'normalizr' - // APIs -import {client} from 'src/utils/api' import {fetchStacks} from 'src/templates/api' -import {createDashboardFromTemplate} from 'src/dashboards/actions/thunks' -import {createVariableFromTemplate} from 'src/variables/actions/thunks' -import {createTaskFromTemplate} from 'src/tasks/actions/thunks' - -// Schemas -import {templateSchema, arrayOfTemplates} from 'src/schemas/templates' // Actions -import {notify, Action as NotifyAction} from 'src/shared/actions/notifications' +import {Action as NotifyAction} from 'src/shared/actions/notifications' import { - addTemplateSummary, setStacks, - populateTemplateSummaries, - removeTemplateSummary, - setExportTemplate, - setTemplatesStatus, - setTemplateSummary, Action as TemplateAction, } from 'src/templates/actions/creators' -// Constants -import * as copy from 'src/shared/copy/notifications' -import {staticTemplates} from 'src/templates/constants/defaultTemplates' - // Types import {Dispatch} from 'react' -import {DocumentCreate, TemplateType} from '@influxdata/influx' -import { - RemoteDataState, - GetState, - DashboardTemplate, - VariableTemplate, - TemplateSummary, - TemplateSummaryEntities, - Label, - Template, - TaskTemplate, - ResourceType, -} from 'src/types' - -// Utils -import {templateToExport} from 'src/shared/utils/resourceToTemplate' -import {getOrg} from 'src/organizations/selectors' -import {getLabels, getStatus} from 'src/resources/selectors' type Action = TemplateAction | NotifyAction -export const getTemplateByID = async (id: string): Promise