-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
711 lines (659 loc) · 19.9 KB
/
database.js
File metadata and controls
711 lines (659 loc) · 19.9 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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
const mongoose = require('mongoose');
// MongoDB Connection with optimized settings
const connectDB = async () => {
try {
const conn = await mongoose.connect(process.env.MONGODB_URI, {
// Connection pool settings for better performance
maxPoolSize: 10,
minPoolSize: 2,
socketTimeoutMS: 45000,
serverSelectionTimeoutMS: 5000,
// Faster failover
heartbeatFrequencyMS: 2000,
// Optimize for read/write operations
retryWrites: true,
retryReads: true,
// Compression for better network performance
compressors: ['zlib'],
});
console.log(`✅ MongoDB Connected: ${conn.connection.host}`);
// Connection event handlers for monitoring
mongoose.connection.on('error', (err) => {
console.error('❌ MongoDB connection error:', err);
});
mongoose.connection.on('disconnected', () => {
console.warn('⚠️ MongoDB disconnected. Attempting to reconnect...');
});
mongoose.connection.on('reconnected', () => {
console.log('✅ MongoDB reconnected');
});
return conn;
} catch (error) {
console.error('❌ MongoDB Connection Error:', error.message);
// In production, you might want to retry instead of exiting
if (process.env.NODE_ENV === 'production') {
console.log('Retrying connection in 5 seconds...');
setTimeout(connectDB, 5000);
} else {
process.exit(1);
}
}
};
// Optimized Conversation Schema
const conversationSchema = new mongoose.Schema({
sessionId: {
type: String,
required: true,
index: true,
trim: true
},
messages: [{
role: {
type: String,
enum: ['user', 'assistant'],
required: true
},
content: {
type: String,
required: true,
maxlength: 5000 // Prevent extremely long messages
},
timestamp: {
type: Date,
default: Date.now,
index: true
},
isDental: {
type: Boolean,
default: false,
index: true
},
// Add token count for analytics
tokens: {
type: Number,
default: 0
}
}],
userInfo: {
ip: {
type: String,
select: false // Don't return IP by default for privacy
},
userAgent: String,
location: String,
// Add browser/device info
device: {
type: String,
enum: ['mobile', 'tablet', 'desktop', 'unknown'],
default: 'unknown'
}
},
// Track conversation metadata
metadata: {
totalMessages: {
type: Number,
default: 0
},
lastActivity: {
type: Date,
default: Date.now
},
isActive: {
type: Boolean,
default: true,
index: true
}
}
}, {
timestamps: true,
minimize: true
});
// Optimized Appointment Schema
const appointmentSchema = new mongoose.Schema({
sessionId: {
type: String,
required: true,
index: true,
trim: true
},
patientInfo: {
name: {
type: String,
trim: true,
maxlength: 100
},
phone: {
type: String,
trim: true,
// Simple validation
validate: {
validator: function(v) {
return !v || /^[\d\s\-\+\(\)]+$/.test(v);
},
message: 'Invalid phone number format'
}
},
email: {
type: String,
trim: true,
lowercase: true,
// Simple email validation
validate: {
validator: function(v) {
return !v || /^[\w\-\.]+@([\w\-]+\.)+[\w\-]{2,4}$/.test(v);
},
message: 'Invalid email format'
}
},
dateOfBirth: Date,
isNewPatient: {
type: Boolean,
default: true
}
},
appointmentDetails: {
preferredDate: String,
preferredTime: String,
dayOfWeek: String,
reason: {
type: String,
maxlength: 500
},
urgency: {
type: String,
enum: ['routine', 'urgent', 'emergency'],
default: 'routine',
index: true
},
// Add appointment type
type: {
type: String,
enum: ['checkup', 'cleaning', 'consultation', 'treatment', 'emergency', 'other'],
default: 'other'
}
},
confirmationNumber: {
type: String,
unique: true,
index: true
},
status: {
type: String,
enum: ['pending', 'confirmed', 'cancelled', 'completed', 'no-show'],
default: 'pending',
index: true
},
// Track status changes
statusHistory: [{
status: String,
changedAt: {
type: Date,
default: Date.now
},
changedBy: String,
note: String
}],
notes: {
type: String,
maxlength: 1000
},
// Add confirmation tracking
confirmationSent: {
type: Boolean,
default: false
},
confirmedAt: Date,
// Add reminder tracking
reminderSent: {
type: Boolean,
default: false
},
todayReminderSent: {
type: Boolean,
default: false
},
// Add priority flag
priority: {
type: Number,
min: 1,
max: 5,
default: 3
}
}, {
timestamps: true,
minimize: true
});
// Optimized Analytics Schema
const analyticsSchema = new mongoose.Schema({
date: {
type: Date,
required: true,
index: true,
unique: true // Prevent duplicate daily records
},
metrics: {
totalMessages: {
type: Number,
default: 0,
min: 0
},
totalSessions: {
type: Number,
default: 0,
min: 0
},
appointmentRequests: {
type: Number,
default: 0,
min: 0
},
averageResponseTime: {
type: Number,
default: 0,
min: 0
},
// Add more metrics
uniqueUsers: {
type: Number,
default: 0,
min: 0
},
dentalQueries: {
type: Number,
default: 0,
min: 0
},
peakHour: {
type: Number,
min: 0,
max: 23
},
conversionRate: {
type: Number,
default: 0,
min: 0,
max: 100
}
},
topQuestions: [{
question: {
type: String,
maxlength: 200
},
count: {
type: Number,
min: 0
},
category: String
}],
// Add hourly distribution
hourlyDistribution: {
type: Map,
of: Number
}
}, {
timestamps: true,
minimize: true
});
// User Feedback Schema with improvements
const feedbackSchema = new mongoose.Schema({
sessionId: {
type: String,
required: true,
index: true,
trim: true
},
messageId: {
type: String,
trim: true
},
rating: {
type: Number,
min: 1,
max: 5,
required: true,
index: true
},
feedback: {
type: String,
maxlength: 1000,
trim: true
},
category: {
type: String,
enum: ['helpful', 'unhelpful', 'inaccurate', 'incomplete', 'other'],
index: true
},
// Add tags for categorization
tags: [{
type: String,
trim: true
}],
// Track if feedback was reviewed
reviewed: {
type: Boolean,
default: false,
index: true
},
reviewedAt: Date,
reviewedBy: String,
// Response to feedback
response: String
}, {
timestamps: true,
minimize: true
});
// Compound indexes for complex queries
conversationSchema.index({ sessionId: 1, 'metadata.lastActivity': -1 });
conversationSchema.index({ 'messages.isDental': 1, createdAt: -1 }, { sparse: true });
conversationSchema.index({ 'metadata.isActive': 1, 'metadata.lastActivity': -1 });
appointmentSchema.index({ status: 1, createdAt: -1 });
appointmentSchema.index({ 'appointmentDetails.urgency': 1, createdAt: -1 });
appointmentSchema.index({ status: 1, 'appointmentDetails.urgency': 1 });
analyticsSchema.index({ date: -1 });
feedbackSchema.index({ rating: 1, createdAt: -1 });
feedbackSchema.index({ reviewed: 1, createdAt: -1 });
// Add pre-save hooks for automatic updates
conversationSchema.pre('save', function(next) {
this.metadata.totalMessages = this.messages.length;
this.metadata.lastActivity = new Date();
next();
});
appointmentSchema.pre('save', function(next) {
// Auto-calculate priority based on urgency
if (this.appointmentDetails.urgency === 'emergency') {
this.priority = 5;
} else if (this.appointmentDetails.urgency === 'urgent') {
this.priority = 4;
}
next();
});
// Add instance methods
conversationSchema.methods.addMessage = function(role, content, isDental = false) {
this.messages.push({ role, content, isDental, timestamp: new Date() });
this.metadata.totalMessages = this.messages.length;
this.metadata.lastActivity = new Date();
return this.save();
};
appointmentSchema.methods.updateStatus = function(newStatus, changedBy = 'system', note = '') {
this.statusHistory.push({
status: this.status,
changedAt: new Date(),
changedBy,
note
});
this.status = newStatus;
if (newStatus === 'confirmed') {
this.confirmedAt = new Date();
}
return this.save();
};
// Add static methods for common queries
conversationSchema.statics.findActiveConversations = function(limit = 100) {
return this.find({ 'metadata.isActive': true })
.sort({ 'metadata.lastActivity': -1 })
.limit(limit)
.select('-userInfo.ip')
.lean();
};
appointmentSchema.statics.findPendingAppointments = function() {
return this.find({ status: 'pending' })
.sort({ priority: -1, createdAt: 1 })
.lean();
};
appointmentSchema.statics.findUrgentAppointments = function() {
return this.find({
'appointmentDetails.urgency': { $in: ['urgent', 'emergency'] },
status: 'pending'
})
.sort({ priority: -1, createdAt: 1 })
.lean();
};
// Create Models
const Conversation = mongoose.model('Conversation', conversationSchema);
const Appointment = mongoose.model('Appointment', appointmentSchema);
const Analytics = mongoose.model('Analytics', analyticsSchema);
const Feedback = mongoose.model('Feedback', feedbackSchema);
// Optimized Helper Functions with error handling and validation
const saveConversation = async (sessionId, userMessage, assistantResponse, userInfo = {}, isDental = false) => {
try {
// Validate inputs
if (!sessionId || !userMessage || !assistantResponse) {
throw new Error('Missing required parameters for saveConversation');
}
// Extract device type from user agent
const device = userInfo.userAgent ?
(/mobile/i.test(userInfo.userAgent) ? 'mobile' :
/tablet/i.test(userInfo.userAgent) ? 'tablet' : 'desktop') : 'unknown';
const conversation = await Conversation.findOneAndUpdate(
{ sessionId },
{
$push: {
messages: {
$each: [
{
role: 'user',
content: userMessage.slice(0, 5000), // Enforce max length
timestamp: new Date(),
isDental
},
{
role: 'assistant',
content: assistantResponse.slice(0, 5000),
timestamp: new Date(),
isDental
}
]
}
},
$set: {
'userInfo.ip': userInfo.ip,
'userInfo.userAgent': userInfo.userAgent,
'userInfo.location': userInfo.location,
'userInfo.device': device,
'metadata.lastActivity': new Date(),
'metadata.isActive': true,
updatedAt: new Date()
},
$inc: {
'metadata.totalMessages': 2
}
},
{
upsert: true,
new: true,
// Only return necessary fields
select: '-userInfo.ip -__v'
}
);
return conversation;
} catch (error) {
console.error('Error saving conversation:', error);
// Don't throw - log and continue
return null;
}
};
const saveAppointment = async (appointmentData) => {
try {
// Validate required fields
if (!appointmentData.sessionId) {
throw new Error('Session ID is required for appointments');
}
const appointment = new Appointment(appointmentData);
await appointment.save();
console.log('📅 Appointment saved:', appointment._id);
return appointment;
} catch (error) {
console.error('Error saving appointment:', error);
// Re-throw for caller to handle
throw error;
}
};
const getConversationHistory = async (sessionId, limit = 10) => {
try {
if (!sessionId) {
return [];
}
const conversation = await Conversation.findOne({ sessionId })
.select('messages')
.lean()
.maxTimeMS(5000); // Timeout after 5 seconds
if (!conversation || !conversation.messages) {
return [];
}
// Return only the most recent messages
return conversation.messages.slice(-Math.min(limit, 50));
} catch (error) {
console.error('Error fetching conversation:', error);
return [];
}
};
const updateAnalytics = async (date = new Date()) => {
try {
const startOfDay = new Date(date);
startOfDay.setHours(0, 0, 0, 0);
const endOfDay = new Date(date);
endOfDay.setHours(23, 59, 59, 999);
// Use aggregation pipeline for efficiency with single pass
const [aggregatedStats, appointmentCount] = await Promise.all([
Conversation.aggregate([
{
$match: {
createdAt: { $gte: startOfDay, $lte: endOfDay }
}
},
{
$facet: {
messageStats: [
{
$project: {
messageCount: { $size: '$messages' },
dentalMessages: {
$size: {
$filter: {
input: '$messages',
as: 'msg',
cond: { $eq: ['$$msg.isDental', true] }
}
}
}
}
},
{
$group: {
_id: null,
totalMessages: { $sum: '$messageCount' },
dentalQueries: { $sum: '$dentalMessages' },
uniqueSessions: { $sum: 1 }
}
}
],
sessionCount: [
{ $count: 'total' }
]
}
}
]),
Appointment.countDocuments({
createdAt: { $gte: startOfDay, $lte: endOfDay }
})
]);
const stats = aggregatedStats[0]?.messageStats[0] || {
totalMessages: 0,
dentalQueries: 0,
uniqueSessions: 0
};
const sessionCount = aggregatedStats[0]?.sessionCount[0]?.total || 0;
// Calculate conversion rate
const conversionRate = stats.uniqueSessions > 0
? parseFloat((appointmentCount / stats.uniqueSessions * 100).toFixed(2))
: 0;
await Analytics.findOneAndUpdate(
{ date: startOfDay },
{
$set: {
'metrics.totalMessages': stats.totalMessages,
'metrics.totalSessions': sessionCount,
'metrics.appointmentRequests': appointmentCount,
'metrics.uniqueUsers': stats.uniqueSessions,
'metrics.dentalQueries': stats.dentalQueries,
'metrics.conversionRate': conversionRate
}
},
{
upsert: true,
new: true,
setDefaultsOnInsert: true
}
);
return true;
} catch (error) {
console.error('Error updating analytics:', error);
return false;
}
};
const saveFeedback = async (feedbackData) => {
try {
// Validate required fields
if (!feedbackData.sessionId || !feedbackData.rating) {
throw new Error('Session ID and rating are required for feedback');
}
const feedback = new Feedback(feedbackData);
await feedback.save();
return feedback;
} catch (error) {
console.error('Error saving feedback:', error);
throw error;
}
};
// Add cleanup function for old data with batch processing
const cleanupOldData = async (daysToKeep = 90) => {
try {
const cutoffDate = new Date();
cutoffDate.setDate(cutoffDate.getDate() - daysToKeep);
// Delete in batches to avoid memory issues with large datasets
let totalDeleted = 0;
let batchDeleted = 0;
const batchSize = 1000;
do {
const result = await Conversation.deleteMany({
'metadata.lastActivity': { $lt: cutoffDate },
'metadata.isActive': false
}).limit(batchSize);
batchDeleted = result.deletedCount;
totalDeleted += batchDeleted;
// Small delay between batches to avoid overwhelming the database
if (batchDeleted === batchSize) {
await new Promise(resolve => setTimeout(resolve, 100));
}
} while (batchDeleted === batchSize);
console.log(`🧹 Cleaned up ${totalDeleted} old conversations`);
return totalDeleted;
} catch (error) {
console.error('Error cleaning up old data:', error);
return 0;
}
};
// Graceful shutdown
const closeConnection = async () => {
try {
await mongoose.connection.close();
console.log('📴 MongoDB connection closed');
} catch (error) {
console.error('Error closing MongoDB connection:', error);
}
};
module.exports = {
connectDB,
closeConnection,
Conversation,
Appointment,
Analytics,
Feedback,
saveConversation,
saveAppointment,
getConversationHistory,
updateAnalytics,
saveFeedback,
cleanupOldData
};