feat:站点使用统计
This commit is contained in:
@ -13,20 +13,18 @@ class SiteStatistic(Base):
|
||||
id = Column(Integer, Sequence('id'), primary_key=True, index=True)
|
||||
# 域名Key
|
||||
domain = Column(String, index=True)
|
||||
# 站点名
|
||||
name = Column(String, nullable=False)
|
||||
# 成功次数
|
||||
success = Column(Integer)
|
||||
# 失败次数
|
||||
fail = Column(Integer)
|
||||
# 平均耗时 秒
|
||||
seconds = Column(Integer)
|
||||
# 耗时记录 Json
|
||||
note = Column(String)
|
||||
# 最后一次访问状态 0-成功 1-失败
|
||||
lst_state = Column(Integer)
|
||||
# 最后访问时间
|
||||
lst_mod_date = Column(String, default=datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
|
||||
# 耗时记录 Json
|
||||
note = Column(String)
|
||||
|
||||
@staticmethod
|
||||
@db_query
|
||||
|
68
app/db/sytestatistic_oper.py
Normal file
68
app/db/sytestatistic_oper.py
Normal file
@ -0,0 +1,68 @@
|
||||
import json
|
||||
from datetime import datetime
|
||||
|
||||
from app.db import DbOper
|
||||
from app.db.models.sitestatistic import SiteStatistic
|
||||
|
||||
|
||||
class SiteStatisticOper(DbOper):
|
||||
"""
|
||||
站点统计管理
|
||||
"""
|
||||
|
||||
def success(self, domain: str, seconds: int = None):
|
||||
"""
|
||||
站点访问成功
|
||||
"""
|
||||
lst_date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
sta = SiteStatistic.get_by_domain(self._db, domain)
|
||||
if sta:
|
||||
avg_seconds, note = None, {}
|
||||
if seconds:
|
||||
note: dict = json.loads(sta.note or "{}")
|
||||
note[lst_date] = seconds
|
||||
avg_times = len(note.keys())
|
||||
avg_seconds = sum([v for v in note.values()]) // avg_times
|
||||
sta.update(self._db, {
|
||||
"success": sta.success + 1,
|
||||
"seconds": avg_seconds or sta.seconds,
|
||||
"lst_state": 0,
|
||||
"lst_mod_date": lst_date,
|
||||
"note": json.dumps(note) if note else sta.note
|
||||
})
|
||||
else:
|
||||
note = {}
|
||||
if seconds:
|
||||
note = {
|
||||
lst_date: seconds
|
||||
}
|
||||
SiteStatistic(
|
||||
domain=domain,
|
||||
success=1,
|
||||
fail=0,
|
||||
seconds=seconds,
|
||||
lst_state=0,
|
||||
lst_mod_date=lst_date,
|
||||
note=json.dumps(note)
|
||||
).create(self._db)
|
||||
|
||||
def fail(self, domain: str):
|
||||
"""
|
||||
站点访问失败
|
||||
"""
|
||||
lst_date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
sta = SiteStatistic.get_by_domain(self._db, domain)
|
||||
if sta:
|
||||
sta.update(self._db, {
|
||||
"fail": sta.fail + 1,
|
||||
"lst_state": 1,
|
||||
"lst_mod_date": lst_date
|
||||
})
|
||||
else:
|
||||
SiteStatistic(
|
||||
domain=domain,
|
||||
success=0,
|
||||
fail=1,
|
||||
lst_state=1,
|
||||
lst_mod_date=lst_date
|
||||
).create(self._db)
|
Reference in New Issue
Block a user