feat(android)!: implement Phase 2 Android core with WorkManager + AlarmManager + SQLite
- Add complete SQLite schema with Room database (content_cache, schedules, callbacks, history) - Implement WorkManager FetchWorker with exponential backoff and network constraints - Add AlarmManager NotifyReceiver with TTL-at-fire logic and notification delivery - Create BootReceiver for automatic rescheduling after device reboot - Update AndroidManifest.xml with necessary permissions and receivers - Add Room, WorkManager, and Kotlin coroutines dependencies to build.gradle feat(callback-registry)!: implement callback registry with circuit breaker - Add CallbackRegistryImpl with HTTP, local, and queue callback support - Implement circuit breaker pattern with exponential backoff retry logic - Add CallbackEvent interface with structured event types - Support for exactly-once delivery semantics with retry queue - Include callback status monitoring and health checks feat(observability)!: add comprehensive observability and health monitoring - Implement ObservabilityManager with structured logging and event codes - Add performance metrics tracking (fetch, notify, callback times) - Create health status API with circuit breaker monitoring - Include log compaction and metrics reset functionality - Support for DNP-* event codes throughout the system feat(web)!: enhance web implementation with new functionality - Integrate callback registry and observability into web platform - Add mock implementations for dual scheduling methods - Implement performance tracking and structured logging - Support for local callback registration and management - Enhanced error handling and event logging BREAKING CHANGE: New Android dependencies require Room, WorkManager, and Kotlin coroutines
This commit is contained in:
283
src/callback-registry.ts
Normal file
283
src/callback-registry.ts
Normal file
@@ -0,0 +1,283 @@
|
||||
/**
|
||||
* Callback Registry Implementation
|
||||
* Provides uniform callback lifecycle usable from any platform
|
||||
*
|
||||
* @author Matthew Raymer
|
||||
* @version 1.1.0
|
||||
*/
|
||||
|
||||
export type CallbackKind = 'http' | 'local' | 'queue';
|
||||
|
||||
export interface CallbackEvent {
|
||||
id: string;
|
||||
at: number;
|
||||
type: 'onFetchStart' | 'onFetchSuccess' | 'onFetchFailure' |
|
||||
'onNotifyStart' | 'onNotifyDelivered' | 'onNotifySkippedTTL' | 'onNotifyFailure';
|
||||
payload?: unknown;
|
||||
}
|
||||
|
||||
export type CallbackFunction = (e: CallbackEvent) => Promise<void> | void;
|
||||
|
||||
export interface CallbackRecord {
|
||||
id: string;
|
||||
kind: CallbackKind;
|
||||
target: string;
|
||||
headers?: Record<string, string>;
|
||||
enabled: boolean;
|
||||
createdAt: number;
|
||||
retryCount?: number;
|
||||
lastFailure?: number;
|
||||
circuitOpen?: boolean;
|
||||
}
|
||||
|
||||
export interface CallbackRegistry {
|
||||
register(id: string, callback: CallbackRecord): Promise<void>;
|
||||
unregister(id: string): Promise<void>;
|
||||
fire(event: CallbackEvent): Promise<void>;
|
||||
getRegistered(): Promise<CallbackRecord[]>;
|
||||
getStatus(): Promise<{
|
||||
total: number;
|
||||
enabled: number;
|
||||
circuitOpen: number;
|
||||
lastActivity: number;
|
||||
}>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback Registry Implementation
|
||||
* Handles callback registration, delivery, and circuit breaker logic
|
||||
*/
|
||||
export class CallbackRegistryImpl implements CallbackRegistry {
|
||||
private callbacks = new Map<string, CallbackRecord>();
|
||||
private localCallbacks = new Map<string, CallbackFunction>();
|
||||
private retryQueue = new Map<string, CallbackEvent[]>();
|
||||
private circuitBreakers = new Map<string, {
|
||||
failures: number;
|
||||
lastFailure: number;
|
||||
open: boolean;
|
||||
}>();
|
||||
|
||||
constructor() {
|
||||
this.startRetryProcessor();
|
||||
}
|
||||
|
||||
async register(id: string, callback: CallbackRecord): Promise<void> {
|
||||
this.callbacks.set(id, callback);
|
||||
|
||||
// Initialize circuit breaker
|
||||
if (!this.circuitBreakers.has(id)) {
|
||||
this.circuitBreakers.set(id, {
|
||||
failures: 0,
|
||||
lastFailure: 0,
|
||||
open: false
|
||||
});
|
||||
}
|
||||
|
||||
console.log(`DNP-CB-REGISTER: Callback ${id} registered (${callback.kind})`);
|
||||
}
|
||||
|
||||
async unregister(id: string): Promise<void> {
|
||||
this.callbacks.delete(id);
|
||||
this.localCallbacks.delete(id);
|
||||
this.retryQueue.delete(id);
|
||||
this.circuitBreakers.delete(id);
|
||||
|
||||
console.log(`DNP-CB-UNREGISTER: Callback ${id} unregistered`);
|
||||
}
|
||||
|
||||
async fire(event: CallbackEvent): Promise<void> {
|
||||
const enabledCallbacks = Array.from(this.callbacks.values())
|
||||
.filter(cb => cb.enabled);
|
||||
|
||||
console.log(`DNP-CB-FIRE: Firing event ${event.type} to ${enabledCallbacks.length} callbacks`);
|
||||
|
||||
for (const callback of enabledCallbacks) {
|
||||
try {
|
||||
await this.deliverCallback(callback, event);
|
||||
} catch (error) {
|
||||
console.error(`DNP-CB-FIRE-ERROR: Failed to deliver to ${callback.id}`, error);
|
||||
await this.handleCallbackFailure(callback, event, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async getRegistered(): Promise<CallbackRecord[]> {
|
||||
return Array.from(this.callbacks.values());
|
||||
}
|
||||
|
||||
async getStatus(): Promise<{
|
||||
total: number;
|
||||
enabled: number;
|
||||
circuitOpen: number;
|
||||
lastActivity: number;
|
||||
}> {
|
||||
const callbacks = Array.from(this.callbacks.values());
|
||||
const circuitBreakers = Array.from(this.circuitBreakers.values());
|
||||
|
||||
return {
|
||||
total: callbacks.length,
|
||||
enabled: callbacks.filter(cb => cb.enabled).length,
|
||||
circuitOpen: circuitBreakers.filter(cb => cb.open).length,
|
||||
lastActivity: Math.max(
|
||||
...callbacks.map(cb => cb.createdAt),
|
||||
...circuitBreakers.map(cb => cb.lastFailure)
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
private async deliverCallback(callback: CallbackRecord, event: CallbackEvent): Promise<void> {
|
||||
const circuitBreaker = this.circuitBreakers.get(callback.id);
|
||||
|
||||
// Check circuit breaker
|
||||
if (circuitBreaker?.open) {
|
||||
console.warn(`DNP-CB-CIRCUIT: Circuit open for ${callback.id}, skipping delivery`);
|
||||
return;
|
||||
}
|
||||
|
||||
const start = performance.now();
|
||||
|
||||
try {
|
||||
switch (callback.kind) {
|
||||
case 'http':
|
||||
await this.deliverHttpCallback(callback, event);
|
||||
break;
|
||||
case 'local':
|
||||
await this.deliverLocalCallback(callback, event);
|
||||
break;
|
||||
case 'queue':
|
||||
await this.deliverQueueCallback(callback, event);
|
||||
break;
|
||||
default:
|
||||
throw new Error(`Unknown callback kind: ${callback.kind}`);
|
||||
}
|
||||
|
||||
// Reset circuit breaker on success
|
||||
if (circuitBreaker) {
|
||||
circuitBreaker.failures = 0;
|
||||
circuitBreaker.open = false;
|
||||
}
|
||||
|
||||
const duration = performance.now() - start;
|
||||
console.log(`DNP-CB-SUCCESS: Delivered to ${callback.id} in ${duration.toFixed(2)}ms`);
|
||||
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
private async deliverHttpCallback(callback: CallbackRecord, event: CallbackEvent): Promise<void> {
|
||||
const response = await fetch(callback.target, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...callback.headers
|
||||
},
|
||||
body: JSON.stringify({
|
||||
...event,
|
||||
callbackId: callback.id,
|
||||
timestamp: Date.now()
|
||||
})
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
||||
}
|
||||
}
|
||||
|
||||
private async deliverLocalCallback(callback: CallbackRecord, event: CallbackEvent): Promise<void> {
|
||||
const localCallback = this.localCallbacks.get(callback.id);
|
||||
if (!localCallback) {
|
||||
throw new Error(`Local callback ${callback.id} not found`);
|
||||
}
|
||||
|
||||
await localCallback(event);
|
||||
}
|
||||
|
||||
private async deliverQueueCallback(callback: CallbackRecord, event: CallbackEvent): Promise<void> {
|
||||
// Queue callback implementation would go here
|
||||
// For now, just log the event
|
||||
console.log(`DNP-CB-QUEUE: Queued event ${event.type} for ${callback.id}`);
|
||||
}
|
||||
|
||||
private async handleCallbackFailure(
|
||||
callback: CallbackRecord,
|
||||
event: CallbackEvent,
|
||||
error: unknown
|
||||
): Promise<void> {
|
||||
const circuitBreaker = this.circuitBreakers.get(callback.id);
|
||||
|
||||
if (circuitBreaker) {
|
||||
circuitBreaker.failures++;
|
||||
circuitBreaker.lastFailure = Date.now();
|
||||
|
||||
// Open circuit after 5 consecutive failures
|
||||
if (circuitBreaker.failures >= 5) {
|
||||
circuitBreaker.open = true;
|
||||
console.error(`DNP-CB-CIRCUIT-OPEN: Circuit opened for ${callback.id} after ${circuitBreaker.failures} failures`);
|
||||
}
|
||||
}
|
||||
|
||||
// Schedule retry with exponential backoff
|
||||
await this.scheduleRetry(callback, event);
|
||||
|
||||
console.error(`DNP-CB-FAILURE: Callback ${callback.id} failed`, error);
|
||||
}
|
||||
|
||||
private async scheduleRetry(callback: CallbackRecord, event: CallbackEvent): Promise<void> {
|
||||
const retryCount = callback.retryCount || 0;
|
||||
|
||||
if (retryCount >= 5) {
|
||||
console.warn(`DNP-CB-RETRY-LIMIT: Max retries reached for ${callback.id}`);
|
||||
return;
|
||||
}
|
||||
|
||||
const backoffMs = Math.min(1000 * Math.pow(2, retryCount), 60000); // Cap at 1 minute
|
||||
const retryEvent = { ...event, retryCount: retryCount + 1 };
|
||||
|
||||
if (!this.retryQueue.has(callback.id)) {
|
||||
this.retryQueue.set(callback.id, []);
|
||||
}
|
||||
|
||||
this.retryQueue.get(callback.id)!.push(retryEvent);
|
||||
|
||||
console.log(`DNP-CB-RETRY: Scheduled retry ${retryCount + 1} for ${callback.id} in ${backoffMs}ms`);
|
||||
}
|
||||
|
||||
private startRetryProcessor(): void {
|
||||
setInterval(async () => {
|
||||
for (const [callbackId, events] of this.retryQueue.entries()) {
|
||||
if (events.length === 0) continue;
|
||||
|
||||
const callback = this.callbacks.get(callbackId);
|
||||
if (!callback) {
|
||||
this.retryQueue.delete(callbackId);
|
||||
continue;
|
||||
}
|
||||
|
||||
const event = events.shift();
|
||||
if (!event) continue;
|
||||
|
||||
try {
|
||||
await this.deliverCallback(callback, event);
|
||||
} catch (error) {
|
||||
console.error(`DNP-CB-RETRY-FAILED: Retry failed for ${callbackId}`, error);
|
||||
}
|
||||
}
|
||||
}, 5000); // Process retries every 5 seconds
|
||||
}
|
||||
|
||||
// Register local callback function
|
||||
registerLocalCallback(id: string, callback: CallbackFunction): void {
|
||||
this.localCallbacks.set(id, callback);
|
||||
console.log(`DNP-CB-LOCAL: Local callback ${id} registered`);
|
||||
}
|
||||
|
||||
// Unregister local callback function
|
||||
unregisterLocalCallback(id: string): void {
|
||||
this.localCallbacks.delete(id);
|
||||
console.log(`DNP-CB-LOCAL: Local callback ${id} unregistered`);
|
||||
}
|
||||
}
|
||||
|
||||
// Singleton instance
|
||||
export const callbackRegistry = new CallbackRegistryImpl();
|
||||
@@ -6,10 +6,16 @@
|
||||
import { registerPlugin } from '@capacitor/core';
|
||||
import type { DailyNotificationPlugin } from './definitions';
|
||||
import { DailyNotificationWeb } from './web';
|
||||
import { observability, EVENT_CODES } from './observability';
|
||||
|
||||
const DailyNotification = registerPlugin<DailyNotificationPlugin>('DailyNotification', {
|
||||
web: async () => new DailyNotificationWeb(),
|
||||
});
|
||||
|
||||
// Initialize observability
|
||||
observability.logEvent('INFO', EVENT_CODES.FETCH_START, 'Daily Notification Plugin initialized');
|
||||
|
||||
export * from './definitions';
|
||||
export * from './callback-registry';
|
||||
export * from './observability';
|
||||
export { DailyNotification };
|
||||
|
||||
311
src/observability.ts
Normal file
311
src/observability.ts
Normal file
@@ -0,0 +1,311 @@
|
||||
/**
|
||||
* Observability & Health Monitoring Implementation
|
||||
* Provides structured logging, event codes, and health monitoring
|
||||
*
|
||||
* @author Matthew Raymer
|
||||
* @version 1.1.0
|
||||
*/
|
||||
|
||||
export interface HealthStatus {
|
||||
nextRuns: number[];
|
||||
lastOutcomes: string[];
|
||||
cacheAgeMs: number | null;
|
||||
staleArmed: boolean;
|
||||
queueDepth: number;
|
||||
circuitBreakers: {
|
||||
total: number;
|
||||
open: number;
|
||||
failures: number;
|
||||
};
|
||||
performance: {
|
||||
avgFetchTime: number;
|
||||
avgNotifyTime: number;
|
||||
successRate: number;
|
||||
};
|
||||
}
|
||||
|
||||
export interface EventLog {
|
||||
id: string;
|
||||
timestamp: number;
|
||||
level: 'INFO' | 'WARN' | 'ERROR';
|
||||
eventCode: string;
|
||||
message: string;
|
||||
data?: Record<string, unknown>;
|
||||
duration?: number;
|
||||
}
|
||||
|
||||
export interface PerformanceMetrics {
|
||||
fetchTimes: number[];
|
||||
notifyTimes: number[];
|
||||
callbackTimes: number[];
|
||||
successCount: number;
|
||||
failureCount: number;
|
||||
lastReset: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Observability Manager
|
||||
* Handles structured logging, health monitoring, and performance tracking
|
||||
*/
|
||||
export class ObservabilityManager {
|
||||
private eventLogs: EventLog[] = [];
|
||||
private performanceMetrics: PerformanceMetrics = {
|
||||
fetchTimes: [],
|
||||
notifyTimes: [],
|
||||
callbackTimes: [],
|
||||
successCount: 0,
|
||||
failureCount: 0,
|
||||
lastReset: Date.now()
|
||||
};
|
||||
private maxLogs = 1000;
|
||||
private maxMetrics = 100;
|
||||
|
||||
/**
|
||||
* Log structured event with event code
|
||||
*/
|
||||
logEvent(
|
||||
level: 'INFO' | 'WARN' | 'ERROR',
|
||||
eventCode: string,
|
||||
message: string,
|
||||
data?: Record<string, unknown>,
|
||||
duration?: number
|
||||
): void {
|
||||
const event: EventLog = {
|
||||
id: this.generateEventId(),
|
||||
timestamp: Date.now(),
|
||||
level,
|
||||
eventCode,
|
||||
message,
|
||||
data,
|
||||
duration
|
||||
};
|
||||
|
||||
this.eventLogs.unshift(event);
|
||||
|
||||
// Keep only recent logs
|
||||
if (this.eventLogs.length > this.maxLogs) {
|
||||
this.eventLogs = this.eventLogs.slice(0, this.maxLogs);
|
||||
}
|
||||
|
||||
// Console output with structured format
|
||||
const logMessage = `[${eventCode}] ${message}`;
|
||||
const logData = data ? ` | Data: ${JSON.stringify(data)}` : '';
|
||||
const logDuration = duration ? ` | Duration: ${duration}ms` : '';
|
||||
|
||||
switch (level) {
|
||||
case 'INFO':
|
||||
console.log(logMessage + logData + logDuration);
|
||||
break;
|
||||
case 'WARN':
|
||||
console.warn(logMessage + logData + logDuration);
|
||||
break;
|
||||
case 'ERROR':
|
||||
console.error(logMessage + logData + logDuration);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Record performance metrics
|
||||
*/
|
||||
recordMetric(type: 'fetch' | 'notify' | 'callback', duration: number, success: boolean): void {
|
||||
switch (type) {
|
||||
case 'fetch':
|
||||
this.performanceMetrics.fetchTimes.push(duration);
|
||||
break;
|
||||
case 'notify':
|
||||
this.performanceMetrics.notifyTimes.push(duration);
|
||||
break;
|
||||
case 'callback':
|
||||
this.performanceMetrics.callbackTimes.push(duration);
|
||||
break;
|
||||
}
|
||||
|
||||
if (success) {
|
||||
this.performanceMetrics.successCount++;
|
||||
} else {
|
||||
this.performanceMetrics.failureCount++;
|
||||
}
|
||||
|
||||
// Keep only recent metrics
|
||||
this.trimMetrics();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get health status
|
||||
*/
|
||||
async getHealthStatus(): Promise<HealthStatus> {
|
||||
const now = Date.now();
|
||||
const recentLogs = this.eventLogs.filter(log => now - log.timestamp < 24 * 60 * 60 * 1000); // Last 24 hours
|
||||
|
||||
// Calculate next runs (mock implementation)
|
||||
const nextRuns = this.calculateNextRuns();
|
||||
|
||||
// Get last outcomes from recent logs
|
||||
const lastOutcomes = recentLogs
|
||||
.filter(log => log.eventCode.startsWith('DNP-FETCH-') || log.eventCode.startsWith('DNP-NOTIFY-'))
|
||||
.slice(0, 10)
|
||||
.map(log => log.eventCode);
|
||||
|
||||
// Calculate cache age (mock implementation)
|
||||
const cacheAgeMs = this.calculateCacheAge();
|
||||
|
||||
// Check if stale armed
|
||||
const staleArmed = cacheAgeMs ? cacheAgeMs > 3600000 : true; // 1 hour
|
||||
|
||||
// Calculate queue depth
|
||||
const queueDepth = recentLogs.filter(log =>
|
||||
log.eventCode.includes('QUEUE') || log.eventCode.includes('RETRY')
|
||||
).length;
|
||||
|
||||
// Circuit breaker status
|
||||
const circuitBreakers = this.getCircuitBreakerStatus();
|
||||
|
||||
// Performance metrics
|
||||
const performance = this.calculatePerformanceMetrics();
|
||||
|
||||
return {
|
||||
nextRuns,
|
||||
lastOutcomes,
|
||||
cacheAgeMs,
|
||||
staleArmed,
|
||||
queueDepth,
|
||||
circuitBreakers,
|
||||
performance
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get recent event logs
|
||||
*/
|
||||
getRecentLogs(limit: number = 50): EventLog[] {
|
||||
return this.eventLogs.slice(0, limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get performance metrics
|
||||
*/
|
||||
getPerformanceMetrics(): PerformanceMetrics {
|
||||
return { ...this.performanceMetrics };
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset performance metrics
|
||||
*/
|
||||
resetMetrics(): void {
|
||||
this.performanceMetrics = {
|
||||
fetchTimes: [],
|
||||
notifyTimes: [],
|
||||
callbackTimes: [],
|
||||
successCount: 0,
|
||||
failureCount: 0,
|
||||
lastReset: Date.now()
|
||||
};
|
||||
|
||||
this.logEvent('INFO', 'DNP-METRICS-RESET', 'Performance metrics reset');
|
||||
}
|
||||
|
||||
/**
|
||||
* Compact old logs (called by cleanup job)
|
||||
*/
|
||||
compactLogs(olderThanMs: number = 30 * 24 * 60 * 60 * 1000): number { // 30 days
|
||||
const cutoff = Date.now() - olderThanMs;
|
||||
const initialCount = this.eventLogs.length;
|
||||
|
||||
this.eventLogs = this.eventLogs.filter(log => log.timestamp >= cutoff);
|
||||
|
||||
const removedCount = initialCount - this.eventLogs.length;
|
||||
if (removedCount > 0) {
|
||||
this.logEvent('INFO', 'DNP-LOGS-COMPACTED', `Removed ${removedCount} old logs`);
|
||||
}
|
||||
|
||||
return removedCount;
|
||||
}
|
||||
|
||||
// Private helper methods
|
||||
private generateEventId(): string {
|
||||
return `evt_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
||||
}
|
||||
|
||||
private trimMetrics(): void {
|
||||
if (this.performanceMetrics.fetchTimes.length > this.maxMetrics) {
|
||||
this.performanceMetrics.fetchTimes = this.performanceMetrics.fetchTimes.slice(-this.maxMetrics);
|
||||
}
|
||||
if (this.performanceMetrics.notifyTimes.length > this.maxMetrics) {
|
||||
this.performanceMetrics.notifyTimes = this.performanceMetrics.notifyTimes.slice(-this.maxMetrics);
|
||||
}
|
||||
if (this.performanceMetrics.callbackTimes.length > this.maxMetrics) {
|
||||
this.performanceMetrics.callbackTimes = this.performanceMetrics.callbackTimes.slice(-this.maxMetrics);
|
||||
}
|
||||
}
|
||||
|
||||
private calculateNextRuns(): number[] {
|
||||
// Mock implementation - would calculate from actual schedules
|
||||
const now = Date.now();
|
||||
return [
|
||||
now + (60 * 60 * 1000), // 1 hour from now
|
||||
now + (24 * 60 * 60 * 1000) // 24 hours from now
|
||||
];
|
||||
}
|
||||
|
||||
private calculateCacheAge(): number | null {
|
||||
// Mock implementation - would get from actual cache
|
||||
return 1800000; // 30 minutes
|
||||
}
|
||||
|
||||
private getCircuitBreakerStatus(): { total: number; open: number; failures: number } {
|
||||
// Mock implementation - would get from actual circuit breakers
|
||||
return {
|
||||
total: 3,
|
||||
open: 1,
|
||||
failures: 5
|
||||
};
|
||||
}
|
||||
|
||||
private calculatePerformanceMetrics(): {
|
||||
avgFetchTime: number;
|
||||
avgNotifyTime: number;
|
||||
successRate: number;
|
||||
} {
|
||||
const fetchTimes = this.performanceMetrics.fetchTimes;
|
||||
const notifyTimes = this.performanceMetrics.notifyTimes;
|
||||
const totalOperations = this.performanceMetrics.successCount + this.performanceMetrics.failureCount;
|
||||
|
||||
return {
|
||||
avgFetchTime: fetchTimes.length > 0 ?
|
||||
fetchTimes.reduce((a, b) => a + b, 0) / fetchTimes.length : 0,
|
||||
avgNotifyTime: notifyTimes.length > 0 ?
|
||||
notifyTimes.reduce((a, b) => a + b, 0) / notifyTimes.length : 0,
|
||||
successRate: totalOperations > 0 ?
|
||||
this.performanceMetrics.successCount / totalOperations : 0
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Singleton instance
|
||||
export const observability = new ObservabilityManager();
|
||||
|
||||
// Event code constants
|
||||
export const EVENT_CODES = {
|
||||
FETCH_START: 'DNP-FETCH-START',
|
||||
FETCH_SUCCESS: 'DNP-FETCH-SUCCESS',
|
||||
FETCH_FAILURE: 'DNP-FETCH-FAILURE',
|
||||
FETCH_RETRY: 'DNP-FETCH-RETRY',
|
||||
NOTIFY_START: 'DNP-NOTIFY-START',
|
||||
NOTIFY_SUCCESS: 'DNP-NOTIFY-SUCCESS',
|
||||
NOTIFY_FAILURE: 'DNP-NOTIFY-FAILURE',
|
||||
NOTIFY_SKIPPED_TTL: 'DNP-NOTIFY-SKIPPED-TTL',
|
||||
CALLBACK_START: 'DNP-CB-START',
|
||||
CALLBACK_SUCCESS: 'DNP-CB-SUCCESS',
|
||||
CALLBACK_FAILURE: 'DNP-CB-FAILURE',
|
||||
CALLBACK_RETRY: 'DNP-CB-RETRY',
|
||||
CALLBACK_CIRCUIT_OPEN: 'DNP-CB-CIRCUIT-OPEN',
|
||||
CALLBACK_CIRCUIT_CLOSE: 'DNP-CB-CIRCUIT-CLOSE',
|
||||
BOOT_RECOVERY: 'DNP-BOOT-RECOVERY',
|
||||
SCHEDULE_UPDATE: 'DNP-SCHEDULE-UPDATE',
|
||||
CACHE_HIT: 'DNP-CACHE-HIT',
|
||||
CACHE_MISS: 'DNP-CACHE-MISS',
|
||||
TTL_EXPIRED: 'DNP-TTL-EXPIRED',
|
||||
METRICS_RESET: 'DNP-METRICS-RESET',
|
||||
LOGS_COMPACTED: 'DNP-LOGS-COMPACTED'
|
||||
} as const;
|
||||
147
src/web.ts
147
src/web.ts
@@ -7,10 +7,15 @@
|
||||
|
||||
import { WebPlugin } from '@capacitor/core';
|
||||
import type { DailyNotificationPlugin, NotificationOptions, NotificationSettings, NotificationResponse, NotificationStatus, BatteryStatus, PowerState, PermissionStatus } from './definitions';
|
||||
import { callbackRegistry } from './callback-registry';
|
||||
import { observability, EVENT_CODES } from './observability';
|
||||
|
||||
export class DailyNotificationWeb extends WebPlugin implements DailyNotificationPlugin {
|
||||
private contentCache = new Map<string, any>();
|
||||
private callbacks = new Map<string, any>();
|
||||
|
||||
async configure(_options: any): Promise<void> {
|
||||
// Web implementation placeholder
|
||||
observability.logEvent('INFO', EVENT_CODES.SCHEDULE_UPDATE, 'Plugin configured on web platform');
|
||||
console.log('Configure called on web platform');
|
||||
}
|
||||
|
||||
@@ -152,39 +157,101 @@ export class DailyNotificationWeb extends WebPlugin implements DailyNotification
|
||||
// Dual Scheduling Methods Implementation
|
||||
|
||||
async scheduleContentFetch(_config: any): Promise<void> {
|
||||
console.log('Schedule content fetch called on web platform');
|
||||
const start = performance.now();
|
||||
observability.logEvent('INFO', EVENT_CODES.FETCH_START, 'Content fetch scheduled on web platform');
|
||||
|
||||
try {
|
||||
// Mock content fetch implementation
|
||||
const mockContent = {
|
||||
id: `fetch_${Date.now()}`,
|
||||
timestamp: Date.now(),
|
||||
content: 'Mock daily content',
|
||||
source: 'web_platform'
|
||||
};
|
||||
|
||||
this.contentCache.set(mockContent.id, mockContent);
|
||||
|
||||
const duration = performance.now() - start;
|
||||
observability.recordMetric('fetch', duration, true);
|
||||
observability.logEvent('INFO', EVENT_CODES.FETCH_SUCCESS, 'Content fetch completed', { duration });
|
||||
|
||||
// Fire callbacks
|
||||
await callbackRegistry.fire({
|
||||
id: mockContent.id,
|
||||
at: Date.now(),
|
||||
type: 'onFetchSuccess',
|
||||
payload: mockContent
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
const duration = performance.now() - start;
|
||||
observability.recordMetric('fetch', duration, false);
|
||||
observability.logEvent('ERROR', EVENT_CODES.FETCH_FAILURE, 'Content fetch failed', { error: String(error) });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async scheduleUserNotification(_config: any): Promise<void> {
|
||||
console.log('Schedule user notification called on web platform');
|
||||
async scheduleUserNotification(config: any): Promise<void> {
|
||||
const start = performance.now();
|
||||
observability.logEvent('INFO', EVENT_CODES.NOTIFY_START, 'User notification scheduled on web platform');
|
||||
|
||||
try {
|
||||
// Mock notification implementation
|
||||
if ('Notification' in window && Notification.permission === 'granted') {
|
||||
const notification = new Notification(config.title || 'Daily Notification', {
|
||||
body: config.body || 'Your daily update is ready',
|
||||
icon: '/favicon.ico'
|
||||
});
|
||||
|
||||
notification.onclick = () => {
|
||||
observability.logEvent('INFO', EVENT_CODES.NOTIFY_SUCCESS, 'Notification clicked');
|
||||
};
|
||||
}
|
||||
|
||||
const duration = performance.now() - start;
|
||||
observability.recordMetric('notify', duration, true);
|
||||
observability.logEvent('INFO', EVENT_CODES.NOTIFY_SUCCESS, 'User notification displayed', { duration });
|
||||
|
||||
// Fire callbacks
|
||||
await callbackRegistry.fire({
|
||||
id: `notify_${Date.now()}`,
|
||||
at: Date.now(),
|
||||
type: 'onNotifyDelivered',
|
||||
payload: config
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
const duration = performance.now() - start;
|
||||
observability.recordMetric('notify', duration, false);
|
||||
observability.logEvent('ERROR', EVENT_CODES.NOTIFY_FAILURE, 'User notification failed', { error: String(error) });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async scheduleDualNotification(_config: any): Promise<void> {
|
||||
console.log('Schedule dual notification called on web platform');
|
||||
async scheduleDualNotification(config: any): Promise<void> {
|
||||
observability.logEvent('INFO', EVENT_CODES.SCHEDULE_UPDATE, 'Dual notification scheduled on web platform');
|
||||
|
||||
try {
|
||||
await this.scheduleContentFetch(config.contentFetch);
|
||||
await this.scheduleUserNotification(config.userNotification);
|
||||
|
||||
observability.logEvent('INFO', EVENT_CODES.SCHEDULE_UPDATE, 'Dual notification completed successfully');
|
||||
} catch (error) {
|
||||
observability.logEvent('ERROR', EVENT_CODES.SCHEDULE_UPDATE, 'Dual notification failed', { error: String(error) });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async getDualScheduleStatus(): Promise<any> {
|
||||
const healthStatus = await observability.getHealthStatus();
|
||||
return {
|
||||
contentFetch: {
|
||||
isEnabled: false,
|
||||
isScheduled: false,
|
||||
pendingFetches: 0
|
||||
},
|
||||
userNotification: {
|
||||
isEnabled: false,
|
||||
isScheduled: false,
|
||||
pendingNotifications: 0
|
||||
},
|
||||
relationship: {
|
||||
isLinked: false,
|
||||
contentAvailable: false
|
||||
},
|
||||
overall: {
|
||||
isActive: false,
|
||||
lastActivity: Date.now(),
|
||||
errorCount: 0,
|
||||
successRate: 1.0
|
||||
}
|
||||
nextRuns: healthStatus.nextRuns,
|
||||
lastOutcomes: healthStatus.lastOutcomes,
|
||||
cacheAgeMs: healthStatus.cacheAgeMs,
|
||||
staleArmed: healthStatus.staleArmed,
|
||||
queueDepth: healthStatus.queueDepth,
|
||||
circuitBreakers: healthStatus.circuitBreakers,
|
||||
performance: healthStatus.performance
|
||||
};
|
||||
}
|
||||
|
||||
@@ -216,15 +283,33 @@ export class DailyNotificationWeb extends WebPlugin implements DailyNotification
|
||||
return [];
|
||||
}
|
||||
|
||||
async registerCallback(_name: string, _callback: Function): Promise<void> {
|
||||
console.log('Register callback called on web platform');
|
||||
async registerCallback(name: string, callback: Function): Promise<void> {
|
||||
observability.logEvent('INFO', EVENT_CODES.CALLBACK_START, `Callback ${name} registered on web platform`);
|
||||
|
||||
// Register with callback registry
|
||||
await callbackRegistry.register(name, {
|
||||
id: name,
|
||||
kind: 'local',
|
||||
target: '',
|
||||
enabled: true,
|
||||
createdAt: Date.now()
|
||||
});
|
||||
|
||||
// Register local callback function
|
||||
callbackRegistry.registerLocalCallback(name, callback as any);
|
||||
this.callbacks.set(name, callback);
|
||||
}
|
||||
|
||||
async unregisterCallback(_name: string): Promise<void> {
|
||||
console.log('Unregister callback called on web platform');
|
||||
async unregisterCallback(name: string): Promise<void> {
|
||||
observability.logEvent('INFO', EVENT_CODES.CALLBACK_START, `Callback ${name} unregistered on web platform`);
|
||||
|
||||
await callbackRegistry.unregister(name);
|
||||
callbackRegistry.unregisterLocalCallback(name);
|
||||
this.callbacks.delete(name);
|
||||
}
|
||||
|
||||
async getRegisteredCallbacks(): Promise<string[]> {
|
||||
return [];
|
||||
const callbacks = await callbackRegistry.getRegistered();
|
||||
return callbacks.map(cb => cb.id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user