-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorchestrator.py
More file actions
227 lines (188 loc) · 6.96 KB
/
Copy pathorchestrator.py
File metadata and controls
227 lines (188 loc) · 6.96 KB
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
"""
orchestrator.py — 2x2 TUI dashboard.
"""
import os
import asyncio
from textual.app import App, ComposeResult
from textual.binding import Binding
from textual.containers import Horizontal, Vertical
from textual.widgets import Header, Footer, Static, Log
BASE = os.path.dirname(os.path.abspath(__file__))
SCRIPTS = {
"Tweet Fetcher": ["python", os.path.join(BASE, "nlp_pipeline", "initial_fetch.py")],
"NLP Processor": ["python", os.path.join(BASE, "nlp_pipeline", "NLProcessing.py")],
"Node Server": ["node", os.path.join(BASE, "backend", "server.js")],
"Frontend": ["python", "-m", "http.server", "8080",
"--directory", os.path.join(BASE, "frontend", "public")],
}
FETCHER_ERROR_PHRASES = ["402", "Payment Required", "credits", "Unauthorized", "401", "ERROR"]
PANEL_COLORS = {
"Tweet Fetcher": "#00aaff", # blue
"NLP Processor": "#00cc66", # green
"Node Server": "#ff9900", # amber
"Frontend": "#cc44ff", # purple
}
def to_id(name: str) -> str:
return name.replace(' ', '-').lower()
class Panel(Vertical):
def __init__(self, script_name, *args, **kwargs):
super().__init__(*args, **kwargs)
self.script_name = script_name
self.pid = to_id(script_name)
def compose(self) -> ComposeResult:
yield Static(
f" ● {self.script_name.upper()} [dim]waiting[/dim]",
id=f"s-{self.pid}",
classes="bar"
)
yield Log(id=f"l-{self.pid}", auto_scroll=True)
def set_status(self, state: str):
icons = {
"running": ("●", "green", "RUNNING"),
"stopped": ("■", "red", "STOPPED"),
"skipped": ("▶", "yellow", "SKIPPED"),
"error": ("▲", "red", "ERROR"),
"waiting": ("○", "white", "WAITING"),
}
dot, color, label = icons.get(state, ("○", "white", "WAITING"))
self.query_one(f"#s-{self.pid}", Static).update(
f" [{color}]{dot}[/{color}] {self.script_name.upper()} [dim]{label}[/dim]"
)
def log(self, line: str):
w = self.query_one(f"#l-{self.pid}", Log)
w.write_line(line)
class Orchestrator(App):
BINDINGS = [
Binding("f", "skip_fetcher", "Skip Fetcher", show=True),
Binding("q", "quit", "Quit", show=True),
]
CSS = f"""
Screen {{
background: #0a0a0a;
}}
Header {{
background: #111111;
color: #888888;
height: 1;
dock: top;
}}
Footer {{
background: #111111;
color: #555555;
height: 1;
dock: bottom;
}}
/* 2x2 grid */
#row-top, #row-bot {{
height: 1fr;
}}
Panel {{
border: solid #2a2a2a;
margin: 0;
padding: 0;
}}
/* Per-panel accent colors on the title bar */
#tweet-fetcher .bar {{ background: #001a2e; color: {PANEL_COLORS["Tweet Fetcher"]}; }}
#nlp-processor .bar {{ background: #001a0f; color: {PANEL_COLORS["NLP Processor"]}; }}
#node-server .bar {{ background: #1a1000; color: {PANEL_COLORS["Node Server"]}; }}
#frontend .bar {{ background: #110022; color: {PANEL_COLORS["Frontend"]}; }}
.bar {{
height: 1;
padding: 0 1;
text-style: bold;
}}
Log {{
height: 1fr;
background: #0d0d0d;
color: #aaaaaa;
scrollbar-size: 1 1;
scrollbar-color: #333333;
text-style: none;
padding: 0 1;
}}
/* Small text via padding trick — Textual doesn't support font-size
but keeping padding tight gives a dense feel */
"""
def __init__(self):
super().__init__()
self.nlp_ready = asyncio.Event()
self.node_ready = asyncio.Event()
self._fetcher_proc = None
def compose(self) -> ComposeResult:
yield Header(show_clock=True)
with Vertical():
with Horizontal(id="row-top"):
yield Panel("Tweet Fetcher", id="tweet-fetcher")
yield Panel("NLP Processor", id="nlp-processor")
with Horizontal(id="row-bot"):
yield Panel("Node Server", id="node-server")
yield Panel("Frontend", id="frontend")
yield Footer()
async def on_mount(self):
asyncio.create_task(self.run_fetcher())
asyncio.create_task(self.run_script(
"NLP Processor", SCRIPTS["NLP Processor"],
wait=self.nlp_ready,
trigger=self.node_ready, trigger_phrase="Sleeping 30s"))
asyncio.create_task(self.run_script(
"Node Server", SCRIPTS["Node Server"],
wait=self.node_ready))
asyncio.create_task(self.run_script(
"Frontend", SCRIPTS["Frontend"]))
# Fetcher with auto-bypass
async def run_fetcher(self):
panel = self.query_one("#tweet-fetcher", Panel)
panel.set_status("running")
proc = await asyncio.create_subprocess_exec(
*SCRIPTS["Tweet Fetcher"],
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.STDOUT
)
self._fetcher_proc = proc
auto_bypassed = False
async for raw in proc.stdout:
line = raw.decode(errors="ignore").rstrip()
panel.log(line)
if not auto_bypassed and any(p in line for p in FETCHER_ERROR_PHRASES):
panel.log("⚠ API error — auto-skipping to NLP")
panel.set_status("error")
auto_bypassed = True
self.nlp_ready.set()
self.node_ready.set()
await proc.wait()
if not auto_bypassed:
panel.set_status("stopped")
self.nlp_ready.set()
self.node_ready.set()
# Manual bypass using F key
def action_skip_fetcher(self):
panel = self.query_one("#tweet-fetcher", Panel)
if self.nlp_ready.is_set():
panel.log("ℹ Already past fetcher stage")
return
if self._fetcher_proc and self._fetcher_proc.returncode is None:
self._fetcher_proc.terminate()
panel.log("▶ Manually skipped — using existing Supabase data")
panel.set_status("skipped")
self.nlp_ready.set()
self.node_ready.set()
# Generic runner
async def run_script(self, name, cmd, wait=None, trigger=None, trigger_phrase=None):
panel = self.query_one(f"#{to_id(name)}", Panel)
if wait:
await wait.wait()
panel.set_status("running")
proc = await asyncio.create_subprocess_exec(
*cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.STDOUT
)
async for raw in proc.stdout:
line = raw.decode(errors="ignore").rstrip()
panel.log(line)
if trigger and trigger_phrase and trigger_phrase in line:
trigger.set()
await proc.wait()
panel.set_status("stopped")
if __name__ == "__main__":
Orchestrator().run()