|
| 1 | +import type {Reducer} from 'redux'; |
| 2 | + |
| 3 | +import type {TDomainKey} from '../../types/api/tablet'; |
| 4 | +import type { |
| 5 | + ITabletAction, |
| 6 | + ITabletDescribeHandledResponse, |
| 7 | + ITabletHandledResponse, |
| 8 | + ITabletPreparedHistoryItem, |
| 9 | + ITabletState, |
| 10 | +} from '../../types/store/tablet'; |
| 11 | +import '../../services/api'; |
| 12 | + |
| 13 | +import {createRequestActionTypes, createApiRequest} from '../utils'; |
| 14 | + |
| 15 | +export const FETCH_TABLET = createRequestActionTypes('TABLET', 'FETCH_TABLET'); |
| 16 | +export const FETCH_TABLET_DESCRIBE = createRequestActionTypes('TABLET', 'FETCH_TABLET_DESCRIBE'); |
| 17 | + |
| 18 | +const initialState = { |
| 19 | + loading: false, |
| 20 | + tenantPath: '-', |
| 21 | +}; |
| 22 | + |
| 23 | +const tablet: Reducer<ITabletState, ITabletAction> = (state = initialState, action) => { |
| 24 | + switch (action.type) { |
| 25 | + case FETCH_TABLET.REQUEST: { |
| 26 | + return { |
| 27 | + ...state, |
| 28 | + loading: true, |
| 29 | + }; |
| 30 | + } |
| 31 | + case FETCH_TABLET.SUCCESS: { |
| 32 | + const {tabletData, historyData} = action.data; |
| 33 | + const {TabletId: id} = tabletData; |
| 34 | + return { |
| 35 | + ...state, |
| 36 | + id, |
| 37 | + data: tabletData, |
| 38 | + history: historyData, |
| 39 | + loading: false, |
| 40 | + error: undefined, |
| 41 | + }; |
| 42 | + } |
| 43 | + case FETCH_TABLET.FAILURE: { |
| 44 | + return { |
| 45 | + ...state, |
| 46 | + error: action.error, |
| 47 | + loading: false, |
| 48 | + }; |
| 49 | + } |
| 50 | + case FETCH_TABLET_DESCRIBE.SUCCESS: { |
| 51 | + const {tenantPath} = action.data; |
| 52 | + |
| 53 | + return { |
| 54 | + ...state, |
| 55 | + tenantPath, |
| 56 | + error: undefined, |
| 57 | + }; |
| 58 | + } |
| 59 | + default: |
| 60 | + return state; |
| 61 | + } |
| 62 | +}; |
| 63 | + |
| 64 | +export const getTablet = (id: string) => { |
| 65 | + return createApiRequest({ |
| 66 | + request: Promise.all([window.api.getTablet({id}), window.api.getTabletHistory({id})]), |
| 67 | + actions: FETCH_TABLET, |
| 68 | + dataHandler: ([tabletResponseData, historyResponseData]): ITabletHandledResponse => { |
| 69 | + const historyData = Object.keys(historyResponseData).reduce< |
| 70 | + ITabletPreparedHistoryItem[] |
| 71 | + >((list, nodeId) => { |
| 72 | + const tabletInfo = historyResponseData[nodeId]?.TabletStateInfo; |
| 73 | + if (tabletInfo && tabletInfo.length) { |
| 74 | + const leaderTablet = tabletInfo.find((t) => t.Leader) || tabletInfo[0]; |
| 75 | + |
| 76 | + const {ChangeTime, Generation, State, Leader, FollowerId} = leaderTablet; |
| 77 | + |
| 78 | + list.push({ |
| 79 | + nodeId, |
| 80 | + generation: Generation, |
| 81 | + changeTime: ChangeTime, |
| 82 | + state: State, |
| 83 | + leader: Leader, |
| 84 | + followerId: FollowerId, |
| 85 | + }); |
| 86 | + } |
| 87 | + return list; |
| 88 | + }, []); |
| 89 | + |
| 90 | + const {TabletStateInfo = []} = tabletResponseData; |
| 91 | + const [tabletData = {}] = TabletStateInfo; |
| 92 | + |
| 93 | + return {tabletData, historyData}; |
| 94 | + }, |
| 95 | + }); |
| 96 | +}; |
| 97 | + |
| 98 | +export const getTabletDescribe = (tenantId: TDomainKey = {}) => { |
| 99 | + return createApiRequest({ |
| 100 | + request: window.api.getTabletDescribe(tenantId), |
| 101 | + actions: FETCH_TABLET_DESCRIBE, |
| 102 | + dataHandler: (tabletDescribe): ITabletDescribeHandledResponse => { |
| 103 | + const {SchemeShard, PathId} = tenantId; |
| 104 | + const tenantPath = tabletDescribe.Path || `${SchemeShard}:${PathId}`; |
| 105 | + |
| 106 | + return {tenantPath}; |
| 107 | + }, |
| 108 | + }); |
| 109 | +}; |
| 110 | + |
| 111 | +export default tablet; |
0 commit comments