diff --git a/client/src/Pages/Incidents/Components/StatusBoxes/index.jsx b/client/src/Pages/Incidents/Components/StatusBoxes/index.jsx index ae29f4a8f..d44e00b22 100644 --- a/client/src/Pages/Incidents/Components/StatusBoxes/index.jsx +++ b/client/src/Pages/Incidents/Components/StatusBoxes/index.jsx @@ -4,9 +4,9 @@ import StatusBox from "./StatusBox"; import { useTheme } from "@emotion/react"; import SkeletonLayout from "./skeleton"; -const StatusBoxes = ({ shouldRender }) => { +const StatusBoxes = ({ isLoading, statusCounts }) => { const theme = useTheme(); - if (!shouldRender) return ; + if (isLoading) return ; return ( { > ); }; StatusBoxes.propTypes = { - shouldRender: PropTypes.bool, + isLoading: PropTypes.bool, + statusCounts: PropTypes.shape({ + total: PropTypes.number, + resolved: PropTypes.number, + cannotResolve: PropTypes.number, + down: PropTypes.number, + }), }; export default StatusBoxes; \ No newline at end of file diff --git a/client/src/Pages/Incidents/index.jsx b/client/src/Pages/Incidents/index.jsx index 8d668cb01..708b443a5 100644 --- a/client/src/Pages/Incidents/index.jsx +++ b/client/src/Pages/Incidents/index.jsx @@ -64,7 +64,7 @@ const Incidents = () => { { } }; +/** + * Get checks and summary by team ID + * @async + * @param {string} teamId + * @returns {Promise} + * @throws {Error} + */ +const getChecksAndSummaryByTeamId = async ({ teamId }) => { + try { + const matchStage = { + teamId: new ObjectId(teamId), + } + const checks = await Check.aggregate(buildChecksAndSummaryByTeamIdPipeline({ matchStage })); + return checks[0].summary; + } catch (error) { + error.service = SERVICE_NAME; + error.method = "getChecksAndSummaryByTeamId"; + throw error; + } +}; + /** * Delete all checks for a monitor * @async @@ -387,6 +409,7 @@ export { getChecksByTeam, ackCheck, ackAllChecks, + getChecksAndSummaryByTeamId, deleteChecks, deleteChecksByTeamId, updateChecksTTL, diff --git a/server/db/mongo/modules/checkModuleQueries.js b/server/db/mongo/modules/checkModuleQueries.js new file mode 100644 index 000000000..097389a52 --- /dev/null +++ b/server/db/mongo/modules/checkModuleQueries.js @@ -0,0 +1,48 @@ +const buildChecksAndSummaryByTeamIdPipeline = ({ matchStage }) => { + return [ + { $match: matchStage }, + { + $facet: { + summary: [ + { + $group: { + _id: null, + totalChecks: { $sum: { $cond: [{ $eq: ["$status", false] }, 1, 0] } }, + resolvedChecks: { + $sum: { + $cond: [{ $eq: ["$ack", true] }, 1, 0], + }, + }, + downChecks: { + $sum: { + $cond: [ + { $and: [{ $eq: ["$ack", false] }, { $eq: ["$status", false] }] }, + 1, + 0, + ], + }, + }, + cannotResolveChecks: { + $sum: { + $cond: [{ $eq: ["$statusCode", 5000] }, 1, 0], + }, + }, + }, + }, + { + $project: { + _id: 0, + }, + }, + ], + }, + }, + { + $project: { + summary: { $arrayElemAt: ["$summary", 0] }, + }, + }, + ]; +}; + +export { buildChecksAndSummaryByTeamIdPipeline };