-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservices.js
More file actions
138 lines (121 loc) · 3.36 KB
/
Copy pathservices.js
File metadata and controls
138 lines (121 loc) · 3.36 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
const API_BASE = window.location.hostname === 'localhost' ? 'http://localhost:5000' : '';
let socket = null;
let eventListeners = {};
async function fetcher(url, options = {}) {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 15000);
try {
const res = await fetch(url, {
...options,
signal: controller.signal,
mode: 'cors'
});
clearTimeout(timeoutId);
return res;
} catch (e) {
clearTimeout(timeoutId);
throw e;
}
}
const api = {
async getEvents(params = {}) {
try {
let url = `${API_BASE}/api/events`;
const queryParams = [];
if (params.category) queryParams.push(`category=${params.category}`);
if (params.lat !== undefined) queryParams.push(`lat=${params.lat}`);
if (params.lng !== undefined) queryParams.push(`lng=${params.lng}`);
if (params.radius) queryParams.push(`radius=${params.radius}`);
if (queryParams.length) url += '?' + queryParams.join('&');
const res = await fetcher(url);
return await res.json();
} catch (e) {
console.error('Failed to fetch events:', e);
return [];
}
},
async getRegions() {
try {
const res = await fetcher(`${API_BASE}/api/regions`);
return await res.json();
} catch (e) {
console.error('Failed to fetch regions:', e);
return [];
}
},
async getStats() {
try {
const res = await fetcher(`${API_BASE}/api/stats`);
return await res.json();
} catch (e) {
console.error('Failed to fetch stats:', e);
return null;
}
},
async summarize(eventId) {
try {
const res = await fetcher(`${API_BASE}/api/summarize`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ event_id: eventId })
});
return await res.json();
} catch (e) {
return null;
}
},
async analyzeSentiment(eventId) {
try {
const res = await fetcher(`${API_BASE}/api/sentiment`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ event_id: eventId })
});
return await res.json();
} catch (e) {
return null;
}
},
async getWeather(lat, lng) {
try {
const res = await fetcher(`${API_BASE}/api/weather/${lat}/${lng}`);
return await res.json();
} catch (e) {
return null;
}
}
};
function initSocketIO() {
if (typeof io === 'undefined') {
console.warn('Socket.IO not loaded');
return;
}
socket = io(API_BASE, {
transports: ['websocket', 'polling'],
reconnection: true
});
socket.on('connect', () => {
console.log('WebSocket connected');
document.getElementById('liveIndicator')?.classList.add('active');
});
socket.on('disconnect', () => {
document.getElementById('liveIndicator')?.classList.remove('active');
});
socket.on('update', (data) => {
if (eventListeners['update']) {
eventListeners['update'].forEach(cb => cb(data));
}
});
}
const socketIO = {
on(event, callback) {
if (!eventListeners[event]) eventListeners[event] = [];
eventListeners[event].push(callback);
},
emit(event, data) {
if (socket) socket.emit(event, data);
}
};
window.api = api;
window.socketIO = socketIO;
document.addEventListener('DOMContentLoaded', initSocketIO);