This commit is contained in:
Oscar Krause 2023-01-17 16:37:45 +01:00
parent b09bb091a5
commit b2e6fab294

View File

@ -1,4 +1,5 @@
import datetime from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
from sqlalchemy import Column, VARCHAR, CHAR, ForeignKey, DATETIME, update, and_, inspect from sqlalchemy import Column, VARCHAR, CHAR, ForeignKey, DATETIME, update, and_, inspect
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
@ -81,7 +82,10 @@ class Lease(Base):
def __repr__(self): def __repr__(self):
return f'Lease(origin_ref={self.origin_ref}, lease_ref={self.lease_ref}, expires={self.lease_expires})' return f'Lease(origin_ref={self.origin_ref}, lease_ref={self.lease_ref}, expires={self.lease_expires})'
def serialize(self, renewal_period: float, renewal_delta: datetime.timedelta) -> dict: def serialize(self, renewal_period: float, renewal_delta: timedelta) -> dict:
lease_renewal = int(Lease.calculate_renewal(renewal_period, renewal_delta).total_seconds())
lease_renewal = self.lease_created + relativedelta(seconds=lease_renewal)
return { return {
'lease_ref': self.lease_ref, 'lease_ref': self.lease_ref,
'origin_ref': self.origin_ref, 'origin_ref': self.origin_ref,
@ -89,7 +93,7 @@ class Lease(Base):
'lease_created': self.lease_created.isoformat(), 'lease_created': self.lease_created.isoformat(),
'lease_expires': self.lease_expires.isoformat(), 'lease_expires': self.lease_expires.isoformat(),
'lease_updated': self.lease_updated.isoformat(), 'lease_updated': self.lease_updated.isoformat(),
'lease_renewal': Lease.calculate_renewal(renewal_period, renewal_delta), 'lease_renewal': lease_renewal.isoformat(),
} }
@staticmethod @staticmethod
@ -134,7 +138,7 @@ class Lease(Base):
return entity return entity
@staticmethod @staticmethod
def renew(engine: Engine, lease: "Lease", lease_expires: datetime.datetime, lease_updated: datetime.datetime): def renew(engine: Engine, lease: "Lease", lease_expires: datetime, lease_updated: datetime):
session = sessionmaker(bind=engine)() session = sessionmaker(bind=engine)()
x = dict(lease_expires=lease_expires, lease_updated=lease_updated) x = dict(lease_expires=lease_expires, lease_updated=lease_updated)
session.execute(update(Lease).where(and_(Lease.origin_ref == lease.origin_ref, Lease.lease_ref == lease.lease_ref)).values(**x)) session.execute(update(Lease).where(and_(Lease.origin_ref == lease.origin_ref, Lease.lease_ref == lease.lease_ref)).values(**x))
@ -158,16 +162,17 @@ class Lease(Base):
return deletions return deletions
@staticmethod @staticmethod
def calculate_renewal(renewal_period: float, delta: datetime.timedelta): def calculate_renewal(renewal_period: float, delta: timedelta) -> timedelta:
""" """
import datetime
LEASE_RENEWAL_PERIOD=0.2 # 20% LEASE_RENEWAL_PERIOD=0.2 # 20%
delta = datetime.timedelta(days=1) delta = datetime.timedelta(days=1)
renew = delta.total_seconds() * LEASE_RENEWAL_PERIOD renew = delta.total_seconds() * LEASE_RENEWAL_PERIOD
renew = timedelta(seconds=renew) renew = datetime.timedelta(seconds=renew)
expires = delta - renew # 19.2 expires = delta - renew # 19.2
""" """
renew = delta.total_seconds() * renewal_period renew = delta.total_seconds() * renewal_period
renew = datetime.timedelta(seconds=renew) renew = timedelta(seconds=renew)
return delta - renew return delta - renew