78 lines
2.0 KiB
Python
78 lines
2.0 KiB
Python
from models import History, Session
|
|
import json
|
|
import uuid
|
|
from datetime import datetime
|
|
class HistoryManager:
|
|
def __init__(self, parent=None):
|
|
super().__init__()
|
|
|
|
def create(self, info):
|
|
data = {
|
|
"id": str(uuid.uuid4()),
|
|
"info": json.dumps(info),
|
|
"datetime": datetime.now()
|
|
}
|
|
print("create history:", data)
|
|
ok, res = Session.addByClass(History, data)
|
|
if ok:
|
|
return str(res["id"])
|
|
else:
|
|
print("create history failed:", res)
|
|
return ""
|
|
|
|
def update(self, id, info):
|
|
|
|
data = {"info": info}
|
|
ok,res = Session.updateById(History, id, data)
|
|
return ok
|
|
|
|
def delete(self, id):
|
|
if isinstance(id, str):
|
|
ok,res = Session.deleteById(History, id)
|
|
return ok
|
|
else:
|
|
ok,res = Session.deleteByIds(History, id)
|
|
return ok
|
|
|
|
def getInfoByDatetime(self, date_time_str):
|
|
ok,res = Session.queryByDatetime(History, date_time_str)
|
|
if ok:
|
|
return res
|
|
else:
|
|
return []
|
|
|
|
def getTrend(self, duration):
|
|
ok,res = Session.queryByTimestampRange(History, duration)
|
|
if ok:
|
|
return res
|
|
else:
|
|
return []
|
|
|
|
def getDateTimes(self,date_str, hour_str):
|
|
ok,res = Session.queryByDateHour(History, date_str, hour_str)
|
|
if ok:
|
|
return res
|
|
else:
|
|
return []
|
|
|
|
def getInfos(self, ids):
|
|
ok,res = Session.queryByIds(History, ids)
|
|
if ok:
|
|
return res
|
|
else:
|
|
return []
|
|
|
|
def getInfo(self, id = "all"):
|
|
if id == "all":
|
|
ok,res = Session.queryByAll(History)
|
|
if ok:
|
|
return res
|
|
else:
|
|
return []
|
|
else:
|
|
ok,res = Session.queryById(History, id)
|
|
if ok:
|
|
return res
|
|
else:
|
|
return None
|
|
historyManager = HistoryManager() |