fix report doc initial count logic

This commit is contained in:
Alberto-Audrix committed 2026-09-02 14:59:48 +07:00
1 parent 53316cfd49
commit c45b6656ae
3 files changed
+107 -17

No files matched your search

+27 -9
View File
@@ -30,6 +30,11 @@ function accuracy(iot: number | null, manual: number | null): number | null {
return Math.round((1 - Math.abs(iot - manual) / Math.abs(manual)) * 1000) / 10;
}
function countingAccuracy(iot: number | null, stockAkhir: number | null): number | null {
if (iot == null || stockAkhir == null || stockAkhir <= 0) return null;
return Math.round((Math.min(iot, stockAkhir) / Math.max(iot, stockAkhir)) * 1000) / 10;
}
/** Feed accuracy: both manual and IoT reading zero means a perfect match (100%);
* if only one side has a reading, the match is 0%. */
function feedAccuracy(iot: number | null, manual: number | null): number | null {
@@ -73,6 +78,10 @@ export function buildInMemoryReport(input: BuildInMemoryReportInput): DashboardR
for (const row of input.kpis) {
kpiByAge.set(row.age, row);
}
const manualByDay = new Map<number, ManualInput>();
for (const row of input.manuals) {
manualByDay.set(cycleDayForDate(cycle.start_date, row.date), row);
}
const manualWeightByDay = new Map<number, number>();
const manualFeedInByDay = new Map<number, number>();
const manualFeedUseByDay = new Map<number, number>();
@@ -84,23 +93,32 @@ export function buildInMemoryReport(input: BuildInMemoryReportInput): DashboardR
manualFeedUseByDay.set(day, row.feed_use_manual);
manualFeedOutByDay.set(day, row.feed_out_manual);
}
const latestManual = input.manuals[0] ?? null;
let previousStockAkhir: number | null = null;
const counting = days.map((day) => {
const iot = countingByDay.get(day) ?? null;
const kpi = kpiByAge.get(day) ?? null;
const manual = manualByDay.get(day) ?? null;
const iotCount = iot?.total_count ?? null;
const after = kpi?.chicken_life ?? iotCount;
const mortality =
manual?.mortality_manual ?? kpi?.mortality ?? iot?.mortality_count ?? null;
const harvest = manual?.harvest_manual ?? kpi?.harvest ?? null;
const harvestWeight = manual?.harvest_weight_manual ?? kpi?.harvest_weight ?? null;
const mati = mortality ?? 0;
const panen = harvest ?? 0;
const countBefore = day === 0 ? cycle.doc_in_count || 0 : (previousStockAkhir ?? 0);
const countAfter = kpi?.chicken_life ?? Math.max(0, countBefore - mati - panen);
previousStockAkhir = countAfter;
return {
date: addDate(cycle.start_date, day),
day,
countBefore: after == null ? null : after + (kpi?.mortality ?? iot?.mortality_count ?? 0),
mortality: kpi?.mortality ?? iot?.mortality_count ?? latestManual?.mortality_manual ?? null,
harvest: kpi?.harvest ?? null,
harvestWeight: kpi?.harvest_weight ?? null,
countAfter: after,
countBefore,
mortality,
harvest,
harvestWeight,
countAfter,
iotCount,
accuracy: accuracy(iotCount, after),
accuracy: countingAccuracy(iotCount, countAfter),
};
});