fix age to start at 0 on start date of cycle instead of 1

This commit is contained in:
Alberto-Audrix committed 2026-09-02 09:58:59 +07:00
1 parent 3411ed4391
commit 54eb277c16
7 files changed
+33 -10

No files matched your search

+4 -2
View File
@@ -116,13 +116,15 @@ class Cycle(models.Model):
@property
def current_day(self) -> int:
"""0-based cycle age: start_date is day 0."""
from apps.operations.services.visibility import visible_through_date
effective = visible_through_date()
if effective < self.start_date:
return 0
elapsed = (effective - self.start_date).days + 1
return min(max(elapsed, 1), self.total_days)
elapsed = (effective - self.start_date).days
max_day = max(self.total_days - 1, 0)
return min(max(elapsed, 0), max_day)
@property
def visible_through_date(self) -> date:
+24
View File
@@ -128,6 +128,30 @@ class FarmAPITests(TestCase):
self.assertEqual(Flock.objects.filter(kandang=self.kandang).count(), 1)
self.assertEqual(flock.kandang_id, self.kandang.pk)
def test_current_day_is_zero_based_from_start_date(self):
start = date(2026, 9, 2)
cycle = Cycle.objects.create(
kandang=self.kandang,
total_days=35,
start_date=start,
end_date=start + timedelta(days=34),
doc_in_weight=40,
doc_in_count=9000,
status=Cycle.STATUS_ACTIVE,
)
with patch(
"apps.operations.services.visibility.visible_through_date",
return_value=start,
):
self.assertEqual(cycle.current_day, 0)
with patch(
"apps.operations.services.visibility.visible_through_date",
return_value=start + timedelta(days=1),
):
self.assertEqual(cycle.current_day, 1)
def test_list_flocks_by_kandang(self):
self.client.force_authenticate(self.user)
Flock.objects.create(flock_name="Lantai 1", kandang=self.kandang)
+1 -1
View File
@@ -74,7 +74,7 @@ const SingleCycleView: React.FC<{ cycle: Cycle }> = ({ cycle }) => {
);
})}
</div>
{cycleStatus === 'active' && currentDay > 0 && (
{cycleStatus === 'active' && (
<div
className="absolute top-0 transition-all duration-500 ease-out"
style={{ left: `calc(${progress}% - 2.5rem)` }}
+1 -1
View File
@@ -95,7 +95,7 @@ const Dashboard: React.FC<Props> = ({ setView }) => {
const eefTrend = chronological(kpis).map((row) => row.eef);
const popTrend = chronological(countings).map((row) => row.total_count);
const weightTrend = chronological(weights).map((row) => row.average_weight);
const age = latest?.age ?? selectedCycle?.current_day ?? null;
const age = selectedCycle?.current_day ?? latest?.age ?? null;
return [
{
key: 'umur',
@@ -75,8 +75,7 @@ const ReportPreviewOverlay: React.FC<Props> = ({ open, reportType, onClose }) =>
const [error, setError] = useState<string | null>(null);
const printRef = useRef<HTMLDivElement>(null);
// cycle.current_day is 1-based (day 1 = start_date); report days are 0-based.
const latestDay = selectedCycle ? Math.max(0, selectedCycle.current_day - 1) : 0;
const latestDay = selectedCycle ? Math.max(0, selectedCycle.current_day) : 0;
const endCycleDay = selectedCycle
? Math.min(
selectedCycle.total_days,
+1 -1
View File
@@ -7,7 +7,7 @@ const cycle: Cycle = {
kandang: 9,
kandang_name: 'Kandang 1',
total_days: 35,
current_day: 2,
current_day: 1,
visible_through_date: '2026-05-02',
start_date: '2026-05-01',
end_date: '2026-06-04',
+1 -3
View File
@@ -47,12 +47,10 @@ export function buildInMemoryReport(input: BuildInMemoryReportInput): DashboardR
// is total_days - 1 for a contiguous cycle.
const lastDay = cycleDayForDate(cycle.start_date, cycle.end_date);
const endCycleUpTo = Math.min(cycle.total_days, lastDay);
// cycle.current_day is 1-based (day 1 = start_date); reports use 0-based days,
// so the latest report day is current_day - 1.
const defaultUpTo =
input.reportType === 'end_cycle'
? endCycleUpTo
: Math.min(Math.max(cycle.current_day - 1, 0), lastDay);
: Math.min(Math.max(cycle.current_day, 0), lastDay);
const upToDay =
input.upToDay != null && Number.isFinite(input.upToDay)
? Math.min(Math.max(Math.floor(input.upToDay), 0), lastDay)