Add redux actions and reducer to handle scheduler enqueue events

This commit is contained in:
Ken Hibino 2020-12-26 10:29:43 -08:00
parent 8d531c04cd
commit 45d77be796
3 changed files with 128 additions and 3 deletions

View File

@ -1,10 +1,21 @@
import { Dispatch } from "@reduxjs/toolkit"; import { Dispatch } from "@reduxjs/toolkit";
import { listSchedulerEntries, ListSchedulerEntriesResponse } from "../api"; import {
listSchedulerEnqueueEvents,
ListSchedulerEnqueueEventsResponse,
listSchedulerEntries,
ListSchedulerEntriesResponse,
} from "../api";
// List of scheduler-entry related action types. // List of scheduler-entry related action types.
export const LIST_SCHEDULER_ENTRIES_BEGIN = "LIST_SCHEDULER_ENTRIES_BEGIN"; export const LIST_SCHEDULER_ENTRIES_BEGIN = "LIST_SCHEDULER_ENTRIES_BEGIN";
export const LIST_SCHEDULER_ENTRIES_SUCCESS = "LIST_SCHEDULER_ENTRIES_SUCCESS"; export const LIST_SCHEDULER_ENTRIES_SUCCESS = "LIST_SCHEDULER_ENTRIES_SUCCESS";
export const LIST_SCHEDULER_ENTRIES_ERROR = "LIST_SCHEDULER_ENTRIES_ERROR"; export const LIST_SCHEDULER_ENTRIES_ERROR = "LIST_SCHEDULER_ENTRIES_ERROR";
export const LIST_SCHEDULER_ENQUEUE_EVENTS_BEGIN =
"LIST_SCHEDULER_ENQUEUE_EVENTS_BEGIN";
export const LIST_SCHEDULER_ENQUEUE_EVENTS_SUCCESS =
"LIST_SCHEDULER_ENQUEUE_EVENTS_SUCCESS";
export const LIST_SCHEDULER_ENQUEUE_EVENTS_ERROR =
"LIST_SCHEDULER_ENQUEUE_EVENTS_ERROR";
interface ListSchedulerEntriesBeginAction { interface ListSchedulerEntriesBeginAction {
type: typeof LIST_SCHEDULER_ENTRIES_BEGIN; type: typeof LIST_SCHEDULER_ENTRIES_BEGIN;
@ -20,11 +31,31 @@ interface ListSchedulerEntriesErrorAction {
error: string; // error description error: string; // error description
} }
interface ListSchedulerEnqueueEventBeginAction {
type: typeof LIST_SCHEDULER_ENQUEUE_EVENTS_BEGIN;
entryId: string;
}
interface ListSchedulerEnqueueEventSuccessAction {
type: typeof LIST_SCHEDULER_ENQUEUE_EVENTS_SUCCESS;
entryId: string;
payload: ListSchedulerEnqueueEventsResponse;
}
interface ListSchedulerEnqueueEventErrorAction {
type: typeof LIST_SCHEDULER_ENQUEUE_EVENTS_ERROR;
entryId: string;
error: string;
}
// Union of all scheduler-entry related actions. // Union of all scheduler-entry related actions.
export type SchedulerEntriesActionTypes = export type SchedulerEntriesActionTypes =
| ListSchedulerEntriesBeginAction | ListSchedulerEntriesBeginAction
| ListSchedulerEntriesSuccessAction | ListSchedulerEntriesSuccessAction
| ListSchedulerEntriesErrorAction; | ListSchedulerEntriesErrorAction
| ListSchedulerEnqueueEventBeginAction
| ListSchedulerEnqueueEventSuccessAction
| ListSchedulerEnqueueEventErrorAction;
export function listSchedulerEntriesAsync() { export function listSchedulerEntriesAsync() {
return async (dispatch: Dispatch<SchedulerEntriesActionTypes>) => { return async (dispatch: Dispatch<SchedulerEntriesActionTypes>) => {
@ -44,3 +75,24 @@ export function listSchedulerEntriesAsync() {
} }
}; };
} }
export function listSchedulerEnqueueEventsAsync(entryId: string) {
return async (dispatch: Dispatch<SchedulerEntriesActionTypes>) => {
dispatch({ type: LIST_SCHEDULER_ENQUEUE_EVENTS_BEGIN, entryId });
try {
const response = await listSchedulerEnqueueEvents(entryId);
dispatch({
type: LIST_SCHEDULER_ENQUEUE_EVENTS_SUCCESS,
payload: response,
entryId,
});
} catch (error) {
console.error("listSchedulerEnqueueEventsAsync: ", error);
dispatch({
type: LIST_SCHEDULER_ENQUEUE_EVENTS_ERROR,
error: `Could not get enqueue events for entry: ${entryId}`,
entryId,
});
}
};
}

View File

