Skip to content

Commit

Permalink
新增:网站监测支持超时时间到毫秒级
Browse files Browse the repository at this point in the history
  • Loading branch information
mouday committed Nov 2, 2024
1 parent 93b66be commit 25daa79
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 9 deletions.
7 changes: 6 additions & 1 deletion domain_admin/api/monitor_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from domain_admin.enums.operation_enum import OperationEnum
from domain_admin.enums.role_enum import RoleEnum
from domain_admin.enums.time_unit_enum import TimeUnitEnum
from domain_admin.model.log_monitor_model import LogMonitorModel
from domain_admin.model.monitor_model import MonitorModel
from domain_admin.service import monitor_service, file_service, async_task_service, operation_service, auth_service
Expand All @@ -32,14 +33,16 @@ def add_monitor():
allow_error_count = request.json.get('allow_error_count') or 0
content = request.json['content']
interval = request.json['interval']
interval_unit = request.json.get('interval_unit', TimeUnitEnum.Minute)

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
interval=interval,
interval_unit=interval_unit
)

scheduler_main.run_one_monitor_task(MonitorModel.get_by_id(monitor_row.id))
Expand All @@ -58,6 +61,7 @@ def update_monitor_by_id():
content = request.json['content']
interval = request.json['interval']
allow_error_count = request.json.get('allow_error_count') or 0
interval_unit = request.json.get('interval_unit', TimeUnitEnum.Minute)

monitor_row = MonitorModel.select().where(
MonitorModel.id == monitor_id,
Expand All @@ -71,6 +75,7 @@ def update_monitor_by_id():
title=title,
content=json.dumps(content),
interval=interval,
interval_unit=interval_unit,
allow_error_count=allow_error_count,
).where(
MonitorModel.id == monitor_id
Expand Down
76 changes: 76 additions & 0 deletions domain_admin/enums/time_unit_enum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# -*- coding: utf-8 -*-
"""
@File : time_unit_enum.py
@Date : 2024-11-02
@Author : Peng Shiyu
"""
from __future__ import print_function, unicode_literals, absolute_import, division


class TimeUnitEnum(object):
"""
时间单位枚举值
@since v1.6.56
"""
# 毫秒
Millisecond = 1

# 秒
Second = 2

# 分钟
Minute = 3

# 小时
Hour = 4

# 天
Day = 5

# 周
Week = 6

# 月
Month = 7

# 年
Year = 8

@staticmethod
def get_config():
return [
(TimeUnitEnum.Millisecond, '毫秒'),
(TimeUnitEnum.Second, '秒'),
(TimeUnitEnum.Minute, '分钟'),
(TimeUnitEnum.Hour, '小时'),
(TimeUnitEnum.Day, '天'),
(TimeUnitEnum.Week, '周'),
(TimeUnitEnum.Month, '月'),
(TimeUnitEnum.Year, '年'),
]

@staticmethod
def get_label(value):
mapping = {k: v for k, v in TimeUnitEnum.get_config()}
return mapping.get(value)

@staticmethod
def get_value(label):
mapping = {k: v for v, k in TimeUnitEnum.get_config()}
return mapping.get(label)

@staticmethod
def to_millisecond(value, unit=Second):
# 基础单位:毫秒
mapping = {
TimeUnitEnum.Millisecond: 1,
TimeUnitEnum.Second: 1000,
TimeUnitEnum.Minute: 1000 * 60,
TimeUnitEnum.Hour: 1000 * 60 * 60,
TimeUnitEnum.Day: 1000 * 60 * 60 * 24,
TimeUnitEnum.Week: 1000 * 60 * 60 * 24 * 7,
TimeUnitEnum.Month: 1000 * 60 * 60 * 24 * 30,
TimeUnitEnum.Year: 1000 * 60 * 60 * 24 * 365,
}

return value * mapping.get(unit)
6 changes: 6 additions & 0 deletions domain_admin/enums/version_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,9 @@ class VersionEnum(object):

Version_1652 = '1.6.52'
Version_1653 = '1.6.53'

Version_1654 = '1.6.54'
Version_1655 = '1.6.55'
Version_1656 = '1.6.56'
Version_1657 = '1.6.57'
Version_1658 = '1.6.58'
32 changes: 32 additions & 0 deletions domain_admin/migrate/history/migrate_1655_to_1656.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-
"""
@File : migrate_1655_to_1656.py
@Date : 2024-08-28
cmd:
$ python domain_admin/migrate/migrate_1655_to_1656.py
"""
from __future__ import print_function, unicode_literals, absolute_import, division

from domain_admin.migrate import migrate_common
from domain_admin.model.base_model import db
from domain_admin.model.monitor_model import MonitorModel


def execute_migrate():
"""
版本升级 v1.6.55 => v1.6.56
:return:
"""

migrator = migrate_common.get_migrator(db)

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

migrate_common.try_execute_migrate(migrate_rows)
18 changes: 17 additions & 1 deletion domain_admin/migrate/migrate_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
migrate_168_to_169,
migrate_1610_to_1611,
migrate_1625_to_1626, migrate_1633_to_1634, migrate_1634_to_1635, migrate_1642_to_1643, migrate_1646_to_1647,
migrate_1647_to_1648)
migrate_1647_to_1648, migrate_1655_to_1656)

