Skip to content

Commit

Permalink
fix: get cases and deaths from indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
9sneha-n committed Oct 30, 2024
1 parent 725cbe7 commit 75cc212
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 1,433 deletions.
189 changes: 131 additions & 58 deletions src/data/repositories/PerformanceOverviewD2Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
DiseaseNames,
PerformanceMetrics717,
} from "../../domain/entities/disease-outbreak-event/PerformanceOverviewMetrics";
import { AlertSynchronizationData } from "../../domain/entities/alert/AlertData";
import { OrgUnit } from "../../domain/entities/OrgUnit";
import { Id } from "../../domain/entities/Ref";
import { OverviewCard } from "../../domain/entities/PerformanceOverview";
Expand All @@ -49,6 +48,11 @@ type EventTrackerOverview = {
probableCasesId: Id;
};

type IdValue = {
id: Id;
value: string;
};

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

Expand Down Expand Up @@ -181,14 +185,25 @@ export class PerformanceOverviewD2Repository implements PerformanceOverviewRepos
if (!currentEventTrackerOverviewId)
return Future.error(
new Error(
`Event Tracke Overview Ids for type ${type} not found in datastore`
`Event Tracker Overview Ids for type ${type} not found in datastore`
)
);
return Future.success(currentEventTrackerOverviewId);
});
});
}

private getAllEventTrackerOverviewIdsFromDatastore(): FutureData<EventTrackerOverview[]> {
return this.datastore
.getObject<EventTrackerOverview[]>(EVENT_TRACKER_OVERVIEW_DATASTORE_KEY)
.flatMap(nullableEventTrackerOverviewIds => {
return assertOrError(
nullableEventTrackerOverviewIds,
EVENT_TRACKER_OVERVIEW_DATASTORE_KEY
);
});
}

