-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
327 lines (279 loc) · 9.9 KB
/
server.js
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
const express = require('express');
const path = require('path');
const { MongoClient } = require('mongodb');
const app = express();
const port = 3000;
const mongoose = require('mongoose');
const cors = require('cors');
const uri = "mongodb+srv://kunalsonne:kunalsonne1847724@cluster0.95mdg.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0";
const client = new MongoClient(uri);
let collection;
async function connectToDatabase() {
try {
await client.connect();
console.log('MongoDB connected successfully');
const db = client.db('home');
collection = db.collection('blogs');
} catch (err) {
console.error('Failed to connect to MongoDB:', err);
process.exit(1);
}
}
app.use(express.static('admin'));
app.use(express.json());
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'admin', 'admin.html'));
});
app.use(express.static('public'));
app.use(express.json());
app.get('/crowd', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.get('/data', async (req, res) => {
console.log('Received request to fetch data');
try {
console.log('Fetching data from MongoDB...');
const data = await collection.find({}).sort({ timestamp: -1 }).limit(50).toArray();
console.log('Data fetched successfully. Number of documents:', data.length);
let maxCrowd = 0;
let totalCrowd = 0;
let preferredShop = { lat: '', lng: '', count: Infinity };
data.forEach(item => {
if (item.count > maxCrowd) {
maxCrowd = item.count;
}
totalCrowd += item.count;
if (item.count < preferredShop.count) {
preferredShop = { lat: item.coordinates.latitude, lng: item.coordinates.longitude, count: item.count };
}
});
const averageCrowd = totalCrowd / data.length || 0;
res.json({
data,
maxCrowd,
averageCrowd: averageCrowd.toFixed(2),
preferredShop
});
} catch (err) {
console.error('Error fetching data from MongoDB:', err);
res.status(500).json({ error: 'Error fetching data from MongoDB' });
}
});
app.get('/history', async (req, res) => {
const { lat, lng, minutes } = req.query;
const timeRange = new Date(Date.now() - minutes * 60 * 1000);
try {
console.log(`Fetching historical data for lat: ${lat}, lng: ${lng}, minutes: ${minutes}`);
const data = await collection.find({
"coordinates.latitude": parseFloat(lat),
"coordinates.longitude": parseFloat(lng),
timestamp: { $gte: timeRange }
}).sort({ timestamp: -1 }).toArray();
console.log('Historical data fetched successfully. Number of documents:', data.length);
res.json(data);
} catch (err) {
console.error('Error fetching historical data from MongoDB:', err);
res.status(500).json({ error: 'Error fetching historical data from MongoDB' });
}
});
app.post('/update_data', async (req, res) => {
const { coordinates, count } = req.body;
try {
console.log('Inserting new data into MongoDB:', { coordinates, count });
const result = await collection.insertOne({
coordinates,
count,
timestamp: new Date()
});
console.log("Data inserted into MongoDB:", result);
const data = await collection.find({}).sort({ timestamp: -1 }).limit(50).toArray();
let maxCrowd = 0;
let totalCrowd = 0;
let preferredShop = { lat: '', lng: '', count: Infinity };
data.forEach(item => {
if (item.count > maxCrowd) {
maxCrowd = item.count;
}
totalCrowd += item.count;
if (item.count < preferredShop.count) {
preferredShop = { lat: item.coordinates.latitude, lng: item.coordinates.longitude, count: item.count };
}
});
const averageCrowd = totalCrowd / data.length || 0;
res.json({
status: "success",
maxCrowd,
averageCrowd: averageCrowd.toFixed(2),
preferredShop
});
} catch (err) {
console.error("Error updating MongoDB:", err);
res.status(500).json({ error: "Error updating MongoDB" });
}
});
const HistorySchema = new mongoose.Schema({
count: Number,
coordinates: {
latitude: Number,
longitude: Number
},
timestamp: { type: Date }
});
const History = mongoose.model('History', HistorySchema);
app.get('/history', async (req, res) => {
const { shop, time } = req.query;
if (!shop || !time) {
return res.status(400).json({ error: 'Shop and time parameters are required.' });
}
const currentTime = new Date();
const pastTime = new Date(currentTime - time * 60 * 1000);
const shopCoordinates = {
'Zudio': { latitude: 18.5204, longitude: 73.8567 },
'Pheonix Mall': { latitude: 18.5250, longitude: 73.8567 },
'Kaka Halwai': { latitude: 18.5369, longitude: 73.8567 },
'Shaniwar Peth': { latitude: 18.5650, longitude: 73.8567 },
'Starbucks': { latitude: 18.5850, longitude: 73.8567 }
};
const coordinates = shopCoordinates[shop];
if (!coordinates) {
return res.status(404).json({ error: 'Shop not found.' });
}
try {
const history = await History.find({
'coordinates.latitude': coordinates.latitude,
'coordinates.longitude': coordinates.longitude,
timestamp: { $gte: pastTime, $lte: currentTime }
}).sort({ timestamp: -1 });
if (history.length === 0) {
return res.json({ message: 'No data available for the selected time range.' });
}
res.json(history);
} catch (error) {
console.error('Error fetching history:', error);
res.status(500).json({ error: 'Server error' });
}
});
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors());
mongoose.connect('mongodb+srv://kunalsonne:kunalsonne1847724@cluster0.95mdg.mongodb.net/Auth', {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => {
console.log('Connected to MongoDB');
}).catch(err => {
console.error('MongoDB connection failed:', err);
});
const userSchema = new mongoose.Schema({
email: { type: String, required: true, unique: true },
password: { type: String, required: true }
});
const User = mongoose.model('User', userSchema);
const shopSchema = new mongoose.Schema({
shopName: String,
coordinates: String,
videoFeed: String
});
const Shop = mongoose.model('Shop', shopSchema);
app.use(express.static(path.join(__dirname, 'admin')));
// app.get('/', (req, res) => {
// res.sendFile(path.join(__dirname, 'admin', 'admin.html'));
// });
app.get('/addShop', (req, res) => {
res.sendFile(path.join(__dirname, 'admin', 'shopDetails.html'));
});
app.post('/signup', async (req, res) => {
const { email, password } = req.body;
try {
const newUser = new User({ email, password });
await newUser.save();
res.redirect('/crowd');
} catch (error) {
console.error('Error registering user:', error);
res.status(500).send('Error registering user');
}
});
app.post('/signin', async (req, res) => {
const { email, password } = req.body;
try {
const user = await User.findOne({ email });
if (!user) return res.status(404).send('User not found');
if (password === user.password) {
res.redirect('/crowd');
} else {
res.status(401).send('Invalid password');
}
} catch (error) {
console.error('Error logging in:', error);
res.status(500).send('Error logging in');
}
});
app.post('/addShop', async (req, res) => {
try {
const newShop = new Shop({
shopName: req.body.shopName,
coordinates: req.body.coordinates,
videoFeed: req.body.videoFeed
});
await newShop.save();
res.status(201).send('Shop details saved successfully!');
} catch (error) {
console.error('Error saving shop details:', error);
res.status(500).send('Error saving shop details');
}
});
app.get('/shops', async (req, res) => {
try {
const shops = await Shop.find();
res.status(200).json(shops);
} catch (error) {
console.error('Error fetching shop details:', error);
res.status(500).send('Error fetching shop details');
}
});
const crowdSchema = new mongoose.Schema({
count: Number,
coordinates: {
latitude: Number,
longitude: Number
},
timestamp: String
});
const Crowd = mongoose.model('Crowd', crowdSchema);
app.get('/api/top-shops', async (req, res) => {
try {
const topShops = await Crowd.aggregate([
{ $group: { _id: "$coordinates", totalCrowd: { $sum: "$count" } } },
{ $sort: { totalCrowd: -1 } },
{ $limit: 5 }
]);
res.json(topShops);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
app.get('/api/historical-data', async (req, res) => {
const { latitude, longitude } = req.query;
try {
const historicalData = await Crowd.find({
"coordinates.latitude": latitude,
"coordinates.longitude": longitude
}).sort({ timestamp: 1 }).limit(50);
res.json(historicalData);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
app.get('/api/heatmap', async (req, res) => {
try {
const heatmapData = await Crowd.find({}).select('coordinates count');
res.json(heatmapData);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
connectToDatabase().then(() => {
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
});