# 参数说明
# local_versions 本地版本
Expand Down Expand Up @@ -439,4 +439,20 @@
'update_version': VersionEnum.Version_1648
},

# 2024-11-02
# 版本升级 1.6.55 => 1.6.56
{
'local_versions': [
VersionEnum.Version_1648,
VersionEnum.Version_1649,
VersionEnum.Version_1650,
VersionEnum.Version_1651,
VersionEnum.Version_1652,
VersionEnum.Version_1653,
VersionEnum.Version_1654,
VersionEnum.Version_1655
],
'migrate_func': migrate_1655_to_1656.execute_migrate,
'update_version': VersionEnum.Version_1656
},
]
36 changes: 32 additions & 4 deletions domain_admin/model/monitor_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from domain_admin.enums.monitor_status_enum import MonitorStatusEnum
from domain_admin.enums.monitor_type_enum import MonitorTypeEnum
from domain_admin.enums.time_unit_enum import TimeUnitEnum
from domain_admin.model.base_model import BaseModel
from domain_admin.utils import datetime_util

Expand All @@ -31,11 +32,15 @@ class MonitorModel(BaseModel):

# 监控参数
# http {url, method, timeout}
# @since v1.6.56 add timeout_unit
content = TextField(default=None)

# 间隔 检测频率 单位:分钟
# 间隔 检测频率
interval = IntegerField(default=60)

# 间隔 检测频率单位:默认:分钟
interval_unit = IntegerField(default=TimeUnitEnum.Minute)

# 允许失败次数
allow_error_count = IntegerField(default=0)

Expand Down Expand Up @@ -82,7 +87,19 @@ def http_url(self):

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

@property
def http_timeout_unit(self):
return self.content_dict.get('timeout_unit')

@property
def http_timeout_unit_label(self):
return TimeUnitEnum.get_label(self.http_timeout_unit)

@property
def interval_unit_label(self):
return TimeUnitEnum.get_label(self.interval_unit)

def to_dict(self):
data = model_to_dict(
Expand All @@ -94,6 +111,9 @@ def to_dict(self):
'monitor_id',
'http_url',
'http_timeout',
'http_timeout_unit',
'http_timeout_unit_label',
'interval_unit_label',
]
)

Expand All @@ -112,11 +132,19 @@ def to_dict(self):
'field': 'http_url',
},
{
'name': '请求超时(秒)',
'name': '请求超时',
'field': 'http_timeout',
},
{
'name': '检测频率(分钟)',
'name': '请求超时单位',
'field': 'http_timeout_unit_label',
},
{
'name': '检测频率',
'field': 'interval',
},
{
'name': '检测频率单位',
'field': 'interval_unit_label',
},
]
18 changes: 15 additions & 3 deletions domain_admin/service/monitor_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from domain_admin.config import USER_AGENT
from domain_admin.enums.monitor_status_enum import MonitorStatusEnum
from domain_admin.enums.monitor_type_enum import MonitorTypeEnum
from domain_admin.enums.time_unit_enum import TimeUnitEnum
from domain_admin.log import logger
from domain_admin.model import monitor_model
from domain_admin.model.base_model import db
from domain_admin.model.log_monitor_model import LogMonitorModel
Expand Down Expand Up @@ -122,7 +124,9 @@ def run_monitor_warp(monitor_row):
error = e

# 计算下次运行时间
next_run_time = datetime.now() + timedelta(minutes=monitor_row.interval)
# @since v1.6.56 精确到毫秒
next_run_time = datetime.now() + timedelta(
milliseconds=TimeUnitEnum.to_millisecond(value=monitor_row.interval, unit=monitor_row.interval_unit))

# 同步任务
MonitorModel.update(
Expand All @@ -144,11 +148,18 @@ def run_monitor(monitor_row):
:param monitor_row:
:return:
"""

if monitor_row.monitor_type == MonitorTypeEnum.HTTP:
# 毫秒
timeout = TimeUnitEnum.to_millisecond(
value=monitor_row.http_timeout,
unit=monitor_row.http_timeout_unit or TimeUnitEnum.Second # 系统原来默认是毫秒
)

run_http_monitor(
method=monitor_row.content_dict['method'],
url=monitor_row.content_dict['url'],
timeout=int(monitor_row.content_dict['timeout'])
timeout=timeout * 0.001 # 毫秒转为秒
)


Expand Down Expand Up @@ -250,17 +261,18 @@ def import_monitor_from_file(filename, user_id):
rows = file_util.read_data_from_file(filename)

lst = file_util.convert_to_import(rows, monitor_model.FIELD_MAPPING)
print(lst)

lst = [
{
'title': item['title'],
'monitor_type': MonitorTypeEnum.HTTP,
'interval': int(item.get('interval') or '60'),
'interval_unit': TimeUnitEnum.get_value(item.get('interval_unit_label') or '分钟'),
'content': json.dumps({
'url': item['http_url'],
'method': 'GET',
'timeout': int(item.get('http_timeout') or '3'),
'timeout_unit': TimeUnitEnum.get_value(item.get('http_timeout_unit_label') or '秒'),
}),
'user_id': user_id,
} for item in lst
Expand Down

0 comments on commit 25daa79

Please sign in to comment.