@hasna/microservices 0.0.16 → 0.0.17

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.
Files changed (293) hide show
  1. package/README.md +143 -23
  2. package/bin/index.js +711 -13998
  3. package/bin/mcp.js +228 -10987
  4. package/dist/index.js +166 -10056
  5. package/package.json +25 -25
  6. package/microservices/microservice-ads/package.json +0 -28
  7. package/microservices/microservice-ads/src/cli/index.ts +0 -605
  8. package/microservices/microservice-ads/src/db/campaigns.ts +0 -797
  9. package/microservices/microservice-ads/src/db/database.ts +0 -91
  10. package/microservices/microservice-ads/src/db/migrations.ts +0 -60
  11. package/microservices/microservice-ads/src/index.ts +0 -39
  12. package/microservices/microservice-ads/src/mcp/index.ts +0 -480
  13. package/microservices/microservice-analytics/package.json +0 -28
  14. package/microservices/microservice-analytics/src/cli/index.ts +0 -373
  15. package/microservices/microservice-analytics/src/db/analytics.ts +0 -564
  16. package/microservices/microservice-analytics/src/db/database.ts +0 -91
  17. package/microservices/microservice-analytics/src/db/migrations.ts +0 -50
  18. package/microservices/microservice-analytics/src/index.ts +0 -37
  19. package/microservices/microservice-analytics/src/mcp/index.ts +0 -334
  20. package/microservices/microservice-assets/package.json +0 -28
  21. package/microservices/microservice-assets/src/cli/index.ts +0 -375
  22. package/microservices/microservice-assets/src/db/assets.ts +0 -370
  23. package/microservices/microservice-assets/src/db/database.ts +0 -91
  24. package/microservices/microservice-assets/src/db/migrations.ts +0 -51
  25. package/microservices/microservice-assets/src/index.ts +0 -32
  26. package/microservices/microservice-assets/src/mcp/index.ts +0 -346
  27. package/microservices/microservice-bookkeeping/package.json +0 -28
  28. package/microservices/microservice-bookkeeping/src/cli/index.ts +0 -386
  29. package/microservices/microservice-bookkeeping/src/db/bookkeeping.ts +0 -591
  30. package/microservices/microservice-bookkeeping/src/db/database.ts +0 -91
  31. package/microservices/microservice-bookkeeping/src/db/migrations.ts +0 -52
  32. package/microservices/microservice-bookkeeping/src/index.ts +0 -32
  33. package/microservices/microservice-bookkeeping/src/mcp/index.ts +0 -284
  34. package/microservices/microservice-calendar/package.json +0 -28
  35. package/microservices/microservice-calendar/src/cli/index.ts +0 -287
  36. package/microservices/microservice-calendar/src/db/calendar.ts +0 -328
  37. package/microservices/microservice-calendar/src/db/database.ts +0 -91
  38. package/microservices/microservice-calendar/src/db/migrations.ts +0 -47
  39. package/microservices/microservice-calendar/src/index.ts +0 -24
  40. package/microservices/microservice-calendar/src/mcp/index.ts +0 -226
  41. package/microservices/microservice-company/package.json +0 -28
  42. package/microservices/microservice-company/src/cli/index.ts +0 -1126
  43. package/microservices/microservice-company/src/db/company.ts +0 -854
  44. package/microservices/microservice-company/src/db/database.ts +0 -91
  45. package/microservices/microservice-company/src/db/migrations.ts +0 -214
  46. package/microservices/microservice-company/src/db/workflow-migrations.ts +0 -44
  47. package/microservices/microservice-company/src/index.ts +0 -60
  48. package/microservices/microservice-company/src/lib/audit.ts +0 -168
  49. package/microservices/microservice-company/src/lib/finance.ts +0 -299
  50. package/microservices/microservice-company/src/lib/settings.ts +0 -85
  51. package/microservices/microservice-company/src/lib/workflows.ts +0 -698
  52. package/microservices/microservice-company/src/mcp/index.ts +0 -991
  53. package/microservices/microservice-compliance/package.json +0 -28
  54. package/microservices/microservice-compliance/src/cli/index.ts +0 -467
  55. package/microservices/microservice-compliance/src/db/compliance.ts +0 -633
  56. package/microservices/microservice-compliance/src/db/database.ts +0 -91
  57. package/microservices/microservice-compliance/src/db/migrations.ts +0 -63
  58. package/microservices/microservice-compliance/src/index.ts +0 -46
  59. package/microservices/microservice-compliance/src/mcp/index.ts +0 -438
  60. package/microservices/microservice-contacts/package.json +0 -28
  61. package/microservices/microservice-contacts/src/cli/index.ts +0 -393
  62. package/microservices/microservice-contacts/src/db/companies.ts +0 -167
  63. package/microservices/microservice-contacts/src/db/contacts.ts +0 -249
  64. package/microservices/microservice-contacts/src/db/database.ts +0 -91
  65. package/microservices/microservice-contacts/src/db/migrations.ts +0 -71
  66. package/microservices/microservice-contacts/src/db/relationships.ts +0 -53
  67. package/microservices/microservice-contacts/src/index.ts +0 -42
  68. package/microservices/microservice-contacts/src/mcp/index.ts +0 -303
  69. package/microservices/microservice-contracts/package.json +0 -28
  70. package/microservices/microservice-contracts/src/cli/index.ts +0 -770
  71. package/microservices/microservice-contracts/src/db/contracts.ts +0 -925
  72. package/microservices/microservice-contracts/src/db/database.ts +0 -91
  73. package/microservices/microservice-contracts/src/db/migrations.ts +0 -141
  74. package/microservices/microservice-contracts/src/index.ts +0 -43
  75. package/microservices/microservice-contracts/src/mcp/index.ts +0 -617
  76. package/microservices/microservice-crm/package.json +0 -28
  77. package/microservices/microservice-crm/src/cli/index.ts +0 -396
  78. package/microservices/microservice-crm/src/db/database.ts +0 -91
  79. package/microservices/microservice-crm/src/db/migrations.ts +0 -66
  80. package/microservices/microservice-crm/src/db/pipeline.ts +0 -397
  81. package/microservices/microservice-crm/src/index.ts +0 -34
  82. package/microservices/microservice-crm/src/mcp/index.ts +0 -294
  83. package/microservices/microservice-documents/package.json +0 -28
  84. package/microservices/microservice-documents/src/cli/index.ts +0 -246
  85. package/microservices/microservice-documents/src/db/database.ts +0 -91
  86. package/microservices/microservice-documents/src/db/documents.ts +0 -316
  87. package/microservices/microservice-documents/src/db/migrations.ts +0 -49
  88. package/microservices/microservice-documents/src/index.ts +0 -24
  89. package/microservices/microservice-documents/src/mcp/index.ts +0 -202
  90. package/microservices/microservice-domains/package.json +0 -28
  91. package/microservices/microservice-domains/src/cli/index.ts +0 -1111
  92. package/microservices/microservice-domains/src/db/database.ts +0 -91
  93. package/microservices/microservice-domains/src/db/domains.ts +0 -1164
  94. package/microservices/microservice-domains/src/db/migrations.ts +0 -60
  95. package/microservices/microservice-domains/src/index.ts +0 -65
  96. package/microservices/microservice-domains/src/lib/brandsight.ts +0 -350
  97. package/microservices/microservice-domains/src/lib/godaddy.ts +0 -338
  98. package/microservices/microservice-domains/src/lib/namecheap.ts +0 -262
  99. package/microservices/microservice-domains/src/lib/registrar.ts +0 -355
  100. package/microservices/microservice-domains/src/mcp/index.ts +0 -781
  101. package/microservices/microservice-expenses/package.json +0 -28
  102. package/microservices/microservice-expenses/src/cli/index.ts +0 -267
  103. package/microservices/microservice-expenses/src/db/database.ts +0 -91
  104. package/microservices/microservice-expenses/src/db/expenses.ts +0 -345
  105. package/microservices/microservice-expenses/src/db/migrations.ts +0 -45
  106. package/microservices/microservice-expenses/src/index.ts +0 -25
  107. package/microservices/microservice-expenses/src/mcp/index.ts +0 -196
  108. package/microservices/microservice-habits/package.json +0 -28
  109. package/microservices/microservice-habits/src/cli/index.ts +0 -315
  110. package/microservices/microservice-habits/src/db/database.ts +0 -91
  111. package/microservices/microservice-habits/src/db/habits.ts +0 -451
  112. package/microservices/microservice-habits/src/db/migrations.ts +0 -46
  113. package/microservices/microservice-habits/src/index.ts +0 -31
  114. package/microservices/microservice-habits/src/mcp/index.ts +0 -313
  115. package/microservices/microservice-health/package.json +0 -28
  116. package/microservices/microservice-health/src/cli/index.ts +0 -484
  117. package/microservices/microservice-health/src/db/database.ts +0 -91
  118. package/microservices/microservice-health/src/db/health.ts +0 -708
  119. package/microservices/microservice-health/src/db/migrations.ts +0 -70
  120. package/microservices/microservice-health/src/index.ts +0 -63
  121. package/microservices/microservice-health/src/mcp/index.ts +0 -437
  122. package/microservices/microservice-hiring/package.json +0 -28
  123. package/microservices/microservice-hiring/src/cli/index.ts +0 -741
  124. package/microservices/microservice-hiring/src/db/database.ts +0 -91
  125. package/microservices/microservice-hiring/src/db/hiring.ts +0 -1085
  126. package/microservices/microservice-hiring/src/db/migrations.ts +0 -89
  127. package/microservices/microservice-hiring/src/index.ts +0 -80
  128. package/microservices/microservice-hiring/src/lib/scoring.ts +0 -206
  129. package/microservices/microservice-hiring/src/mcp/index.ts +0 -709
  130. package/microservices/microservice-inventory/package.json +0 -28
  131. package/microservices/microservice-inventory/src/cli/index.ts +0 -365
  132. package/microservices/microservice-inventory/src/db/database.ts +0 -91
  133. package/microservices/microservice-inventory/src/db/inventory.ts +0 -393
  134. package/microservices/microservice-inventory/src/db/migrations.ts +0 -54
  135. package/microservices/microservice-inventory/src/index.ts +0 -28
  136. package/microservices/microservice-inventory/src/mcp/index.ts +0 -250
  137. package/microservices/microservice-invoices/dashboard/index.html +0 -12
  138. package/microservices/microservice-invoices/dashboard/package.json +0 -29
  139. package/microservices/microservice-invoices/dashboard/tsconfig.json +0 -14
  140. package/microservices/microservice-invoices/dashboard/vite.config.ts +0 -15
  141. package/microservices/microservice-invoices/package.json +0 -31
  142. package/microservices/microservice-invoices/src/cli/index.ts +0 -308
  143. package/microservices/microservice-invoices/src/db/business.ts +0 -241
  144. package/microservices/microservice-invoices/src/db/clients.ts +0 -127
  145. package/microservices/microservice-invoices/src/db/database.ts +0 -91
  146. package/microservices/microservice-invoices/src/db/invoices.ts +0 -345
  147. package/microservices/microservice-invoices/src/db/migrations.ts +0 -184
  148. package/microservices/microservice-invoices/src/index.ts +0 -56
  149. package/microservices/microservice-invoices/src/mcp/index.ts +0 -242
  150. package/microservices/microservice-invoices/src/server/index.ts +0 -162
  151. package/microservices/microservice-leads/package.json +0 -28
  152. package/microservices/microservice-leads/src/cli/index.ts +0 -596
  153. package/microservices/microservice-leads/src/db/database.ts +0 -91
  154. package/microservices/microservice-leads/src/db/leads.ts +0 -520
  155. package/microservices/microservice-leads/src/db/lists.ts +0 -151
  156. package/microservices/microservice-leads/src/db/migrations.ts +0 -93
  157. package/microservices/microservice-leads/src/index.ts +0 -65
  158. package/microservices/microservice-leads/src/lib/enrichment.ts +0 -202
  159. package/microservices/microservice-leads/src/lib/scoring.ts +0 -134
  160. package/microservices/microservice-leads/src/mcp/index.ts +0 -533
  161. package/microservices/microservice-notes/package.json +0 -28
  162. package/microservices/microservice-notes/src/cli/index.ts +0 -63
  163. package/microservices/microservice-notes/src/db/database.ts +0 -91
  164. package/microservices/microservice-notes/src/db/migrations.ts +0 -40
  165. package/microservices/microservice-notes/src/db/notes.ts +0 -114
  166. package/microservices/microservice-notes/src/index.ts +0 -2
  167. package/microservices/microservice-notes/src/mcp/index.ts +0 -37
  168. package/microservices/microservice-notifications/package.json +0 -28
  169. package/microservices/microservice-notifications/src/cli/index.ts +0 -349
  170. package/microservices/microservice-notifications/src/db/database.ts +0 -91
  171. package/microservices/microservice-notifications/src/db/migrations.ts +0 -62
  172. package/microservices/microservice-notifications/src/db/notifications.ts +0 -509
  173. package/microservices/microservice-notifications/src/index.ts +0 -41
  174. package/microservices/microservice-notifications/src/mcp/index.ts +0 -422
  175. package/microservices/microservice-payments/package.json +0 -28
  176. package/microservices/microservice-payments/src/cli/index.ts +0 -609
  177. package/microservices/microservice-payments/src/db/database.ts +0 -91
  178. package/microservices/microservice-payments/src/db/migrations.ts +0 -81
  179. package/microservices/microservice-payments/src/db/payments.ts +0 -1204
  180. package/microservices/microservice-payments/src/index.ts +0 -51
  181. package/microservices/microservice-payments/src/mcp/index.ts +0 -683
  182. package/microservices/microservice-payroll/package.json +0 -28
  183. package/microservices/microservice-payroll/src/cli/index.ts +0 -643
  184. package/microservices/microservice-payroll/src/db/database.ts +0 -91
  185. package/microservices/microservice-payroll/src/db/migrations.ts +0 -95
  186. package/microservices/microservice-payroll/src/db/payroll.ts +0 -1377
  187. package/microservices/microservice-payroll/src/index.ts +0 -48
  188. package/microservices/microservice-payroll/src/mcp/index.ts +0 -666
  189. package/microservices/microservice-products/package.json +0 -28
  190. package/microservices/microservice-products/src/cli/index.ts +0 -416
  191. package/microservices/microservice-products/src/db/categories.ts +0 -154
  192. package/microservices/microservice-products/src/db/database.ts +0 -91
  193. package/microservices/microservice-products/src/db/migrations.ts +0 -58
  194. package/microservices/microservice-products/src/db/pricing-tiers.ts +0 -66
  195. package/microservices/microservice-products/src/db/products.ts +0 -452
  196. package/microservices/microservice-products/src/index.ts +0 -53
  197. package/microservices/microservice-products/src/mcp/index.ts +0 -453
  198. package/microservices/microservice-projects/package.json +0 -28
  199. package/microservices/microservice-projects/src/cli/index.ts +0 -480
  200. package/microservices/microservice-projects/src/db/database.ts +0 -91
  201. package/microservices/microservice-projects/src/db/migrations.ts +0 -65
  202. package/microservices/microservice-projects/src/db/projects.ts +0 -715
  203. package/microservices/microservice-projects/src/index.ts +0 -57
  204. package/microservices/microservice-projects/src/mcp/index.ts +0 -501
  205. package/microservices/microservice-proposals/package.json +0 -28
  206. package/microservices/microservice-proposals/src/cli/index.ts +0 -400
  207. package/microservices/microservice-proposals/src/db/database.ts +0 -91
  208. package/microservices/microservice-proposals/src/db/migrations.ts +0 -52
  209. package/microservices/microservice-proposals/src/db/proposals.ts +0 -532
  210. package/microservices/microservice-proposals/src/index.ts +0 -37
  211. package/microservices/microservice-proposals/src/mcp/index.ts +0 -375
  212. package/microservices/microservice-reading/package.json +0 -28
  213. package/microservices/microservice-reading/src/cli/index.ts +0 -464
  214. package/microservices/microservice-reading/src/db/database.ts +0 -91
  215. package/microservices/microservice-reading/src/db/migrations.ts +0 -59
  216. package/microservices/microservice-reading/src/db/reading.ts +0 -524
  217. package/microservices/microservice-reading/src/index.ts +0 -51
  218. package/microservices/microservice-reading/src/mcp/index.ts +0 -368
  219. package/microservices/microservice-shipping/package.json +0 -28
  220. package/microservices/microservice-shipping/src/cli/index.ts +0 -606
  221. package/microservices/microservice-shipping/src/db/database.ts +0 -91
  222. package/microservices/microservice-shipping/src/db/migrations.ts +0 -69
  223. package/microservices/microservice-shipping/src/db/shipping.ts +0 -1093
  224. package/microservices/microservice-shipping/src/index.ts +0 -53
  225. package/microservices/microservice-shipping/src/mcp/index.ts +0 -533
  226. package/microservices/microservice-social/package.json +0 -29
  227. package/microservices/microservice-social/src/cli/index.ts +0 -1583
  228. package/microservices/microservice-social/src/db/database.ts +0 -91
  229. package/microservices/microservice-social/src/db/migrations.ts +0 -160
  230. package/microservices/microservice-social/src/db/social.ts +0 -1076
  231. package/microservices/microservice-social/src/index.ts +0 -46
  232. package/microservices/microservice-social/src/lib/audience.ts +0 -353
  233. package/microservices/microservice-social/src/lib/content-ai.ts +0 -278
  234. package/microservices/microservice-social/src/lib/media.ts +0 -311
  235. package/microservices/microservice-social/src/lib/mentions.ts +0 -434
  236. package/microservices/microservice-social/src/lib/metrics-sync.ts +0 -264
  237. package/microservices/microservice-social/src/lib/publisher.ts +0 -377
  238. package/microservices/microservice-social/src/lib/scheduler.ts +0 -229
  239. package/microservices/microservice-social/src/lib/sentiment.ts +0 -256
  240. package/microservices/microservice-social/src/lib/threads.ts +0 -291
  241. package/microservices/microservice-social/src/mcp/index.ts +0 -1425
  242. package/microservices/microservice-social/src/server/index.ts +0 -441
  243. package/microservices/microservice-subscriptions/package.json +0 -28
  244. package/microservices/microservice-subscriptions/src/cli/index.ts +0 -715
  245. package/microservices/microservice-subscriptions/src/db/database.ts +0 -91
  246. package/microservices/microservice-subscriptions/src/db/migrations.ts +0 -125
  247. package/microservices/microservice-subscriptions/src/db/subscriptions.ts +0 -1256
  248. package/microservices/microservice-subscriptions/src/index.ts +0 -41
  249. package/microservices/microservice-subscriptions/src/mcp/index.ts +0 -631
  250. package/microservices/microservice-timesheets/package.json +0 -28
  251. package/microservices/microservice-timesheets/src/cli/index.ts +0 -373
  252. package/microservices/microservice-timesheets/src/db/database.ts +0 -91
  253. package/microservices/microservice-timesheets/src/db/locale.ts +0 -217
  254. package/microservices/microservice-timesheets/src/db/migrations.ts +0 -74
  255. package/microservices/microservice-timesheets/src/db/timesheets.ts +0 -447
  256. package/microservices/microservice-timesheets/src/index.ts +0 -44
  257. package/microservices/microservice-timesheets/src/mcp/index.ts +0 -269
  258. package/microservices/microservice-transcriber/package.json +0 -29
  259. package/microservices/microservice-transcriber/src/cli/index.ts +0 -1593
  260. package/microservices/microservice-transcriber/src/db/annotations.ts +0 -37
  261. package/microservices/microservice-transcriber/src/db/comments.ts +0 -166
  262. package/microservices/microservice-transcriber/src/db/database.ts +0 -91
  263. package/microservices/microservice-transcriber/src/db/migrations.ts +0 -118
  264. package/microservices/microservice-transcriber/src/db/proofread.ts +0 -119
  265. package/microservices/microservice-transcriber/src/db/transcripts.ts +0 -395
  266. package/microservices/microservice-transcriber/src/index.ts +0 -43
  267. package/microservices/microservice-transcriber/src/lib/config.ts +0 -77
  268. package/microservices/microservice-transcriber/src/lib/diff.ts +0 -91
  269. package/microservices/microservice-transcriber/src/lib/downloader.ts +0 -638
  270. package/microservices/microservice-transcriber/src/lib/feeds.ts +0 -62
  271. package/microservices/microservice-transcriber/src/lib/live.ts +0 -94
  272. package/microservices/microservice-transcriber/src/lib/notion.ts +0 -129
  273. package/microservices/microservice-transcriber/src/lib/proofread.ts +0 -296
  274. package/microservices/microservice-transcriber/src/lib/providers.ts +0 -713
  275. package/microservices/microservice-transcriber/src/lib/summarizer.ts +0 -147
  276. package/microservices/microservice-transcriber/src/lib/translator.ts +0 -75
  277. package/microservices/microservice-transcriber/src/lib/webhook.ts +0 -37
  278. package/microservices/microservice-transcriber/src/mcp/index.ts +0 -1330
  279. package/microservices/microservice-transcriber/src/server/index.ts +0 -199
  280. package/microservices/microservice-travel/package.json +0 -28
  281. package/microservices/microservice-travel/src/cli/index.ts +0 -505
  282. package/microservices/microservice-travel/src/db/database.ts +0 -91
  283. package/microservices/microservice-travel/src/db/migrations.ts +0 -77
  284. package/microservices/microservice-travel/src/db/travel.ts +0 -802
  285. package/microservices/microservice-travel/src/index.ts +0 -60
  286. package/microservices/microservice-travel/src/mcp/index.ts +0 -495
  287. package/microservices/microservice-wiki/package.json +0 -28
  288. package/microservices/microservice-wiki/src/cli/index.ts +0 -345
  289. package/microservices/microservice-wiki/src/db/database.ts +0 -91
  290. package/microservices/microservice-wiki/src/db/migrations.ts +0 -55
  291. package/microservices/microservice-wiki/src/db/wiki.ts +0 -395
  292. package/microservices/microservice-wiki/src/index.ts +0 -32
  293. package/microservices/microservice-wiki/src/mcp/index.ts +0 -344
