-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd-redis-test-data.js
More file actions
122 lines (104 loc) Β· 4.34 KB
/
Copy pathadd-redis-test-data.js
File metadata and controls
122 lines (104 loc) Β· 4.34 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
import redis from 'redis';
async function addTestDataToRedis() {
console.log('π Adding test data to Redis for web interface demo...\n');
try {
const client = redis.createClient({
url: 'redis://localhost:6380'
});
await client.connect();
console.log('β
Connected to Redis');
// Add sample Housy data
console.log('π Adding Housy Tunisia sample data...');
// 1. User session
await client.setEx('housy:session:admin', 3600, JSON.stringify({
userId: 1,
username: 'admin',
email: 'admin@housy.tn',
role: 'admin',
loginTime: new Date().toISOString(),
permissions: ['read', 'write', 'admin']
}));
console.log('β
Added admin session');
// 2. Project cache
await client.setEx('housy:cache:projects:list', 300, JSON.stringify({
projects: [
{ id: 1, name: 'Villa Moderne Tunis', status: 'active', budget: 150000 },
{ id: 2, name: 'Immeuble Sousse', status: 'planning', budget: 300000 },
{ id: 3, name: 'Bureau Commercial', status: 'completed', budget: 80000 }
],
totalCount: 3,
cachedAt: new Date().toISOString()
}));
console.log('β
Added projects cache');
// 3. User notifications
const notifications = [
{ message: 'Project "Villa Moderne" status updated to active', type: 'info', timestamp: new Date().toISOString() },
{ message: 'New quotation requires approval', type: 'warning', timestamp: new Date().toISOString() },
{ message: 'Payment received for project #123', type: 'success', timestamp: new Date().toISOString() }
];
for (const notification of notifications) {
await client.lPush('housy:notifications:user:1', JSON.stringify(notification));
}
console.log('β
Added user notifications');
// 4. Background tasks queue
const tasks = [
{ type: 'email', data: { to: 'client@example.tn', subject: 'Project Update', projectId: 1 }},
{ type: 'pdf_generation', data: { quotationId: 123, clientName: 'Ahmed Ben Ali' }},
{ type: 'backup', data: { table: 'projects', timestamp: new Date().toISOString() }}
];
for (const task of tasks) {
await client.lPush('housy:queue:background', JSON.stringify(task));
}
console.log('β
Added background tasks');
// 5. Material prices cache
await client.hSet('housy:cache:materials:prices', {
'cement': '12.50',
'brick': '0.85',
'steel': '1.20',
'concrete': '45.00',
'tile': '25.00'
});
console.log('β
Added material prices');
// 6. System statistics
await client.setEx('housy:stats:daily', 86400, JSON.stringify({
date: new Date().toISOString().split('T')[0],
activeProjects: 15,
completedToday: 2,
newQuotations: 5,
totalRevenue: 125000,
activeUsers: 8
}));
console.log('β
Added daily statistics');
// 7. Rate limiting counters
await client.incr('housy:ratelimit:api:user:1');
await client.expire('housy:ratelimit:api:user:1', 60);
console.log('β
Added rate limiting data');
// 8. Search cache
await client.setEx('housy:search:projects:villa', 600, JSON.stringify({
query: 'villa',
results: [
{ id: 1, name: 'Villa Moderne Tunis', score: 0.95 },
{ id: 4, name: 'Villa Luxe Hammamet', score: 0.87 }
],
count: 2,
searchTime: '15ms'
}));
console.log('β
Added search cache');
await client.disconnect();
console.log('\nπ Sample data added successfully!');
console.log('\nπ Now you can view Redis data in your browser:');
console.log(' URL: http://localhost:8081');
console.log('\nπ You should see these keys:');
console.log(' β’ housy:session:admin (User session)');
console.log(' β’ housy:cache:projects:list (Project cache)');
console.log(' β’ housy:notifications:user:1 (Notifications list)');
console.log(' β’ housy:queue:background (Task queue)');
console.log(' β’ housy:cache:materials:prices (Material prices hash)');
console.log(' β’ housy:stats:daily (Daily statistics)');
console.log(' β’ housy:ratelimit:api:user:1 (Rate limiting)');
console.log(' β’ housy:search:projects:villa (Search cache)');
} catch (error) {
console.error('β Error adding test data:', error);
}
}
addTestDataToRedis();