getEventTrackerOverviewMetrics(type: string): FutureData<OverviewCard[]> {
return this.getEventTrackerOverviewIdsFromDatastore(type).flatMap(eventTrackerOverview => {
const { suspectedCasesId, probableCasesId, confirmedCasesId, deathsId } =
Expand Down Expand Up @@ -316,46 +331,123 @@ export class PerformanceOverviewD2Repository implements PerformanceOverviewRepos
endDate: DEFAULT_END_DATE,
})
).flatMap(indicatorsProgramFuture => {
const mappedIndicators =
indicatorsProgramFuture?.rows.map((row: string[]) =>
this.mapRowToBaseIndicator(
row,
indicatorsProgramFuture.headers,
indicatorsProgramFuture.metaData
return this.getAllEventTrackerOverviewIdsFromDatastore().flatMap(
eventTrackerOverviews => {
const mappedIndicators =
indicatorsProgramFuture?.rows.map((row: string[]) =>
this.mapRowToBaseIndicator(
row,
indicatorsProgramFuture.headers,
indicatorsProgramFuture.metaData
)
) || [];

const keys = _(
diseaseOutbreakEvents.map(
diseaseOutbreak =>
diseaseOutbreak.suspectedDiseaseCode || diseaseOutbreak.hazardType
)
)
) || [];
.compact()
.uniq()
.value();

const performanceOverviewMetrics = diseaseOutbreakEvents.map(event => {
const baseIndicator = mappedIndicators.find(indicator => indicator.id === event.id);
const key = baseIndicator?.suspectedDisease || baseIndicator?.hazardType;

return this.getCasesAndDeathsFromDatastore(key).map(casesAndDeaths => {
const duration = `${moment()
.diff(moment(event.emerged.date), "days")
.toString()}d`;
if (!baseIndicator) {
return {
id: event.id,
event: event.name,
manager: event.incidentManagerName,
duration: duration,
nationalIncidentStatus: event.incidentStatus,
cases: casesAndDeaths.cases.toString(),
deaths: casesAndDeaths.deaths.toString(),
} as PerformanceOverviewMetrics;
}
return {
...baseIndicator,
nationalIncidentStatus: event.incidentStatus,
manager: event.incidentManagerName,
duration: duration,
cases: casesAndDeaths.cases.toString(),
deaths: casesAndDeaths.deaths.toString(),
} as PerformanceOverviewMetrics;
});
});
const eventTrackerOverviewsForKeys = eventTrackerOverviews.filter(overview =>
keys.includes(overview.key)
);

const casesIndicatorIds = eventTrackerOverviewsForKeys.map(
overview => overview.suspectedCasesId
);

return Future.sequential(performanceOverviewMetrics);
const deathsIndicatorIds = eventTrackerOverviewsForKeys.map(
overview => overview.deathsId
);

return Future.joinObj({
allCases: this.getAnalyticsByIndicators(casesIndicatorIds),
allDeaths: this.getAnalyticsByIndicators(deathsIndicatorIds),
}).flatMap(({ allCases, allDeaths }) => {
const performanceOverviewMetrics: FutureData<PerformanceOverviewMetrics>[] =
diseaseOutbreakEvents.map(event => {
const baseIndicator = mappedIndicators.find(
indicator => indicator.id === event.id
);

const key = event.hazardType || event.suspectedDiseaseCode;
if (!key)
return Future.error(
new Error(
`No hazard type or suspected disease found for event : ${event.id}`
)
);
const currentEventTrackerOverview =
eventTrackerOverviewsForKeys.find(
overview => overview.key === key
);

const currentCases = allCases.find(
caseIdValue =>
caseIdValue.id ===
currentEventTrackerOverview?.suspectedCasesId
);

const currentDeaths = allDeaths.find(
death => death.id === currentEventTrackerOverview?.deathsId
);

const duration = `${moment()
.diff(moment(event.emerged.date), "days")
.toString()}d`;

if (!baseIndicator) {
const metrics = {
id: event.id,
event: event.name,
manager: event.incidentManagerName,
duration: duration,
nationalIncidentStatus: event.incidentStatus,
cases: currentCases?.value || "",
deaths: currentDeaths?.value || "",
} as PerformanceOverviewMetrics;
return Future.success(metrics);
} else {
const metrics = {
...baseIndicator,
nationalIncidentStatus: event.incidentStatus,
manager: event.incidentManagerName,
duration: duration,
cases: currentCases?.value || "",
deaths: currentDeaths?.value || "",
} as PerformanceOverviewMetrics;
return Future.success(metrics);
}
});

return Future.sequential(performanceOverviewMetrics);
});
}
);
});
}

private getAnalyticsByIndicators(ids: Id[]): FutureData<IdValue[]> {
return apiToFuture(
this.api.analytics.get({
dimension: [`dx:${ids.join(";")}`],
startDate: DEFAULT_START_DATE,
endDate: DEFAULT_END_DATE,
includeMetadataDetails: true,
})
).flatMap(response => {
const analytics = _(
response.rows.map(row => {
if (row[0] && row[1]) return { id: row[0], value: parseInt(row[1]).toString() };
})
)
.compact()
.value();
return Future.success(analytics);
});
}

Expand Down Expand Up @@ -430,25 +522,6 @@ export class PerformanceOverviewD2Repository implements PerformanceOverviewRepos
});
}

private getCasesAndDeathsFromDatastore(
key: string | undefined
): FutureData<{ cases: number; deaths: number }> {
if (!key) return Future.success({ cases: 0, deaths: 0 });
return this.datastore.getObject<AlertSynchronizationData>(key).flatMap(data => {
if (!data) return Future.success({ cases: 0, deaths: 0 });
const casesDeaths = data.alerts.reduce(
(acc, alert) => {
acc.cases += parseInt(alert.suspectedCases) || 0;
acc.deaths += parseInt(alert.deaths) || 0;
return acc;
},
{ cases: 0, deaths: 0 }
);

return Future.success(casesDeaths);
});
}

private mapRowToBaseIndicator(
row: string[],
headers: { name: string; column: string }[],
Expand Down
Loading

0 comments on commit 75cc212

Please sign in to comment.