Skip to content

Commit

Permalink
refactor: naming refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
9sneha-n committed Sep 27, 2024
1 parent 0bd5edb commit 686088d
Show file tree
Hide file tree
Showing 14 changed files with 167 additions and 136 deletions.
12 changes: 7 additions & 5 deletions src/CompositionRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type Repositories = {
optionsRepository: OptionsRepository;
teamMemberRepository: TeamMemberRepository;
orgUnitRepository: OrgUnitRepository;
analytics: PerformanceOverviewRepository;
performanceOverviewRepository: PerformanceOverviewRepository;
};

function getCompositionRoot(repositories: Repositories) {
Expand All @@ -62,11 +62,13 @@ function getCompositionRoot(repositories: Repositories) {
repositories.optionsRepository
),
},
analytics: {
performanceOverview: {
getPerformanceOverviewMetrics: new GetAllPerformanceOverviewMetricsUseCase(
repositories
),
getDiseasesTotal: new GetDiseasesTotalUseCase(repositories),
getEventTrackerCounts: new GetDiseasesTotalUseCase(
repositories.performanceOverviewRepository
),
},
};
}
Expand All @@ -81,7 +83,7 @@ export function getWebappCompositionRoot(api: D2Api) {
optionsRepository: new OptionsD2Repository(api),
teamMemberRepository: new TeamMemberD2Repository(api),
orgUnitRepository: new OrgUnitD2Repository(api),
analytics: new PerformanceOverviewD2Repository(api, dataStoreClient),
performanceOverviewRepository: new PerformanceOverviewD2Repository(api, dataStoreClient),
};

return getCompositionRoot(repositories);
Expand All @@ -96,7 +98,7 @@ export function getTestCompositionRoot() {
optionsRepository: new OptionsTestRepository(),
teamMemberRepository: new TeamMemberTestRepository(),
orgUnitRepository: new OrgUnitTestRepository(),
analytics: new PerformanceOverviewTestRepository(),
performanceOverviewRepository: new PerformanceOverviewTestRepository(),
};

return getCompositionRoot(repositories);
Expand Down
2 changes: 1 addition & 1 deletion src/data/DataStoreClient.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { D2Api, DataStore } from "@eyeseetea/d2-api/2.36";
import { apiToFuture, FutureData } from "./api-futures";

export const dataStoreNamespace = "zebra";
const dataStoreNamespace = "zebra";