@ -41,6 +41,10 @@ export interface ListSchedulerEntriesResponse {
entries: SchedulerEntry[]; entries: SchedulerEntry[];
} }
export interface ListSchedulerEnqueueEventsResponse {
events: SchedulerEnqueueEvent[];
}
export interface BatchCancelTasksResponse { export interface BatchCancelTasksResponse {
canceled_ids: string[]; canceled_ids: string[];
error_ids: string[]; error_ids: string[];
@ -136,6 +140,11 @@ export interface SchedulerEntry {
prev_enqueue_at?: string; prev_enqueue_at?: string;
} }
export interface SchedulerEnqueueEvent {
task_id: string;
enqueued_at: string;
}
export interface PaginationOptions extends Record<string, number | undefined> { export interface PaginationOptions extends Record<string, number | undefined> {
size?: number; // size of the page size?: number; // size of the page
page?: number; // page number (1 being the first page) page?: number; // page number (1 being the first page)
@ -539,3 +548,13 @@ export async function listSchedulerEntries(): Promise<ListSchedulerEntriesRespon
}); });
return resp.data; return resp.data;
} }
export async function listSchedulerEnqueueEvents(
entryId: string
): Promise<ListSchedulerEnqueueEventsResponse> {
const resp = await axios({
method: "get",
url: `${BASE_URL}/scheduler_entries/${entryId}/enqueue_events`,
});
return resp.data;
}

View File

@ -1,21 +1,35 @@
import { import {
LIST_SCHEDULER_ENQUEUE_EVENTS_BEGIN,
LIST_SCHEDULER_ENQUEUE_EVENTS_ERROR,
LIST_SCHEDULER_ENQUEUE_EVENTS_SUCCESS,
LIST_SCHEDULER_ENTRIES_BEGIN, LIST_SCHEDULER_ENTRIES_BEGIN,
LIST_SCHEDULER_ENTRIES_ERROR, LIST_SCHEDULER_ENTRIES_ERROR,
LIST_SCHEDULER_ENTRIES_SUCCESS, LIST_SCHEDULER_ENTRIES_SUCCESS,
SchedulerEntriesActionTypes, SchedulerEntriesActionTypes,
} from "../actions/schedulerEntriesActions"; } from "../actions/schedulerEntriesActions";
import { SchedulerEntry } from "../api"; import { SchedulerEnqueueEvent, SchedulerEntry } from "../api";
interface SchedulerEntriesState { interface SchedulerEntriesState {
loading: boolean; loading: boolean;
data: SchedulerEntry[]; data: SchedulerEntry[];
error: string; // error description error: string; // error description
enqueueEventsByEntryId: {
[entryId: string]: { data: SchedulerEnqueueEvent[]; loading: boolean };
};
}
function getEnqueueEventsEntry(
state: SchedulerEntriesState,
entryId: string
): { data: SchedulerEnqueueEvent[]; loading: boolean } {
return state.enqueueEventsByEntryId[entryId] || { data: [], loading: false };
} }
const initialState: SchedulerEntriesState = { const initialState: SchedulerEntriesState = {
loading: false, loading: false,
data: [], data: [],
error: "", error: "",
enqueueEventsByEntryId: {},
}; };
function schedulerEntriesReducer( function schedulerEntriesReducer(
@ -30,6 +44,7 @@ function schedulerEntriesReducer(
}; };
case LIST_SCHEDULER_ENTRIES_SUCCESS: case LIST_SCHEDULER_ENTRIES_SUCCESS:
return { return {
...state,
error: "", error: "",
loading: false, loading: false,
data: action.payload.entries, data: action.payload.entries,
@ -41,6 +56,45 @@ function schedulerEntriesReducer(
loading: false, loading: false,
error: action.error, error: action.error,
}; };
case LIST_SCHEDULER_ENQUEUE_EVENTS_BEGIN: {
const entry = getEnqueueEventsEntry(state, action.entryId);
return {
...state,
enqueueEventsByEntryId: {
...state.enqueueEventsByEntryId,
[action.entryId]: {
...entry,
loading: true,
},
},
};
}
case LIST_SCHEDULER_ENQUEUE_EVENTS_SUCCESS: {
const entry = getEnqueueEventsEntry(state, action.entryId);
return {
...state,
enqueueEventsByEntryId: {
...state.enqueueEventsByEntryId,
[action.entryId]: {
loading: false,
data: [...entry.data, ...action.payload.events],
},
},
};
}
case LIST_SCHEDULER_ENQUEUE_EVENTS_ERROR: {
const entry = getEnqueueEventsEntry(state, action.entryId);
return {
...state,
enqueueEventsByEntryId: {
...state.enqueueEventsByEntryId,
[action.entryId]: {
...entry,
loading: false,
},
},
};
}
default: default:
return state; return state;
} }