37 lines
923 B
Python
37 lines
923 B
Python
import datetime
|
|
|
|
import transaction
|
|
import data_checker
|
|
|
|
|
|
class DataMgr:
|
|
|
|
TR_MAX_CNT = 500
|
|
TR_MAINTAIN_CNT = 100
|
|
|
|
def __init__(self):
|
|
self.transactions = []
|
|
self.data_checker = data_checker.DataChecker()
|
|
|
|
def contains(self, tr: transaction.Transaction):
|
|
next_item = next((i for i, x in enumerate(self.transactions) if x.key == tr.key), None)
|
|
return (next_item is not None)
|
|
|
|
def insert(self, item):
|
|
tr = transaction.Transaction(item)
|
|
if self.contains(tr):
|
|
return False
|
|
|
|
self.data_checker.add(tr)
|
|
self.transactions.append(tr)
|
|
|
|
if len(self.transactions) >= DataMgr.TR_MAX_CNT:
|
|
today = datetime.datetime.now().strftime('%Y-%m-%d')
|
|
with open('log/log-{}.txt'.format(today), 'a') as log_file:
|
|
for key in self.transactions[:-DataMgr.TR_MAINTAIN_CNT]:
|
|
log_file.write('{}\n'.format(self.transactions[key]))
|
|
|
|
self.transactions = self.transactions[-DataMgr.TR_MAINTAIN_CNT:]
|
|
|
|
return True
|