export class DataStoreClient {
private dataStore: DataStore;
Expand Down
97 changes: 28 additions & 69 deletions src/data/repositories/PerformanceOverviewD2Repository.ts
Original file line number Diff line number Diff line change
@@ -1,79 +1,31 @@
// @ts-nocheck
import { Maybe } from "../../utils/ts-utils";
import { AnalyticsResponse, D2Api } from "../../types/d2-api";
import { PerformanceOverviewRepository } from "../../domain/repositories/PerformanceOverviewRepository";
import { apiToFuture, FutureData } from "../api-futures";
import { RTSL_ZEBRA_PROGRAM_ID } from "./consts/DiseaseOutbreakConstants";
import _ from "../../domain/entities/generic/Collection";
import { Future } from "../../domain/entities/generic/Future";
import { IndicatorsId, NB_OF_ACTIVE_VERIFIED } from "./consts/AnalyticsConstants";
import { EvenTrackerCountsIndicatorMap, IndicatorsId } from "./consts/PerformanceOverviewConstants";
import moment from "moment";
import { DiseaseOutbreakEventBaseAttrs } from "../../domain/entities/disease-outbreak-event/DiseaseOutbreakEvent";
import { DataStoreClient } from "../DataStoreClient";
import { Id } from "../../domain/entities/Ref";

type Disease =
| "AFP"
| "Acute VHF"
| "Acute respiratory"
| "Anthrax"
| "Bacterial meningitis"
| "COVID19"
| "Cholera"
| "Diarrhoea with blood"
| "Measles"
| "Monkeypox"
| "Neonatal tetanus"
| "Plague"
| "SARIs"
| "Typhoid fever"
| "Zika fever";

type Hazard = "Animal type" | "Human type" | "Human and Animal type" | "Environmental type";

export type PerformanceOverviewMetrics = {
id: Id;
event: string;
province: string;
duration: string;
manager: string;
cases: string;
deaths: string;
era1: string;
era2: string;
era3: string;
era4: string;
era5: string;
era6: string;
era7: string;
detect7d: string;
notify1d: string;
respond7d: string;
creationDate: string;
suspectedDisease: Disease;
hazardType: Hazard;
};

interface DiseaseEntry {
disease: Disease;
total: number;
}

interface HazardEntry {
hazard: Hazard;
total: number;
}

export type DiseaseTotalAttrs = DiseaseEntry | HazardEntry;
import {
DiseaseNames,
EventTrackerCounts,
HazardNames,
PerformanceOverviewMetrics,
} from "../../domain/entities/disease-outbreak-event/PerformanceOverviewMetrics";
import { AlertSynchronizationData } from "../../domain/entities/alert/AlertData";
import { OrgUnit } from "../../domain/entities/OrgUnit";

export class PerformanceOverviewD2Repository implements PerformanceOverviewRepository {
constructor(private api: D2Api, private datastore: DataStoreClient) {}

getDiseasesTotal(filters?: Record<string, string[]>): FutureData<DiseaseTotalAttrs[]> {
const transformData = (data: string[][], activeVerified: typeof NB_OF_ACTIVE_VERIFIED) => {
getDiseasesTotal(filters?: Record<string, string[]>): FutureData<EventTrackerCounts[]> {
const transformData = (data: string[][]) => {
return data
.flatMap(([id, _period, _orgUnit, total]) => {
const indicator = activeVerified.find(d => d.id === id);
const indicator = EvenTrackerCountsIndicatorMap.find(d => d.id === id);
if (!indicator || !total) {
return [];
}
Expand Down Expand Up @@ -108,15 +60,15 @@ export class PerformanceOverviewD2Repository implements PerformanceOverviewRepos
apiToFuture(
this.api.analytics.get({
dimension: [
`dx:${NB_OF_ACTIVE_VERIFIED.map(({ id }) => id).join(";")}`,
`dx:${EvenTrackerCountsIndicatorMap.map(({ id }) => id).join(";")}`,
"ou:LEVEL-2",
"pe:THIS_YEAR",
],
includeMetadataDetails: true,
})
);
return fetchActiveVerifiedAnalytics().map(res => {
const rows = transformData(res.rows, NB_OF_ACTIVE_VERIFIED) || [];
const rows = transformData(res.rows) || [];

return Object.values(
rows.reduce((acc, { disease, hazard, total }) => {
Expand All @@ -128,13 +80,13 @@ export class PerformanceOverviewD2Repository implements PerformanceOverviewRepos
const existingEntry =
acc[name] ||
(disease
? { disease: disease as Disease, total: 0 }
: { hazard: hazard as Hazard, total: 0 });
? { name: disease as DiseaseNames, type: "disease", total: 0 }
: { name: hazard as HazardNames, type: "hazard", total: 0 });

existingEntry.total += total;
acc[name] = existingEntry;
return acc;
}, {} as Record<string, DiseaseEntry | HazardEntry>)
}, {} as Record<string, EventTrackerCounts>)
);
});
}
Expand Down Expand Up @@ -173,6 +125,7 @@ export class PerformanceOverviewD2Repository implements PerformanceOverviewRepos
this.mapRowToBaseIndicator(
row,
indicatorsProgramFuture.headers,
//@ts-ignore
indicatorsProgramFuture.metaData
)
) || [];
Expand All @@ -190,14 +143,14 @@ export class PerformanceOverviewD2Repository implements PerformanceOverviewRepos
manager: event.incidentManagerName,
cases: casesAndDeaths.cases.toString(),
deaths: casesAndDeaths.deaths.toString(),
} as ProgramIndicatorBaseAttrs;
} as PerformanceOverviewMetrics;
}
return {
...baseIndicator,
manager: event.incidentManagerName,
cases: casesAndDeaths.cases.toString(),
deaths: casesAndDeaths.deaths.toString(),
} as ProgramIndicatorBaseAttrs;
} as PerformanceOverviewMetrics;
});
});

