Skip to content

Commit 70ab46d

Browse files
refactor(Tablet): migrate reducer to ts
1 parent 48706b9 commit 70ab46d

File tree

7 files changed

+188
-104
lines changed

7 files changed

+188
-104
lines changed

src/containers/Tablet/Tablet.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ class Tablet extends React.Component {
111111
const {isFirstFetchData} = this.state;
112112

113113
if (version && this.isValidVersion()) {
114-
this.props.getTablet(id).then(({tablet}) => {
115-
if (isFirstFetchData && tablet.TenantId) {
116-
this.props.getTabletDescribe(tablet.TenantId);
114+
this.props.getTablet(id).then(({tabletData}) => {
115+
if (isFirstFetchData && tabletData.TenantId) {
116+
this.props.getTabletDescribe(tabletData.TenantId);
117117
}
118118

119119
this.setState({isFirstFetchData: false});

src/services/api.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ interface Window {
5757
nodes?: string[];
5858
path?: string;
5959
}) => Promise<import('../types/api/tablet').TEvTabletStateResponse>;
60+
getTabletDescribe: (
61+
tenantId?: import('../types/api/tablet').TDomainKey,
62+
) => Promise<import('../types/api/schema').TEvDescribeSchemeResult>;
63+
getTablet: (params: {
64+
id?: string;
65+
}) => Promise<import('../types/api/tablet').TEvTabletStateResponse>;
66+
getTabletHistory: (params: {
67+
id?: string;
68+
}) => Promise<import('../types/api/tablet').UnmergedTEvTabletStateResponse>;
6069
getHeatmapData: (params: {
6170
path: string;
6271
}) => Promise<import('../types/api/schema').TEvDescribeSchemeResult>;

src/services/api.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,10 @@ export class YdbEmbeddedAPI extends AxiosWrapper {
247247
this.getPath(`/tablets/app?TabletID=${hiveId}&page=ResumeTablet&tablet=${id}`),
248248
);
249249
}
250-
getTabletDescribe(TenantId) {
250+
getTabletDescribe(tenantId) {
251251
return this.get(this.getPath('/viewer/json/describe'), {
252-
schemeshard_id: TenantId.SchemeShard,
253-
path_id: TenantId.PathId,
252+
schemeshard_id: tenantId?.SchemeShard,
253+
path_id: tenantId?.PathId,
254254
});
255255
}
256256
postSetting(name, value) {

src/store/reducers/tablet.js

Lines changed: 0 additions & 94 deletions
This file was deleted.

src/store/reducers/tablet.ts

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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;

src/types/api/tablet.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import {EFlag} from './enums';
22

3-
// endpoint: /viewer/json/tabletinfo
4-
// source: https://github.com/ydb-platform/ydb/blob/main/ydb/core/protos/sys_view.proto
5-
3+
/**
4+
* endpoint: /viewer/json/tabletinfo
5+
*
6+
* source: https://github.com/ydb-platform/ydb/blob/main/ydb/core/protos/sys_view.proto
7+
*/
68
export interface TEvTabletStateResponse {
79
TabletStateInfo?: TTabletStateInfo[];
810

@@ -16,6 +18,12 @@ export interface TEvTabletStateResponse {
1618
Packed5?: unknown;
1719
}
1820

21+
/**
22+
* endpoint: /viewer/json/tabletinfo with merge=false in query.
23+
* Maps nodes to their tablets
24+
*/
25+
export type UnmergedTEvTabletStateResponse = Record<string, TEvTabletStateResponse>;
26+
1927
export interface TTabletStateInfo {
2028
/** uint64 */
2129
TabletId?: string;
@@ -46,7 +54,7 @@ interface TCustomTabletAttribute {
4654
Value?: string;
4755
}
4856

49-
interface TDomainKey {
57+
export interface TDomainKey {
5058
/** fixed64 */
5159
SchemeShard?: string;
5260
/** fixed64 */

src/types/store/tablet.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import type {IResponseError} from '../api/error';
2+
import type {ETabletState, TTabletStateInfo} from '../api/tablet';
3+
4+
import {FETCH_TABLET, FETCH_TABLET_DESCRIBE} from '../../store/reducers/tablet';
5+
import {ApiRequestAction} from '../../store/utils';
6+
7+
export interface ITabletPreparedHistoryItem {
8+
nodeId: string;
9+
generation: number | undefined;
10+
changeTime: string | undefined;
11+
state: ETabletState | undefined;
12+
leader: boolean | undefined;
13+
followerId: number | undefined;
14+
}
15+
16+
export interface ITabletState {
17+
loading: boolean;
18+
tenantPath: string;
19+
error?: IResponseError;
20+
id?: string;
21+
history?: ITabletPreparedHistoryItem[];
22+
data?: TTabletStateInfo;
23+
}
24+
25+
export interface ITabletHandledResponse {
26+
tabletData: TTabletStateInfo;
27+
historyData: ITabletPreparedHistoryItem[];
28+
}
29+
30+
export interface ITabletDescribeHandledResponse {
31+
tenantPath: string;
32+
}
33+
34+
type ITabletApiRequestAction = ApiRequestAction<
35+
typeof FETCH_TABLET,
36+
ITabletHandledResponse,
37+
IResponseError
38+
>;
39+
40+
type ITabletDescribeApiRequestAction = ApiRequestAction<
41+
typeof FETCH_TABLET_DESCRIBE,
42+
ITabletDescribeHandledResponse,
43+
IResponseError
44+
>;
45+
46+
export type ITabletAction = ITabletApiRequestAction | ITabletDescribeApiRequestAction;
47+
48+
export interface ITabletRootStateSlice {
49+
tablet: ITabletState;
50+
}

0 commit comments

Comments
 (0)