refactor networkSerivce.getMonitorsByTeamId to take a config objet

This commit is contained in:
Alex Holliday committed 2024-09-18 10:57:44 +08:00
1 parent cab023709b
commit 4748afb93a
5 files changed
+74 -58

No files matched your search

+14 -11
View File
@@ -25,17 +25,20 @@ const Incidents = () => {
useEffect(() => { useEffect(() => {
const fetchMonitors = async () => { const fetchMonitors = async () => {
setLoading(true); setLoading(true);
const res = await networkService.getMonitorsByTeamId( const res = await networkService.getMonitorsByTeamId({
authState.authToken, authToken: authState.authToken,
authState.user.teamId, teamId: authState.user.teamId,
-1, limit: -1,
null, types: null,
null, status: null,
null, checkOrder: null,
null, normalize: null,
null, page: null,
null rowsPerPage: null,
); filter: null,
field: null,
order: null,
});
// Reduce to a lookup object for 0(1) lookup // Reduce to a lookup object for 0(1) lookup
if (res?.data?.data?.monitors?.length > 0) { if (res?.data?.data?.monitors?.length > 0) {
const monitorLookup = res.data.data.monitors.reduce((acc, monitor) => { const monitorLookup = res.data.data.monitors.reduce((acc, monitor) => {
@@ -142,20 +142,20 @@ const MonitorTable = ({ isAdmin }) => {
try { try {
const { authToken } = authState; const { authToken } = authState;
const user = jwtDecode(authToken); const user = jwtDecode(authToken);
const res = await networkService.getMonitorsByTeamId( const res = await networkService.getMonitorsByTeamId({
authToken, authToken,
user.teamId, teamId: user.teamId,
25, limit: 25,
["http", "ping"], types: ["http", "ping"],
null, status: null,
"desc", checkOrder: "desc",
true, normalize: true,
page, page: page,
rowsPerPage, rowsPerPage: rowsPerPage,
null, filter: null,
null, field: null,
null order: null,
); });
setMonitors(res?.data?.data?.monitors ?? []); setMonitors(res?.data?.data?.monitors ?? []);
setMonitorCount(res?.data?.data?.monitorCount ?? 0); setMonitorCount(res?.data?.data?.monitorCount ?? 0);
} catch (error) { } catch (error) {
+14 -11
View File
@@ -29,17 +29,20 @@ const PageSpeed = ({ isAdmin }) => {
const fetchMonitors = async () => { const fetchMonitors = async () => {
try { try {
setIsLoading(true); setIsLoading(true);
const res = await networkService.getMonitorsByTeamId( const res = await networkService.getMonitorsByTeamId({
authToken, authToken: authToken,
user.teamId, teamId: user.teamId,
10, limit: 10,
["pagespeed"], types: ["pagespeed"],
null, status: null,
"desc", checkOrder: "desc",
true, normalize: true,
null, page: null,
null rowsPerPage: null,
); filter: null,
field: null,
order: null,
});
if (res?.data?.data?.monitors) { if (res?.data?.data?.monitors) {
setMonitors(res.data.data.monitors); setMonitors(res.data.data.monitors);
} }
+29 -21
View File
@@ -83,29 +83,37 @@ class NetworkService {
* ************************************ * ************************************
* *
* @async * @async
* @param {string} authToken - The authorization token to be used in the request header. * @param {Object} config - The configuration object.
* @param {string} userId - The ID of the user whose monitors are to be retrieved. * @param {string} config.authToken - The authorization token to be used in the request header.
* @param {number} [limit] - The maximum number of monitors to retrieve. * @param {string} config.teamId - The ID of the team whose monitors are to be retrieved.
* @param {Array<string>} [types] - The types of monitors to retrieve. * @param {number} [config.limit] - The maximum number of monitors to retrieve.
* @param {string} [status] - The status of the monitors to retrieve. * @param {Array<string>} [config.types] - The types of monitors to retrieve.
* @param {string} [sortOrder] - The order in which to sort the retrieved monitors. * @param {string} [config.status] - The status of the monitors to retrieve.
* @param {boolean} [normalize] - Whether to normalize the retrieved monitors. * @param {string} [config.checkOrder] - The order in which to sort the retrieved monitors.
* @param {boolean} [config.normalize] - Whether to normalize the retrieved monitors.
* @param {number} [config.page] - The page number for pagination.
* @param {number} [config.rowsPerPage] - The number of rows per page for pagination.
* @param {string} [config.filter] - The filter to apply to the monitors.
* @param {string} [config.field] - The field to sort by.
* @param {string} [config.order] - The order in which to sort the field.
* @returns {Promise<AxiosResponse>} The response from the axios GET request. * @returns {Promise<AxiosResponse>} The response from the axios GET request.
*/ */
async getMonitorsByTeamId( async getMonitorsByTeamId(config) {
authToken, const {
teamId, authToken,
limit, teamId,
types, limit,
status, types,
checkOrder, status,
normalize, checkOrder,
page, normalize,
rowsPerPage, page,
filter, rowsPerPage,
field, filter,
order field,
) { order,
} = config;
const params = new URLSearchParams(); const params = new URLSearchParams();
if (limit) params.append("limit", limit); if (limit) params.append("limit", limit);
+4 -2
View File
@@ -359,8 +359,10 @@ const getMonitorsByTeamId = async (req, res) => {
field, field,
order, order,
} = req.query || {}; } = req.query || {};
const monitorQuery = { teamId: req.params.teamId };
const monitorQuery = { teamId: req.params.teamId, type }; if (type !== undefined) {
monitorQuery.type = type;
}
// Add filter if provided // Add filter if provided
// $options: "i" makes the search case-insensitive // $options: "i" makes the search case-insensitive
if (filter !== undefined) { if (filter !== undefined) {