mirror of
https://github.com/fastapi-practices/fastapi_best_architecture.git
synced 2025-08-18 15:00:46 +08:00
Refactor global datetime to timezone datetime (#152)
This commit is contained in:
@ -2,12 +2,12 @@ import os
|
||||
import platform
|
||||
import socket
|
||||
import sys
|
||||
from datetime import datetime, timedelta
|
||||
from datetime import timedelta
|
||||
from typing import List
|
||||
|
||||
import psutil
|
||||
|
||||
from backend.app.core.conf import settings
|
||||
from backend.app.utils.timezone import timezone_utils
|
||||
|
||||
|
||||
class ServerInfo:
|
||||
@ -23,16 +23,24 @@ class ServerInfo:
|
||||
|
||||
@staticmethod
|
||||
def fmt_timedelta(td: timedelta) -> str:
|
||||
"""格式化时间戳"""
|
||||
days, rem = divmod(td.seconds, 86400)
|
||||
"""格式化时间差"""
|
||||
total_seconds = round(td.total_seconds())
|
||||
days, rem = divmod(total_seconds, 86400)
|
||||
hours, rem = divmod(rem, 3600)
|
||||
minutes, _ = divmod(rem, 60)
|
||||
res = f'{minutes} 分钟'
|
||||
if hours:
|
||||
res = f'{hours} 小时 {res}'
|
||||
minutes, seconds = divmod(rem, 60)
|
||||
parts = []
|
||||
if days:
|
||||
res = f'{days} 天 {res}'
|
||||
return res
|
||||
parts.append('{} 天'.format(days))
|
||||
if hours:
|
||||
parts.append('{} 小时'.format(hours))
|
||||
if minutes:
|
||||
parts.append('{} 分钟'.format(minutes))
|
||||
if seconds:
|
||||
parts.append('{} 秒'.format(seconds))
|
||||
if len(parts) == 0:
|
||||
return '0 秒'
|
||||
else:
|
||||
return ' '.join(parts)
|
||||
|
||||
@staticmethod
|
||||
def get_cpu_info() -> dict:
|
||||
@ -96,7 +104,7 @@ class ServerInfo:
|
||||
"""获取服务信息"""
|
||||
process = psutil.Process(os.getpid())
|
||||
mem_info = process.memory_info()
|
||||
start_time = datetime.fromtimestamp(process.create_time())
|
||||
start_time = timezone_utils.utc_timestamp_to_timezone_datetime(process.create_time())
|
||||
return {
|
||||
'name': 'Python3',
|
||||
'version': platform.python_version(),
|
||||
@ -105,6 +113,6 @@ class ServerInfo:
|
||||
'mem_vms': ServerInfo.format_bytes(mem_info.vms), # 虚拟内存, 即当前进程申请的虚拟内存
|
||||
'mem_rss': ServerInfo.format_bytes(mem_info.rss), # 常驻内存, 即当前进程实际使用的物理内存
|
||||
'mem_free': ServerInfo.format_bytes(mem_info.vms - mem_info.rss), # 空闲内存
|
||||
'startup': start_time.strftime(settings.DATETIME_FORMAT),
|
||||
'elapsed': f'{ServerInfo.fmt_timedelta(datetime.now() - start_time)}',
|
||||
'startup': start_time,
|
||||
'elapsed': f'{ServerInfo.fmt_timedelta(timezone_utils.get_timezone_datetime() - start_time)}',
|
||||
}
|
||||
|
Reference in New Issue
Block a user