@@ -1,564 +0,0 @@
1
- /**
2
- * Analytics CRUD operations — KPIs, Dashboards, Reports
3
- */
4
-
5
- import { getDatabase } from "./database.js";
6
-
7
- // ─── KPI Types ───
8
-
9
- export interface Kpi {
10
- id: string;
11
- name: string;
12
- category: string | null;
13
- value: number;
14
- period: string | null;
15
- source_service: string | null;
16
- metadata: Record<string, unknown>;
17
- recorded_at: string;
18
- }
19
-
20
- interface KpiRow {
21
- id: string;
22
- name: string;
23
- category: string | null;
24
- value: number;
25
- period: string | null;
26
- source_service: string | null;
27
- metadata: string;
28
- recorded_at: string;
29
- }
30
-
31
- function rowToKpi(row: KpiRow): Kpi {
32
- return {
33
- ...row,
34
- metadata: JSON.parse(row.metadata || "{}"),
35
- };
36
- }
37
-
38
- // ─── Dashboard Types ───
39
-
40
- export interface Dashboard {
41
- id: string;
42
- name: string;
43
- description: string | null;
44
- widgets: unknown[];
45
- created_at: string;
46
- }
47
-
48
- interface DashboardRow {
49
- id: string;
50
- name: string;
51
- description: string | null;
52
- widgets: string;
53
- created_at: string;
54
- }
55
-
56
- function rowToDashboard(row: DashboardRow): Dashboard {
57
- return {
58
- ...row,
59
- widgets: JSON.parse(row.widgets || "[]"),
60
- };
61
- }
62
-
63
- // ─── Report Types ───
64
-
65
- export type ReportType = "daily" | "weekly" | "monthly" | "quarterly" | "annual" | "custom";
66
-
67
- export interface Report {
68
- id: string;
69
- name: string;
70
- type: ReportType;
71
- content: string | null;
72
- period: string | null;
73
- generated_at: string;
74
- metadata: Record<string, unknown>;
75
- }
76
-
77
- interface ReportRow {
78
- id: string;
79
- name: string;
80
- type: ReportType;
81
- content: string | null;
82
- period: string | null;
83
- generated_at: string;
84
- metadata: string;
85
- }
86
-
87
- function rowToReport(row: ReportRow): Report {
88
- return {
89
- ...row,
90
- metadata: JSON.parse(row.metadata || "{}"),
91
- };
92
- }
93
-
94
- // ─── KPI Operations ───
95
-
96
- export interface RecordKpiInput {
97
- name: string;
98
- value: number;
99
- category?: string;
100
- source_service?: string;
101
- period?: string;
102
- metadata?: Record<string, unknown>;
103
- }
104
-
105
- export function recordKpi(input: RecordKpiInput): Kpi {
106
- const db = getDatabase();
107
- const id = crypto.randomUUID();
108
- const metadata = JSON.stringify(input.metadata || {});
109
-
110
- db.prepare(
111
- `INSERT INTO kpis (id, name, value, category, source_service, period, metadata)
112
- VALUES (?, ?, ?, ?, ?, ?, ?)`
113
- ).run(
114
- id,
115
- input.name,
116
- input.value,
117
- input.category || null,
118
- input.source_service || null,
119
- input.period || null,
120
- metadata
121
- );
122
-
123
- return getKpiById(id)!;
124
- }
125
-
126
- export function getKpiById(id: string): Kpi | null {
127
- const db = getDatabase();
128
- const row = db.prepare("SELECT * FROM kpis WHERE id = ?").get(id) as KpiRow | null;
129
- return row ? rowToKpi(row) : null;
130
- }
131
-
132
- export function getKpi(name: string, period?: string): Kpi | null {
133
- const db = getDatabase();
134
- let sql = "SELECT * FROM kpis WHERE name = ?";
135
- const params: unknown[] = [name];
136
-
137
- if (period) {
138
- sql += " AND period = ?";
139
- params.push(period);
140
- }
141
-
142
- sql += " ORDER BY recorded_at DESC LIMIT 1";
143
-
144
- const row = db.prepare(sql).get(...params) as KpiRow | null;
145
- return row ? rowToKpi(row) : null;
146
- }
147
-
148
- export function getKpiTrend(name: string, days: number = 30): Kpi[] {
149
- const db = getDatabase();
150
- const cutoff = new Date(Date.now() - days * 24 * 60 * 60 * 1000).toISOString();
151
-
152
- const rows = db
153
- .prepare(
154
- "SELECT * FROM kpis WHERE name = ? AND recorded_at >= ? ORDER BY recorded_at ASC"
155
- )
156
- .all(name, cutoff) as KpiRow[];
157
-
158
- return rows.map(rowToKpi);
159
- }
160
-
161
- export interface ListKpisOptions {
162
- category?: string;
163
- source_service?: string;
164
- limit?: number;
165
- offset?: number;
166
- }
167
-
168
- export function listKpis(options: ListKpisOptions = {}): Kpi[] {
169
- const db = getDatabase();
170
- const conditions: string[] = [];
171
- const params: unknown[] = [];
172
-
173
- if (options.category) {
174
- conditions.push("category = ?");
175
- params.push(options.category);
176
- }
177
-
178
- if (options.source_service) {
179
- conditions.push("source_service = ?");
180
- params.push(options.source_service);
181
- }
182
-
183
- let sql = "SELECT * FROM kpis";
184
- if (conditions.length > 0) {
185
- sql += " WHERE " + conditions.join(" AND ");
186
- }
187
- sql += " ORDER BY recorded_at DESC";
188
-
189
- if (options.limit) {
190
- sql += " LIMIT ?";
191
- params.push(options.limit);
192
- }
193
- if (options.offset) {
194
- sql += " OFFSET ?";
195
- params.push(options.offset);
196
- }
197
-
198
- const rows = db.prepare(sql).all(...params) as KpiRow[];
199
- return rows.map(rowToKpi);
200
- }
201
-
202
- export function getLatestKpis(): Kpi[] {
203
- const db = getDatabase();
204
- // Get the most recent value for each unique KPI name (using MAX(rowid) to break ties)
205
- const rows = db
206
- .prepare(
207
- `SELECT * FROM kpis WHERE rowid IN (
208
- SELECT MAX(rowid) FROM kpis GROUP BY name
209
- ) ORDER BY name`
210
- )
211
- .all() as KpiRow[];
212
-
213
- return rows.map(rowToKpi);
214
- }
215
-
216
- export function deleteKpi(id: string): boolean {
217
- const db = getDatabase();
218
- const result = db.prepare("DELETE FROM kpis WHERE id = ?").run(id);
219
- return result.changes > 0;
220
- }
221
-
222
- // ─── Dashboard Operations ───
223
-
224
- export interface CreateDashboardInput {
225
- name: string;
226
- description?: string;
227
- widgets?: unknown[];
228
- }
229
-
230
- export function createDashboard(input: CreateDashboardInput): Dashboard {
231
- const db = getDatabase();
232
- const id = crypto.randomUUID();
233
- const widgets = JSON.stringify(input.widgets || []);
234
-
235
- db.prepare(
236
- `INSERT INTO dashboards (id, name, description, widgets)
237
- VALUES (?, ?, ?, ?)`
238
- ).run(id, input.name, input.description || null, widgets);
239
-
240
- return getDashboard(id)!;
241
- }
242
-
243
- export function getDashboard(id: string): Dashboard | null {
244
- const db = getDatabase();
245
- const row = db.prepare("SELECT * FROM dashboards WHERE id = ?").get(id) as DashboardRow | null;
246
- return row ? rowToDashboard(row) : null;
247
- }
248
-
249
- export function listDashboards(): Dashboard[] {
250
- const db = getDatabase();
251
- const rows = db
252
- .prepare("SELECT * FROM dashboards ORDER BY created_at DESC")
253
- .all() as DashboardRow[];
254
- return rows.map(rowToDashboard);
255
- }
256
-
257
- export interface UpdateDashboardInput {
258
- name?: string;
259
- description?: string;
260
- widgets?: unknown[];
261
- }
262
-
263
- export function updateDashboard(
264
- id: string,
265
- input: UpdateDashboardInput
266
- ): Dashboard | null {
267
- const db = getDatabase();
268
- const existing = getDashboard(id);
269
- if (!existing) return null;
270
-
271
- const sets: string[] = [];
272
- const params: unknown[] = [];
273
-
274
- if (input.name !== undefined) {
275
- sets.push("name = ?");
276
- params.push(input.name);
277
- }
278
- if (input.description !== undefined) {
279
- sets.push("description = ?");
280
- params.push(input.description);
281
- }
282
- if (input.widgets !== undefined) {
283
- sets.push("widgets = ?");
284
- params.push(JSON.stringify(input.widgets));
285
- }
286
-
287
- if (sets.length === 0) return existing;
288
-
289
- params.push(id);
290
-
291
- db.prepare(
292
- `UPDATE dashboards SET ${sets.join(", ")} WHERE id = ?`
293
- ).run(...params);
294
-
295
- return getDashboard(id);
296
- }
297
-
298
- export function deleteDashboard(id: string): boolean {
299
- const db = getDatabase();
300
- const result = db.prepare("DELETE FROM dashboards WHERE id = ?").run(id);
301
- return result.changes > 0;
302
- }
303
-
304
- // ─── Report Operations ───
305
-
306
- export interface GenerateReportInput {
307
- name: string;
308
- type: ReportType;
309
- period?: string;
310
- metadata?: Record<string, unknown>;
311
- }
312
-
313
- export function generateReport(input: GenerateReportInput): Report {
314
- const db = getDatabase();
315
- const id = crypto.randomUUID();
316
- const metadata = JSON.stringify(input.metadata || {});
317
-
318
- // Build report content from current KPIs
319
- const latestKpis = getLatestKpis();
320
- const categories = new Map<string, Kpi[]>();
321
- for (const kpi of latestKpis) {
322
- const cat = kpi.category || "Uncategorized";
323
- if (!categories.has(cat)) categories.set(cat, []);
324
- categories.get(cat)!.push(kpi);
325
- }
326
-
327
- const lines: string[] = [];
328
- lines.push(`=== ${input.type.toUpperCase()} REPORT: ${input.name} ===`);
329
- if (input.period) lines.push(`Period: ${input.period}`);
330
- lines.push(`Generated: ${new Date().toISOString()}`);
331
- lines.push("");
332
-
333
- for (const [category, kpis] of categories) {
334
- lines.push(`--- ${category} ---`);
335
- for (const kpi of kpis) {
336
- lines.push(` ${kpi.name}: ${kpi.value}${kpi.period ? ` (${kpi.period})` : ""}`);
337
- }
338
- lines.push("");
339
- }
340
-
341
- if (latestKpis.length === 0) {
342
- lines.push("No KPIs recorded yet.");
343
- }
344
-
345
- const content = lines.join("\n");
346
-
347
- db.prepare(
348
- `INSERT INTO reports (id, name, type, content, period, metadata)
349
- VALUES (?, ?, ?, ?, ?, ?)`
350
- ).run(id, input.name, input.type, content, input.period || null, metadata);
351
-
352
- return getReport(id)!;
353
- }
354
-
355
- export function getReport(id: string): Report | null {
356
- const db = getDatabase();
357
- const row = db.prepare("SELECT * FROM reports WHERE id = ?").get(id) as ReportRow | null;
358
- return row ? rowToReport(row) : null;
359
- }
360
-
361
- export interface ListReportsOptions {
362
- type?: ReportType;
363
- limit?: number;
364
- offset?: number;
365
- }
366
-
367
- export function listReports(options: ListReportsOptions = {}): Report[] {
368
- const db = getDatabase();
369
- const conditions: string[] = [];
370
- const params: unknown[] = [];
371
-
372
- if (options.type) {
373
- conditions.push("type = ?");
374
- params.push(options.type);
375
- }
376
-
377
- let sql = "SELECT * FROM reports";
378
- if (conditions.length > 0) {
379
- sql += " WHERE " + conditions.join(" AND ");
380
- }
381
- sql += " ORDER BY generated_at DESC";
382
-
383
- if (options.limit) {
384
- sql += " LIMIT ?";
385
- params.push(options.limit);
386
- }
387
- if (options.offset) {
388
- sql += " OFFSET ?";
389
- params.push(options.offset);
390
- }
391
-
392
- const rows = db.prepare(sql).all(...params) as ReportRow[];
393
- return rows.map(rowToReport);
394
- }
395
-
396
- export function deleteReport(id: string): boolean {
397
- const db = getDatabase();
398
- const result = db.prepare("DELETE FROM reports WHERE id = ?").run(id);
399
- return result.changes > 0;
400
- }
401
-
402
- // ─── Business Health ───
403
-
404
- export interface BusinessHealth {
405
- total_kpis: number;
406
- categories: { category: string; count: number; latest_value: number }[];
407
- latest_kpis: Kpi[];
408
- report_count: number;
409
- dashboard_count: number;
410
- }
411
-
412
- export function getBusinessHealth(): BusinessHealth {
413
- const db = getDatabase();
414
-
415
- const totalKpis = (
416
- db.prepare("SELECT COUNT(DISTINCT name) as count FROM kpis").get() as { count: number }
417
- ).count;
418
-
419
- const categoryRows = db
420
- .prepare(
421
- `SELECT category, COUNT(DISTINCT name) as count
422
- FROM kpis WHERE category IS NOT NULL
423
- GROUP BY category ORDER BY category`
424
- )
425
- .all() as { category: string; count: number }[];
426
-
427
- // Get latest value per category
428
- const categories = categoryRows.map((row) => {
429
- const latestInCat = db
430
- .prepare(
431
- `SELECT value FROM kpis WHERE category = ? ORDER BY recorded_at DESC LIMIT 1`
432
- )
433
- .get(row.category) as { value: number } | null;
434
- return {
435
- category: row.category,
436
- count: row.count,
437
- latest_value: latestInCat?.value ?? 0,
438
- };
439
- });
440
-
441
- const latestKpis = getLatestKpis();
442
-
443
- const reportCount = (
444
- db.prepare("SELECT COUNT(*) as count FROM reports").get() as { count: number }
445
- ).count;
446
-
447
- const dashboardCount = (
448
- db.prepare("SELECT COUNT(*) as count FROM dashboards").get() as { count: number }
449
- ).count;
450
-
451
- return {
452
- total_kpis: totalKpis,
453
- categories,
454
- latest_kpis: latestKpis,
455
- report_count: reportCount,
456
- dashboard_count: dashboardCount,
457
- };
458
- }
459
-
460
- // ─── AI Executive Summary ───
461
-
462
- export async function generateExecutiveSummary(): Promise<string> {
463
- const health = getBusinessHealth();
464
- const latestKpis = getLatestKpis();
465
-
466
- // Build context for AI
467
- const kpiSummary = latestKpis
468
- .map((k) => `${k.name} (${k.category || "uncategorized"}): ${k.value}`)
469
- .join("\n");
470
-
471
- const prompt = `You are a business analyst. Generate a concise executive summary based on these KPIs:
472
-
473
- ${kpiSummary || "No KPIs recorded yet."}
474
-
475
- Total unique KPIs: ${health.total_kpis}
476
- Categories: ${health.categories.map((c) => `${c.category} (${c.count} KPIs)`).join(", ") || "none"}
477
- Reports generated: ${health.report_count}
478
- Dashboards: ${health.dashboard_count}
479
-
480
- Provide a brief, actionable executive summary in 3-5 sentences.`;
481
-
482
- // Try Anthropic first, then OpenAI, then fallback
483
- const anthropicKey = process.env["ANTHROPIC_API_KEY"];
484
- const openaiKey = process.env["OPENAI_API_KEY"];
485
-
486
- if (anthropicKey) {
487
- try {
488
- const response = await fetch("https://api.anthropic.com/v1/messages", {
489
- method: "POST",
490
- headers: {
491
- "Content-Type": "application/json",
492
- "x-api-key": anthropicKey,
493
- "anthropic-version": "2023-06-01",
494
- },
495
- body: JSON.stringify({
496
- model: "claude-sonnet-4-20250514",
497
- max_tokens: 500,
498
- messages: [{ role: "user", content: prompt }],
499
- }),
500
- });
501
-
502
- if (response.ok) {
503
- const data = (await response.json()) as {
504
- content: { type: string; text: string }[];
505
- };
506
- return data.content[0].text;
507
- }
508
- } catch {
509
- // Fall through to OpenAI
510
- }
511
- }
512
-
513
- if (openaiKey) {
514
- try {
515
- const response = await fetch("https://api.openai.com/v1/chat/completions", {
516
- method: "POST",
517
- headers: {
518
- "Content-Type": "application/json",
519
- Authorization: `Bearer ${openaiKey}`,
520
- },
521
- body: JSON.stringify({
522
- model: "gpt-4o-mini",
523
- max_tokens: 500,
524
- messages: [{ role: "user", content: prompt }],
525
- }),
526
- });
527
-
528
- if (response.ok) {
529
- const data = (await response.json()) as {
530
- choices: { message: { content: string } }[];
531
- };
532
- return data.choices[0].message.content;
533
- }
534
- } catch {
535
- // Fall through to local summary
536
- }
537
- }
538
-
539
- // Local fallback — no AI API keys
540
- const lines: string[] = [];
541
- lines.push("=== Executive Summary ===");
542
- lines.push("");
543
-
544
- if (latestKpis.length === 0) {
545
- lines.push("No KPIs have been recorded yet. Start tracking key metrics to enable business health reporting.");
546
- return lines.join("\n");
547
- }
548
-
549
- lines.push(`Tracking ${health.total_kpis} unique KPI(s) across ${health.categories.length} category(s).`);
550
-
551
- for (const cat of health.categories) {
552
- lines.push(`- ${cat.category}: ${cat.count} KPI(s), latest value: ${cat.latest_value}`);
553
- }
554
-
555
- if (health.report_count > 0) {
556
- lines.push(`\n${health.report_count} report(s) have been generated.`);
557
- }
558
-
559
- if (health.dashboard_count > 0) {
560
- lines.push(`${health.dashboard_count} dashboard(s) configured.`);
561
- }
562
-
563
- return lines.join("\n");
564
- }
@@ -1,91 +0,0 @@
1
- /**
2
- * Database connection for microservice-analytics
3
- */
4
-
5
- import { SqliteAdapter } from "@hasna/cloud";
6
- import type { Database } from "bun:sqlite";
7
- import { existsSync, mkdirSync, cpSync } from "node:fs";
8
- import { dirname, join, resolve } from "node:path";
9
- import { MIGRATIONS } from "./migrations.js";
10
-
11
- let _db: Database | null = null;
12
-
13
- function getDbPath(): string {
14
- const explicit = process.env["HASNA_MICROSERVICES_DIR"] ?? process.env["MICROSERVICES_DIR"];
15
- if (explicit) {
16
- return join(explicit, "microservice-analytics", "data.db");
17
- }
18
-
19
- let dir = resolve(process.cwd());
20
- while (true) {
21
- const msDir = join(dir, ".microservices");
22
- if (existsSync(msDir)) return join(msDir, "microservice-analytics", "data.db");
23
- const parent = dirname(dir);
24
- if (parent === dir) break;
25
- dir = parent;
26
- }
27
-
28
- const home = process.env["HOME"] || process.env["USERPROFILE"] || "~";
29
- const newDir = join(home, ".hasna", "microservices");
30
- const oldDir = join(home, ".microservices");
31
- if (!existsSync(newDir) && existsSync(oldDir)) {
32
- mkdirSync(join(home, ".hasna"), { recursive: true });
33
- cpSync(oldDir, newDir, { recursive: true });
34
- }
35
- return join(newDir, "microservice-analytics", "data.db");
36
- }
37
-
38
- export function getDatabase(): Database {
39
- if (_db) return _db;
40
-
41
- const dbPath = getDbPath();
42
- const dir = dirname(resolve(dbPath));
43
- if (!existsSync(dir)) {
44
- mkdirSync(dir, { recursive: true });
45
- }
46
-
47
- const adapter = new SqliteAdapter(dbPath);
48
- _db = adapter.raw;
49
- // SqliteAdapter already sets WAL and foreign_keys
50
-
51
- _db.exec(`
52
- CREATE TABLE IF NOT EXISTS _migrations (
53
- id INTEGER PRIMARY KEY,
54
- name TEXT NOT NULL,
55
- applied_at TEXT NOT NULL DEFAULT (datetime('now'))
56
- )
57
- `);
58
-
59
- const applied = _db
60
- .query("SELECT id FROM _migrations ORDER BY id")
61
- .all() as { id: number }[];
62
- const appliedIds = new Set(applied.map((r) => r.id));
63
-
64
- for (const migration of MIGRATIONS) {
65
- if (appliedIds.has(migration.id)) continue;
66
-
67
- _db.exec("BEGIN");
68
- try {
69
- _db.exec(migration.sql);
70
- _db.prepare("INSERT INTO _migrations (id, name) VALUES (?, ?)").run(
71
- migration.id,
72
- migration.name
73
- );
74
- _db.exec("COMMIT");
75
- } catch (error) {
76
- _db.exec("ROLLBACK");
77
- throw new Error(
78
- `Migration ${migration.id} (${migration.name}) failed: ${error instanceof Error ? error.message : String(error)}`
79
- );
80
- }
81
- }
82
-
83
- return _db;
84
- }
85
-
86
- export function closeDatabase(): void {
87
- if (_db) {
88
- _db.close();
89
- _db = null;
90
- }
91
- }
@@ -1,50 +0,0 @@
1
- export interface MigrationEntry {
2
- id: number;
3
- name: string;
4
- sql: string;
5
- }
6
-
7
- export const MIGRATIONS: MigrationEntry[] = [
8
- {
9
- id: 1,
10
- name: "initial_schema",
11
- sql: `
12
- CREATE TABLE IF NOT EXISTS kpis (
13
- id TEXT PRIMARY KEY,
14
- name TEXT NOT NULL,
15
- category TEXT,
16
- value REAL NOT NULL,
17
- period TEXT,
18
- source_service TEXT,
19
- metadata TEXT NOT NULL DEFAULT '{}',
20
- recorded_at TEXT NOT NULL DEFAULT (datetime('now'))
21
- );
22
-
23
- CREATE TABLE IF NOT EXISTS dashboards (
24
- id TEXT PRIMARY KEY,
25
- name TEXT NOT NULL,
26
- description TEXT,
27
- widgets TEXT NOT NULL DEFAULT '[]',
28
- created_at TEXT NOT NULL DEFAULT (datetime('now'))
29
- );
30
-
31
- CREATE TABLE IF NOT EXISTS reports (
32
- id TEXT PRIMARY KEY,
33
- name TEXT NOT NULL,
34
- type TEXT NOT NULL CHECK(type IN ('daily','weekly','monthly','quarterly','annual','custom')),
35
- content TEXT,
36
- period TEXT,
37
- generated_at TEXT NOT NULL DEFAULT (datetime('now')),
38
- metadata TEXT NOT NULL DEFAULT '{}'
39
- );
40
-
41
- CREATE INDEX IF NOT EXISTS idx_kpis_name ON kpis(name);
42
- CREATE INDEX IF NOT EXISTS idx_kpis_category ON kpis(category);
43
- CREATE INDEX IF NOT EXISTS idx_kpis_recorded_at ON kpis(recorded_at);
44
- CREATE INDEX IF NOT EXISTS idx_kpis_name_recorded ON kpis(name, recorded_at);
45
- CREATE INDEX IF NOT EXISTS idx_reports_type ON reports(type);
46
- CREATE INDEX IF NOT EXISTS idx_reports_generated_at ON reports(generated_at);
47
- CREATE INDEX IF NOT EXISTS idx_dashboards_name ON dashboards(name);
48
- `,
49
- },
50
- ];