-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe54-analytics-granularity.js
More file actions
345 lines (271 loc) · 12.5 KB
/
Copy pathe54-analytics-granularity.js
File metadata and controls
345 lines (271 loc) · 12.5 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
/**
* Analytics Granularity Examples
*
* Demonstrates how to query analytics with different time granularities:
* - Month broken down by days
* - Day broken down by hours
* - Week broken down by days
* - Year broken down by months
*/
import S3db from '../src/s3db.class.js';
import { EventualConsistencyPlugin } from '../src/plugins/eventual-consistency/index.js';
async function main() {
const s3db = new S3db({
connectionString: "s3://test:test@granularity-demo/analytics",
enableCache: false
});
await s3db.connect();
// Create wallets resource
const wallets = await s3db.createResource({
name: 'wallets',
attributes: {
id: 'string|required',
balance: 'number|default:0'
}
});
// Enable analytics
const plugin = new EventualConsistencyPlugin({
resource: 'wallets',
field: 'balance',
mode: 'sync',
autoConsolidate: false,
enableAnalytics: true
});
await s3db.usePlugin(plugin);
// Simulate transactions across different hours and days
console.log('Creating sample transactions...\n');
await wallets.insert({ id: 'w1', balance: 0 });
// Simulate 20 transactions
for (let i = 0; i < 20; i++) {
const amount = Math.floor(Math.random() * 100) + 10;
await wallets.add('w1', amount);
}
await wallets.consolidate('w1');
console.log('✅ Transactions created and consolidated\n');
// Get plugin reference
const analyticsPlugin = s3db.plugins.find(p => p instanceof EventualConsistencyPlugin);
// ========================================
// 1. Day broken down by hours
// ========================================
console.log('═'.repeat(70));
console.log('📊 TODAY - HOUR BY HOUR');
console.log('═'.repeat(70));
const today = new Date().toISOString().substring(0, 10);
const todayByHour = await analyticsPlugin.getDayByHour('wallets', 'balance', today);
console.log(`\nDate: ${today}`);
console.log(`Total Hours with Transactions: ${todayByHour.length}\n`);
todayByHour.forEach(hour => {
const hourNum = hour.cohort.substring(11);
console.log(` ${hourNum}:00 → ${hour.count.toString().padStart(3)} transactions, $${hour.sum.toString().padStart(6)}, avg: $${hour.avg.toFixed(2)}`);
});
// ========================================
// 2. Last 7 days broken down by days
// ========================================
console.log('\n' + '═'.repeat(70));
console.log('📊 LAST 7 DAYS - DAY BY DAY');
console.log('═'.repeat(70));
const last7Days = await analyticsPlugin.getLastNDays('wallets', 'balance', 7);
console.log(`\nPeriod: Last 7 days`);
console.log(`Total Days with Transactions: ${last7Days.length}\n`);
last7Days.forEach(day => {
const dayName = new Date(day.cohort).toLocaleDateString('en-US', { weekday: 'short' });
console.log(` ${dayName} ${day.cohort} → ${day.count.toString().padStart(3)} txns, $${day.sum.toString().padStart(6)}, avg: $${day.avg.toFixed(2)}`);
});
// Calculate week totals
const weekTotal = last7Days.reduce((acc, day) => ({
count: acc.count + day.count,
sum: acc.sum + day.sum
}), { count: 0, sum: 0 });
console.log(`\n Week Total: ${weekTotal.count} transactions, $${weekTotal.sum}`);
console.log(` Daily Average: ${(weekTotal.count / 7).toFixed(1)} transactions, $${(weekTotal.sum / 7).toFixed(2)}`);
// ========================================
// 3. Month broken down by days
// ========================================
console.log('\n' + '═'.repeat(70));
console.log('📊 THIS MONTH - DAY BY DAY');
console.log('═'.repeat(70));
const currentMonth = new Date().toISOString().substring(0, 7); // YYYY-MM
const monthByDay = await analyticsPlugin.getMonthByDay('wallets', 'balance', currentMonth);
console.log(`\nMonth: ${currentMonth}`);
console.log(`Total Days with Transactions: ${monthByDay.length}\n`);
// Show first 10 days
const daysToShow = Math.min(10, monthByDay.length);
monthByDay.slice(0, daysToShow).forEach(day => {
console.log(` ${day.cohort} → ${day.count.toString().padStart(3)} txns, $${day.sum.toString().padStart(6)}, avg: $${day.avg.toFixed(2)}`);
});
if (monthByDay.length > 10) {
console.log(` ... (${monthByDay.length - 10} more days)`);
}
// Month totals
const monthTotal = monthByDay.reduce((acc, day) => ({
count: acc.count + day.count,
sum: acc.sum + day.sum
}), { count: 0, sum: 0 });
console.log(`\n Month Total: ${monthTotal.count} transactions, $${monthTotal.sum}`);
// ========================================
// 4. Last month broken down by hours
// ========================================
console.log('\n' + '═'.repeat(70));
console.log('📊 LAST MONTH - HOUR BY HOUR');
console.log('═'.repeat(70));
const lastMonthByHour = await analyticsPlugin.getMonthByHour('wallets', 'balance', 'last');
console.log(`\nLast Month Total Hours with Transactions: ${lastMonthByHour.length}`);
console.log('(Showing first 24 hours and statistics)\n');
// Show first 24 hours
const hoursToShow = Math.min(24, lastMonthByHour.length);
lastMonthByHour.slice(0, hoursToShow).forEach(hour => {
const date = hour.cohort.substring(0, 10);
const time = hour.cohort.substring(11);
console.log(` ${date} ${time}:00 → ${hour.count.toString().padStart(3)} txns, $${hour.sum.toString().padStart(6)}`);
});
if (lastMonthByHour.length > 24) {
console.log(` ... (${lastMonthByHour.length - 24} more hours)`);
}
// Calculate month totals
const lastMonthTotal = lastMonthByHour.reduce((acc, hour) => ({
count: acc.count + hour.count,
sum: acc.sum + hour.sum
}), { count: 0, sum: 0 });
console.log(`\n Last Month Total: ${lastMonthTotal.count} transactions, $${lastMonthTotal.sum}`);
console.log(` Hourly Average: ${(lastMonthTotal.count / Math.max(lastMonthByHour.length, 1)).toFixed(1)} transactions, $${(lastMonthTotal.sum / Math.max(lastMonthByHour.length, 1)).toFixed(2)}`);
// ========================================
// 5. Year broken down by months
// ========================================
console.log('\n' + '═'.repeat(70));
console.log('📊 THIS YEAR - MONTH BY MONTH');
console.log('═'.repeat(70));
const currentYear = new Date().getFullYear();
const yearByMonth = await analyticsPlugin.getYearByMonth('wallets', 'balance', currentYear);
console.log(`\nYear: ${currentYear}`);
console.log(`Total Months with Transactions: ${yearByMonth.length}\n`);
yearByMonth.forEach(month => {
const monthName = new Date(month.cohort + '-01').toLocaleDateString('en-US', { month: 'long' });
console.log(` ${monthName.padEnd(10)} → ${month.count.toString().padStart(4)} txns, $${month.sum.toString().padStart(8)}, avg: $${month.avg.toFixed(2)}`);
});
// Year totals
const yearTotal = yearByMonth.reduce((acc, month) => ({
count: acc.count + month.count,
sum: acc.sum + month.sum
}), { count: 0, sum: 0 });
console.log(`\n Year Total: ${yearTotal.count} transactions, $${yearTotal.sum}`);
// ========================================
// 6. Custom granularity - Business hours
// ========================================
console.log('\n' + '═'.repeat(70));
console.log('📊 TODAY - BUSINESS HOURS ONLY (9am-5pm)');
console.log('═'.repeat(70));
const allHours = await analyticsPlugin.getDayByHour('wallets', 'balance', today);
const businessHours = allHours.filter(h => {
const hour = parseInt(h.cohort.substring(11, 13));
return hour >= 9 && hour <= 17;
});
console.log(`\nBusiness Hours: 9am - 5pm`);
console.log(`Hours with Transactions: ${businessHours.length}\n`);
businessHours.forEach(hour => {
const hourNum = hour.cohort.substring(11);
console.log(` ${hourNum}:00 → ${hour.count.toString().padStart(3)} txns, $${hour.sum.toString().padStart(6)}`);
});
// Business hours totals
const businessTotal = businessHours.reduce((acc, h) => ({
count: acc.count + h.count,
sum: acc.sum + h.sum
}), { count: 0, sum: 0 });
console.log(`\n Business Hours Total: ${businessTotal.count} transactions, $${businessTotal.sum}`);
// ========================================
// 7. Chart-Ready Data with fillGaps
// ========================================
console.log('\n' + '═'.repeat(70));
console.log('📊 CHART-READY DATA - LAST 7 DAYS WITH GAPS FILLED');
console.log('═'.repeat(70));
// Without fillGaps (sparse - only days with transactions)
const last7DaysSparse = await analyticsPlugin.getLastNDays('wallets', 'balance', 7);
console.log(`\nWithout fillGaps: ${last7DaysSparse.length} days with transactions`);
// With fillGaps (continuous - all 7 days guaranteed)
const last7DaysFilled = await analyticsPlugin.getLastNDays('wallets', 'balance', 7, {
fillGaps: true // Perfect for charts!
});
console.log(`With fillGaps: ${last7DaysFilled.length} days (guaranteed)`);
console.log('\nLast 7 Days (Chart-Ready):');
last7DaysFilled.forEach(day => {
const dayName = new Date(day.cohort).toLocaleDateString('en-US', { weekday: 'short' });
const bar = '█'.repeat(Math.floor(day.count / 2) || 1);
console.log(` ${dayName} ${day.cohort} → ${day.count.toString().padStart(3)} txns ${bar}`);
});
// Perfect for Chart.js
const chartLabels = last7DaysFilled.map(d => d.cohort);
const chartData = last7DaysFilled.map(d => d.count);
console.log(`\n Chart.js Ready:`);
console.log(` labels: [${chartLabels.map(l => `'${l}'`).join(', ')}]`);
console.log(` data: [${chartData.join(', ')}]`);
// ========================================
// 8. Custom range - Specific week
// ========================================
console.log('\n' + '═'.repeat(70));
console.log('📊 CUSTOM RANGE - SPECIFIC WEEK (Mon-Sun)');
console.log('═'.repeat(70));
// Get current week's Monday
const now = new Date();
const dayOfWeek = now.getDay();
const diffToMonday = (dayOfWeek === 0 ? -6 : 1) - dayOfWeek;
const monday = new Date(now);
monday.setDate(now.getDate() + diffToMonday);
const sunday = new Date(monday);
sunday.setDate(monday.getDate() + 6);
const weekStart = monday.toISOString().substring(0, 10);
const weekEnd = sunday.toISOString().substring(0, 10);
const customWeek = await analyticsPlugin.getAnalytics('wallets', 'balance', {
period: 'day',
startDate: weekStart,
endDate: weekEnd
});
console.log(`\nWeek: ${weekStart} to ${weekEnd}`);
console.log(`Days with Transactions: ${customWeek.length}\n`);
customWeek.forEach((day, idx) => {
const dayNames = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
console.log(` ${dayNames[idx]} ${day.cohort} → ${day.count.toString().padStart(3)} txns, $${day.sum.toString().padStart(6)}`);
});
// ========================================
// Summary
// ========================================
console.log('\n' + '═'.repeat(70));
console.log('📋 SUMMARY');
console.log('═'.repeat(70));
console.log(`
Available Granularity Methods (all support fillGaps option):
1. getDayByHour(resource, field, date, { fillGaps })
→ Returns up to 24 hourly records for a specific day
→ With fillGaps: Always 24 hours (00:00-23:00)
2. getLastNDays(resource, field, days, { fillGaps })
→ Returns N daily records going back from today
→ With fillGaps: Always N days (no gaps)
3. getMonthByDay(resource, field, 'YYYY-MM', { fillGaps })
→ Returns daily records for entire month (28-31 days)
→ With fillGaps: Always 28-31 days (complete month)
4. getMonthByHour(resource, field, 'YYYY-MM' or 'last', { fillGaps })
→ Returns hourly records for entire month (up to 744 hours)
→ With fillGaps: Always 672-744 hours (complete month)
5. getYearByMonth(resource, field, year, { fillGaps })
→ Returns 12 monthly records for entire year
→ With fillGaps: Always 12 months (Jan-Dec)
6. getAnalytics(resource, field, options)
→ Flexible queries with custom date ranges
→ No fillGaps support (use helper methods instead)
All methods return pre-calculated aggregations:
• count: Number of transactions
• sum: Total value (signed)
• avg: Average transaction value
• min: Minimum transaction value
• max: Maximum transaction value
• operations: Breakdown by add/sub/set
• recordCount: Distinct records
fillGaps option:
• Fills missing periods with zeros
• Perfect for continuous charts (Chart.js, D3.js, etc.)
• No performance penalty (~1ms to fill gaps)
• Works with all time periods (hour, day, month)
Performance: ~2ms per query (regardless of transaction count!)
`);
await s3db.disconnect();
console.log('✅ Done!\n');
}
main().catch(console.error);