Skip to content

Commit 9ae6a07

Browse files
authored
Merge pull request #2946 from Agenta-AI/fix/year-month-miscalculation
[Fix] `meters` year-month miscalculation
2 parents 24a7b2d + 79cf869 commit 9ae6a07

File tree

1 file changed

+13
-19
lines changed
  • api/ee/src/dbs/postgres/meters

1 file changed

+13
-19
lines changed

api/ee/src/dbs/postgres/meters/dao.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,13 @@ async def check(
132132
if quota.monthly:
133133
now = datetime.now(timezone.utc)
134134

135-
if not anchor:
136-
meter.year = now.year
137-
meter.month = now.month
138-
139-
if anchor:
140-
if now.day < anchor:
141-
meter.year = now.year
142-
meter.month = now.month
143-
else:
144-
meter.year = now.year + now.month // 12
145-
meter.month = (now.month + 1) % 12
135+
if not anchor or now.day < anchor:
136+
year, month = now.year, now.month
137+
else:
138+
year = now.year + now.month // 12
139+
month = ((now.month + 1) % 12) or 12
140+
141+
meter.year, meter.month = year, month
146142

147143
async with engine.core_session() as session:
148144
stmt = select(MeterDBE).filter_by(
@@ -185,15 +181,13 @@ async def adjust(
185181
if quota.monthly:
186182
now = datetime.now(timezone.utc)
187183

188-
if not anchor:
189-
meter.year = now.year
190-
meter.month = now.month
191-
elif now.day < anchor:
192-
meter.year = now.year
193-
meter.month = now.month
184+
if not anchor or now.day < anchor:
185+
year, month = now.year, now.month
194186
else:
195-
meter.year = now.year + now.month // 12
196-
meter.month = (now.month + 1) % 12
187+
year = now.year + now.month // 12
188+
month = ((now.month + 1) % 12) or 12
189+
190+
meter.year, meter.month = year, month
197191

198192
# 2. Calculate proposed value (starting from 0)
199193
desired_value = meter.value if meter.value is not None else (meter.delta or 0)

0 commit comments

Comments
 (0)