File size: 9,988 Bytes
590a501
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
"""Platform module launcher — start/stop functional services on demand."""

from __future__ import annotations

import asyncio
import logging
import os
import signal
import subprocess
import sys
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any

logger = logging.getLogger(__name__)

WORKSPACE_ROOT = Path(__file__).resolve().parents[4]
WEB_ROOT = WORKSPACE_ROOT / "web_development"
FRONTEND_DIR = WEB_ROOT / "frontend"


@dataclass
class ServiceSpec:
    id: str
    name: str
    description: str
    kind: str  # embedded | process
    port: int | None = None
    url: str | None = None
    tags: list[str] = field(default_factory=list)


@dataclass
class ServiceStatus:
    id: str
    name: str
    description: str
    kind: str
    status: str  # running | stopped | error | unavailable
    port: int | None = None
    url: str | None = None
    pid: int | None = None
    message: str = ""
    tags: list[str] = field(default_factory=list)

    def to_dict(self) -> dict[str, Any]:
        return {
            "id": self.id,
            "name": self.name,
            "description": self.description,
            "kind": self.kind,
            "status": self.status,
            "port": self.port,
            "url": self.url,
            "pid": self.pid,
            "message": self.message,
            "tags": self.tags,
        }


SERVICE_CATALOG: list[ServiceSpec] = [
    ServiceSpec(
        id="trading_api",
        name="期货交易 API",
        description="FastAPI 后端:行情、下单、持仓、期货策略回测",
        kind="embedded",
        port=8000,
        url="http://localhost:8000/docs",
        tags=["futures", "core"],
    ),
    ServiceSpec(
        id="market_data",
        name="行情推送",
        description="模拟/实时行情 WebSocket 与 K 线生成",
        kind="embedded",
        tags=["futures", "market"],
    ),
    ServiceSpec(
        id="futures_strategies",
        name="期货量化策略",
        description="MA/Bollinger/DualThrust 策略引擎",
        kind="embedded",
        tags=["futures", "strategy"],
    ),
    ServiceSpec(
        id="qlib_research",
        name="Qlib 因子研究",
        description="因子公式库、算子构建、单因子 IC/回测、A 股策略",
        kind="embedded",
        tags=["research", "qlib", "stock"],
    ),
    ServiceSpec(
        id="frontend_ui",
        name="Web 可视化前端",
        description="Vue3 仪表盘、K 线、交易、研究面板",
        kind="process",
        port=5173,
        url="http://localhost:5173",
        tags=["ui"],
    ),
]


