Skip to content

Commit

Permalink
refactor: Update Status.Entities.ts and Status.Trans.ts
Browse files Browse the repository at this point in the history
- Update Status.Entities.ts to add a new property "updates" to the IncidentEntityV2 interface.
- Update Status.Trans.ts to rename it to Status.Trans.V1.ts and update the Logger name to "TransformerV1".
- Add a new file Status.Trans.V2.ts to introduce a new TransformerV2 function for handling v2 status data.
- Update Status.tsx to use the appropriate Transformer function based on the value of the environment variable "SD_BACKEND_V2".
  • Loading branch information
Aloento committed Oct 25, 2024
1 parent 38ffcb9 commit 100710c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/Services/Status.Entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ interface IncidentEntityV1 {
* @since 1.0.0
* @version 0.1.0
*/
export interface IncidentEntityV2 extends IncidentEntityV1 {
export interface IncidentEntityV2 extends Omit<IncidentEntityV1, "updates"> {
title: string;
components: number[];
system: boolean;
updates: UpdateEntityV2[];
updates?: UpdateEntityV2[];
}

interface UpdateEntityV1 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import { EmptyDB } from "./Status";
import { NameEnum, StatusEntityV1, StatusEnum } from "./Status.Entities";
import { IStatusContext } from "./Status.Models";

const log = new Logger("Service", "Status", "Transformer");
const log = new Logger("Service", "Status", "TransformerV1");

/**
* @author Aloento
* @since 1.0.0
* @version 0.1.0
*/
export function Transformer(list: StatusEntityV1[]): IStatusContext {
export function TransformerV1(list: StatusEntityV1[]): IStatusContext {
let id = 0;
const db = EmptyDB();

Expand Down
23 changes: 23 additions & 0 deletions src/Services/Status.Trans.V2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Logger } from "~/Helpers/Logger";
import { EmptyDB } from "./Status";
import { IncidentEntityV2, StatusEntityV2 } from "./Status.Entities";
import { IStatusContext } from "./Status.Models";

const log = new Logger("Service", "Status", "TransformerV1");

/**
* @author Aloento
* @since 1.0.0
* @version 0.1.0
*/
export function TransformerV2({ Components, Events }: { Components: StatusEntityV2[], Events: IncidentEntityV2[] }): IStatusContext {
const db = EmptyDB();

if (!Components?.length || !Events?.length) {
log.warn("Empty List.");
return db;
}

log.info("Status data loaded.", db);
return db;
}
35 changes: 26 additions & 9 deletions src/Services/Status.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { openDB } from "idb";
import { createContext, useContext, useState } from "react";
import { Dic } from "~/Helpers/Entities";
import { Logger } from "~/Helpers/Logger";
import { StatusEntityV1 } from "./Status.Entities";
import { IncidentEntityV2, StatusEntityV1, StatusEntityV2 } from "./Status.Entities";
import { IStatusContext } from "./Status.Models";
import { Transformer } from "./Status.Trans";
import { TransformerV1 } from "./Status.Trans.V1";
import { TransformerV2 } from "./Status.Trans.V2";

/**
* @author Aloento
Expand Down Expand Up @@ -94,24 +95,40 @@ export function useStatus() {
*/
export function StatusContext({ children }: { children: JSX.Element }) {
const [db, setDB] = useState(DB);

const url = process.env.SD_BACKEND_URL;
const uri = process.env.SD_BACKEND_API;
const file = process.env.SD_BACKEND_FILE === "true";
const v2 = process.env.SD_BACKEND_V2 === "true";

useRequest(
async () => {
log.info("Loading status data...");
log.info(`Loading status data from ${v2 ? "v2" : "v1"}...`);

const compLink = file ? "/mock.json" : `${url}${uri}/${v2 ? "components" : "component_status"}`;
const compRes = await fetch(compLink);
const compData = await compRes.json();

log.debug("Components Status loaded.", compData);

if (v2) {
const eventLink = file ? "/event.json" : `${url}${uri}/incidents`;
const eventRes = await fetch(eventLink);
const eventData = await eventRes.json();

log.debug("Events loaded.", eventData);

const link = file ? "/mock.json" : `${url}${uri}/component_status`;
const response = await fetch(link);
const data = await response.json();
return {
Components: compData as StatusEntityV2[],
Events: eventData as IncidentEntityV2[]
};
}

log.debug("Status data loaded.", data);
return data as StatusEntityV1[];
return compData as StatusEntityV1[];
},
{
cacheKey: log.namespace,
onSuccess: (list) => update(Transformer(list)),
onSuccess: (res: any) => update(v2 ? TransformerV2(res) : TransformerV1(res)),
}
);

Expand Down

0 comments on commit 100710c

Please sign in to comment.