feat: don't allow re-creation of existing logs

This prevents lock spam - where 100ds of identical rows can be inserted
into the database
pull/13032/head
Matthias 2 weeks ago
parent 04990a888b
commit 9582bd3cd7

@ -29,6 +29,11 @@ class PairLock(ModelBase):
active: Mapped[bool] = mapped_column(nullable=False, default=True, index=True)
@property
def lock_end_time_utc(self) -> datetime:
"""Lock end time with UTC timezoneinfo"""
return self.lock_end_time.replace(tzinfo=UTC)
def __repr__(self) -> str:
lock_time = self.lock_time.strftime(DATETIME_PRINT_FORMAT)
lock_end_time = self.lock_end_time.strftime(DATETIME_PRINT_FORMAT)

@ -42,6 +42,7 @@ class PairLocks:
) -> PairLock:
"""
Create PairLock from now to "until".
Does not create a new lock if there is already a lock with the same Reason, side and end time.
Uses database by default, unless PairLocks.use_db is set to False,
in which case a list is maintained.
:param pair: pair to lock. use '*' to lock all pairs
@ -50,10 +51,19 @@ class PairLocks:
:param now: Current timestamp. Used to determine lock start time.
:param side: Side to lock pair, can be 'long', 'short' or '*'
"""
lock_end_time = timeframe_to_next_date(PairLocks.timeframe, until)
existing_locks = PairLocks.get_pair_locks(pair, now, side=side)
for lock in existing_locks:
if (
lock.reason == reason
and lock.lock_end_time_utc == lock_end_time
and lock.side == side
):
return lock
lock = PairLock(
pair=pair,
lock_time=now or datetime.now(UTC),
lock_end_time=timeframe_to_next_date(PairLocks.timeframe, until),
lock_end_time=lock_end_time,
reason=reason,
side=side,
active=True,

Loading…
Cancel
Save