feat(android): implement Phase 1.3 rolling window safety
- Add DailyNotificationRollingWindow with capacity-aware scheduling - Implement iOS capacity limits (64 pending, 20 daily) vs Android (100, 50) - Add automatic window maintenance every 15 minutes - Add manual maintenance triggers and statistics API - Integrate rolling window with TTL enforcer and scheduler - Add comprehensive unit tests for rolling window functionality - Add rolling window methods to TypeScript interface - Add phase1-3-rolling-window.ts usage examples This completes Phase 1 core infrastructure: - Today's remaining notifications are always armed - Tomorrow's notifications armed only if within iOS caps - Automatic window maintenance prevents notification gaps - Platform-specific capacity management prevents limits - Integration with existing TTL enforcement and scheduling Files: 7 changed, 928 insertions(+)
This commit is contained in:
224
examples/phase1-3-rolling-window.ts
Normal file
224
examples/phase1-3-rolling-window.ts
Normal file
@@ -0,0 +1,224 @@
|
||||
/**
|
||||
* Phase 1.3 Rolling Window Safety Usage Example
|
||||
*
|
||||
* Demonstrates rolling window safety functionality
|
||||
* Shows how notifications are maintained for today and tomorrow
|
||||
*
|
||||
* @author Matthew Raymer
|
||||
* @version 1.0.0
|
||||
*/
|
||||
|
||||
import { DailyNotification } from '@timesafari/daily-notification-plugin';
|
||||
|
||||
/**
|
||||
* Example: Configure rolling window safety
|
||||
*/
|
||||
async function configureRollingWindowSafety() {
|
||||
try {
|
||||
console.log('Configuring rolling window safety...');
|
||||
|
||||
// Configure with rolling window settings
|
||||
await DailyNotification.configure({
|
||||
storage: 'shared',
|
||||
ttlSeconds: 1800, // 30 minutes TTL
|
||||
prefetchLeadMinutes: 15,
|
||||
maxNotificationsPerDay: 20 // iOS limit
|
||||
});
|
||||
|
||||
console.log('✅ Rolling window safety configured');
|
||||
|
||||
// The plugin will now automatically:
|
||||
// - Keep today's remaining notifications armed
|
||||
// - Arm tomorrow's notifications if within iOS caps
|
||||
// - Maintain window state every 15 minutes
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Rolling window configuration failed:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Example: Manual rolling window maintenance
|
||||
*/
|
||||
async function manualRollingWindowMaintenance() {
|
||||
try {
|
||||
console.log('Triggering manual rolling window maintenance...');
|
||||
|
||||
// Force window maintenance (useful for testing)
|
||||
await DailyNotification.maintainRollingWindow();
|
||||
|
||||
console.log('✅ Rolling window maintenance completed');
|
||||
|
||||
// This will:
|
||||
// - Arm today's remaining notifications
|
||||
// - Arm tomorrow's notifications if within capacity
|
||||
// - Update window state and statistics
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Manual maintenance failed:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Example: Check rolling window statistics
|
||||
*/
|
||||
async function checkRollingWindowStats() {
|
||||
try {
|
||||
console.log('Checking rolling window statistics...');
|
||||
|
||||
// Get rolling window statistics
|
||||
const stats = await DailyNotification.getRollingWindowStats();
|
||||
|
||||
console.log('📊 Rolling Window Statistics:');
|
||||
console.log(` Stats: ${stats.stats}`);
|
||||
console.log(` Maintenance Needed: ${stats.maintenanceNeeded}`);
|
||||
console.log(` Time Until Next Maintenance: ${stats.timeUntilNextMaintenance}ms`);
|
||||
|
||||
// Example output:
|
||||
// Stats: Rolling window stats: pending=5/100, daily=3/50, platform=Android
|
||||
// Maintenance Needed: false
|
||||
// Time Until Next Maintenance: 450000ms (7.5 minutes)
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Rolling window stats check failed:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Example: Schedule multiple notifications with rolling window
|
||||
*/
|
||||
async function scheduleMultipleNotifications() {
|
||||
try {
|
||||
console.log('Scheduling multiple notifications with rolling window...');
|
||||
|
||||
// Configure rolling window safety
|
||||
await configureRollingWindowSafety();
|
||||
|
||||
// Schedule notifications for different times
|
||||
const notifications = [
|
||||
{ time: '08:00', title: 'Morning Update', body: 'Good morning!' },
|
||||
{ time: '12:00', title: 'Lunch Reminder', body: 'Time for lunch!' },
|
||||
{ time: '18:00', title: 'Evening Summary', body: 'End of day summary' },
|
||||
{ time: '22:00', title: 'Good Night', body: 'Time to rest' }
|
||||
];
|
||||
|
||||
for (const notification of notifications) {
|
||||
await DailyNotification.scheduleDailyNotification({
|
||||
url: 'https://api.example.com/daily-content',
|
||||
time: notification.time,
|
||||
title: notification.title,
|
||||
body: notification.body
|
||||
});
|
||||
}
|
||||
|
||||
console.log('✅ Multiple notifications scheduled');
|
||||
|
||||
// The rolling window will ensure:
|
||||
// - All future notifications today are armed
|
||||
// - Tomorrow's notifications are armed if within iOS caps
|
||||
// - Window state is maintained automatically
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Multiple notification scheduling failed:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Example: Demonstrate iOS capacity limits
|
||||
*/
|
||||
async function demonstrateIOSCapacityLimits() {
|
||||
try {
|
||||
console.log('Demonstrating iOS capacity limits...');
|
||||
|
||||
// Configure with iOS-like limits
|
||||
await DailyNotification.configure({
|
||||
storage: 'shared',
|
||||
ttlSeconds: 3600, // 1 hour TTL
|
||||
prefetchLeadMinutes: 30,
|
||||
maxNotificationsPerDay: 20 // iOS limit
|
||||
});
|
||||
|
||||
// Schedule many notifications to test capacity
|
||||
const notifications = [];
|
||||
for (let i = 0; i < 25; i++) {
|
||||
notifications.push({
|
||||
time: `${8 + i}:00`,
|
||||
title: `Notification ${i + 1}`,
|
||||
body: `This is notification number ${i + 1}`
|
||||
});
|
||||
}
|
||||
|
||||
for (const notification of notifications) {
|
||||
await DailyNotification.scheduleDailyNotification({
|
||||
url: 'https://api.example.com/daily-content',
|
||||
time: notification.time,
|
||||
title: notification.title,
|
||||
body: notification.body
|
||||
});
|
||||
}
|
||||
|
||||
console.log('✅ Many notifications scheduled');
|
||||
|
||||
// Check statistics to see capacity management
|
||||
const stats = await DailyNotification.getRollingWindowStats();
|
||||
console.log('📊 Capacity Management:', stats.stats);
|
||||
|
||||
// The rolling window will:
|
||||
// - Arm notifications up to the daily limit
|
||||
// - Skip additional notifications if at capacity
|
||||
// - Log capacity violations for debugging
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ iOS capacity demonstration failed:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Example: Monitor rolling window over time
|
||||
*/
|
||||
async function monitorRollingWindowOverTime() {
|
||||
try {
|
||||
console.log('Monitoring rolling window over time...');
|
||||
|
||||
// Configure rolling window
|
||||
await configureRollingWindowSafety();
|
||||
|
||||
// Schedule some notifications
|
||||
await scheduleMultipleNotifications();
|
||||
|
||||
// Monitor window state over time
|
||||
const monitorInterval = setInterval(async () => {
|
||||
try {
|
||||
const stats = await DailyNotification.getRollingWindowStats();
|
||||
console.log('📊 Window State:', stats.stats);
|
||||
|
||||
if (stats.maintenanceNeeded) {
|
||||
console.log('⚠️ Maintenance needed, triggering...');
|
||||
await DailyNotification.maintainRollingWindow();
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Monitoring error:', error);
|
||||
}
|
||||
}, 60000); // Check every minute
|
||||
|
||||
// Stop monitoring after 5 minutes
|
||||
setTimeout(() => {
|
||||
clearInterval(monitorInterval);
|
||||
console.log('✅ Monitoring completed');
|
||||
}, 300000);
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Rolling window monitoring failed:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// Export examples for use
|
||||
export {
|
||||
configureRollingWindowSafety,
|
||||
manualRollingWindowMaintenance,
|
||||
checkRollingWindowStats,
|
||||
scheduleMultipleNotifications,
|
||||
demonstrateIOSCapacityLimits,
|
||||
monitorRollingWindowOverTime
|
||||
};
|
||||
Reference in New Issue
Block a user