Skip to content

Commit deb50ce

Browse files
committed
refactor(mcsmapi): 重构Daemon相关API及模型
- 修改Daemon类中的方法名称,以更准确地反映其功能 - 新增Daemon信息获取方法,包括配置、状态和系统信息 - 更新相关模型以适应新的API结构 - 优化模型定义,移除不必要的字段和方法
1 parent 6d013ff commit deb50ce

File tree

9 files changed

+94
-93
lines changed

9 files changed

+94
-93
lines changed

example/daemon.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
# show Daemon list
1212

13-
print(daemon_object.show())
13+
print(daemon_object.config())
1414

1515
# 创建节点
1616
daemonId = daemon_object.add(

mcsmapi/apis/daemon.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
from typing import Any
22
from mcsmapi.pool import ApiPool
33
from mcsmapi.request import send
4-
from mcsmapi.models.daemon import DaemonConfig, DaemonModel
4+
from mcsmapi.models.daemon import (
5+
DaemonConfig,
6+
DaemonInfo,
7+
DaemonSystemInfo,
8+
DaemonStatus,
9+
DaemonUpdateConfig,
10+
)
511

612

713
class Daemon:
814
@staticmethod
9-
def show() -> list[DaemonConfig]:
15+
def config() -> list[DaemonStatus]:
1016
"""
1117
获取全部节点配置信息
1218
@@ -16,10 +22,23 @@ def show() -> list[DaemonConfig]:
1622
"GET",
1723
f"{ApiPool.SERVICE}/remote_services_list",
1824
)
19-
return [DaemonConfig(**daemon) for daemon in daemons]
25+
return [DaemonStatus(**daemon) for daemon in daemons]
2026

2127
@staticmethod
22-
def system() -> list[DaemonModel]:
28+
def info() -> list[DaemonInfo]:
29+
"""
30+
获取全部节点信息
31+
32+
:returns: 节点信息列表
33+
"""
34+
daemons = send(
35+
"GET",
36+
f"{ApiPool.SERVICE}/remote_services",
37+
)
38+
return [DaemonInfo(**daemon) for daemon in daemons]
39+
40+
@staticmethod
41+
def system() -> list[DaemonSystemInfo]:
2342
"""
2443
获取全部节点的系统信息
2544
@@ -29,7 +48,7 @@ def system() -> list[DaemonModel]:
2948
"GET",
3049
f"{ApiPool.SERVICE}/remote_services_system",
3150
)
32-
return [DaemonModel(**daemon) for daemon in daemons]
51+
return [DaemonSystemInfo(**daemon) for daemon in daemons]
3352

3453
@staticmethod
3554
def add(config: dict[str, Any]) -> str:
@@ -77,16 +96,14 @@ def update(daemonId: str, config: dict[str, Any]) -> bool:
7796
"""
7897
更新一个节点的配置
7998
80-
**不建议直接使用此函数,建议调用overview()后在remote属性内使用节点对象的updateConfig方法按需更新**
81-
8299
:params daemonId: 节点的UUID
83-
:params config: 节点的配置信息,以字典形式提供,缺失内容由DaemonConfig模型补全
100+
:params config: 节点的配置信息,以字典形式提供,缺失内容由DaemonUpdateConfig模型补全
84101
85102
:returns: 操作成功后返回True
86103
"""
87104
return send(
88105
"PUT",
89106
f"{ApiPool.SERVICE}/remote_service",
90107
params={"uuid": daemonId},
91-
data=DaemonConfig(**config).model_dump(),
108+
data=DaemonUpdateConfig(**config).model_dump(),
92109
)

mcsmapi/apis/instance.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def detail(daemonId: str, uuid: str) -> InstanceDetail:
6363
ApiPool.INSTANCE,
6464
params={"uuid": uuid, "daemonId": daemonId},
6565
)
66-
return InstanceDetail(**result)
66+
return InstanceDetail(**result, daemonId=daemonId)
6767

6868
@staticmethod
6969
def create(daemonId: str, config: dict[str, Any]) -> InstanceCreateResult:

mcsmapi/models/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class ProcessInfo(BaseModel):
2121
"""工作路径"""
2222

2323

