add Computer_status

This commit is contained in:
XDL163 2024-12-18 10:37:23 +08:00
parent 3b8190cf66
commit 7be76df50b
6 changed files with 340 additions and 3 deletions

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

226
Computer_status.py Normal file
View File

@ -0,0 +1,226 @@
import random
import shelve
import json
from datetime import datetime
# 定义数据对象
class Computer_status:
def __init__(self, time, ram_all, ram_use, cpu_use, cpu_c, disk, web, gpu):
self.time = time # 时间戳,单位:秒
self.ram_all = ram_all # 总内存
self.ram_use = ram_use # 已占用内存
self.cpu_use = cpu_use # CPU 占用百分比
self.cpu_c = cpu_c # CPU 温度
self.disk = disk # 磁盘信息
self.web = web # 网络流量信息
self.gpu = gpu # GPU 信息
def to_dict(self):
"""将对象转换为字典"""
return {
"time": self.time,
"ram_all": self.ram_all,
"ram_use": self.ram_use,
"cpu_use": self.cpu_use,
"cpu_c": self.cpu_c,
"disk": self.disk,
"web": self.web,
"gpu": self.gpu,
}
@staticmethod
def from_dict(data):
"""从字典创建 Computer_status 对象"""
return Computer_status(
time=data["time"],
ram_all=data["ram_all"],
ram_use=data["ram_use"],
cpu_use=data["cpu_use"],
cpu_c=data["cpu_c"],
disk=data["disk"],
web=data["web"],
gpu=data["gpu"],
)
# 数据操作方法
DATABASE_FILE = "data/computer_status.db" # 数据库存储文件
def write_data(computer_status):
"""
写入一条数据到对象数据库
参数
computer_status: Computer_status 对象
"""
if not isinstance(computer_status, Computer_status):
raise ValueError("Input must be a Computer_status object")
with shelve.open(DATABASE_FILE) as db:
db[str(computer_status.time)] = computer_status
# def get_data_30s():
# """
# 获取最新30秒的数据
# 返回:
# List[Computer_status],按时间升序排列
# """
# current_time = int(datetime.now().timestamp())
# with shelve.open(DATABASE_FILE) as db:
# data = [
# db[key]
# for key in db
# if current_time - int(key) <= 30 # 过滤最近30秒数据
# ]
# return sorted(data, key=lambda x: x.time)
#
#
# def get_data(time_start, time_end):
# """
# 获取指定时间戳到指定时间戳之间的数据
# 参数:
# time_start: 开始时间戳int
# time_end: 结束时间戳int
# 返回:
# List[Computer_status],按时间升序排列
# """
# with shelve.open(DATABASE_FILE) as db:
# data = [
# db[key]
# for key in db
# if time_start <= int(key) <= time_end
# ]
# return sorted(data, key=lambda x: x.time)
#
#
# def get_data_json_30s():
# """
# 获取最新30秒数据并转为 JSON
# 返回:
# JSON 数据List[Dict]
# """
# data = get_data_30s()
# return json.dumps([cs.to_dict() for cs in data], indent=4)
#
#
# def get_data_json(time_start, time_end):
# """
# 获取指定时间范围的数据并转为 JSON
# 参数:
# time_start: 开始时间戳int
# time_end: 结束时间戳int
# 返回:
# JSON 数据List[Dict]
# """
# data = get_data(time_start, time_end)
# return json.dumps([cs.to_dict() for cs in data], indent=4)
def delete_data(time_start):
"""
删除 time_start 之前的数据
参数
time_start: 开始时间戳int保留此时间戳之后的数据
"""
with shelve.open(DATABASE_FILE, writeback=True) as db:
keys_to_delete = [key for key in db if int(key) < time_start]
for key in keys_to_delete:
del db[key]
print(f"已删除 {len(keys_to_delete)} 条记录")
# 实时数据生成函数
def generate_data(timestamp):
"""生成一个 Computer_status 对象"""
return Computer_status(
time=timestamp,
ram_all=16000,
ram_use=random.randint(8000, 16000),
cpu_use=random.randint(10, 90),
cpu_c=random.randint(40, 70),
disk={"disk1": [100000, random.randint(50000, 90000), random.randint(100, 500), random.randint(200, 600)]},
web={"eth0": [random.randint(500, 1500), random.randint(1000, 2500)]},
gpu={"GPU1": {"gpu_ram_all": 8, "gpu_ram_use": random.uniform(2, 8), "gpu_use": random.randint(10, 90), "gpu_fan": random.randint(20, 80)}}
)
# 数据操作方法
def get_data_30s():
"""
实时生成最新30秒的数据
返回
List[Computer_status]按时间升序排列
"""
current_time = int(datetime.now().timestamp())
data = [generate_data(current_time - i) for i in range(30)] # 生成过去30秒的数据
return sorted(data, key=lambda x: x.time)
def get_data(time_start, time_end):
"""
实时生成指定时间范围的数据
参数
time_start: 开始时间戳int
time_end: 结束时间戳int
返回
List[Computer_status]按时间升序排列
"""
data = [generate_data(timestamp) for timestamp in range(time_start, time_end + 1)]
return sorted(data, key=lambda x: x.time)
def get_data_json_30s():
"""
实时生成最新30秒数据并转为 JSON
返回
JSON 数据List[Dict]
"""
data = get_data_30s()
return json.dumps([cs.to_dict() for cs in data], indent=4)
def get_data_json(time_start, time_end):
"""
实时生成指定时间范围的数据并转为 JSON
参数
time_start: 开始时间戳int
time_end: 结束时间戳int
返回
JSON 数据List[Dict]
"""
data = get_data(time_start, time_end)
return json.dumps([cs.to_dict() for cs in data], indent=4)
# 示例测试
if __name__ == "__main__":
# # 创建一些测试数据
# for i in range(40):
# cs = Computer_status(
# time=int(datetime.now().timestamp()) - i,
# ram_all=16000,
# ram_use=8000 + i * 100,
# cpu_use=50 + i,
# cpu_c=60 - i,
# disk={"disk1": [100000, 50000 + i * 100, 100, 200]},
# web={"eth0": [1000 + i * 10, 2000 + i * 20]},
# gpu={"GPU1": {"gpu_ram_all": 8, "gpu_ram_use": 4 + i / 10, "gpu_use": 30 + i, "gpu_fan": 50 + i}},
# )
# write_data(cs)
#
# # 获取最新30秒数据
# print("最新30秒数据JSON:")
# print(get_data_json_30s())
#
# # 获取指定时间段的数据
# time_now = int(datetime.now().timestamp())
# print(f"指定时间段数据({time_now - 20} 到 {time_now - 10}:")
# print(get_data_json(time_now - 20, time_now - 10))
# 获取最新30秒的实时数据
print("最新30秒数据JSON:")
print(get_data_json_30s())
# 获取指定时间范围的实时数据
time_now = int(datetime.now().timestamp())
print(f"指定时间段数据({time_now - 20}{time_now - 10}:")
print(get_data_json(time_now - 20, time_now - 10))

