Spaces:
Running
Running
File size: 5,369 Bytes
f287aeb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | import { apiGet } from '../api';
import {
getTankHealthHistory,
HealthCheckResponseDTO,
isActiveHealthConcern,
sortHealthChecks,
} from './healthCheckApi';
import { resolveHealthReportTemplate } from './healthKnowledgeBase';
const extractArray = (payload: unknown): any[] => {
const root = payload && typeof payload === 'object' ? (payload as Record<string, any>) : {};
const unwrapped = root.data ?? root;
if (Array.isArray(unwrapped)) return unwrapped;
if (Array.isArray((unwrapped as Record<string, any>)?.items)) {
return (unwrapped as Record<string, any>).items;
}
if (Array.isArray((unwrapped as Record<string, any>)?.records)) {
return (unwrapped as Record<string, any>).records;
}
if (Array.isArray((unwrapped as Record<string, any>)?.history)) {
return (unwrapped as Record<string, any>).history;
}
return [];
};
export interface BatchHealthOverview {
batch: any;
history: HealthCheckResponseDTO[];
latestRecord: HealthCheckResponseDTO | null;
latestActiveRecord: HealthCheckResponseDTO | null;
requiresAttention: boolean;
isRecovered: boolean;
recoveredAt?: string;
}
export interface TankHealthOverview {
tank: any;
batches: any[];
healthChecks: HealthCheckResponseDTO[];
batchOverviews: BatchHealthOverview[];
latestRecord: HealthCheckResponseDTO | null;
latestActiveRecord: HealthCheckResponseDTO | null;
latestHealthyRecord: HealthCheckResponseDTO | null;
requiresAttention: boolean;
isRecovered: boolean;
recoveredAt?: string;
currentDiseaseLabel: string;
currentTemplateKey: string;
}
const buildBatchOverview = (batch: any, history: HealthCheckResponseDTO[]): BatchHealthOverview => {
const sorted = sortHealthChecks(history);
const latestRecord = sorted[0] || null;
const latestActiveRecord = sorted.find(isActiveHealthConcern) || null;
const latestHealthyRecord = sorted.find((record) => !isActiveHealthConcern(record)) || null;
const requiresAttention =
!!latestActiveRecord &&
(!latestHealthyRecord ||
new Date(latestHealthyRecord.checkedAt).getTime() <
new Date(latestActiveRecord.checkedAt).getTime());
const isRecovered =
!!latestActiveRecord &&
!!latestHealthyRecord &&
new Date(latestHealthyRecord.checkedAt).getTime() >
new Date(latestActiveRecord.checkedAt).getTime();
return {
batch,
history: sorted,
latestRecord,
latestActiveRecord,
requiresAttention,
isRecovered,
recoveredAt: isRecovered ? latestHealthyRecord?.checkedAt : undefined,
};
};
export const buildTankHealthOverview = (
tank: any,
batches: any[],
records: HealthCheckResponseDTO[],
): TankHealthOverview => {
const sorted = sortHealthChecks(records);
const latestRecord = sorted[0] || null;
const latestActiveRecord = sorted.find(isActiveHealthConcern) || null;
const latestHealthyRecord = sorted.find((record) => !isActiveHealthConcern(record)) || null;
const requiresAttention =
!!latestActiveRecord &&
(!latestHealthyRecord ||
new Date(latestHealthyRecord.checkedAt).getTime() <
new Date(latestActiveRecord.checkedAt).getTime());
const isRecovered =
!!latestActiveRecord &&
!!latestHealthyRecord &&
new Date(latestHealthyRecord.checkedAt).getTime() >
new Date(latestActiveRecord.checkedAt).getTime();
const batchOverviews = batches.map((batch) =>
buildBatchOverview(
batch,
sorted.filter((record) => record.batchId === batch.id),
),
);
const diseaseSource = latestActiveRecord || latestRecord;
const template = resolveHealthReportTemplate(diseaseSource?.bacterialType);
return {
tank,
batches,
healthChecks: sorted,
batchOverviews,
latestRecord,
latestActiveRecord,
latestHealthyRecord,
requiresAttention,
isRecovered,
recoveredAt: isRecovered ? latestHealthyRecord?.checkedAt : undefined,
currentDiseaseLabel: diseaseSource?.bacterialType || template.title,
currentTemplateKey: template.key,
};
};
export const fetchTankHealthOverview = async (
tank: any,
): Promise<TankHealthOverview> => {
const batchesPayload = await apiGet<unknown>(`/tanks/${tank.id}/batches`);
const batches = extractArray(batchesPayload);
const history = await getTankHealthHistory(batches.map((batch) => batch.id));
return buildTankHealthOverview(tank, batches, history);
};
export const fetchAllTankHealthOverviews = async (
incomingTanks?: any[],
): Promise<{
tanks: any[];
overviews: TankHealthOverview[];
batchesByTank: Record<string, any[]>;
historyByTank: Record<string, HealthCheckResponseDTO[]>;
}> => {
const tanks = incomingTanks && incomingTanks.length > 0
? incomingTanks
: extractArray(await apiGet<unknown>('/tanks'));
const overviewResults = await Promise.all(
tanks.map(async (tank) => {
try {
return await fetchTankHealthOverview(tank);
} catch {
return buildTankHealthOverview(tank, [], []);
}
}),
);
const batchesByTank: Record<string, any[]> = {};
const historyByTank: Record<string, HealthCheckResponseDTO[]> = {};
overviewResults.forEach((overview) => {
batchesByTank[overview.tank.id] = overview.batches;
historyByTank[overview.tank.id] = overview.healthChecks;
});
return {
tanks,
overviews: overviewResults,
batchesByTank,
historyByTank,
};
};
|