Skip to content

Commit 5c1ef4d

Browse files
added api_usage endpoint
1 parent 7cb3164 commit 5c1ef4d

File tree

7 files changed

+81
-1
lines changed

7 files changed

+81
-1
lines changed

client/packages/lowcoder/src/api/orgApi.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ export interface CreateOrgResponse extends ApiResponse {
2929
data: { orgId: string };
3030
}
3131

32+
export interface OrgAPIUsageResponse extends ApiResponse {
33+
data: number;
34+
}
35+
3236
export class OrgApi extends Api {
3337
static createGroupURL = "/v1/groups";
3438
static updateGroupURL = (groupId: string) => `/v1/groups/${groupId}/update`;
@@ -47,6 +51,7 @@ export class OrgApi extends Api {
4751
static createOrgURL = "/v1/organizations";
4852
static deleteOrgURL = (orgId: string) => `/v1/organizations/${orgId}`;
4953
static updateOrgURL = (orgId: string) => `/v1/organizations/${orgId}/update`;
54+
static fetchUsage = (orgId: string) => `/v1/organizations/${orgId}/api-usage`;
5055

5156
static createGroup(request: { name: string }): AxiosPromise<GenericApiResponse<OrgGroup>> {
5257
return Api.post(OrgApi.createGroupURL, request);
@@ -127,6 +132,10 @@ export class OrgApi extends Api {
127132
static updateOrg(request: UpdateOrgPayload): AxiosPromise<ApiResponse> {
128133
return Api.put(OrgApi.updateOrgURL(request.id), request);
129134
}
135+
136+
static fetchAPIUsage(orgId: string, lastMonthOnly?: boolean): AxiosPromise<ApiResponse> {
137+
return Api.get(OrgApi.fetchUsage(orgId), lastMonthOnly);
138+
}
130139
}
131140

132141
export default OrgApi;

client/packages/lowcoder/src/constants/reduxActionConstants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ export const ReduxActionTypes = {
100100
UPDATE_USER_PROFILE_SUCCESS: "UPDATE_USER_PROFILE_SUCCESS",
101101
UPLOAD_USER_HEAD_SUCCESS: "UPLOAD_USER_HEAD_SUCCESS", // update avatar
102102
MARK_USER_STATUS: "MARK_USER_STATUS",
103+
FETCH_ORG_API_USAGE: "FETCH_ORG_API_USAGE",
104+
FETCH_ORG_API_USAGE_SUCCESS: "FETCH_ORG_API_USAGE_SUCCESS",
103105

104106
/* home data */
105107
FETCH_HOME_DATA: "FETCH_HOME_DATA",

client/packages/lowcoder/src/pages/setting/index.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ import SettingHome from "./settingHome";
88

99
export function Setting() {
1010
const user = useSelector(getUser);
11+
12+
/* fetch Org's API usage
13+
14+
const apiUsage = useSelector(getOrgApiUsage);
15+
useEffect(() => {
16+
dispatch(fetchAPIUsageAction(user.currentOrgId));
17+
}, [user.currentOrgId])
18+
19+
*/
20+
1121
if (!currentOrgAdminOrDev(user)) {
1222
history.push(BASE_URL);
1323
}

client/packages/lowcoder/src/redux/reducers/uiReducers/orgReducer.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { User } from "constants/userConstants";
88
import {
99
DeleteOrgUserPayload,
1010
GroupUsersPayload,
11+
OrgAPIUsagePayload,
1112
OrgUsersPayload,
1213
RemoveGroupUserPayload,
1314
} from "redux/reduxActions/orgActions";
@@ -24,6 +25,7 @@ const initialState: OrgReduxState = {
2425
groupUsersFetching: true,
2526
fetchOrgGroupsFinished: false,
2627
orgCreateStatus: "init",
28+
apiUsage: 0,
2729
};
2830

2931
const orgReducer = createImmerReducer(initialState, {
@@ -104,6 +106,13 @@ const orgReducer = createImmerReducer(initialState, {
104106
...state,
105107
orgCreateStatus: "error",
106108
}),
109+
[ReduxActionTypes.FETCH_ORG_API_USAGE_SUCCESS]: (
110+
state: OrgReduxState,
111+
action: ReduxAction<OrgAPIUsagePayload>
112+
): OrgReduxState => ({
113+
...state,
114+
apiUsage: action.payload.apiUsage,
115+
})
107116
});
108117

109118
export interface OrgReduxState {
@@ -115,6 +124,7 @@ export interface OrgReduxState {
115124
groupUsersFetching: boolean;
116125
fetchOrgGroupsFinished: boolean;
117126
orgCreateStatus: ApiRequestStatus;
127+
apiUsage: number;
118128
}
119129

120130
export default orgReducer;

client/packages/lowcoder/src/redux/reduxActions/orgActions.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,25 @@ export const updateOrgSuccess = (payload: UpdateOrgPayload) => {
151151
payload: payload,
152152
};
153153
};
154+
155+
export type OrgAPIUsagePayload = {
156+
apiUsage: number,
157+
};
158+
159+
export const fetchAPIUsageAction = (
160+
orgId: string,
161+
lastMonthOnly?: boolean,
162+
) => ({
163+
type: ReduxActionTypes.FETCH_ORG_API_USAGE,
164+
payload: {
165+
orgId,
166+
lastMonthOnly,
167+
},
168+
});
169+
170+
export const fetchAPIUsageSuccessAction = (apiUsage: number) => ({
171+
type: ReduxActionTypes.FETCH_ORG_API_USAGE_SUCCESS,
172+
payload: {
173+
apiUsage,
174+
},
175+
});

client/packages/lowcoder/src/redux/sagas/orgSagas.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { messageInstance } from "lowcoder-design";
22

33
import { ApiResponse, GenericApiResponse } from "api/apiResponses";
4-
import OrgApi, { CreateOrgResponse, GroupUsersResponse, OrgUsersResponse } from "api/orgApi";
4+
import OrgApi, { CreateOrgResponse, GroupUsersResponse, OrgAPIUsageResponse, OrgUsersResponse } from "api/orgApi";
55
import { AxiosResponse } from "axios";
66
import { OrgGroup } from "constants/orgConstants";
77
import {
@@ -280,6 +280,28 @@ export function* updateOrgSaga(action: ReduxAction<UpdateOrgPayload>) {
280280
}
281281
}
282282

283+
export function* fetchAPIUsageSaga(action: ReduxAction<{
284+
orgId: string,
285+
lastMonthOnly?: boolean,
286+
}>) {
287+
try {
288+
const response: AxiosResponse<OrgAPIUsageResponse> = yield call(
289+
OrgApi.fetchAPIUsage,
290+
action.payload.orgId,
291+
action.payload.lastMonthOnly,
292+
);
293+
const isValidResponse: boolean = validateResponse(response);
294+
if (isValidResponse) {
295+
yield put({
296+
type: ReduxActionTypes.FETCH_ORG_API_USAGE_SUCCESS,
297+
payload: response.data.data,
298+
});
299+
}
300+
} catch (error) {
301+
log.error(error);
302+
}
303+
}
304+
283305
export default function* orgSagas() {
284306
yield all([
285307
takeLatest(ReduxActionTypes.UPDATE_GROUP_INFO, updateGroupSaga),
@@ -297,5 +319,6 @@ export default function* orgSagas() {
297319
takeLatest(ReduxActionTypes.CREATE_ORG, createOrgSaga),
298320
takeLatest(ReduxActionTypes.DELETE_ORG, deleteOrgSaga),
299321
takeLatest(ReduxActionTypes.UPDATE_ORG, updateOrgSaga),
322+
takeLatest(ReduxActionTypes.FETCH_ORG_API_USAGE, fetchAPIUsageSaga),
300323
]);
301324
}

client/packages/lowcoder/src/redux/selectors/orgSelectors.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ export const getFetchOrgGroupsFinished = (state: AppState) => {
1515
export const getOrgCreateStatus = (state: AppState) => {
1616
return state.ui.org.orgCreateStatus;
1717
};
18+
19+
export const getOrgApiUsage = (state: AppState) => {
20+
return state.ui.org.apiUsage;
21+
}

0 commit comments

Comments
 (0)