File size: 17,568 Bytes
a4a766c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI Campaign Monitor</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>

        .stage-active { background: linear-gradient(45deg, #007bff, #0056b3); color: white; }

        .stage-completed { background: linear-gradient(45deg, #28a745, #1e7e34); color: white; }

        .live-update { animation: slideIn 0.5s ease-out; }

        @keyframes slideIn {

            from { opacity: 0; transform: translateY(-20px); }

            to { opacity: 1; transform: translateY(0); }

        }

    </style>
</head>
<body class="bg-light">
    <div class="container-fluid py-4">
        <!-- Progress Header -->
        <div class="row mb-4">
            <div class="col-12">
                <div class="card shadow-sm">
                    <div class="card-body">
                        <div class="row align-items-center">
                            <div class="col-md-8">
                                <h2 id="campaignTitle">AI Campaign in Progress</h2>
                                <p class="text-muted" id="campaignSubtitle">Processing...</p>
                            </div>
                            <div class="col-md-4 text-end">
                                <div class="progress" style="height: 20px;">
                                    <div class="progress-bar" id="progressBar" style="width: 0%">0%</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Stage Indicators -->
        <div class="row mb-4">
            <div class="col-12">
                <div class="card shadow-sm">
                    <div class="card-body">
                        <h5>πŸ“Š Campaign Stages</h5>
                        <div class="row" id="stageIndicators">
                            <!-- Will be populated by JavaScript -->
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Real-time Metrics -->
        <div class="row mb-4">
            <div class="col-md-3">
                <div class="card text-center">
                    <div class="card-body">
                        <i class="fas fa-users fa-2x text-primary mb-2"></i>
                        <h4 id="influencerCount">0</h4>
                        <small>Influencers Found</small>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card text-center">
                    <div class="card-body">
                        <i class="fas fa-phone fa-2x text-success mb-2"></i>
                        <h4 id="callsCompleted">0</h4>
                        <small>Calls Completed</small>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card text-center">
                    <div class="card-body">
                        <i class="fas fa-handshake fa-2x text-warning mb-2"></i>
                        <h4 id="successfulDeals">0</h4>
                        <small>Successful Deals</small>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card text-center">
                    <div class="card-body">
                        <i class="fas fa-dollar-sign fa-2x text-info mb-2"></i>
                        <h4 id="totalCost">$0</h4>
                        <small>Total Cost</small>
                    </div>
                </div>
            </div>
        </div>

        <!-- Live Activity Feed -->
        <div class="row">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">
                        <h5>πŸ”΄ Live Activity Feed</h5>
                    </div>
                    <div class="card-body" style="height: 400px; overflow-y: auto;">
                        <div id="liveUpdates">
                            <div class="text-center py-4">
                                <div class="spinner-border text-primary"></div>
                                <div class="mt-2">Connecting...</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="card">
                    <div class="card-header">
                        <h5>⏱️ Timing Info</h5>
                    </div>
                    <div class="card-body">
                        <div class="mb-3">
                            <small class="text-muted">Started At</small>
                            <div id="startedAt">-</div>
                        </div>
                        <div class="mb-3">
                            <small class="text-muted">Duration</small>
                            <div id="duration">-</div>
                        </div>
                        <div class="mb-3">
                            <small class="text-muted">Estimated Remaining</small>
                            <div id="estimatedRemaining">-</div>
                        </div>
                        <div>
                            <small class="text-muted">Current Activity</small>
                            <div id="currentInfluencer">None</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <script>

        // Get the task ID - replace this with your actual task ID

        const TASK_ID = 'your-task-id-here'; // ⚠️ Replace with actual task ID

        

        class CampaignMonitor {

            constructor(taskId) {

                this.taskId = taskId;

                this.updateInterval = 5000; // 5 seconds

                this.isMonitoring = false;

                this.pollingId = null;

                

                this.stages = [

                    { key: 'webhook_received', name: 'Starting', icon: 'fas fa-play' },

                    { key: 'discovery', name: 'Discovery', icon: 'fas fa-search' },

                    { key: 'negotiations', name: 'Negotiations', icon: 'fas fa-phone' },

                    { key: 'contracts', name: 'Contracts', icon: 'fas fa-file-contract' },

                    { key: 'completed', name: 'Complete', icon: 'fas fa-check' }

                ];

                

                this.initializeUI();

            }



            initializeUI() {

                this.renderStageIndicators();

            }



            renderStageIndicators() {

                const container = document.getElementById('stageIndicators');

                container.innerHTML = this.stages.map(stage => `

                    <div class="col">

                        <div class="text-center p-3 rounded border" id="stage-${stage.key}">

                            <i class="${stage.icon} fa-lg mb-2"></i>

                            <div class="small">${stage.name}</div>

                        </div>

                    </div>

                `).join('');

            }



            async startMonitoring() {

                console.log(`πŸš€ Starting monitoring for: ${this.taskId}`);

                this.isMonitoring = true;

                

                // Initial fetch

                await this.fetchCampaignStatus();

                

                // Start polling

                this.pollingId = setInterval(async () => {

                    if (this.isMonitoring) {

                        await this.fetchCampaignStatus();

                    }

                }, this.updateInterval);

            }



            async fetchCampaignStatus() {

                try {

                    const response = await fetch(`/api/monitor/campaign/${this.taskId}`);

                    

                    if (!response.ok) {

                        throw new Error(`HTTP ${response.status}: ${response.statusText}`);

                    }

                    

                    const data = await response.json();

                    this.updateUI(data);

                    

                    if (data.is_complete) {

                        this.onCampaignComplete(data);

                    }

                    

                } catch (error) {

                    console.error('❌ Monitoring error:', error);

                    this.handleError(error);

                }

            }



            updateUI(data) {

                // Update header

                document.getElementById('campaignTitle').textContent = 

                    `${data.campaign_info.brand_name} - ${data.campaign_info.product_name}`;

                document.getElementById('campaignSubtitle').textContent = 

                    this.getStageDescription(data.current_stage, data.current_influencer);



                // Update progress bar

                const progressBar = document.getElementById('progressBar');

                progressBar.style.width = `${data.progress_percentage}%`;

                progressBar.textContent = `${Math.round(data.progress_percentage)}%`;



                // Update stage indicators

                this.updateStageIndicators(data.current_stage);



                // Update metrics

                const details = data.progress_details;

                document.getElementById('influencerCount').textContent = details.discovered_influencers;

                document.getElementById('callsCompleted').textContent = details.completed_negotiations;

                document.getElementById('successfulDeals').textContent = details.successful_negotiations;

                document.getElementById('totalCost').textContent = `$${details.total_cost.toLocaleString()}`;



                // Update live feed

                this.updateLiveFeed(data.live_updates);



                // Update timing

                const timing = data.timing;

                document.getElementById('startedAt').textContent = new Date(timing.started_at).toLocaleString();

                document.getElementById('duration').textContent = `${Math.round(timing.duration_so_far)} min`;

                document.getElementById('estimatedRemaining').textContent = timing.estimated_remaining;

                document.getElementById('currentInfluencer').textContent = data.current_influencer || 'None';

            }



            updateStageIndicators(currentStage) {

                this.stages.forEach(stage => {

                    const element = document.getElementById(`stage-${stage.key}`);

                    element.className = 'text-center p-3 rounded border';

                    

                    if (stage.key === currentStage) {

                        element.classList.add('stage-active');

                    } else if (this.isStageCompleted(stage.key, currentStage)) {

                        element.classList.add('stage-completed');

                    }

                });

            }



            updateLiveFeed(updates) {

                const container = document.getElementById('liveUpdates');

                

                // Clear loading state

                if (container.innerHTML.includes('Connecting')) {

                    container.innerHTML = '';

                }

                

                // Add new updates

                updates.forEach(update => {

                    if (!this.isUpdateDisplayed(update)) {

                        const updateElement = this.createUpdateElement(update);

                        container.insertBefore(updateElement, container.firstChild);

                    }

                });

                

                // Limit to last 10 updates

                while (container.children.length > 10) {

                    container.removeChild(container.lastChild);

                }

            }



            createUpdateElement(update) {

                const div = document.createElement('div');

                div.className = 'border-bottom pb-2 mb-2 live-update';

                div.innerHTML = `

                    <div class="d-flex justify-content-between">

                        <div>${this.formatUpdate(update)}</div>

                        <small class="text-muted">${new Date().toLocaleTimeString()}</small>

                    </div>

                `;

                return div;

            }



            formatUpdate(update) {

                if (update.includes('βœ…')) return `<span class="text-success">${update}</span>`;

                if (update.includes('❌')) return `<span class="text-danger">${update}</span>`;

                if (update.includes('πŸ“ž')) return `<span class="text-primary">${update}</span>`;

                if (update.includes('πŸ”')) return `<span class="text-info">${update}</span>`;

                return update;

            }



            getStageDescription(stage, currentInfluencer) {

                const descriptions = {

                    'webhook_received': 'Initializing campaign workflow...',

                    'discovery': 'Analyzing and discovering potential influencers...',

                    'negotiations': currentInfluencer ? 

                        `Currently negotiating with ${currentInfluencer}...` : 

                        'Conducting influencer negotiations...',

                    'contracts': 'Generating contracts and finalizing deals...',

                    'completed': 'Campaign completed successfully!'

                };

                return descriptions[stage] || 'Processing...';

            }



            isStageCompleted(stage, currentStage) {

                const stageOrder = this.stages.map(s => s.key);

                const currentIndex = stageOrder.indexOf(currentStage);

                const stageIndex = stageOrder.indexOf(stage);

                return stageIndex < currentIndex;

            }



            isUpdateDisplayed(update) {

                const container = document.getElementById('liveUpdates');

                return Array.from(container.children).some(child => 

                    child.textContent.includes(update.substring(0, 50))

                );

            }



            onCampaignComplete(data) {

                this.stopMonitoring();

                

                // Show success message

                const container = document.getElementById('liveUpdates');

                const completionMsg = document.createElement('div');

                completionMsg.className = 'alert alert-success';

                completionMsg.innerHTML = `

                    <h5>πŸŽ‰ Campaign Complete!</h5>

                    <p>Successfully completed ${data.progress_details.successful_negotiations} deals 

                       for $${data.progress_details.total_cost.toLocaleString()}</p>

                `;

                container.insertBefore(completionMsg, container.firstChild);

            }



            handleError(error) {

                const container = document.getElementById('liveUpdates');

                const errorMsg = document.createElement('div');

                errorMsg.className = 'alert alert-warning';

                errorMsg.innerHTML = `❌ Connection error: ${error.message}. Retrying...`;

                container.insertBefore(errorMsg, container.firstChild);

            }



            stopMonitoring() {

                this.isMonitoring = false;

                if (this.pollingId) {

                    clearInterval(this.pollingId);

                    this.pollingId = null;

                }

                console.log('⏹️ Campaign monitoring stopped');

            }

        }



        // Initialize when page loads

        document.addEventListener('DOMContentLoaded', function() {

            // Get task ID from URL parameters or use the constant

            const urlParams = new URLSearchParams(window.location.search);

            const taskId = urlParams.get('task_id') || TASK_ID;

            

            if (taskId && taskId !== 'your-task-id-here') {

                const monitor = new CampaignMonitor(taskId);

                monitor.startMonitoring();

                

                // Stop monitoring when page closes

                window.addEventListener('beforeunload', () => {

                    monitor.stopMonitoring();

                });

            } else {

                document.getElementById('liveUpdates').innerHTML = `

                    <div class="alert alert-info">

                        <h5>ℹ️ Setup Required</h5>

                        <p>Please provide a task_id parameter in the URL or update the TASK_ID constant in the JavaScript.</p>

                        <p><strong>Example:</strong> monitor.html?task_id=your-actual-task-id</p>

                    </div>

                `;

            }

        });

    </script>
</body>
</html>