Skip to content

Commit

Permalink
Merge pull request #622 from masterchief-Dave/feat/api-get-status
Browse files Browse the repository at this point in the history
feat: get api status
  • Loading branch information
AdeGneus authored Aug 24, 2024
2 parents 392fe97 + 8fa41b9 commit bf0f8db
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 4 deletions.
13 changes: 12 additions & 1 deletion src/controllers/api-status.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Request, Response } from "express";
import { asyncHandler } from "../middleware/asyncHandler";
import { parseJsonResponse } from "../services/api-status.services";
import {
fetchApiStatusService,
parseJsonResponse,
} from "../services/api-status.services";
import { sendJsonResponse } from "../utils/sendJsonResponse";

export const createApiStatus = asyncHandler(
Expand All @@ -11,3 +14,11 @@ export const createApiStatus = asyncHandler(
sendJsonResponse(res, 201, "API status updated successfully");
},
);

export const getApiStatus = asyncHandler(
async (_req: Request, res: Response) => {
const response = await fetchApiStatusService();

sendJsonResponse(res, 200, "Api status", { response });
},
);
6 changes: 5 additions & 1 deletion src/routes/api-status.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { Router } from "express";
import { createApiStatus } from "../controllers/api-status.controller";
import {
createApiStatus,
getApiStatus,
} from "../controllers/api-status.controller";

const apiStatusRouter = Router();

apiStatusRouter.post("/api-status", createApiStatus);
apiStatusRouter.get("/api-status", getApiStatus);

export { apiStatusRouter };
52 changes: 51 additions & 1 deletion src/services/api-status.services.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import AppDataSource from "../data-source";
import { API_STATUS, ApiStatus } from "../models/api-model";
import { GroupedApi } from "../types";

const apiStatusRepository = AppDataSource.getRepository(ApiStatus);
const MAX_ALLOWED_RESPONSE_TIME = 2000;
Expand Down Expand Up @@ -47,4 +48,53 @@ const parseJsonResponse = async (resultJson: any): Promise<void> => {
}
};

export { parseJsonResponse };
const fetchApiStatusService = async () => {
const api_status_response = await apiStatusRepository.find();

const groupedAPIs = api_status_response.reduce((acc, current) => {
const existingGroup = acc.find(
(group) => group.api_group === current.api_group,
);

if (existingGroup) {
existingGroup.collection.push({
api_name: current.api_name,
is_operational: current.status,
details: current.details,
last_checked: current.updated_at,
});
} else {
acc.push({
api_group: current.api_group,
is_operational: API_STATUS.OPERATIONAL,
collection: [
{
api_name: current.api_name,
is_operational: current.status,
details: current.details,
last_checked: current.updated_at,
},
],
});
}

return acc;
}, [] as GroupedApi[]);

const response_dto = groupedAPIs.map((api) => {
const hasDegraded = api.collection.some(
(apiCollection) =>
apiCollection.is_operational !== API_STATUS.OPERATIONAL,
);

if (hasDegraded) {
api.is_operational = API_STATUS.DEGRADED;
}

return api;
});

return response_dto;
};

export { fetchApiStatusService, parseJsonResponse };
12 changes: 11 additions & 1 deletion src/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { User } from "../models";
import { Permissions } from "../models/permissions.entity";

export interface IUserService {
getUserById(id: string): Promise<User | null>;
Expand Down Expand Up @@ -123,3 +122,14 @@ export type UpdateUserRecordOption = {
export interface IBillingPlanService {
createBillingPlan(planData: Partial<BillingPlan>): Promise<BillingPlan[]>;
}

export type GroupedApi = {
api_group: string;
is_operational: API_STATUS;
collection: {
api_name: string;
is_operational: string;
details: string;
last_checked: Date;
}[];
};

0 comments on commit bf0f8db

Please sign in to comment.