class PlatformManager:
    def __init__(self):
        self._processes: dict[str, subprocess.Popen] = {}
        self._qlib_research_enabled = False
        self._market_data_running = False
        self._futures_strategies_enabled = True

    def _spec(self, service_id: str) -> ServiceSpec:
        for s in SERVICE_CATALOG:
            if s.id == service_id:
                return s
        raise KeyError(f"Unknown service: {service_id}")

    def list_services(self) -> list[ServiceStatus]:
        return [self.get_status(s.id) for s in SERVICE_CATALOG]

    def get_status(self, service_id: str) -> ServiceStatus:
        spec = self._spec(service_id)

        if service_id == "trading_api":
            return ServiceStatus(
                id=spec.id, name=spec.name, description=spec.description, kind=spec.kind,
                status="running", port=spec.port, url=spec.url, message="当前进程即 Trading API",
                tags=spec.tags,
            )

        if service_id == "market_data":
            from app.services.market_data import market_data_service
            running = market_data_service._running
            return ServiceStatus(
                id=spec.id, name=spec.name, description=spec.description, kind=spec.kind,
                status="running" if running else "stopped",
                message="行情 WebSocket 推送" if running else "已停止",
                tags=spec.tags,
            )

        if service_id == "futures_strategies":
            from app.services.strategy_engine import strategy_engine
            n = len(strategy_engine._tasks)
            return ServiceStatus(
                id=spec.id, name=spec.name, description=spec.description, kind=spec.kind,
                status="running" if self._futures_strategies_enabled else "stopped",
                message=f"策略引擎就绪,{n} 个策略运行中",
                tags=spec.tags,
            )

        if service_id == "qlib_research":
            return ServiceStatus(
                id=spec.id, name=spec.name, description=spec.description, kind=spec.kind,
                status="running" if self._qlib_research_enabled else "stopped",
                url="/api/research/health",
                message="Qlib 因子/策略研究模块" if self._qlib_research_enabled else "未启动(按需加载 qlib)",
                tags=spec.tags,
            )

        if service_id == "frontend_ui":
            proc = self._processes.get("frontend_ui")
            if proc and proc.poll() is None:
                return ServiceStatus(
                    id=spec.id, name=spec.name, description=spec.description, kind=spec.kind,
                    status="running", port=spec.port, url=spec.url, pid=proc.pid,
                    message="Vue dev server", tags=spec.tags,
                )
            return ServiceStatus(
                id=spec.id, name=spec.name, description=spec.description, kind=spec.kind,
                status="stopped", port=spec.port, url=spec.url,
                message="前端未启动", tags=spec.tags,
            )

        return ServiceStatus(
            id=spec.id, name=spec.name, description=spec.description, kind=spec.kind,
            status="unavailable", message="Unknown service", tags=spec.tags,
        )

    async def start(self, service_id: str) -> ServiceStatus:
        spec = self._spec(service_id)

        if service_id == "trading_api":
            return self.get_status(service_id)

        if service_id == "market_data":
            from app.services.market_data import market_data_service
            if not market_data_service._running:
                await market_data_service.start()
            self._market_data_running = True
            return self.get_status(service_id)

        if service_id == "futures_strategies":
            self._futures_strategies_enabled = True
            return self.get_status(service_id)

        if service_id == "qlib_research":
            if str(WORKSPACE_ROOT) not in sys.path:
                sys.path.insert(0, str(WORKSPACE_ROOT))
            os.environ.setdefault("MLFLOW_ALLOW_FILE_STORE", "true")
            from data_pipeline.init_qlib import init_qlib
            loop = asyncio.get_event_loop()
            await loop.run_in_executor(None, init_qlib)
            self._qlib_research_enabled = True
            logger.info("Qlib research module enabled")
            return self.get_status(service_id)

        if service_id == "frontend_ui":
            proc = self._processes.get("frontend_ui")
            if proc and proc.poll() is None:
                return self.get_status(service_id)
            if not FRONTEND_DIR.exists():
                raise FileNotFoundError(f"Frontend not found: {FRONTEND_DIR}")
            proc = subprocess.Popen(
                ["npm", "run", "dev", "--", "--host", "0.0.0.0", "--port", "5173"],
                cwd=str(FRONTEND_DIR),
                stdout=subprocess.PIPE,
                stderr=subprocess.STDOUT,
                start_new_session=True,
            )
            self._processes["frontend_ui"] = proc
            await asyncio.sleep(2)
            return self.get_status(service_id)

        raise ValueError(f"Cannot start service: {service_id}")

    async def stop(self, service_id: str) -> ServiceStatus:
        if service_id == "market_data":
            from app.services.market_data import market_data_service
            await market_data_service.stop()
            self._market_data_running = False
            return self.get_status(service_id)

        if service_id == "futures_strategies":
            from app.services.strategy_engine import strategy_engine
            for sid in list(strategy_engine._tasks.keys()):
                strategy_engine.stop_strategy(sid)
            self._futures_strategies_enabled = False
            return self.get_status(service_id)

        if service_id == "qlib_research":
            self._qlib_research_enabled = False
            return self.get_status(service_id)

        if service_id == "frontend_ui":
            proc = self._processes.get("frontend_ui")
            if proc and proc.poll() is None:
                os.killpg(os.getpgid(proc.pid), signal.SIGTERM)
                proc.wait(timeout=5)
            self._processes.pop("frontend_ui", None)
            return self.get_status(service_id)

        if service_id == "trading_api":
            return ServiceStatus(
                **self.get_status(service_id).to_dict(),
                message="Trading API 由 uvicorn 管理,请停止 uvicorn 进程",
            )

        raise ValueError(f"Cannot stop service: {service_id}")

    async def start_all(self) -> list[ServiceStatus]:
        results = []
        for spec in SERVICE_CATALOG:
            if spec.id == "trading_api":
                results.append(self.get_status(spec.id))
                continue
            try:
                results.append(await self.start(spec.id))
            except Exception as exc:
                st = self.get_status(spec.id)
                st.status = "error"
                st.message = str(exc)
                results.append(st)
        return results

    @property
    def qlib_research_enabled(self) -> bool:
        return self._qlib_research_enabled


platform_manager = PlatformManager()