-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
258 lines (217 loc) · 10.4 KB
/
App.tsx
File metadata and controls
258 lines (217 loc) · 10.4 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
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
import React, { useState, useCallback, useRef } from 'react';
import { Play, RotateCcw, Box, Layers, Settings, Activity, Trash2 } from 'lucide-react';
import { LogConsole } from './components/LogConsole';
import { TestRunnerCard } from './components/TestRunnerCard';
import { GeminiAnalysis } from './components/GeminiAnalysis';
import { MockAutomationRunner, generateLog } from './services/mockAutomationService';
import { generateHeuristicReport } from './services/geminiService';
import { LogEntry, TestCaseState, TestStage } from './types';
// Constants
const TARGET_VS = "backend-vs-t1r_1000-1";
export default function App() {
const [isRunning, setIsRunning] = useState(false);
const [globalLogs, setGlobalLogs] = useState<LogEntry[]>([]);
const [analysisReport, setAnalysisReport] = useState<string | null>(null);
const [isAnalyzing, setIsAnalyzing] = useState(false);
// Two parallel test cases state
const [testCases, setTestCases] = useState<TestCaseState[]>([
{ id: 't1', name: 'Thread-1: Disable VS', status: TestStage.IDLE, logs: [], targetVs: TARGET_VS, progress: 0 },
{ id: 't2', name: 'Thread-2: Validate Status', status: TestStage.IDLE, logs: [], targetVs: TARGET_VS, progress: 0 }
]);
const automationRunner = useRef(new MockAutomationRunner());
const addLog = useCallback((source: string, message: string, level: LogEntry['level'] = 'INFO') => {
const newLog = generateLog(source, message, level);
setGlobalLogs(prev => [...prev, newLog]);
// Also update specific test case logs if source matches
if (source.startsWith('Thread-')) {
const threadId = source.includes('1') ? 't1' : 't2';
setTestCases(prev => prev.map(tc =>
tc.id === threadId ? { ...tc, logs: [...tc.logs, newLog] } : tc
));
}
}, []);
const updateTestStatus = (id: string, status: TestStage, progress: number) => {
setTestCases(prev => prev.map(tc => tc.id === id ? { ...tc, status, progress } : tc));
};
const runTestCase = async (testId: string, threadName: string) => {
const runner = automationRunner.current;
try {
// Stage 0: Init
updateTestStatus(testId, TestStage.PRE_FETCHER, 10);
addLog(threadName, "Starting Test Case Execution...", 'INFO');
// Stage 1: Pre-Fetcher
addLog(threadName, "PRE-FETCHER: Fetching Tenants, SEs, and Virtual Services...", 'INFO');
const fetchLogs = await runner.fetchResources(threadName);
fetchLogs.forEach(l => addLog(threadName, l, 'INFO'));
// Mock SSH
const sshLog = await runner.runSSHStub();
addLog(threadName, sshLog, 'MOCK');
updateTestStatus(testId, TestStage.PRE_VALIDATION, 35);
// Stage 2: Pre-Validation
addLog(threadName, `PRE-VALIDATION: Looking for target VS: ${TARGET_VS}`, 'INFO');
const vs = await runner.getVirtualService(TARGET_VS);
if (!vs) {
throw new Error(`Virtual Service ${TARGET_VS} not found!`);
}
addLog(threadName, `Found VS UUID: ${vs.uuid}. Enabled: ${vs.enabled}`, 'SUCCESS');
if (testId === 't1') {
// Thread 1 Logic: Disable the VS
updateTestStatus(testId, TestStage.TASK_TRIGGER, 60);
// Mock RDP
const rdpLog = await runner.runRDPStub();
addLog(threadName, rdpLog, 'MOCK');
addLog(threadName, `TASK: Sending PUT to disable VS ${vs.uuid}...`, 'WARN');
await runner.updateVirtualService(vs.uuid, false);
addLog(threadName, "TASK: Configuration update successful.", 'SUCCESS');
updateTestStatus(testId, TestStage.POST_VALIDATION, 85);
addLog(threadName, "POST-VALIDATION: Verifying state change...", 'INFO');
const updatedVs = await runner.getVirtualService(vs.uuid);
if (updatedVs?.enabled === false) {
addLog(threadName, "VALIDATION PASSED: VS is disabled.", 'SUCCESS');
updateTestStatus(testId, TestStage.COMPLETED, 100);
} else {
throw new Error("Validation Failed: VS is still enabled!");
}
} else {
// Thread 2 Logic: Just monitors (Read-only test case simulation)
// Wait for Thread 1 to do something roughly
updateTestStatus(testId, TestStage.TASK_TRIGGER, 60);
addLog(threadName, "TASK: Monitoring active connections...", 'INFO');
await runner.runSSHStub(); // another delay
updateTestStatus(testId, TestStage.POST_VALIDATION, 85);
addLog(threadName, "POST-VALIDATION: Confirming VS accessibility...", 'INFO');
const updatedVs = await runner.getVirtualService(vs.uuid);
addLog(threadName, `Current Status of ${TARGET_VS}: ${updatedVs?.enabled ? 'UP' : 'DOWN'}`, 'INFO');
updateTestStatus(testId, TestStage.COMPLETED, 100);
}
} catch (e: any) {
addLog(threadName, e.message, 'ERROR');
updateTestStatus(testId, TestStage.FAILED, 100);
}
};
const handleStartAutomation = async () => {
if (isRunning) return;
setIsRunning(true);
setGlobalLogs([]);
setAnalysisReport(null);
setTestCases(prev => prev.map(tc => ({ ...tc, status: TestStage.IDLE, logs: [], progress: 0 })));
addLog('Main', 'Initializing Framework...', 'INFO');
addLog('Main', 'Loading YAML configuration...', 'INFO');
// Auth Step
try {
const token = await automationRunner.current.login("admin");
addLog('Main', `Authenticated successfully. Token: ${token.substr(0, 10)}...`, 'SUCCESS');
} catch (e) {
addLog('Main', 'Authentication Failed', 'ERROR');
setIsRunning(false);
return;
}
addLog('Main', `Spawning ${testCases.length} parallel threads...`, 'INFO');
// Run parallel
await Promise.all([
runTestCase('t1', 'Thread-1'),
runTestCase('t2', 'Thread-2')
]);
addLog('Main', 'All test cases completed execution.', 'INFO');
setIsRunning(false);
};
const handleReset = () => {
setIsRunning(false);
setGlobalLogs([]);
setAnalysisReport(null);
setTestCases([
{ id: 't1', name: 'Thread-1: Disable VS', status: TestStage.IDLE, logs: [], targetVs: TARGET_VS, progress: 0 },
{ id: 't2', name: 'Thread-2: Validate Status', status: TestStage.IDLE, logs: [], targetVs: TARGET_VS, progress: 0 }
]);
};
const handleGenerateReport = async () => {
setIsAnalyzing(true);
const result = await generateHeuristicReport(globalLogs);
setAnalysisReport(result);
setIsAnalyzing(false);
};
return (
<div className="min-h-screen bg-slate-950 text-slate-200 pb-12">
{/* Header */}
<header className="bg-slate-900 border-b border-slate-800 sticky top-0 z-50">
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 h-16 flex items-center justify-between">
<div className="flex items-center gap-3">
<div className="bg-blue-600 p-2 rounded-lg">
<Box className="w-6 h-6 text-white" />
</div>
<div>
<h1 className="text-xl font-bold text-white tracking-tight">Avi AutoDash</h1>
<p className="text-xs text-slate-400">VMware Load Balancer Automation Framework</p>
</div>
</div>
<div className="flex items-center gap-4">
<span className="text-xs bg-slate-800 border border-slate-700 px-3 py-1 rounded-full text-slate-400 font-mono">
Env: Mock / v10.2.1
</span>
<button className="p-2 text-slate-400 hover:text-white transition-colors">
<Settings className="w-5 h-5" />
</button>
</div>
</div>
</header>
<main className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
{/* Controls */}
<div className="mb-8 flex flex-col md:flex-row gap-6 items-start md:items-center justify-between bg-slate-900/50 p-6 rounded-xl border border-slate-800">
<div className="flex flex-col gap-2">
<h2 className="text-lg font-semibold text-white flex items-center gap-2">
<Layers className="w-5 h-5 text-blue-400" />
Execution Control
</h2>
<p className="text-sm text-slate-400 max-w-lg">
Simulates parallel execution of Python automation scripts against the Avi Controller API.
Verifies <code className="bg-slate-800 px-1 rounded text-blue-300">{TARGET_VS}</code> configuration.
</p>
</div>
<div className="flex gap-3 w-full md:w-auto">
<button
onClick={handleStartAutomation}
disabled={isRunning}
className={`flex-1 md:flex-none flex items-center justify-center gap-2 px-6 py-3 rounded-lg font-semibold shadow-lg transition-all
${isRunning
? 'bg-slate-800 text-slate-500 cursor-not-allowed'
: 'bg-blue-600 hover:bg-blue-500 text-white hover:shadow-blue-500/20'
}`}
>
{isRunning ? <RotateCcw className="w-5 h-5 animate-spin" /> : <Play className="w-5 h-5" />}
{isRunning ? 'Running...' : 'Start Automation'}
</button>
<button
onClick={handleReset}
disabled={isRunning}
className="flex-1 md:flex-none flex items-center justify-center gap-2 px-6 py-3 rounded-lg font-semibold bg-slate-800 text-slate-300 hover:bg-slate-700 hover:text-white transition-all border border-slate-700 disabled:opacity-50 disabled:cursor-not-allowed"
>
<Trash2 className="w-5 h-5" />
Reset All
</button>
</div>
</div>
{/* Test Runners Grid */}
<div className="grid grid-cols-1 lg:grid-cols-2 gap-6 mb-8">
{testCases.map(tc => (
<TestRunnerCard key={tc.id} testState={tc} />
))}
</div>
{/* Real-time Logs */}
<div className="grid grid-cols-1 gap-6">
<div className="flex items-center gap-2 mb-2">
<Activity className="w-5 h-5 text-slate-400" />
<h2 className="text-lg font-semibold text-white">Execution Logs</h2>
</div>
<LogConsole logs={globalLogs} className="h-[450px]" />
</div>
{/* Report Integration */}
<GeminiAnalysis
analysis={analysisReport}
loading={isAnalyzing}
onAnalyze={handleGenerateReport}
canAnalyze={globalLogs.length > 0 && !isRunning}
/>
</main>
</div>
);
}