Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: get api status #622

Merged
merged 1 commit into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}[];
};
Loading