diff --git a/ui/src/labels/utils/index.ts b/ui/src/labels/utils/index.ts index 18968708cc9..9eb3a3fa2b7 100644 --- a/ui/src/labels/utils/index.ts +++ b/ui/src/labels/utils/index.ts @@ -1,4 +1,6 @@ import _ from 'lodash' +import {Label as GenLabel} from 'src/client' +import {Label} from 'src/types' import {HEX_CODE_CHAR_LENGTH, PRESET_LABEL_COLORS} from 'src/labels/constants/' @@ -54,3 +56,15 @@ export const validateHexCode = (colorHex: string): string | null => { return errorMessage.join(', ') } + +const DEFAULT_LABEL_COLOR = '#326BBA' + +export const addLabelDefaults = (l: GenLabel): Label => ({ + ...l, + properties: { + ...l.properties, + // add default color hex if missing + color: (l.properties || {}).color || DEFAULT_LABEL_COLOR, + description: (l.properties || {}).description || '', + }, +}) diff --git a/ui/src/shared/utils/mocks/resourceToTemplate.ts b/ui/src/shared/utils/mocks/resourceToTemplate.ts index 35e7d8e2600..baa01c7b9e5 100644 --- a/ui/src/shared/utils/mocks/resourceToTemplate.ts +++ b/ui/src/shared/utils/mocks/resourceToTemplate.ts @@ -1,4 +1,4 @@ -import {Task, Dashboard, View, Label, TaskStatus} from 'src/types' +import {Task, Dashboard, View, Label} from 'src/types' import {IVariable as Variable} from '@influxdata/influx' export const myDashboard: Dashboard = { @@ -99,7 +99,7 @@ export const myfavetask: Task = { offset: '1m0s', org: 'org', orgID: '037b084ec8ebc000', - status: TaskStatus.Active, + status: 'active', } export const myVariable: Variable = { diff --git a/ui/src/tasks/actions/index.ts b/ui/src/tasks/actions/index.ts index a0c0b85fae7..e9fa03b32a0 100644 --- a/ui/src/tasks/actions/index.ts +++ b/ui/src/tasks/actions/index.ts @@ -1,10 +1,7 @@ // Libraries import {push, goBack} from 'react-router-redux' import _ from 'lodash' - // APIs -import {LogEvent, ITask as Task} from '@influxdata/influx' -import {client} from 'src/utils/api' import {notify} from 'src/shared/actions/notifications' import { taskNotCreated, @@ -19,13 +16,23 @@ import { taskCloneFailed, taskRunSuccess, taskGetFailed, -} from 'src/shared/copy/notifications' -import { importTaskFailed, importTaskSucceeded, } from 'src/shared/copy/notifications' import {createTaskFromTemplate as createTaskFromTemplateAJAX} from 'src/templates/api' - +import {addLabelDefaults} from 'src/labels/utils' +import { + deleteTask as apiDeleteTask, + deleteTasksLabel, + getTask as apiGetTask, + getTasks as apiGetTasks, + getTasksRuns as apiGetTasksRuns, + getTasksRunsLogs as apiGetTasksRunsLogs, + patchTask as apiPatchTask, + postTask as apiPostTask, + postTasksLabel as apiPostTasksLabel, + postTasksRun as apiPostTasksRun, +} from 'src/client' // Actions import {setExportTemplate} from 'src/templates/actions' @@ -33,9 +40,9 @@ import {setExportTemplate} from 'src/templates/actions' import * as copy from 'src/shared/copy/notifications' // Types -import {AppState, Label, TaskTemplate} from 'src/types' +import {AppState, Label, TaskTemplate, LogEvent, Run, Task} from 'src/types' import {RemoteDataState} from '@influxdata/clockface' -import {Run} from 'src/tasks/components/TaskRunsPage' +import {Task as ITask} from 'src/client' // Utils import {getErrorMessage} from 'src/utils/api' @@ -162,6 +169,13 @@ export interface UpdateTask { } } +export const addDefaults = (task: ITask): Task => { + return { + ...task, + labels: (task.labels || []).map(addLabelDefaults), + } +} + export const setTaskOption = (taskOption: { key: TaskOptionKeys value: string @@ -240,7 +254,12 @@ export const getTasks = () => async ( orgs: {org}, } = getState() - const tasks = await client.tasks.getAll(org.id) + const resp = await apiGetTasks({query: {orgID: org.id}}) + if (resp.status !== 200) { + throw new Error(resp.data.message) + } + + const tasks = resp.data.tasks.map(task => addDefaults(task)) dispatch(setTasks(tasks)) dispatch(setTasksStatus(RemoteDataState.Done)) @@ -252,12 +271,18 @@ export const getTasks = () => async ( } } -export const addTaskLabelsAsync = (taskID: string, labels: Label[]) => async ( +export const addTaskLabelAsync = (taskID: string, label: Label) => async ( dispatch ): Promise => { try { - await client.tasks.addLabels(taskID, labels.map(l => l.id)) - const task = await client.tasks.get(taskID) + await apiPostTasksLabel({taskID, data: {labelID: label.id}}) + const resp = await apiGetTask({taskID}) + + if (resp.status !== 200) { + throw new Error(resp.data.message) + } + + const task = addDefaults(resp.data) dispatch(updateTask(task)) } catch (error) { @@ -266,13 +291,17 @@ export const addTaskLabelsAsync = (taskID: string, labels: Label[]) => async ( } } -export const removeTaskLabelsAsync = ( - taskID: string, - labels: Label[] -) => async (dispatch): Promise => { +export const removeTaskLabelAsync = (taskID: string, label: Label) => async ( + dispatch +): Promise => { try { - await client.tasks.removeLabels(taskID, labels.map(l => l.id)) - const task = await client.tasks.get(taskID) + await deleteTasksLabel({taskID, labelID: label.id}) + const resp = await apiGetTask({taskID}) + if (resp.status !== 200) { + throw new Error(resp.data.message) + } + + const task = addDefaults(resp.data) dispatch(updateTask(task)) } catch (error) { @@ -283,7 +312,14 @@ export const removeTaskLabelsAsync = ( export const updateTaskStatus = (task: Task) => async dispatch => { try { - await client.tasks.updateStatus(task.id, task.status) + const resp = await apiPatchTask({ + taskID: task.id, + data: {status: task.status}, + }) + + if (resp.status !== 200) { + throw new Error(resp.data.message) + } dispatch(getTasks()) dispatch(notify(taskUpdateSuccess())) @@ -294,9 +330,16 @@ export const updateTaskStatus = (task: Task) => async dispatch => { } } -export const updateTaskName = (task: Task) => async dispatch => { +export const updateTaskName = ( + name: string, + taskID: string +) => async dispatch => { try { - await client.tasks.update(task.id, task) + const resp = await apiPatchTask({taskID, data: {name}}) + + if (resp.status !== 200) { + throw new Error(resp.data.message) + } dispatch(getTasks()) dispatch(notify(taskUpdateSuccess())) @@ -309,7 +352,11 @@ export const updateTaskName = (task: Task) => async dispatch => { export const deleteTask = (task: Task) => async dispatch => { try { - await client.tasks.delete(task.id) + const resp = await apiDeleteTask({taskID: task.id}) + + if (resp.status !== 204) { + throw new Error(resp.data.message) + } dispatch(getTasks()) dispatch(notify(taskDeleteSuccess())) @@ -322,7 +369,19 @@ export const deleteTask = (task: Task) => async dispatch => { export const cloneTask = (task: Task, _) => async dispatch => { try { - await client.tasks.clone(task.id) + const resp = await apiGetTask({taskID: task.id}) + + if (resp.status !== 200) { + throw new Error(resp.data.message) + } + + const postData = addDefaults(resp.data) + + const newTask = await apiPostTask({data: postData}) + + if (newTask.status !== 201) { + throw new Error(newTask.data.message) + } dispatch(notify(taskCloneSuccess(task.name))) dispatch(getTasks()) @@ -342,7 +401,12 @@ export const selectTaskByID = (id: string) => async ( dispatch ): Promise => { try { - const task = await client.tasks.get(id) + const resp = await apiGetTask({taskID: id}) + if (resp.status !== 200) { + throw new Error(resp.data.message) + } + + const task = addDefaults(resp.data) dispatch(setCurrentTask(task)) } catch (e) { console.error(e) @@ -356,7 +420,12 @@ export const setAllTaskOptionsByID = (taskID: string) => async ( dispatch ): Promise => { try { - const task = await client.tasks.get(taskID) + const resp = await apiGetTask({taskID}) + if (resp.status !== 200) { + throw new Error(resp.data.message) + } + + const task = addDefaults(resp.data) dispatch(setAllTaskOptions(task)) } catch (e) { console.error(e) @@ -415,7 +484,11 @@ export const updateScript = () => async (dispatch, getState: GetStateFunc) => { updatedTask.every = null } - await client.tasks.update(task.id, updatedTask) + const resp = await apiPatchTask({taskID: task.id, data: updatedTask}) + + if (resp.status !== 200) { + throw new Error(resp.data.message) + } dispatch(goToTasks()) dispatch(setCurrentTask(null)) @@ -437,7 +510,10 @@ export const saveNewScript = (script: string, preamble: string) => async ( const { orgs: {org}, } = getState() - await client.tasks.createByOrgID(org.id, fluxScript, null) + const resp = await apiPostTask({data: {orgID: org.id, flux: fluxScript}}) + if (resp.status !== 201) { + throw new Error(resp.data.message) + } dispatch(setNewScript('')) dispatch(clearTask()) @@ -459,13 +535,14 @@ export const saveNewScript = (script: string, preamble: string) => async ( export const getRuns = (taskID: string) => async (dispatch): Promise => { try { dispatch(setRuns([], RemoteDataState.Loading)) + dispatch(selectTaskByID(taskID)) + const resp = await apiGetTasksRuns({taskID}) - const [runs] = await Promise.all([ - client.tasks.getRunsByTaskID(taskID), - dispatch(selectTaskByID(taskID)), - ]) + if (resp.status !== 200) { + throw new Error(resp.data.message) + } - const runsWithDuration = runs.map(run => { + const runsWithDuration = resp.data.runs.map(run => { const finished = new Date(run.finishedAt) const started = new Date(run.startedAt) @@ -486,7 +563,7 @@ export const getRuns = (taskID: string) => async (dispatch): Promise => { export const runTask = (taskID: string) => async dispatch => { try { - await client.tasks.startRunByTaskID(taskID) + await apiPostTasksRun({taskID}) dispatch(notify(taskRunSuccess())) } catch (error) { const message = getErrorMessage(error) @@ -499,8 +576,11 @@ export const getLogs = (taskID: string, runID: string) => async ( dispatch ): Promise => { try { - const logs = await client.tasks.getLogEventsByRunID(taskID, runID) - dispatch(setLogs(logs)) + const resp = await apiGetTasksRunsLogs({taskID, runID}) + if (resp.status !== 200) { + throw new Error(resp.data.message) + } + dispatch(setLogs(resp.data.events)) } catch (error) { console.error(error) dispatch(setLogs([])) @@ -512,7 +592,11 @@ export const convertToTemplate = (taskID: string) => async ( ): Promise => { try { dispatch(setExportTemplate(RemoteDataState.Loading)) - const task = await client.tasks.get(taskID) + const resp = await apiGetTask({taskID}) + if (resp.status !== 200) { + throw new Error(resp.data.message) + } + const task = addDefaults(resp.data) const taskTemplate = taskToTemplate(task) dispatch(setExportTemplate(RemoteDataState.Done, taskTemplate)) diff --git a/ui/src/tasks/components/RunLogRow.tsx b/ui/src/tasks/components/RunLogRow.tsx index 10038367d63..d9501443cea 100644 --- a/ui/src/tasks/components/RunLogRow.tsx +++ b/ui/src/tasks/components/RunLogRow.tsx @@ -7,7 +7,7 @@ import moment from 'moment' import {IndexList} from '@influxdata/clockface' // Types -import {LogEvent} from '@influxdata/influx' +import {LogEvent} from 'src/types' import {DEFAULT_TIME_FORMAT} from 'src/shared/constants' interface Props { @@ -36,7 +36,7 @@ class RunLogRow extends PureComponent { ) } - private dateTimeString(dt: Date): string { + private dateTimeString(dt: string): string { if (!dt) { return '' } diff --git a/ui/src/tasks/components/RunLogsList.tsx b/ui/src/tasks/components/RunLogsList.tsx index 49c1fb567e1..cb458162d9c 100644 --- a/ui/src/tasks/components/RunLogsList.tsx +++ b/ui/src/tasks/components/RunLogsList.tsx @@ -8,7 +8,7 @@ import RunLogRow from 'src/tasks/components/RunLogRow' import FancyScrollbar from 'src/shared/components/fancy_scrollbar/FancyScrollbar' // Types -import {LogEvent} from '@influxdata/influx' +import {LogEvent} from 'src/types' interface Props { onDismissOverlay: () => void diff --git a/ui/src/tasks/components/TaskCard.test.tsx b/ui/src/tasks/components/TaskCard.test.tsx index d331db5e895..799ff62c925 100644 --- a/ui/src/tasks/components/TaskCard.test.tsx +++ b/ui/src/tasks/components/TaskCard.test.tsx @@ -21,8 +21,8 @@ const setup = (override = {}) => { onRunTask: jest.fn(), onFilterChange: jest.fn(), onUpdate: jest.fn(), - onAddTaskLabels: jest.fn(), - onRemoveTaskLabels: jest.fn(), + onAddTaskLabel: jest.fn(), + onRemoveTaskLabel: jest.fn(), onCreateLabel: jest.fn(), labels: [], // all labels ...override, diff --git a/ui/src/tasks/components/TaskCard.tsx b/ui/src/tasks/components/TaskCard.tsx index 0282046159f..eab4336a9cc 100644 --- a/ui/src/tasks/components/TaskCard.tsx +++ b/ui/src/tasks/components/TaskCard.tsx @@ -17,7 +17,7 @@ import InlineLabels from 'src/shared/components/inlineLabels/InlineLabels' import LastRunTaskStatus from 'src/shared/components/lastRunTaskStatus/LastRunTaskStatus' // Actions -import {addTaskLabelsAsync, removeTaskLabelsAsync} from 'src/tasks/actions' +import {addTaskLabelAsync, removeTaskLabelAsync} from 'src/tasks/actions' import {createLabel as createLabelAsync} from 'src/labels/actions' // Selectors @@ -26,7 +26,7 @@ import {viewableLabels} from 'src/labels/selectors' // Types import {ComponentColor} from '@influxdata/clockface' import {ILabel} from '@influxdata/influx' -import {AppState, TaskStatus, Task} from 'src/types' +import {AppState, Task} from 'src/types' // Constants import {DEFAULT_TASK_NAME} from 'src/dashboards/constants' @@ -38,7 +38,7 @@ interface PassedProps { onSelect: (task: Task) => void onClone: (task: Task) => void onRunTask: (taskID: string) => void - onUpdate: (task: Task) => void + onUpdate: (name: string, taskID: string) => void onFilterChange: (searchTerm: string) => void } @@ -47,8 +47,8 @@ interface StateProps { } interface DispatchProps { - onAddTaskLabels: typeof addTaskLabelsAsync - onRemoveTaskLabels: typeof removeTaskLabelsAsync + onAddTaskLabel: typeof addTaskLabelAsync + onRemoveTaskLabel: typeof removeTaskLabelAsync onCreateLabel: typeof createLabelAsync } @@ -153,8 +153,11 @@ export class TaskCard extends PureComponent { } private handleRenameTask = (name: string) => { - const {onUpdate, task} = this.props - onUpdate({...task, name}) + const { + onUpdate, + task: {id}, + } = this.props + onUpdate(name, id) } private handleExport = () => { @@ -182,15 +185,15 @@ export class TaskCard extends PureComponent { } private handleAddLabel = (label: ILabel) => { - const {task, onAddTaskLabels} = this.props + const {task, onAddTaskLabel} = this.props - onAddTaskLabels(task.id, [label]) + onAddTaskLabel(task.id, label) } private handleRemoveLabel = (label: ILabel) => { - const {task, onRemoveTaskLabels} = this.props + const {task, onRemoveTaskLabel} = this.props - onRemoveTaskLabels(task.id, [label]) + onRemoveTaskLabel(task.id, label) } private handleCreateLabel = (label: ILabel) => { @@ -199,7 +202,7 @@ export class TaskCard extends PureComponent { private get isTaskActive(): boolean { const {task} = this.props - if (task.status === TaskStatus.Active) { + if (task.status === 'active') { return true } return false @@ -207,10 +210,10 @@ export class TaskCard extends PureComponent { private changeToggle = () => { const {task, onActivate} = this.props - if (task.status === TaskStatus.Active) { - task.status = TaskStatus.Inactive + if (task.status === 'active') { + task.status = 'inactive' } else { - task.status = TaskStatus.Active + task.status = 'active' } onActivate(task) } @@ -238,8 +241,8 @@ const mstp = ({labels}: AppState): StateProps => { const mdtp: DispatchProps = { onCreateLabel: createLabelAsync, - onAddTaskLabels: addTaskLabelsAsync, - onRemoveTaskLabels: removeTaskLabelsAsync, + onAddTaskLabel: addTaskLabelAsync, + onRemoveTaskLabel: removeTaskLabelAsync, } export default connect( diff --git a/ui/src/tasks/components/TaskRunsList.tsx b/ui/src/tasks/components/TaskRunsList.tsx index 61720ecf248..9d6ef7be6cc 100644 --- a/ui/src/tasks/components/TaskRunsList.tsx +++ b/ui/src/tasks/components/TaskRunsList.tsx @@ -8,7 +8,7 @@ import TaskRunsRow from 'src/tasks/components/TaskRunsRow' // Types import {Sort, ComponentSize} from '@influxdata/clockface' -import {Run} from 'src/tasks/components/TaskRunsPage' +import {Run} from 'src/types' import {SortTypes} from 'src/shared/utils/sort' // Utils diff --git a/ui/src/tasks/components/TaskRunsPage.tsx b/ui/src/tasks/components/TaskRunsPage.tsx index 97688d4df64..cd1d92014d0 100644 --- a/ui/src/tasks/components/TaskRunsPage.tsx +++ b/ui/src/tasks/components/TaskRunsPage.tsx @@ -10,9 +10,7 @@ import TaskRunsList from 'src/tasks/components/TaskRunsList' import PageTitleWithOrg from 'src/shared/components/PageTitleWithOrg' // Types -import {AppState} from 'src/types' -import {RemoteDataState} from 'src/types' -import {Run as APIRun, Task} from '@influxdata/influx' +import {AppState, RemoteDataState, Task, Run} from 'src/types' import { SpinnerContainer, TechnoSpinner, @@ -29,10 +27,6 @@ import {pageTitleSuffixer} from 'src/shared/utils/pageTitles' // Types import {SortTypes} from 'src/shared/utils/sort' -export interface Run extends APIRun { - duration: string -} - interface OwnProps { params: {id: string} } diff --git a/ui/src/tasks/components/TaskRunsRow.tsx b/ui/src/tasks/components/TaskRunsRow.tsx index 66a1c518a47..f80e7a69cda 100644 --- a/ui/src/tasks/components/TaskRunsRow.tsx +++ b/ui/src/tasks/components/TaskRunsRow.tsx @@ -12,10 +12,8 @@ import {getLogs} from 'src/tasks/actions' // Types import {ComponentSize, ComponentColor, Button} from '@influxdata/clockface' -import {LogEvent} from '@influxdata/influx' -import {AppState} from 'src/types' +import {AppState, LogEvent, Run} from 'src/types' import {DEFAULT_TIME_FORMAT} from 'src/shared/constants' -import {Run} from 'src/tasks/components/TaskRunsPage' interface OwnProps { taskID: string @@ -71,7 +69,7 @@ class TaskRunsRow extends PureComponent { ) } - private dateTimeString(dt: Date): string { + private dateTimeString(dt: string): string { if (!dt) { return '' } diff --git a/ui/src/tasks/components/TasksList.tsx b/ui/src/tasks/components/TasksList.tsx index 3d5245bf26c..3a6da52906b 100644 --- a/ui/src/tasks/components/TasksList.tsx +++ b/ui/src/tasks/components/TasksList.tsx @@ -9,13 +9,13 @@ import TaskCard from 'src/tasks/components/TaskCard' // Types import EmptyTasksList from 'src/tasks/components/EmptyTasksList' -import {ITask as Task} from '@influxdata/influx' +import {Task} from 'src/types' import {SortTypes} from 'src/shared/utils/sort' import {Sort} from '@influxdata/clockface' import { - addTaskLabelsAsync, - removeTaskLabelsAsync, + addTaskLabelAsync, + removeTaskLabelAsync, runTask, } from 'src/tasks/actions' import {checkTaskLimits as checkTaskLimitsAction} from 'src/cloud/actions/limits' @@ -33,10 +33,10 @@ interface Props { onClone: (task: Task) => void onFilterChange: (searchTerm: string) => void totalCount: number - onRemoveTaskLabels: typeof removeTaskLabelsAsync - onAddTaskLabels: typeof addTaskLabelsAsync + onRemoveTaskLabel: typeof removeTaskLabelAsync + onAddTaskLabel: typeof addTaskLabelAsync onRunTask: typeof runTask - onUpdate: (task: Task) => void + onUpdate: (name: string, taskID: string) => void filterComponent?: JSX.Element onImportTask: () => void sortKey: string diff --git a/ui/src/tasks/containers/TasksPage.tsx b/ui/src/tasks/containers/TasksPage.tsx index 6c89a31d252..8a7dd98b9e2 100644 --- a/ui/src/tasks/containers/TasksPage.tsx +++ b/ui/src/tasks/containers/TasksPage.tsx @@ -26,8 +26,8 @@ import { cloneTask, setSearchTerm as setSearchTermAction, setShowInactive as setShowInactiveAction, - addTaskLabelsAsync, - removeTaskLabelsAsync, + addTaskLabelAsync, + removeTaskLabelAsync, runTask, } from 'src/tasks/actions' import { @@ -36,7 +36,7 @@ import { } from 'src/cloud/actions/limits' // Types -import {AppState, Task, TaskStatus, RemoteDataState} from 'src/types' +import {AppState, Task, RemoteDataState} from 'src/types' import {InjectedRouter, WithRouterProps} from 'react-router' import {Sort} from '@influxdata/clockface' import {SortTypes} from 'src/shared/utils/sort' @@ -54,8 +54,8 @@ interface ConnectedDispatchProps { selectTask: typeof selectTask setSearchTerm: typeof setSearchTermAction setShowInactive: typeof setShowInactiveAction - onAddTaskLabels: typeof addTaskLabelsAsync - onRemoveTaskLabels: typeof removeTaskLabelsAsync + onAddTaskLabel: typeof addTaskLabelAsync + onRemoveTaskLabel: typeof removeTaskLabelAsync onRunTask: typeof runTask checkTaskLimits: typeof checkTasksLimitsAction } @@ -110,8 +110,8 @@ class TasksPage extends PureComponent { searchTerm, setShowInactive, showInactive, - onAddTaskLabels, - onRemoveTaskLabels, + onAddTaskLabel, + onRemoveTaskLabel, onRunTask, checkTaskLimits, limitStatus, @@ -151,8 +151,8 @@ class TasksPage extends PureComponent { onCreate={this.handleCreateTask} onClone={this.handleClone} onSelect={this.props.selectTask} - onAddTaskLabels={onAddTaskLabels} - onRemoveTaskLabels={onRemoveTaskLabels} + onAddTaskLabel={onAddTaskLabel} + onRemoveTaskLabel={onRemoveTaskLabel} onRunTask={onRunTask} onFilterChange={setSearchTerm} filterComponent={this.search} @@ -246,7 +246,7 @@ class TasksPage extends PureComponent { const matchingTasks = tasks.filter(t => { let activeFilter = true if (!showInactive) { - activeFilter = t.status === TaskStatus.Active + activeFilter = t.status === 'active' } return activeFilter @@ -262,8 +262,7 @@ class TasksPage extends PureComponent { private get hiddenTaskAlert(): JSX.Element { const {showInactive, tasks} = this.props - const hiddenCount = tasks.filter(t => t.status === TaskStatus.Inactive) - .length + const hiddenCount = tasks.filter(t => t.status === 'inactive').length const allTasksAreHidden = hiddenCount === tasks.length @@ -303,8 +302,8 @@ const mdtp: ConnectedDispatchProps = { cloneTask, setSearchTerm: setSearchTermAction, setShowInactive: setShowInactiveAction, - onRemoveTaskLabels: removeTaskLabelsAsync, - onAddTaskLabels: addTaskLabelsAsync, + onRemoveTaskLabel: removeTaskLabelAsync, + onAddTaskLabel: addTaskLabelAsync, onRunTask: runTask, checkTaskLimits: checkTasksLimitsAction, } diff --git a/ui/src/tasks/reducers/index.ts b/ui/src/tasks/reducers/index.ts index 613e6c915c3..182716f9152 100644 --- a/ui/src/tasks/reducers/index.ts +++ b/ui/src/tasks/reducers/index.ts @@ -1,11 +1,9 @@ import {TaskOptions, TaskSchedule} from 'src/utils/taskOptionsToFluxScript' -import {LogEvent} from '@influxdata/influx' // Types import {Action} from 'src/tasks/actions' -import {ITask as Task} from '@influxdata/influx' +import {Task, LogEvent, Run} from 'src/types' import {RemoteDataState} from '@influxdata/clockface' -import {Run} from 'src/tasks/components/TaskRunsPage' export interface TasksState { status: RemoteDataState diff --git a/ui/src/templates/api/index.ts b/ui/src/templates/api/index.ts index 137e4050174..e1db865d90f 100644 --- a/ui/src/templates/api/index.ts +++ b/ui/src/templates/api/index.ts @@ -13,6 +13,7 @@ import { import {IDashboard, Cell} from '@influxdata/influx' import {client} from 'src/utils/api' +// Utils import { findIncludedsFromRelationships, findLabelsToCreate, @@ -22,7 +23,13 @@ import { hasLabelsRelationships, getLabelRelationships, } from 'src/templates/utils/' - +import {addDefaults} from 'src/tasks/actions' +// API +import { + getTask as apiGetTask, + postTask as apiPostTask, + postTasksLabel as apiPostTasksLabel, +} from 'src/client' // Create Dashboard Templates export const createDashboardFromTemplate = async ( @@ -230,30 +237,41 @@ export const createTaskFromTemplate = async ( orgID: string ): Promise => { const {content} = template + try { + if ( + content.data.type !== TemplateType.Task || + template.meta.version !== '1' + ) { + throw new Error('Cannot create task from this template') + } - if ( - content.data.type !== TemplateType.Task || - template.meta.version !== '1' - ) { - throw new Error('Cannot create task from this template') - } + const flux = content.data.attributes.flux - const flux = content.data.attributes.flux + const postResp = await apiPostTask({data: {orgID, flux}}) - const createdTask = await client.tasks.createByOrgID(orgID, flux, null) + if (postResp.status !== 201) { + throw new Error(postResp.data.message) + } - if (!createdTask || !createdTask.id) { - throw new Error('Could not create task') - } + const postedTask = addDefaults(postResp.data) - // associate imported label.id with created label - const labelMap = await createLabelsFromTemplate(template, orgID) + // associate imported label.id with created label + const labelMap = await createLabelsFromTemplate(template, orgID) - await addTaskLabelsFromTemplate(template, labelMap, createdTask) + await addTaskLabelsFromTemplate(template, labelMap, postedTask) - const task = await client.tasks.get(createdTask.id) + const resp = await apiGetTask({taskID: postedTask.id}) - return task + if (resp.status !== 200) { + throw new Error(resp.data.message) + } + + const task = addDefaults(resp.data) + + return task + } catch (e) { + console.error(e) + } } const addTaskLabelsFromTemplate = async ( @@ -262,8 +280,8 @@ const addTaskLabelsFromTemplate = async ( task: Task ) => { const relationships = getLabelRelationships(template.content.data) - const labelIDs = relationships.map(l => labelMap[l.id] || '') - await client.tasks.addLabels(task.id, labelIDs) + const [labelID] = relationships.map(l => labelMap[l.id] || '') + await apiPostTasksLabel({taskID: task.id, data: {labelID}}) } export const createVariableFromTemplate = async ( diff --git a/ui/src/types/index.ts b/ui/src/types/index.ts index 4d1979eeb7d..b9985a3886d 100644 --- a/ui/src/types/index.ts +++ b/ui/src/types/index.ts @@ -15,6 +15,7 @@ export * from './labels' export * from './layouts' export * from './links' export * from './localStorage' +export * from './logEvent' export * from './notifications' export * from './orgs' export * from './overlay' @@ -38,3 +39,4 @@ export * from './auth' export * from './cloud' export * from './resources' export * from './redux' +export * from './run' diff --git a/ui/src/types/logEvent.ts b/ui/src/types/logEvent.ts new file mode 100644 index 00000000000..d300f9dcff8 --- /dev/null +++ b/ui/src/types/logEvent.ts @@ -0,0 +1 @@ +export {LogEvent} from 'src/client' diff --git a/ui/src/types/run.ts b/ui/src/types/run.ts new file mode 100644 index 00000000000..dd97c1bc7ef --- /dev/null +++ b/ui/src/types/run.ts @@ -0,0 +1,5 @@ +import {Run as IRun} from 'src/client' + +export interface Run extends IRun { + duration?: string +} diff --git a/ui/src/types/tasks.ts b/ui/src/types/tasks.ts index e10f65c8799..b412462991e 100644 --- a/ui/src/types/tasks.ts +++ b/ui/src/types/tasks.ts @@ -1,7 +1,6 @@ -import {Task as TaskAPI, ITask} from '@influxdata/influx' +import {Task as ITask} from 'src/client' +import {Label} from 'src/types' -export const TaskStatus = TaskAPI.StatusEnum export interface Task extends ITask { - lastRunError?: string - lastRunStatus?: 'failed' | 'success' | 'canceled' + labels?: Label[] } diff --git a/ui/src/utils/api.ts b/ui/src/utils/api.ts index df47c6cce56..abc6f380389 100644 --- a/ui/src/utils/api.ts +++ b/ui/src/utils/api.ts @@ -20,6 +20,10 @@ export const getErrorMessage = (e: any) => { message = get(e, 'response.data.message', '') } + if (message === '') { + message = get(e, 'message', '') + } + if (message === '') { message = 'unknown error' }