Expand Down Expand Up @@ -227,6 +180,7 @@ export class PerformanceOverviewD2Repository implements PerformanceOverviewRepos
private mapRowToBaseIndicator(
row: string[],
headers: { name: string; column: string }[],
//@ts-ignore
metaData: AnalyticsResponse["metaData"]
): Partial<PerformanceOverviewMetrics> {
return headers.reduce((acc, header, index) => {
Expand All @@ -238,17 +192,22 @@ export class PerformanceOverviewD2Repository implements PerformanceOverviewRepos

if (key === "suspectedDisease") {
acc[key] =
//@ts-ignore
Object.values(metaData.items).find(item => item.code === row[index])?.name ||
"";
} else if (key === "hazardType") {
acc[key] =
//@ts-ignore
Object.values(metaData.items).find(item => item.code === row[index])?.name ||
"";
} else if (key === "eventDetectionDate") {
}
//@ts-ignore
else if (key === "eventDetectionDate") {
acc.duration = `${moment().diff(moment(row[index]), "days").toString()}d`;
//@ts-ignore
acc[key] = moment(row[index]).format("YYYY-MM-DD");
} else {
acc[key] = row[index] as (Hazard & OrgUnit[]) | undefined;
acc[key] = row[index] as (HazardNames & OrgUnit[]) | undefined;
}

return acc;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import {
DiseaseNames,
HazardNames,
} from "../../../domain/entities/disease-outbreak-event/PerformanceOverviewMetrics";
import { Id } from "../../../domain/entities/Ref";

export enum IndicatorsId {
suspectedDisease = "jLvbkuvPdZ6",
hazardType = "Dzrw3Tf0ukB",
Expand Down Expand Up @@ -160,7 +166,29 @@ export const NB_OF_DEATHS = [
},
];

export const NB_OF_ACTIVE_VERIFIED = [
type EventTrackerCountIndicatorBase = {
id: Id;
type: "disease" | "hazard";
name: DiseaseNames | HazardNames;
incidentStatus: "Watch" | "Alert" | "Respond";
count?: number;
};

export type EventTrackerCountDiseaseIndicator = EventTrackerCountIndicatorBase & {
type: "disease";
name: DiseaseNames;
};

export type EventTrackerCountHazardIndicator = EventTrackerCountIndicatorBase & {
type: "hazard";
name: HazardNames;
};

export type EventTrackerCountIndicator =
| EventTrackerCountDiseaseIndicator
| EventTrackerCountHazardIndicator;

export const EvenTrackerCountsIndicatorMap: EventTrackerCountIndicator[] = [
{ id: "SGGbbu0AKUv", type: "disease", name: "Acute respiratory", incidentStatus: "Watch" },
{ id: "QnhsQnEsp1p", type: "disease", name: "Acute respiratory", incidentStatus: "Alert" },
{ id: "Rt5KNVqBEO7", type: "disease", name: "Acute respiratory", incidentStatus: "Respond" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { RiskAssessment } from "../risk-assessment/RiskAssessment";
import { Maybe } from "../../../utils/ts-utils";
import { ValidationError } from "../ValidationError";


export const hazardTypes = [
"Biological:Human",
"Biological:Animal",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { Id } from "../Ref";

export type DiseaseNames =
| "AFP"
| "Acute VHF"
| "Acute respiratory"
| "Anthrax"
| "Bacterial meningitis"
| "COVID19"
| "Cholera"
| "Diarrhoea with blood"
| "Measles"
| "Monkeypox"
| "Neonatal tetanus"
| "Plague"
| "SARIs"
| "Typhoid fever"
| "Zika fever";

export type HazardNames =
| "Animal type"
| "Human type"
| "Human and Animal type"
| "Environmental type";

export type PerformanceOverviewMetrics = {
id: Id;
event: string;
province: string;
duration: string;
manager: string;
cases: string;
deaths: string;
era1: string;
era2: string;
era3: string;
era4: string;
era5: string;
era6: string;
era7: string;
detect7d: string;
notify1d: string;
respond7d: string;
creationDate: string;
suspectedDisease: DiseaseNames;
hazardType: HazardNames;
};

type BaseCounts = {
name: DiseaseNames | HazardNames;
total: number;
type: "disease" | "hazard";
};

type DiseaseCounts = BaseCounts & {
name: DiseaseNames;
type: "disease";
};

type HazardCounts = BaseCounts & {
name: HazardNames;
type: "hazard";
};

export type EventTrackerCounts = DiseaseCounts | HazardCounts;
8 changes: 4 additions & 4 deletions src/domain/repositories/PerformanceOverviewRepository.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { FutureData } from "../../data/api-futures";
import { DiseaseOutbreakEventBaseAttrs } from "../entities/disease-outbreak-event/DiseaseOutbreakEvent";
import {
DiseaseTotalAttrs,
EventTrackerCounts,
PerformanceOverviewMetrics,
} from "../../data/repositories/PerformanceOverviewD2Repository";
import { DiseaseOutbreakEventBaseAttrs } from "../entities/disease-outbreak-event/DiseaseOutbreakEvent";
} from "../entities/disease-outbreak-event/PerformanceOverviewMetrics";

export interface PerformanceOverviewRepository {
getPerformanceOverviewMetrics(
diseaseOutbreakEvents: DiseaseOutbreakEventBaseAttrs[]
): FutureData<PerformanceOverviewMetrics[]>;
getDiseasesTotal(filters?: Record<string, string[]>): FutureData<DiseaseTotalAttrs[]>;
getDiseasesTotal(filters?: Record<string, string[]>): FutureData<EventTrackerCounts[]>;
}
Loading

0 comments on commit 686088d

Please sign in to comment.