24-
class InstanceInfo(BaseModel):
24+
class InstanceStat(BaseModel):
2525
"""实例统计信息"""
2626

2727
running: int

mcsmapi/models/daemon.py

Lines changed: 61 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from typing import Any
22
from pydantic import BaseModel
3-
from mcsmapi.models.instance import InstanceCreateResult
4-
from mcsmapi.models.common import ProcessInfo, InstanceInfo, CpuMemChart
3+
from mcsmapi.models.instance import InstanceCreateResult, InstanceDetail
4+
from mcsmapi.models.common import ProcessInfo, InstanceStat, CpuMemChart
55

66

7-
class DaemonSystemInfo(BaseModel):
7+
class SystemInfo(BaseModel):
88
"""节点系统信息"""
99

1010
type: str
@@ -35,31 +35,44 @@ class DaemonSystemInfo(BaseModel):
3535
"""未知"""
3636

3737

38-
class DaemonModel(BaseModel):
39-
"""节点详细信息"""
38+
class DaemonSetting(BaseModel):
39+
"""节点系统配置信息"""
40+
41+
language: str
42+
"""节点语言"""
43+
uploadSpeedRate: int
44+
"""上传速度限制(0为不限制, 限制为(n * 64)KB/s)"""
45+
downloadSpeedRate: int
46+
"""下载速度限制(0为不限制, 限制为(n * 64)KB/s)"""
47+
portRangeStart: int
48+
"""端口范围起始值"""
49+
portRangeEnd: int
50+
"""端口范围结束值"""
51+
portAssignInterval: int
52+
"""未知"""
53+
port: int
54+
"""节点监听端口"""
55+
56+
57+
class DaemonSystemInfo(BaseModel):
58+
"""节点系统信息"""
4059

41-
version: str
60+
version: str | None = None
4261
"""远程节点版本"""
43-
process: ProcessInfo
62+
process: ProcessInfo | None = None
4463
"""远程节点的基本信息"""
45-
instance: InstanceInfo
64+
instance: InstanceStat | None = None
4665
"""远程节点实例基本信息"""
47-
system: DaemonSystemInfo
66+
system: SystemInfo | None = None
4867
"""远程节点系统信息"""
49-
cpuMemChart: list[CpuMemChart]
68+
cpuMemChart: list[CpuMemChart] | None = None
5069
"""cpu和内存使用趋势"""
70+
config: DaemonSetting
71+
72+
73+
class DaemonOperation(BaseModel):
5174
uuid: str
52-
"""远程节点的uuid"""
53-
ip: str
54-
"""远程节点的ip"""
55-
port: int
56-
"""远程节点的端口"""
57-
prefix: str
58-
"""远程节点的路径前缀"""
59-
available: bool
60-
"""远程节点的可用状态"""
61-
remarks: str
62-
"""远程节点的名称"""
75+
"""节点UUID"""
6376

