Skip to content

Commit 7ba754a

Browse files
feat(paginatedTasks): fetch all tasks in batches of 500 (#3053)
1 parent 457e6db commit 7ba754a

File tree

5 files changed

+90
-31
lines changed

5 files changed

+90
-31
lines changed

src/shared/containers/SetOrg.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
TasksPagePaginated,
1313
TaskPage,
1414
TaskRunsPage,
15+
TaskRunsPagePaginated,
1516
TaskEditPage,
1617
DashboardsIndex,
1718
DataExplorerPage,
@@ -155,7 +156,17 @@ const SetOrg: FC = () => {
155156
<Route path={`${orgPath}/checks/:checkID`} component={CheckHistory} />
156157

157158
{/* Tasks */}
158-
<Route path={`${orgPath}/tasks/:id/runs`} component={TaskRunsPage} />
159+
{isFlagEnabled('paginatedTasks') ? (
160+
<Route
161+
path={`${orgPath}/tasks/:id/runs`}
162+
component={TaskRunsPagePaginated}
163+
/>
164+
) : (
165+
<Route
166+
path={`${orgPath}/tasks/:id/runs`}
167+
component={TaskRunsPage}
168+
/>
169+
)}
159170
<Route path={`${orgPath}/tasks/:id/edit`} component={TaskEditPage} />
160171
<Route path={`${orgPath}/tasks/new`} component={TaskPage} />
161172
{isFlagEnabled('paginatedTasks') ? (

src/shared/containers/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ export const TaskPage = lazy(() => import('src/tasks/containers/TaskPage'))
1212
export const TaskRunsPage = lazy(() =>
1313
import('src/tasks/components/TaskRunsPage')
1414
)
15+
export const TaskRunsPagePaginated = lazy(() =>
16+
import('src/tasks/pagination/TaskRunsPage')
17+
)
1518
export const TaskEditPage = lazy(() =>
1619
import('src/tasks/containers/TaskEditPage')
1720
)

src/tasks/actions/thunks.ts

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ import {TASK_LIMIT} from 'src/resources/constants'
6262
type Action = TaskAction | ExternalActions | ReturnType<typeof getTasks>
6363
type ExternalActions = NotifyAction | ReturnType<typeof checkTaskLimits>
6464

65+
const fetchTasks = async (query: GetTasksParams['query']) => {
66+
const resp = await api.getTasks({query})
67+
68+
if (resp.status !== 200) {
69+
throw new Error(resp.data.message)
70+
}
71+
72+
return resp
73+
}
74+
6575
// Thunks
6676
export const getTasks = (limit: number = TASK_LIMIT) => async (
6777
dispatch: Dispatch<TaskAction | NotifyAction>,
@@ -76,15 +86,62 @@ export const getTasks = (limit: number = TASK_LIMIT) => async (
7686
const org = getOrg(state)
7787

7888
const query: GetTasksParams['query'] = {orgID: org.id, limit}
89+
const resp = await fetchTasks(query)
7990

80-
const resp = await api.getTasks({query})
91+
const tasks = normalize<Task, TaskEntities, string[]>(
92+
resp.data.tasks,
93+
arrayOfTasks
94+
)
8195

82-
if (resp.status !== 200) {
83-
throw new Error(resp.data.message)
96+
dispatch(setTasks(RemoteDataState.Done, tasks))
97+
} catch (error) {
98+
dispatch(setTasks(RemoteDataState.Error))
99+
const message = getErrorMessage(error)
100+
console.error(error)
101+
dispatch(notify(copy.tasksFetchFailed(message)))
102+
}
103+
}
104+
105+
export const getAllTasks = () => async (
106+
dispatch: Dispatch<TaskAction | NotifyAction>,
107+
getState: GetState
108+
): Promise<void> => {
109+
try {
110+
const state = getState()
111+
if (getStatus(state, ResourceType.Tasks) === RemoteDataState.NotStarted) {
112+
dispatch(setTasks(RemoteDataState.Loading))
113+
}
114+
const org = getOrg(state)
115+
116+
// fetching 500 tasks at once strikes a balance between large requests and many requests
117+
const limit = 500
118+
const query: GetTasksParams['query'] = {orgID: org.id, limit}
119+
const resp = await fetchTasks(query)
120+
121+
let nonNormalizedTasks = resp.data.tasks
122+
let next = resp.data.links.next
123+
while (next && next.includes('after=')) {
124+
const afterAndExtras = next.split('after=')
125+
if (afterAndExtras.length < 2) {
126+
break
127+
}
128+
129+
const after = afterAndExtras[1].split('&')[0]
130+
if (!after) {
131+
break
132+
}
133+
134+
query.after = after
135+
const resp = await api.getTasks({query})
136+
if (resp.status !== 200) {
137+
throw new Error(resp.data.message)
138+
}
139+
nonNormalizedTasks = [...nonNormalizedTasks, ...resp.data.tasks]
140+
next = resp.data.links.next
84141
}
85142

86143
const tasks = normalize<Task, TaskEntities, string[]>(
87-
resp.data.tasks,
144+
nonNormalizedTasks,
88145
arrayOfTasks
89146
)
90147

src/tasks/pagination/TaskRunsPage.tsx

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {withRouter, RouteComponentProps} from 'react-router-dom'
77
import {Page, Sort} from '@influxdata/clockface'
88
import TaskRunsList from 'src/tasks/components/TaskRunsList'
99
import RateLimitAlert from 'src/cloud/components/RateLimitAlert'
10-
import GetResources from 'src/resources/components/GetResources'
1110
import {TaskRunsCard} from 'src/tasks/components/TaskRunsCard'
1211
import {PageBreadcrumbs} from 'src/tasks/components/PageBreadcrumbs'
1312

@@ -18,16 +17,12 @@ import {SpinnerContainer, TechnoSpinner} from '@influxdata/clockface'
1817
// Actions
1918
import {getRuns, runTask, updateTaskStatus} from 'src/tasks/actions/thunks'
2019

21-
// Selectors
22-
import {getByID} from 'src/resources/selectors'
23-
2420
// Utils
2521
import {pageTitleSuffixer} from 'src/shared/utils/pageTitles'
2622

2723
// Types
2824
import {SortTypes} from 'src/shared/utils/sort'
2925
import TimeZoneDropdown from 'src/shared/components/TimeZoneDropdown'
30-
import {ResourceType, Task} from 'src/types'
3126

3227
type ReduxProps = ConnectedProps<typeof connector>
3328
type Props = ReduxProps & RouteComponentProps<{id: string; orgID: string}>
@@ -87,16 +82,14 @@ class TaskRunsPage extends PureComponent<Props, State> {
8782
</Page.ControlBarRight>
8883
</Page.ControlBar>
8984
<Page.Contents fullWidth={false} scrollable={true}>
90-
<GetResources resources={[ResourceType.Tasks]}>
91-
<TaskRunsList
92-
taskID={match.params.id}
93-
runs={runs}
94-
sortKey={sortKey}
95-
sortDirection={sortDirection}
96-
sortType={sortType}
97-
onClickColumn={this.handleClickColumn}
98-
/>
99-
</GetResources>
85+
<TaskRunsList
86+
taskID={match.params.id}
87+
runs={runs}
88+
sortKey={sortKey}
89+
sortDirection={sortDirection}
90+
sortType={sortType}
91+
onClickColumn={this.handleClickColumn}
92+
/>
10093
</Page.Contents>
10194
</Page>
10295
</SpinnerContainer>
@@ -143,13 +136,8 @@ class TaskRunsPage extends PureComponent<Props, State> {
143136
}
144137
}
145138

146-
const mstp = (state: AppState, props) => {
147-
const {runs, runStatus} = state.resources.tasks
148-
const currentTask = getByID<Task>(
149-
state,
150-
ResourceType.Tasks,
151-
props.match.params.id
152-
)
139+
const mstp = (state: AppState) => {
140+
const {currentTask, runs, runStatus} = state.resources.tasks
153141

154142
return {
155143
runs,
@@ -159,9 +147,9 @@ const mstp = (state: AppState, props) => {
159147
}
160148

161149
const mdtp = {
162-
updateTaskStatus,
163150
getRuns,
164151
onRunTask: runTask,
152+
updateTaskStatus,
165153
}
166154

167155
const connector = connect(mstp, mdtp)

src/tasks/pagination/TasksPage.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import {TaskSortKey} from 'src/shared/components/resource_sort_dropdown/generate
4848
import {getAll} from 'src/resources/selectors'
4949
import {getResourcesStatus} from 'src/resources/selectors/getResourcesStatus'
5050

51-
import {getTasks} from 'src/tasks/actions/thunks'
51+
import {getAllTasks} from 'src/tasks/actions/thunks'
5252
import {getLabels} from 'src/labels/actions/thunks'
5353

5454
type ReduxProps = ConnectedProps<typeof connector>
@@ -83,7 +83,7 @@ class TasksPage extends PureComponent<Props, State> {
8383
}
8484

8585
public componentDidMount() {
86-
this.props.getTasks(-1) // -1 means fetch all tasks with no limit
86+
this.props.getAllTasks()
8787
this.props.getLabels()
8888

8989
let sortType: SortTypes = this.state.sortType
@@ -313,7 +313,7 @@ const mdtp = {
313313
checkTaskLimits: checkTasksLimitsAction,
314314
cloneTask,
315315
deleteTask,
316-
getTasks,
316+
getAllTasks,
317317
getLabels,
318318
onAddTaskLabel: addTaskLabel,
319319
onRunTask: runTask,

0 commit comments

Comments
 (0)