Unverified Commit 0418b964 authored by Diksha Thakur's avatar Diksha Thakur Committed by GitHub

fix: Create separate reducer for last indexed value (#543)

* Added required new files

* Added new reducer for handling lastIndexed

* removed all comments

* suggested changes
parent bcb47ec3
......@@ -85,6 +85,6 @@ describe('mapStateToProps', () => {
});
it('sets lastIndexed on the props', () => {
expect(result.lastIndexed).toEqual(globalState.tableMetadata.lastIndexed);
expect(result.lastIndexed).toEqual(globalState.lastIndexed.lastIndexed);
});
});
......@@ -7,8 +7,8 @@ import { bindActionCreators } from 'redux';
// TODO: Use css-modules instead of 'import'
import './styles.scss';
import { GlobalState } from 'ducks/rootReducer';
import { getLastIndexed } from 'ducks/tableMetadata/reducer';
import { GetLastIndexedRequest } from 'ducks/tableMetadata/types';
import { getLastIndexed } from 'ducks/lastIndexed/reducer';
import { GetLastIndexedRequest } from 'ducks/lastIndexed/types';
import { formatDateTimeLong } from 'utils/dateUtils';
......@@ -33,7 +33,9 @@ const ShimmeringFooterLoader: React.FC = () => {
export class Footer extends React.Component<FooterProps> {
componentDidMount() {
this.props.getLastIndexed();
const { getLastIndexed } = this.props;
getLastIndexed();
}
generateDateTimeString = () => {
......@@ -62,7 +64,7 @@ export class Footer extends React.Component<FooterProps> {
export const mapStateToProps = (state: GlobalState) => {
return {
lastIndexed: state.tableMetadata.lastIndexed,
lastIndexed: state.lastIndexed.lastIndexed,
};
};
......
import axios, { AxiosResponse, AxiosError } from 'axios';
export const API_PATH = '/api/metadata/v0';
type MessageAPI = { msg: string };
export type LastIndexedAPI = { timestamp: string } & MessageAPI;
export function getLastIndexed() {
return axios
.get(`${API_PATH}/get_last_indexed`)
.then((response: AxiosResponse<LastIndexedAPI>) => {
const { data } = response;
return data.timestamp;
})
.catch((e) => {
const timestamp = null;
return Promise.reject({
timestamp,
});
});
}
import {
GetLastIndexed,
GetLastIndexedRequest,
GetLastIndexedResponse,
} from './types';
export interface LastIndexedReducerState {
lastIndexed: number;
}
export const initialState: LastIndexedReducerState = {
lastIndexed: null,
};
/* ACTIONS */
export function getLastIndexed(): GetLastIndexedRequest {
return { type: GetLastIndexed.REQUEST };
}
export function getLastIndexedFailure(): GetLastIndexedResponse {
return { type: GetLastIndexed.FAILURE };
}
export function getLastIndexedSuccess(
lastIndexedEpoch: number
): GetLastIndexedResponse {
return {
type: GetLastIndexed.SUCCESS,
payload: {
lastIndexedEpoch,
},
};
}
/* REDUCER */
export default function reducer(
state: LastIndexedReducerState = initialState,
action
): LastIndexedReducerState {
switch (action.type) {
case GetLastIndexed.REQUEST:
return initialState;
case GetLastIndexed.SUCCESS:
return {
lastIndexed: (<GetLastIndexedResponse>action).payload.lastIndexedEpoch,
};
case GetLastIndexed.FAILURE:
return {
lastIndexed: null,
};
default:
return state;
}
}
import { SagaIterator } from 'redux-saga';
import { call, put, takeEvery } from 'redux-saga/effects';
import * as API from 'ducks/lastIndexed/api/v0';
import { getLastIndexedFailure, getLastIndexedSuccess } from './reducer';
import { GetLastIndexed, GetLastIndexedRequest } from './types';
export function* getLastIndexedWorker(
action: GetLastIndexedRequest
): SagaIterator {
try {
const lastIndexed = yield call(API.getLastIndexed);
if (lastIndexed) {
yield put(getLastIndexedSuccess(lastIndexed));
} else {
yield put(getLastIndexedFailure());
}
} catch (e) {
yield put(getLastIndexedFailure());
}
}
export function* getLastIndexedWatcher(): SagaIterator {
yield takeEvery(GetLastIndexed.REQUEST, getLastIndexedWorker);
}
import { testSaga } from 'redux-saga-test-plan';
import * as API from '../api/v0';
import reducer, {
getLastIndexed,
getLastIndexedFailure,
getLastIndexedSuccess,
initialState,
LastIndexedReducerState,
} from '../reducer';
import { getLastIndexedWatcher, getLastIndexedWorker } from '../sagas';
import { GetLastIndexed } from '../types';
describe('lastIndexed ducks', () => {
let testEpoch: number;
beforeAll(() => {
testEpoch = 1545925769;
});
describe('actions', () => {
it('getLastIndexed - returns the action to get the last indexed date', () => {
const action = getLastIndexed();
expect(action.type).toBe(GetLastIndexed.REQUEST);
});
it('getLastIndexedFailure - returns the action to process failure', () => {
const action = getLastIndexedFailure();
expect(action.type).toBe(GetLastIndexed.FAILURE);
});
it('getLastIndexedSuccess - returns the action to process success', () => {
const action = getLastIndexedSuccess(testEpoch);
const { payload } = action;
expect(action.type).toBe(GetLastIndexed.SUCCESS);
expect(payload.lastIndexedEpoch).toBe(testEpoch);
});
});
describe('reducer', () => {
let testState: LastIndexedReducerState;
beforeAll(() => {
testState = initialState;
});
it('should return the existing state if action is not handled', () => {
expect(reducer(testState, { type: 'INVALID.ACTION' })).toEqual(testState);
});
it('should handle GetLastIndexed.REQUEST', () => {
expect(reducer(testState, getLastIndexed())).toEqual(initialState);
});
it('should handle GetLastIndexed.FAILURE', () => {
expect(reducer(testState, getLastIndexedFailure())).toEqual({
lastIndexed: null,
});
});
it('should handle GetLastIndexed.SUCCESS', () => {
expect(reducer(testState, getLastIndexedSuccess(testEpoch))).toEqual({
lastIndexed: testEpoch,
});
});
});
describe('sagas', () => {
describe('getLastIndexedWatcher', () => {
it('takes every GetLastIndexed.REQUEST with getLastIndexedWorker', () => {
testSaga(getLastIndexedWatcher)
.next()
.takeEvery(GetLastIndexed.REQUEST, getLastIndexedWorker)
.next()
.isDone();
});
});
describe('getLastIndexedWorker', () => {
it('executes flow for getting last indexed value', () => {
testSaga(getLastIndexedWorker, getLastIndexed())
.next()
.call(API.getLastIndexed)
.next(testEpoch)
.put(getLastIndexedSuccess(testEpoch))
.next()
.isDone();
});
it('handles request error', () => {
testSaga(getLastIndexedWorker, getLastIndexed())
.next()
.throw(new Error())
.put(getLastIndexedFailure())
.next()
.isDone();
});
});
});
});
export enum GetLastIndexed {
REQUEST = 'amundsen/GET_LAST_UPDATED_REQUEST',
SUCCESS = 'amundsen/GET_LAST_UPDATED_SUCCESS',
FAILURE = 'amundsen/GET_LAST_UPDATED_FAILURE',
}
export interface GetLastIndexedRequest {
type: GetLastIndexed.REQUEST;
}
export interface GetLastIndexedResponse {
type: GetLastIndexed.SUCCESS | GetLastIndexed.FAILURE;
payload?: GetLastIndexedPayload;
}
export interface GetLastIndexedPayload {
lastIndexedEpoch?: number;
}
......@@ -12,6 +12,7 @@ import search, { SearchReducerState } from './search/reducer';
import tableMetadata, {
TableMetadataReducerState,
} from './tableMetadata/reducer';
import lastIndexed, { LastIndexedReducerState } from './lastIndexed/reducer';
import tags, { TagsReducerState } from './tags/reducer';
import user, { UserReducerState } from './user/reducer';
import bookmarks, { BookmarkReducerState } from './bookmark/reducer';
......@@ -28,6 +29,7 @@ export interface GlobalState {
popularTables: PopularTablesReducerState;
search: SearchReducerState;
tableMetadata: TableMetadataReducerState;
lastIndexed: LastIndexedReducerState;
tags: TagsReducerState;
user: UserReducerState;
}
......@@ -42,6 +44,7 @@ export default combineReducers<GlobalState>({
popularTables,
search,
tableMetadata,
lastIndexed,
tags,
user,
});
......@@ -44,13 +44,15 @@ import { updateTableOwnerWatcher } from './tableMetadata/owners/sagas';
import {
getTableDataWatcher,
getColumnDescriptionWatcher,
getLastIndexedWatcher,
getPreviewDataWatcher,
getTableDescriptionWatcher,
updateColumnDescriptionWatcher,
updateTableDescriptionWatcher,
} from './tableMetadata/sagas';
// LastIndexed
import { getLastIndexedWatcher } from './lastIndexed/sagas';
// Tags
import { getAllTagsWatcher, updateResourceTagsWatcher } from './tags/sagas';
......@@ -100,12 +102,13 @@ export default function* rootSaga() {
// TableDetail
getTableDataWatcher(),
getColumnDescriptionWatcher(),
getLastIndexedWatcher(),
getPreviewDataWatcher(),
getTableDescriptionWatcher(),
updateColumnDescriptionWatcher(),
updateTableDescriptionWatcher(),
updateTableOwnerWatcher(),
// LastIndexed
getLastIndexedWatcher(),
// User
getLoggedInUserWatcher(),
getUserWatcher(),
......
......@@ -31,7 +31,6 @@ export type TableData = TableMetadata & {
tags: Tag[];
};
export type DescriptionAPI = { description: string } & MessageAPI;
export type LastIndexedAPI = { timestamp: string } & MessageAPI;
export type PreviewDataAPI = { previewData: PreviewData } & MessageAPI;
export type TableDataAPI = { tableData: TableData } & MessageAPI;
export type RelatedDashboardDataAPI = {
......@@ -173,14 +172,6 @@ export function updateColumnDescription(
});
}
export function getLastIndexed() {
return axios
.get(`${API_PATH}/get_last_indexed`)
.then((response: AxiosResponse<LastIndexedAPI>) => {
return response.data.timestamp;
});
}
export function getPreviewData(queryParams: PreviewQueryParams) {
return axios({
url: '/api/preview/v0/',
......
......@@ -24,9 +24,6 @@ import {
GetColumnDescriptionRequest,
UpdateColumnDescription,
UpdateColumnDescriptionRequest,
GetLastIndexed,
GetLastIndexedRequest,
GetLastIndexedResponse,
GetPreviewData,
GetPreviewDataRequest,
GetPreviewDataResponse,
......@@ -66,7 +63,6 @@ export const initialTableDataState: TableMetadata = {
export const initialState: TableMetadataReducerState = {
isLoading: true,
lastIndexed: null,
preview: initialPreviewState,
statusCode: null,
tableData: initialTableDataState,
......@@ -231,23 +227,6 @@ export function updateColumnDescription(
};
}
export function getLastIndexed(): GetLastIndexedRequest {
return { type: GetLastIndexed.REQUEST };
}
export function getLastIndexedFailure(): GetLastIndexedResponse {
return { type: GetLastIndexed.FAILURE };
}
export function getLastIndexedSuccess(
lastIndexedEpoch: number
): GetLastIndexedResponse {
return {
type: GetLastIndexed.SUCCESS,
payload: {
lastIndexedEpoch,
},
};
}
export function getPreviewData(
queryParams: PreviewQueryParams
): GetPreviewDataRequest {
......@@ -286,7 +265,6 @@ export interface TableMetadataReducerState {
errorMessage?: string;
};
isLoading: boolean;
lastIndexed: number;
preview: {
data: PreviewData;
status: number | null;
......@@ -311,10 +289,7 @@ export default function reducer(
},
};
case GetTableData.REQUEST:
return {
...initialState,
lastIndexed: state.lastIndexed,
};
return initialState;
case GetTableData.FAILURE:
return {
...state,
......@@ -344,13 +319,6 @@ export default function reducer(
...state,
tableData: (<GetColumnDescriptionResponse>action).payload.tableMetadata,
};
case GetLastIndexed.FAILURE:
return { ...state, lastIndexed: null };
case GetLastIndexed.SUCCESS:
return {
...state,
lastIndexed: (<GetLastIndexedResponse>action).payload.lastIndexedEpoch,
};
case GetPreviewData.FAILURE:
case GetPreviewData.SUCCESS:
return { ...state, preview: (<GetPreviewDataResponse>action).payload };
......
......@@ -11,15 +11,11 @@ import {
getTableDescriptionSuccess,
getColumnDescriptionFailure,
getColumnDescriptionSuccess,
getLastIndexedFailure,
getLastIndexedSuccess,
getPreviewDataFailure,
getPreviewDataSuccess,
} from './reducer';
import {
GetLastIndexed,
GetLastIndexedRequest,
GetPreviewData,
GetPreviewDataRequest,
GetTableData,
......@@ -164,20 +160,6 @@ export function* updateColumnDescriptionWatcher(): SagaIterator {
);
}
export function* getLastIndexedWorker(
action: GetLastIndexedRequest
): SagaIterator {
try {
const lastIndexed = yield call(API.getLastIndexed);
yield put(getLastIndexedSuccess(lastIndexed));
} catch (e) {
yield put(getLastIndexedFailure());
}
}
export function* getLastIndexedWatcher(): SagaIterator {
yield takeEvery(GetLastIndexed.REQUEST, getLastIndexedWorker);
}
export function* getPreviewDataWorker(
action: GetPreviewDataRequest
): SagaIterator {
......
......@@ -28,9 +28,6 @@ import reducer, {
getColumnDescriptionFailure,
getColumnDescriptionSuccess,
updateColumnDescription,
getLastIndexed,
getLastIndexedFailure,
getLastIndexedSuccess,
getPreviewData,
getPreviewDataFailure,
getPreviewDataSuccess,
......@@ -51,8 +48,6 @@ import {
getColumnDescriptionWorker,
updateColumnDescriptionWatcher,
updateColumnDescriptionWorker,
getLastIndexedWatcher,
getLastIndexedWorker,
getPreviewDataWatcher,
getPreviewDataWorker,
} from '../sagas';
......@@ -63,7 +58,6 @@ import {
UpdateTableDescription,
GetColumnDescription,
UpdateColumnDescription,
GetLastIndexed,
GetPreviewData,
} from '../types';
......@@ -84,7 +78,6 @@ describe('tableMetadata ducks', () => {
let newDescription: string;
let previewData: PreviewData;
let queryParams: PreviewQueryParams;
let testEpoch: number;
beforeAll(() => {
expectedData = globalState.tableMetadata.tableData;
expectedOwners = {
......@@ -130,7 +123,6 @@ describe('tableMetadata ducks', () => {
schema: 'testSchema',
tableName: 'testName',
};
testEpoch = 1545925769;
});
describe('actions', () => {
......@@ -245,23 +237,6 @@ describe('tableMetadata ducks', () => {
expect(payload.onFailure).toBe(mockFailure);
});
it('getLastIndexed - returns the action to get the last indexed date of the table', () => {
const action = getLastIndexed();
expect(action.type).toBe(GetLastIndexed.REQUEST);
});
it('getLastIndexedFailure - returns the action to process failure', () => {
const action = getLastIndexedFailure();
expect(action.type).toBe(GetLastIndexed.FAILURE);
});
it('getLastIndexedSuccess - returns the action to process success', () => {
const action = getLastIndexedSuccess(testEpoch);
const { payload } = action;
expect(action.type).toBe(GetLastIndexed.SUCCESS);
expect(payload.lastIndexedEpoch).toBe(testEpoch);
});
it('getPreviewData - returns the action to get the preview table data', () => {
const action = getPreviewData(queryParams);
const { payload } = action;
......@@ -352,20 +327,6 @@ describe('tableMetadata ducks', () => {
});
});
it('should handle GetLastIndexed.FAILURE', () => {
expect(reducer(testState, getLastIndexedFailure())).toEqual({
...testState,
lastIndexed: null,
});
});
it('should handle GetLastIndexed.SUCCESS', () => {
expect(reducer(testState, getLastIndexedSuccess(testEpoch))).toEqual({
...testState,
lastIndexed: testEpoch,
});
});
it('should handle GetPreviewData.FAILURE', () => {
const action = getPreviewDataFailure({}, 500);
expect(reducer(testState, action)).toEqual({
......@@ -720,37 +681,6 @@ describe('tableMetadata ducks', () => {
});
});
describe('getLastIndexedWatcher', () => {
it('takes every GetLastIndexed.REQUEST with getLastIndexedWorker', () => {
testSaga(getLastIndexedWatcher)
.next()
.takeEvery(GetLastIndexed.REQUEST, getLastIndexedWorker)
.next()
.isDone();
});
});
describe('getLastIndexedWorker', () => {
it('executes flow for getting last indexed value', () => {
testSaga(getLastIndexedWorker, getLastIndexed())
.next()
.call(API.getLastIndexed)
.next(testEpoch)
.put(getLastIndexedSuccess(testEpoch))
.next()
.isDone();
});
it('handles request error', () => {
testSaga(getLastIndexedWorker, getLastIndexed())
.next()
.throw(new Error())
.put(getLastIndexedFailure())
.next()
.isDone();
});
});
describe('getPreviewDataWatcher', () => {
it('takes every GetPreviewData.REQUEST with getPreviewDataWorker', () => {
testSaga(getPreviewDataWatcher)
......
......@@ -116,21 +116,6 @@ export interface UpdateColumnDescriptionResponse {
type: UpdateColumnDescription.SUCCESS | UpdateColumnDescription.FAILURE;
}
export enum GetLastIndexed {
REQUEST = 'amundsen/tableMetadata/GET_LAST_UPDATED_REQUEST',
SUCCESS = 'amundsen/tableMetadata/GET_LAST_UPDATED_SUCCESS',
FAILURE = 'amundsen/tableMetadata/GET_LAST_UPDATED_FAILURE',
}
export interface GetLastIndexedRequest {
type: GetLastIndexed.REQUEST;
}
export interface GetLastIndexedResponse {
type: GetLastIndexed.SUCCESS | GetLastIndexed.FAILURE;
payload?: {
lastIndexedEpoch: number;
};
}
export enum GetPreviewData {
REQUEST = 'amundsen/preview/GET_PREVIEW_DATA_REQUEST',
SUCCESS = 'amundsen/preview/GET_PREVIEW_DATA_SUCCESS',
......
......@@ -148,7 +148,6 @@ const globalState: GlobalState = {
},
tableMetadata: {
isLoading: true,
lastIndexed: 1555632106,
preview: {
data: {},
status: null,
......@@ -179,6 +178,7 @@ const globalState: GlobalState = {
owners: {},
},
},
lastIndexed: { lastIndexed: 1555632106 },
tags: {
allTags: {
isLoading: false,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment