Skip to content

Commit

Permalink
网站监控增加错误重试次数;网站监控支持导入导出;优化耗时显示
Browse files Browse the repository at this point in the history
  • Loading branch information
mouday committed Feb 24, 2024
1 parent a2e8e8c commit 6322036
Show file tree
Hide file tree
Showing 12 changed files with 289 additions and 16 deletions.
82 changes: 80 additions & 2 deletions domain_admin/api/monitor_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from domain_admin.model.log_monitor_model import LogMonitorModel
from domain_admin.model.monitor_model import MonitorModel
from domain_admin.service import monitor_service
from domain_admin.service import monitor_service, file_service, async_task_service
from domain_admin.service.scheduler_service import scheduler_main


Expand All @@ -25,12 +25,14 @@ def add_monitor():
current_user_id = g.user_id
title = request.json['title']
monitor_type = request.json['monitor_type']
allow_error_count = request.json.get('allow_error_count') or 0
content = request.json['content']
interval = request.json['interval']

monitor_row = MonitorModel.create(
user_id=current_user_id,
title=title,
allow_error_count=allow_error_count,
monitor_type=monitor_type,
content=json.dumps(content),
interval=interval
Expand All @@ -50,11 +52,13 @@ def update_monitor_by_id():
title = request.json['title']
content = request.json['content']
interval = request.json['interval']
allow_error_count = request.json.get('allow_error_count') or 0

MonitorModel.update(
title=title,
content=json.dumps(content),
interval=interval
interval=interval,
allow_error_count=allow_error_count,
).where(
MonitorModel.id == monitor_id
).execute()
Expand Down Expand Up @@ -119,6 +123,7 @@ def get_monitor_list():
order_prop = request.json.get('order_prop') or 'create_time'
order_type = request.json.get('order_type') or 'desc'
keyword = request.json.get('keyword')
status = request.json.get('status')

query = MonitorModel.select().where(
MonitorModel.user_id == current_user_id
Expand All @@ -127,6 +132,9 @@ def get_monitor_list():
if keyword:
query = query.where(MonitorModel.title.contains(keyword))

if isinstance(status, int):
query = query.where(MonitorModel.status == status)

total = query.count()

lst = []
Expand All @@ -147,3 +155,73 @@ def get_monitor_list():
'list': lst,
'total': total
}


def export_monitor_file():
"""
导出监控文件
csv格式
:return:
"""
current_user_id = g.user_id

keyword = request.json.get('keyword')
status = request.json.get('status')
ext = request.json.get('ext', 'csv')

order_prop = request.json.get('order_prop') or 'create_time'
order_type = request.json.get('order_type') or 'desc'

params = {
'keyword': keyword,
'status': status,
'user_id': current_user_id,
}

query = monitor_service.get_monitor_list_query(**params)

ordering = [
SQL(f"`{order_prop}` {order_type}"),
MonitorModel.id.desc()
]

rows = query.order_by(*ordering)

lst = [row.to_dict() for row in rows]

filename = monitor_service.export_monitor_to_file(rows=lst, ext=ext)

return {
'name': filename,
'url': file_service.resolve_temp_url(filename)
}


def import_monitor_from_file():
"""
从文件导入域名
支持 xlsx 和 csv格式
:return:
"""
current_user_id = g.user_id

update_file = request.files.get('file')

filename = file_service.save_temp_file(update_file)

# 导入数据
monitor_service.import_monitor_from_file(filename, current_user_id)

# 异步查询
run_init_monitor_task_async(user_id=current_user_id)


@async_task_service.async_task_decorator("更新监控信息")
def run_init_monitor_task_async(user_id):
rows = MonitorModel.select().where(
MonitorModel.user_id == user_id,
MonitorModel.version == 0
)

for row in rows:
scheduler_main.run_one_monitor_task(row)
7 changes: 7 additions & 0 deletions domain_admin/api/system_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from domain_admin.model.monitor_model import MonitorModel
from domain_admin.model.system_model import SystemModel
from domain_admin.service import scheduler_service
from domain_admin.service.scheduler_service import scheduler_main
from domain_admin.utils import datetime_util


Expand Down Expand Up @@ -218,3 +219,9 @@ def get_system_data():
'path': '/monitor/list'
}
]


def get_monitor_task_next_run_time():
return {
'next_run_time': scheduler_main.get_monitor_task_next_run_time()
}
2 changes: 1 addition & 1 deletion domain_admin/config/env_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@
# token_expire_days
TOKEN_EXPIRE_DAYS = env.int("TOKEN_EXPIRE_DAYS", DEFAULT_TOKEN_EXPIRE_DAYS)

# APP_MODE
# APP_MODE : production, development
APP_MODE = env.str("APP_MODE", 'production')
4 changes: 4 additions & 0 deletions domain_admin/enums/version_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,7 @@ class VersionEnum(object):
Version_167 = '1.6.7'
Version_168 = '1.6.8'
Version_169 = '1.6.9'

Version_1610 = '1.6.10'
Version_1611 = '1.6.11'
Version_1612 = '1.6.12'
3 changes: 2 additions & 1 deletion domain_admin/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from domain_admin import compat
from domain_admin.log import logger
from domain_admin.config import TEMP_DIR
from domain_admin.config import TEMP_DIR, APP_MODE
from domain_admin.model.base_model import db
from domain_admin.model.database import init_database
from domain_admin.router import api_map, permission
Expand Down Expand Up @@ -107,6 +107,7 @@ def init_app(flask_app):
# 版本自动升级
version_service.update_version()