6477
def delete(self):
6578
"""
@@ -93,14 +106,8 @@ def updateConfig(self, config: dict[str, Any]) -> bool:
93106

94107
updated_config = self.model_dump()
95108
updated_config.update(config)
96-
# 过滤节点配置中不需要的字段
97-
daemon_config_dict = {
98-
key: updated_config[key]
99-
for key in DaemonConfig.model_fields.keys()
100-
if key in updated_config
101-
}
102109

103-
daemon_config = DaemonConfig(**daemon_config_dict).model_dump()
110+
daemon_config = DaemonUpdateConfig(**updated_config).model_dump()
104111

105112
return Daemon.update(self.uuid, daemon_config)
106113

@@ -142,5 +149,29 @@ class DaemonConfig(BaseModel):
142149
"""远程节点的路径前缀"""
143150
remarks: str = "Unnamed Node"
144151
"""远程节点的备注"""
145-
available: bool = True
146-
"""远程节点的可用状态"""
152+
apiKey: str = ""
153+
"""远程节点的apiKey"""
154+
155+
156+
class DaemonStatus(DaemonOperation):
157+
"""节点状态信息"""
158+
ip: str = "localhost"
159+
"""远程节点的ip"""
160+
port: int = 24444
161+
"""远程节点的端口"""
162+
prefix: str = ""
163+
"""远程节点的路径前缀"""
164+
remarks: str = "Unnamed Node"
165+
"""远程节点的备注"""
166+
available: bool
167+
"""节点可用状态"""
168+
169+
170+
class DaemonInfo(DaemonStatus):
171+
"""节点信息"""
172+
instances: list[InstanceDetail]
173+
174+
175+
class DaemonUpdateConfig(DaemonConfig):
176+
"""节点更新配置信息"""
177+
setting: DaemonSetting

mcsmapi/models/instance.py

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -121,55 +121,15 @@ class InstanceConfig(BaseModel):
121121
"""运行该实例的系统用户,为空则使用启动面板的系统用户"""
122122

123123

124-
class InstanceProcessInfo(BaseModel):
125-
"""进程信息"""
126-
127-
cpu: int
128-
"""CPU 使用率 (单位: %)"""
129-
memory: int
130-
"""进程占用内存 (单位: KB)"""
131-
ppid: int
132-
"""父进程 ID"""
133-
pid: int
134-
"""进程 ID"""
135-
ctime: int
136-
"""进程创建时间 (Unix 时间戳)"""
137-
elapsed: int
138-
"""进程运行时长 (单位: 秒)"""
139-
timestamp: int
140-
"""时间戳"""
141-
142-
143-
class InstanceInfo(BaseModel):
144-
"""实例运行状态信息(这些选项在新版中已不再支持设置,但仍在API中返回)"""
145-
146-
currentPlayers: int
147-
"""当前玩家数量"""
148-
fileLock: int = 0
149-
"""文件锁状态 (0: 无锁)"""
150-
maxPlayers: int = -1
151-
"""最大允许玩家数 (-1 表示未知)"""
152-
openFrpStatus: bool = False
153-
"""是否启用 FRP 远程服务"""
154-
playersChart: list[dict] = []
155-
"""玩家数量变化图表数据"""
156-
version: str = ""
157-
"""服务器版本"""
158-
159-
160124
class InstanceDetail(BaseModel):
161125
"""实例详细信息"""
162126

163127
config: InstanceConfig
164128
"""实例的配置信息"""
165-
info: InstanceInfo
166-
"""实例的运行状态信息"""
167129
daemonId: str
168130
"""所属的节点UUID"""
169131
instanceUuid: str
170132
"""实例UUID"""
171-
processInfo: InstanceProcessInfo
172-
"""实例的进程信息"""
173133
started: int
174134
"""实例的启动次数"""
175135
status: Status

mcsmapi/models/overview.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import Literal
33
from pydantic import BaseModel
44
from mcsmapi.models.common import CpuMemChart, ProcessInfo
5-
from mcsmapi.models.daemon import DaemonModel
5+
from mcsmapi.models.daemon import DaemonSystemInfo
66

77

88
class SystemUser(BaseModel):
@@ -110,7 +110,7 @@ class OverviewModel(BaseModel):
110110
"""系统与请求统计图表数据"""
111111
remoteCount: RemoteCountInfo
112112
"""远程节点统计信息"""
113-
remote: list[DaemonModel]
113+
remote: list[DaemonSystemInfo]
114114
"""远程节点详细信息"""
115115

116116

mcsmapi/models/user.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,7 @@ def update(self, config: dict[str, Any]):
7575

7676
updated_config = self.model_dump()
7777
updated_config.update(config)
78-
# 过滤用户信息中不需要的字段
79-
user_config_dict = {
80-
key: updated_config[key]
81-
for key in UserConfig.model_fields.keys()
82-
if key in updated_config
83-
}
84-
85-
user_config = UserConfig(**user_config_dict).model_dump()
78+
user_config = UserConfig(**updated_config).model_dump()
8679

8780
return User().update(self.uuid, user_config)
8881

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ requires = ["setuptools", "wheel"]
44

55
[project]
66
name = "mcsmapi"
7-
version = "0.1.8.b1"
7+
version = "0.1.8.b2"
88
description = "Shortcut the pypi package of MCSM./快捷操作MCSM的pypi包"
99
readme = "README.md"
1010
authors = [

0 commit comments

Comments
 (0)