View File

@ -1,5 +1,3 @@
# Computer_status
# 服务器监控面板
@ -144,6 +142,25 @@ time_end
}
### 数据删除方法
删除time_start之前的数据
方法名称delete_data
参数:{
time_start
类型 int
备注:开始时间
}
## 数据存储部分
每隔一秒读取电脑状态打包成Computer_status对象,调用Computer_status.write_data

View File

@ -0,0 +1,43 @@
'1734488810', (20480, 254)
'1734488809', (20992, 254)
'1734488808', (21504, 254)
'1734488807', (22016, 254)
'1734488806', (22528, 254)
'1734488805', (23040, 254)
'1734488804', (23552, 254)
'1734488803', (24064, 254)
'1734488802', (24576, 254)
'1734488801', (25088, 254)
'1734488800', (25600, 254)
'1734488799', (26112, 254)
'1734488798', (26624, 254)
'1734488797', (27136, 254)
'1734488796', (27648, 254)
'1734488795', (28160, 254)
'1734488794', (28672, 254)
'1734488793', (29184, 254)
'1734488792', (29696, 254)
'1734488791', (30208, 254)
'1734488790', (30720, 254)
'1734488832', (36864, 254)
'1734488831', (37376, 254)
'1734488830', (37888, 254)
'1734488829', (38400, 254)
'1734488828', (38912, 254)
'1734488827', (39424, 254)
'1734488826', (39936, 254)
'1734488825', (40448, 254)
'1734488824', (40960, 254)
'1734488823', (41472, 254)
'1734488822', (41984, 254)
'1734488821', (42496, 254)
'1734488820', (43008, 254)
'1734488819', (43520, 254)
'1734488818', (44032, 254)
'1734488817', (44544, 254)
'1734488816', (45056, 254)
'1734488815', (45568, 254)
'1734488814', (46080, 254)
'1734488813', (46592, 254)
'1734488812', (47104, 254)
'1734488811', (47616, 254)

BIN
data/computer_status.db.dat Normal file

Binary file not shown.

View File

@ -0,0 +1,43 @@
'1734488810', (20480, 254)
'1734488809', (20992, 254)
'1734488808', (21504, 254)
'1734488807', (22016, 254)
'1734488806', (22528, 254)
'1734488805', (23040, 254)
'1734488804', (23552, 254)
'1734488803', (24064, 254)
'1734488802', (24576, 254)
'1734488801', (25088, 254)
'1734488800', (25600, 254)
'1734488799', (26112, 254)
'1734488798', (26624, 254)
'1734488797', (27136, 254)
'1734488796', (27648, 254)
'1734488795', (28160, 254)
'1734488794', (28672, 254)
'1734488793', (29184, 254)
'1734488792', (29696, 254)
'1734488791', (30208, 254)
'1734488790', (30720, 254)
'1734488832', (36864, 254)
'1734488831', (37376, 254)
'1734488830', (37888, 254)
'1734488829', (38400, 254)
'1734488828', (38912, 254)
'1734488827', (39424, 254)
'1734488826', (39936, 254)
'1734488825', (40448, 254)
'1734488824', (40960, 254)
'1734488823', (41472, 254)
'1734488822', (41984, 254)
'1734488821', (42496, 254)
'1734488820', (43008, 254)
'1734488819', (43520, 254)
'1734488818', (44032, 254)
'1734488817', (44544, 254)
'1734488816', (45056, 254)
'1734488815', (45568, 254)
'1734488814', (46080, 254)
'1734488813', (46592, 254)
'1734488812', (47104, 254)
'1734488811', (47616, 254)