# if APP_MODE == 'production':
# 启动定时器
scheduler_service.init_scheduler()

Expand Down
35 changes: 35 additions & 0 deletions domain_admin/migrate/history/migrate_1610_to_1611.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
"""
@File : migrate_1610_to_1611.py
@Date : 2024-02-24
cmd:
$ python domain_admin/migrate/migrate_1610_to_1611.py
"""
from __future__ import print_function, unicode_literals, absolute_import, division

from domain_admin.migrate import migrate_common
from domain_admin.model.address_model import AddressModel
from domain_admin.model.base_model import db
from domain_admin.model.domain_model import DomainModel
from domain_admin.model.monitor_model import MonitorModel
from domain_admin.model.notify_model import NotifyModel


def execute_migrate():
"""
版本升级 1.6.10 => 1.6.11
:return:
"""
migrator = migrate_common.get_migrator(db)

migrate_rows = [
# version
migrator.add_column(
table=MonitorModel._meta.table_name,
column_name=MonitorModel.version.name,
field=MonitorModel.version
)
]

migrate_common.try_execute_migrate(migrate_rows)
19 changes: 17 additions & 2 deletions domain_admin/migrate/migrate_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@
migrate_1520_to_1521,
migrate_154_to_155,
migrate_106_to_110,
migrate_1533_to_1534, migrate_162_to_163, migrate_168_to_169)
migrate_1533_to_1534,
migrate_162_to_163,
migrate_168_to_169,
migrate_1610_to_1611
)

# 参数说明
# local_versions 本地版本
# migrate_func 升级函数
# update_version
# update_version 升级后的版本

MIGRATE_CONFIG = [
# 1.0.0 1.0.1 1.0.2 => 1.0.3
Expand Down Expand Up @@ -340,4 +345,14 @@
'update_version': VersionEnum.Version_169
},

# 2024-02-24
# 1.6.10 => 1.6.11
{
'local_versions': [
VersionEnum.Version_169,
VersionEnum.Version_1610,
],
'migrate_func': migrate_1610_to_1611.execute_migrate,
'update_version': VersionEnum.Version_1611
},
]
34 changes: 34 additions & 0 deletions domain_admin/model/monitor_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ class MonitorModel(BaseModel):
# 下一次运行时间
next_run_time = DateTimeField(default=None, null=True)

# 数据版本号 @since 1.6.11
version = IntegerField(default=0, null=False)

# 创建时间
create_time = DateTimeField(default=datetime.now)

Expand All @@ -73,6 +76,14 @@ def update_time_label(self):
def content_dict(self):
return json.loads(self.content)

@property
def http_url(self):
return self.content_dict.get('url')

@property
def http_timeout(self):
return self.content_dict.get('timeout')

def to_dict(self):
data = model_to_dict(
model=self,
Expand All @@ -81,8 +92,31 @@ def to_dict(self):
'update_time_label',
'content_dict',
'monitor_id',
'http_url',
'http_timeout',
]
)

data['content'] = data.pop('content_dict')
return data


# 数据导入导出字段关系
FIELD_MAPPING = [
{
'name': '名称',
'field': 'title',
},
{
'name': '请求URL',
'field': 'http_url',
},
{
'name': '请求超时(秒)',
'field': 'http_timeout',
},
{
'name': '检测频率(分钟)',
'field': 'interval',
},
]
5 changes: 4 additions & 1 deletion domain_admin/router/api_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"/api/updateAllDomainCertInfo": domain_api.update_all_domain_cert_info,
"/api/getDomainGroupFilter": domain_api.get_domain_group_filter,


# "/api/updateDomainSetting": domain_api.update_domain_setting,

"/api/updateAllDomainCertInfoOfUser": domain_api.update_all_domain_cert_info_of_user,
Expand Down Expand Up @@ -83,6 +82,7 @@
'/api/updateCronConfig': system_api.update_cron_config,
'/api/getSystemVersion': system_api.get_system_version,
'/api/getSystemData': system_api.get_system_data,
'/api/getMonitorTaskNextRunTime': system_api.get_monitor_task_next_run_time,

# 用户管理 (管理员权限)
'/api/getUserList': user_api.get_user_list,
Expand Down Expand Up @@ -189,11 +189,14 @@
'/api/removeMonitorById': monitor_api.remove_monitor_by_id,
'/api/getMonitorById': monitor_api.get_monitor_by_id,
'/api/getMonitorList': monitor_api.get_monitor_list,
'/api/exportMonitorFile': monitor_api.export_monitor_file,
'/api/importMonitorFromFile': monitor_api.import_monitor_from_file,

# http监控日志
'/api/getLogMonitorList': log_monitor_api.get_log_monitor_list,
'/api/clearLogMonitor': log_monitor_api.clear_log_monitor,
'/api/clearAllLogMonitor': log_monitor_api.clear_all_log_monitor,

'/api/getTagList': tag_api.get_tag_list,

}
Loading

0 comments on commit 6322036

Please sign in to comment.