@k-msg/analytics 0.7.1 → 0.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @k-msg/analytics
2
2
 
3
- Analytics and insights engine for the K-Message platform.
3
+ Analytics and reporting for `k-msg`, built on top of delivery-tracking records.
4
4
 
5
5
  ## Installation
6
6
 
@@ -12,86 +12,69 @@ bun add @k-msg/analytics @k-msg/core
12
12
 
13
13
  ## Features
14
14
 
15
- - **Metrics Collection**: Comprehensive message and system metrics collection
16
- - **Real-time Analytics**: Live analytics processing and aggregation
17
- - **Anomaly Detection**: Intelligent anomaly detection for message patterns
18
- - **Recommendation Engine**: AI-powered recommendations for optimization
19
- - **Dashboard Generation**: Automated dashboard and report generation
20
- - **Data Export**: Multiple export formats (CSV, JSON, Excel)
15
+ - **Query-based (recommended)**: compute KPIs by reading `DeliveryTrackingStore` records (SQLite / Bun.SQL / memory)
16
+ - **Breakdowns**: by status, provider, message type
17
+ - **(Experimental)** in-memory collectors/insights/reporting utilities (subject to change)
21
18
 
22
- ## Basic Usage
19
+ ## Basic Usage (Query-Based)
23
20
 
24
21
  ```typescript
25
- import { AnalyticsService, MetricsCollector } from '@k-msg/analytics';
26
-
27
- const analytics = new AnalyticsService();
28
- const collector = new MetricsCollector();
29
-
30
- // Collect message metrics
31
- await collector.collect({
32
- type: 'MESSAGE_SENT',
33
- provider: 'iwinv',
34
- channel: 'alimtalk',
35
- count: 1,
36
- metadata: {
37
- templateId: 'TPL001',
38
- region: 'KR'
39
- }
22
+ import { KMsg } from "k-msg";
23
+ import {
24
+ DeliveryTrackingService,
25
+ SqliteDeliveryTrackingStore,
26
+ createDeliveryTrackingHooks,
27
+ } from "k-msg";
28
+ import { DeliveryTrackingAnalyticsService } from "@k-msg/analytics";
29
+
30
+ const providers = [
31
+ /* new SolapiProvider(...), new IWINVProvider(...), ... */
32
+ ];
33
+
34
+ // 1) Tracking (writes to store)
35
+ const store = new SqliteDeliveryTrackingStore({ dbPath: "./kmsg.sqlite" });
36
+ const tracking = new DeliveryTrackingService({ providers, store });
37
+ await tracking.init();
38
+
39
+ const kmsg = new KMsg({
40
+ providers,
41
+ hooks: createDeliveryTrackingHooks(tracking),
40
42
  });
41
43
 
42
- // Generate dashboard data
43
- const dashboard = await analytics.generateDashboard({
44
- timeRange: '24h',
45
- metrics: ['delivery_rate', 'failure_rate', 'response_time']
46
- });
47
- ```
44
+ await kmsg.send({ to: "01012345678", text: "hello" });
48
45
 
49
- ## Insights and Recommendations
50
-
51
- ```typescript
52
- import { InsightEngine, RecommendationEngine } from '@k-msg/analytics';
53
-
54
- const insightEngine = new InsightEngine();
55
- const recommendationEngine = new RecommendationEngine();
56
-
57
- // Generate insights
58
- const insights = await insightEngine.generateInsights({
59
- timeRange: '7d',
60
- providers: ['iwinv'],
61
- includeAnomalies: true
62
- });
46
+ // 2) Analytics (reads from the same store)
47
+ const analytics = new DeliveryTrackingAnalyticsService({ store });
48
+ const summary = await analytics.getSummary(
49
+ { requestedAt: { start: new Date(Date.now() - 24 * 60 * 60 * 1000), end: new Date() } },
50
+ { includeByProviderId: true, includeByType: true },
51
+ );
63
52
 
64
- // Get optimization recommendations
65
- const recommendations = await recommendationEngine.generateRecommendations({
66
- metrics: insights.metrics,
67
- thresholds: {
68
- deliveryRate: 0.95,
69
- errorRate: 0.05
70
- }
71
- });
53
+ console.log(summary);
72
54
  ```
73
55
 
74
- ## Export and Reporting
56
+ ## Using Bun.SQL (Postgres/MySQL/SQLite)
75
57
 
76
58
  ```typescript
77
- import { ExportManager } from '@k-msg/analytics';
78
-
79
- const exportManager = new ExportManager();
80
-
81
- // Export metrics to CSV
82
- await exportManager.exportToCSV({
83
- metrics: dashboard.metrics,
84
- filename: 'monthly-report.csv',
85
- timeRange: '30d'
59
+ import { BunSqlDeliveryTrackingStore } from "k-msg";
60
+ import { DeliveryTrackingAnalyticsService } from "@k-msg/analytics";
61
+
62
+ const store = new BunSqlDeliveryTrackingStore({
63
+ options: {
64
+ adapter: "postgres",
65
+ url: process.env.DATABASE_URL!,
66
+ },
86
67
  });
87
68
 
88
- // Export to JSON for API consumption
89
- const jsonReport = await exportManager.exportToJSON({
90
- includeRawData: true,
91
- format: 'detailed'
92
- });
69
+ const analytics = new DeliveryTrackingAnalyticsService({ store });
70
+ await analytics.init();
93
71
  ```
94
72
 
73
+ ## Notes
74
+
75
+ - `@k-msg/analytics` does not create its own database. It reads from the `kmsg_delivery_tracking` table written by `DeliveryTrackingService`.
76
+ - For production usage, prefer a durable store (`SqliteDeliveryTrackingStore` or `BunSqlDeliveryTrackingStore`).
77
+
95
78
  ## License
96
79
 
97
- MIT
80
+ MIT
package/dist/index.d.ts CHANGED
@@ -11,6 +11,7 @@ export { AnomalyDetector, RecommendationEngine } from "./insights/index";
11
11
  export type { DashboardConfig, DashboardData, DashboardWidget, ExportConfig, ExportFormat, ExportResult, KPIData, WidgetData, } from "./reports/index";
12
12
  export { DashboardGenerator, ExportManager } from "./reports/index";
13
13
  export { AnalyticsService } from "./services/analytics.service";
14
+ export { DeliveryTrackingAnalyticsService } from "./services/delivery-tracking.analytics";
14
15
  export { InsightEngine } from "./services/insight.engine";
15
16
  export { MetricsCollector } from "./services/metrics.collector";
16
17
  export { ReportGenerator } from "./services/report.generator";