@k-msg/analytics 0.7.2 → 0.8.0
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 +50 -67
- package/dist/index.d.ts +1 -0
- package/dist/index.js +43 -43
- package/dist/index.js.map +6 -5
- package/dist/index.mjs +43 -43
- package/dist/index.mjs.map +6 -5
- package/dist/services/delivery-tracking.analytics.d.ts +37 -0
- package/package.json +9 -3
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @k-msg/analytics
|
|
2
2
|
|
|
3
|
-
Analytics and
|
|
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
|
-
- **
|
|
16
|
-
- **
|
|
17
|
-
- **
|
|
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 {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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
|
-
|
|
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
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
-
|
|
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
|
-
##
|
|
56
|
+
## Using Bun.SQL (Postgres/MySQL/SQLite)
|
|
75
57
|
|
|
76
58
|
```typescript
|
|
77
|
-
import {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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
|
-
|
|
89
|
-
|
|
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";
|