medusa-stats 1.0.5

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 (180) hide show
  1. package/.medusa/server/src/admin/composite.png +0 -0
  2. package/.medusa/server/src/admin/index.js +10512 -0
  3. package/.medusa/server/src/admin/index.mjs +10510 -0
  4. package/.medusa/server/src/admin/params.png +0 -0
  5. package/.medusa/server/src/admin/view.png +0 -0
  6. package/.medusa/server/src/admin/visualizations.png +0 -0
  7. package/.medusa/server/src/api/admin/statistics/alert-logs/[id]/route.d.ts +6 -0
  8. package/.medusa/server/src/api/admin/statistics/alert-logs/[id]/route.js +18 -0
  9. package/.medusa/server/src/api/admin/statistics/alert-logs/route.d.ts +6 -0
  10. package/.medusa/server/src/api/admin/statistics/alert-logs/route.js +33 -0
  11. package/.medusa/server/src/api/admin/statistics/alerts/[id]/logs/route.d.ts +6 -0
  12. package/.medusa/server/src/api/admin/statistics/alerts/[id]/logs/route.js +23 -0
  13. package/.medusa/server/src/api/admin/statistics/alerts/[id]/route.d.ts +17 -0
  14. package/.medusa/server/src/api/admin/statistics/alerts/[id]/route.js +64 -0
  15. package/.medusa/server/src/api/admin/statistics/alerts/[id]/toggle/route.d.ts +6 -0
  16. package/.medusa/server/src/api/admin/statistics/alerts/[id]/toggle/route.js +18 -0
  17. package/.medusa/server/src/api/admin/statistics/alerts/route.d.ts +12 -0
  18. package/.medusa/server/src/api/admin/statistics/alerts/route.js +94 -0
  19. package/.medusa/server/src/api/admin/statistics/charts/[id]/route.d.ts +17 -0
  20. package/.medusa/server/src/api/admin/statistics/charts/[id]/route.js +50 -0
  21. package/.medusa/server/src/api/admin/statistics/charts/[id]/statistics/route.d.ts +15 -0
  22. package/.medusa/server/src/api/admin/statistics/charts/[id]/statistics/route.js +40 -0
  23. package/.medusa/server/src/api/admin/statistics/charts/route.d.ts +4 -0
  24. package/.medusa/server/src/api/admin/statistics/charts/route.js +55 -0
  25. package/.medusa/server/src/api/admin/statistics/options/[id]/calculate/route.d.ts +7 -0
  26. package/.medusa/server/src/api/admin/statistics/options/[id]/calculate/route.js +23 -0
  27. package/.medusa/server/src/api/admin/statistics/options/[id]/clone/route.d.ts +7 -0
  28. package/.medusa/server/src/api/admin/statistics/options/[id]/clone/route.js +15 -0
  29. package/.medusa/server/src/api/admin/statistics/options/[id]/route.d.ts +17 -0
  30. package/.medusa/server/src/api/admin/statistics/options/[id]/route.js +69 -0
  31. package/.medusa/server/src/api/admin/statistics/options/route.d.ts +12 -0
  32. package/.medusa/server/src/api/admin/statistics/options/route.js +87 -0
  33. package/.medusa/server/src/api/admin/statistics/providers/[id]/route.d.ts +6 -0
  34. package/.medusa/server/src/api/admin/statistics/providers/[id]/route.js +18 -0
  35. package/.medusa/server/src/api/admin/statistics/providers/[id]/statistics/route.d.ts +6 -0
  36. package/.medusa/server/src/api/admin/statistics/providers/[id]/statistics/route.js +18 -0
  37. package/.medusa/server/src/api/admin/statistics/providers/route.d.ts +7 -0
  38. package/.medusa/server/src/api/admin/statistics/providers/route.js +57 -0
  39. package/.medusa/server/src/api/admin/statistics/utils/option-graph.d.ts +3 -0
  40. package/.medusa/server/src/api/admin/statistics/utils/option-graph.js +34 -0
  41. package/.medusa/server/src/api/admin/statistics/views/[id]/calculate/route.d.ts +7 -0
  42. package/.medusa/server/src/api/admin/statistics/views/[id]/calculate/route.js +21 -0
  43. package/.medusa/server/src/api/admin/statistics/views/[id]/clone/route.d.ts +7 -0
  44. package/.medusa/server/src/api/admin/statistics/views/[id]/clone/route.js +21 -0
  45. package/.medusa/server/src/api/admin/statistics/views/[id]/route.d.ts +17 -0
  46. package/.medusa/server/src/api/admin/statistics/views/[id]/route.js +71 -0
  47. package/.medusa/server/src/api/admin/statistics/views/route.d.ts +12 -0
  48. package/.medusa/server/src/api/admin/statistics/views/route.js +74 -0
  49. package/.medusa/server/src/api/middlewares.d.ts +2 -0
  50. package/.medusa/server/src/api/middlewares.js +194 -0
  51. package/.medusa/server/src/api/validation/statistics/schemas.d.ts +1013 -0
  52. package/.medusa/server/src/api/validation/statistics/schemas.js +288 -0
  53. package/.medusa/server/src/index.d.ts +1 -0
  54. package/.medusa/server/src/index.js +18 -0
  55. package/.medusa/server/src/jobs/evaluate-statistics-alerts.d.ts +6 -0
  56. package/.medusa/server/src/jobs/evaluate-statistics-alerts.js +110 -0
  57. package/.medusa/server/src/links/statistics-alert-user.d.ts +2 -0
  58. package/.medusa/server/src/links/statistics-alert-user.js +16 -0
  59. package/.medusa/server/src/links/statistics-option-user.d.ts +2 -0
  60. package/.medusa/server/src/links/statistics-option-user.js +16 -0
  61. package/.medusa/server/src/loaders/statistics/index.d.ts +5 -0
  62. package/.medusa/server/src/loaders/statistics/index.js +47 -0
  63. package/.medusa/server/src/modules/statistics/index.d.ts +107 -0
  64. package/.medusa/server/src/modules/statistics/index.js +32 -0
  65. package/.medusa/server/src/modules/statistics/migrations/Migration20260223131741.d.ts +5 -0
  66. package/.medusa/server/src/modules/statistics/migrations/Migration20260223131741.js +57 -0
  67. package/.medusa/server/src/modules/statistics/models/alert-log.d.ts +74 -0
  68. package/.medusa/server/src/modules/statistics/models/alert-log.js +16 -0
  69. package/.medusa/server/src/modules/statistics/models/alert.d.ts +74 -0
  70. package/.medusa/server/src/modules/statistics/models/alert.js +24 -0
  71. package/.medusa/server/src/modules/statistics/models/chart.d.ts +81 -0
  72. package/.medusa/server/src/modules/statistics/models/chart.js +28 -0
  73. package/.medusa/server/src/modules/statistics/models/index.d.ts +7 -0
  74. package/.medusa/server/src/modules/statistics/models/index.js +24 -0
  75. package/.medusa/server/src/modules/statistics/models/option-input.d.ts +135 -0
  76. package/.medusa/server/src/modules/statistics/models/option-input.js +27 -0
  77. package/.medusa/server/src/modules/statistics/models/option.d.ts +78 -0
  78. package/.medusa/server/src/modules/statistics/models/option.js +41 -0
  79. package/.medusa/server/src/modules/statistics/models/provider.d.ts +75 -0
  80. package/.medusa/server/src/modules/statistics/models/provider.js +15 -0
  81. package/.medusa/server/src/modules/statistics/models/view.d.ts +79 -0
  82. package/.medusa/server/src/modules/statistics/models/view.js +30 -0
  83. package/.medusa/server/src/modules/statistics/providers/index.d.ts +1 -0
  84. package/.medusa/server/src/modules/statistics/providers/index.js +18 -0
  85. package/.medusa/server/src/modules/statistics/providers/provider.d.ts +254 -0
  86. package/.medusa/server/src/modules/statistics/providers/provider.js +307 -0
  87. package/.medusa/server/src/modules/statistics/service.d.ts +953 -0
  88. package/.medusa/server/src/modules/statistics/service.js +190 -0
  89. package/.medusa/server/src/modules/statistics/utils/dependency-option-map.d.ts +13 -0
  90. package/.medusa/server/src/modules/statistics/utils/dependency-option-map.js +67 -0
  91. package/.medusa/server/src/modules/statistics/utils/period-utils.d.ts +23 -0
  92. package/.medusa/server/src/modules/statistics/utils/period-utils.js +78 -0
  93. package/.medusa/server/src/modules/statistics/utils/time-series-utils.d.ts +6 -0
  94. package/.medusa/server/src/modules/statistics/utils/time-series-utils.js +8 -0
  95. package/.medusa/server/src/providers/common/index.d.ts +2 -0
  96. package/.medusa/server/src/providers/common/index.js +1232 -0
  97. package/.medusa/server/src/providers/composite/index.d.ts +2 -0
  98. package/.medusa/server/src/providers/composite/index.js +147 -0
  99. package/.medusa/server/src/workflows/statistics/calculate-single-statistic.d.ts +15 -0
  100. package/.medusa/server/src/workflows/statistics/calculate-single-statistic.js +28 -0
  101. package/.medusa/server/src/workflows/statistics/calculate-statistics.d.ts +20 -0
  102. package/.medusa/server/src/workflows/statistics/calculate-statistics.js +15 -0
  103. package/.medusa/server/src/workflows/statistics/calculate-view.d.ts +31 -0
  104. package/.medusa/server/src/workflows/statistics/calculate-view.js +30 -0
  105. package/.medusa/server/src/workflows/statistics/clone-view.d.ts +227 -0
  106. package/.medusa/server/src/workflows/statistics/clone-view.js +10 -0
  107. package/.medusa/server/src/workflows/statistics/create-alert.d.ts +139 -0
  108. package/.medusa/server/src/workflows/statistics/create-alert.js +12 -0
  109. package/.medusa/server/src/workflows/statistics/create-chart.d.ts +122 -0
  110. package/.medusa/server/src/workflows/statistics/create-chart.js +14 -0
  111. package/.medusa/server/src/workflows/statistics/create-view-with-options.d.ts +1342 -0
  112. package/.medusa/server/src/workflows/statistics/create-view-with-options.js +29 -0
  113. package/.medusa/server/src/workflows/statistics/delete-chart.d.ts +10 -0
  114. package/.medusa/server/src/workflows/statistics/delete-chart.js +14 -0
  115. package/.medusa/server/src/workflows/statistics/evaluate-alerts.d.ts +28 -0
  116. package/.medusa/server/src/workflows/statistics/evaluate-alerts.js +67 -0
  117. package/.medusa/server/src/workflows/statistics/get-available-statistics.d.ts +5 -0
  118. package/.medusa/server/src/workflows/statistics/get-available-statistics.js +10 -0
  119. package/.medusa/server/src/workflows/statistics/index.d.ts +14 -0
  120. package/.medusa/server/src/workflows/statistics/index.js +31 -0
  121. package/.medusa/server/src/workflows/statistics/manage-chart-statistics.d.ts +12 -0
  122. package/.medusa/server/src/workflows/statistics/manage-chart-statistics.js +14 -0
  123. package/.medusa/server/src/workflows/statistics/steps/calculate-statistics.d.ts +37 -0
  124. package/.medusa/server/src/workflows/statistics/steps/calculate-statistics.js +222 -0
  125. package/.medusa/server/src/workflows/statistics/steps/clone-view.d.ts +227 -0
  126. package/.medusa/server/src/workflows/statistics/steps/clone-view.js +39 -0
  127. package/.medusa/server/src/workflows/statistics/steps/create-alert-log-entries.d.ts +17 -0
  128. package/.medusa/server/src/workflows/statistics/steps/create-alert-log-entries.js +50 -0
  129. package/.medusa/server/src/workflows/statistics/steps/create-alert.d.ts +120 -0
  130. package/.medusa/server/src/workflows/statistics/steps/create-alert.js +21 -0
  131. package/.medusa/server/src/workflows/statistics/steps/create-chart.d.ts +118 -0
  132. package/.medusa/server/src/workflows/statistics/steps/create-chart.js +19 -0
  133. package/.medusa/server/src/workflows/statistics/steps/create-statistics-options.d.ts +117 -0
  134. package/.medusa/server/src/workflows/statistics/steps/create-statistics-options.js +16 -0
  135. package/.medusa/server/src/workflows/statistics/steps/create-statistics-view.d.ts +119 -0
  136. package/.medusa/server/src/workflows/statistics/steps/create-statistics-view.js +16 -0
  137. package/.medusa/server/src/workflows/statistics/steps/dedupe-alerts.d.ts +22 -0
  138. package/.medusa/server/src/workflows/statistics/steps/dedupe-alerts.js +64 -0
  139. package/.medusa/server/src/workflows/statistics/steps/delete-chart.d.ts +6 -0
  140. package/.medusa/server/src/workflows/statistics/steps/delete-chart.js +17 -0
  141. package/.medusa/server/src/workflows/statistics/steps/emit-alert-events.d.ts +17 -0
  142. package/.medusa/server/src/workflows/statistics/steps/emit-alert-events.js +41 -0
  143. package/.medusa/server/src/workflows/statistics/steps/evaluate-each-condition.d.ts +15 -0
  144. package/.medusa/server/src/workflows/statistics/steps/evaluate-each-condition.js +109 -0
  145. package/.medusa/server/src/workflows/statistics/steps/extract-alert-values.d.ts +12 -0
  146. package/.medusa/server/src/workflows/statistics/steps/extract-alert-values.js +86 -0
  147. package/.medusa/server/src/workflows/statistics/steps/fetch-active-alerts-for-option.d.ts +112 -0
  148. package/.medusa/server/src/workflows/statistics/steps/fetch-active-alerts-for-option.js +16 -0
  149. package/.medusa/server/src/workflows/statistics/steps/fetch-available-statistics.d.ts +4 -0
  150. package/.medusa/server/src/workflows/statistics/steps/fetch-available-statistics.js +39 -0
  151. package/.medusa/server/src/workflows/statistics/steps/fetch-option-with-relations.d.ts +112 -0
  152. package/.medusa/server/src/workflows/statistics/steps/fetch-option-with-relations.js +11 -0
  153. package/.medusa/server/src/workflows/statistics/steps/fetch-view-with-options.d.ts +112 -0
  154. package/.medusa/server/src/workflows/statistics/steps/fetch-view-with-options.js +20 -0
  155. package/.medusa/server/src/workflows/statistics/steps/manage-chart-statistics.d.ts +8 -0
  156. package/.medusa/server/src/workflows/statistics/steps/manage-chart-statistics.js +26 -0
  157. package/.medusa/server/src/workflows/statistics/steps/update-chart.d.ts +117 -0
  158. package/.medusa/server/src/workflows/statistics/steps/update-chart.js +32 -0
  159. package/.medusa/server/src/workflows/statistics/steps/update-view.d.ts +120 -0
  160. package/.medusa/server/src/workflows/statistics/steps/update-view.js +18 -0
  161. package/.medusa/server/src/workflows/statistics/steps/validate-alert-input.d.ts +1 -0
  162. package/.medusa/server/src/workflows/statistics/steps/validate-alert-input.js +34 -0
  163. package/.medusa/server/src/workflows/statistics/steps/validate-option-parameters.d.ts +21 -0
  164. package/.medusa/server/src/workflows/statistics/steps/validate-option-parameters.js +43 -0
  165. package/.medusa/server/src/workflows/statistics/steps/validate-view-input.d.ts +10 -0
  166. package/.medusa/server/src/workflows/statistics/steps/validate-view-input.js +15 -0
  167. package/.medusa/server/src/workflows/statistics/update-chart.d.ts +120 -0
  168. package/.medusa/server/src/workflows/statistics/update-chart.js +13 -0
  169. package/.medusa/server/src/workflows/statistics/update-view-configuration.d.ts +128 -0
  170. package/.medusa/server/src/workflows/statistics/update-view-configuration.js +18 -0
  171. package/.medusa/server/src/workflows/statistics/utils/cache-utils.d.ts +60 -0
  172. package/.medusa/server/src/workflows/statistics/utils/cache-utils.js +66 -0
  173. package/.medusa/server/src/workflows/statistics/utils/dependency-graph.d.ts +51 -0
  174. package/.medusa/server/src/workflows/statistics/utils/dependency-graph.js +131 -0
  175. package/.medusa/server/src/workflows/statistics/utils/parameter-utils.d.ts +54 -0
  176. package/.medusa/server/src/workflows/statistics/utils/parameter-utils.js +147 -0
  177. package/.medusa/server/src/workflows/statistics/validate-option-configuration.d.ts +9 -0
  178. package/.medusa/server/src/workflows/statistics/validate-option-configuration.js +35 -0
  179. package/README.md +310 -0
  180. package/package.json +87 -0
@@ -0,0 +1,9 @@
1
+ export interface ValidateOptionConfigurationInput {
2
+ option_id: string;
3
+ }
4
+ export declare const validateOptionConfigurationWorkflow: import("@medusajs/framework/workflows-sdk").ReturnWorkflow<ValidateOptionConfigurationInput, {
5
+ isValid: boolean;
6
+ errors: string[];
7
+ isComplete: boolean;
8
+ statDefinition: any;
9
+ }, []>;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateOptionConfigurationWorkflow = void 0;
4
+ const workflows_sdk_1 = require("@medusajs/framework/workflows-sdk");
5
+ const fetch_option_with_relations_1 = require("./steps/fetch-option-with-relations");
6
+ const fetch_available_statistics_1 = require("./steps/fetch-available-statistics");
7
+ const validate_option_parameters_1 = require("./steps/validate-option-parameters");
8
+ exports.validateOptionConfigurationWorkflow = (0, workflows_sdk_1.createWorkflow)("validate-option-configuration", (input) => {
9
+ const option = (0, fetch_option_with_relations_1.fetchOptionWithRelationsStep)({ option_id: input.option_id });
10
+ const availableStatistics = (0, fetch_available_statistics_1.fetchAvailableStatisticsStep)({});
11
+ const validation = (0, validate_option_parameters_1.validateOptionParametersStep)({
12
+ options: [
13
+ {
14
+ provider_option_name: option.provider_option_name,
15
+ local_option_name: option.local_option_name,
16
+ data: option.data,
17
+ provider_id: option.provider.id,
18
+ },
19
+ ],
20
+ availableStatistics,
21
+ partialValidation: false,
22
+ throwOnError: false,
23
+ });
24
+ const result = (0, workflows_sdk_1.transform)(validation, (data) => {
25
+ const firstResult = data.validationResults[0];
26
+ return {
27
+ isValid: firstResult?.isValid ?? false,
28
+ errors: firstResult?.errors ?? ["Invalid option configuration"],
29
+ isComplete: firstResult?.isComplete ?? false,
30
+ statDefinition: firstResult?.statDefinition,
31
+ };
32
+ });
33
+ return new workflows_sdk_1.WorkflowResponse(result);
34
+ });
35
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidmFsaWRhdGUtb3B0aW9uLWNvbmZpZ3VyYXRpb24uanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi8uLi9zcmMvd29ya2Zsb3dzL3N0YXRpc3RpY3MvdmFsaWRhdGUtb3B0aW9uLWNvbmZpZ3VyYXRpb24udHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7O0FBQUEscUVBQWdHO0FBQ2hHLHFGQUFtRjtBQUNuRixtRkFBa0Y7QUFDbEYsbUZBQWtGO0FBTXJFLFFBQUEsbUNBQW1DLEdBQUcsSUFBQSw4QkFBYyxFQUM3RCwrQkFBK0IsRUFDL0IsQ0FBQyxLQUF1QyxFQUFFLEVBQUU7SUFFeEMsTUFBTSxNQUFNLEdBQUcsSUFBQSwwREFBNEIsRUFBQyxFQUFFLFNBQVMsRUFBRSxLQUFLLENBQUMsU0FBUyxFQUFFLENBQUMsQ0FBQztJQUc1RSxNQUFNLG1CQUFtQixHQUFHLElBQUEseURBQTRCLEVBQUMsRUFBRSxDQUFDLENBQUM7SUFHN0QsTUFBTSxVQUFVLEdBQUcsSUFBQSx5REFBNEIsRUFBQztRQUM1QyxPQUFPLEVBQUU7WUFDTDtnQkFDSSxvQkFBb0IsRUFBRSxNQUFNLENBQUMsb0JBQW9CO2dCQUNqRCxpQkFBaUIsRUFBRSxNQUFNLENBQUMsaUJBQWlCO2dCQUMzQyxJQUFJLEVBQUUsTUFBTSxDQUFDLElBQUk7Z0JBQ2pCLFdBQVcsRUFBRSxNQUFNLENBQUMsUUFBUSxDQUFDLEVBQUU7YUFDbEM7U0FDSjtRQUNELG1CQUFtQjtRQUNuQixpQkFBaUIsRUFBRSxLQUFLO1FBQ3hCLFlBQVksRUFBRSxLQUFLO0tBQ3RCLENBQUMsQ0FBQztJQUVILE1BQU0sTUFBTSxHQUFHLElBQUEseUJBQVMsRUFBQyxVQUFVLEVBQUUsQ0FBQyxJQUFJLEVBQUUsRUFBRTtRQUMxQyxNQUFNLFdBQVcsR0FBRyxJQUFJLENBQUMsaUJBQWlCLENBQUMsQ0FBQyxDQUFDLENBQUM7UUFFOUMsT0FBTztZQUNILE9BQU8sRUFBRSxXQUFXLEVBQUUsT0FBTyxJQUFJLEtBQUs7WUFDdEMsTUFBTSxFQUFFLFdBQVcsRUFBRSxNQUFNLElBQUksQ0FBQyw4QkFBOEIsQ0FBQztZQUMvRCxVQUFVLEVBQUUsV0FBVyxFQUFFLFVBQVUsSUFBSSxLQUFLO1lBQzVDLGNBQWMsRUFBRSxXQUFXLEVBQUUsY0FBYztTQUM5QyxDQUFDO0lBQ04sQ0FBQyxDQUFDLENBQUM7SUFFSCxPQUFPLElBQUksZ0NBQWdCLENBQUMsTUFBTSxDQUFDLENBQUM7QUFDeEMsQ0FBQyxDQUNKLENBQUMifQ==
package/README.md ADDED
@@ -0,0 +1,310 @@
1
+ <p align="center">
2
+ <a href="https://www.medusajs.com">
3
+ <picture>
4
+ <source media="(prefers-color-scheme: dark)" srcset="https://user-images.githubusercontent.com/59018053/229103275-b5e482bb-4601-46e6-8142-244f531cebdb.svg">
5
+ <source media="(prefers-color-scheme: light)" srcset="https://user-images.githubusercontent.com/59018053/229103726-e5b529a3-9b3f-4970-8a1f-c6af37f087bf.svg">
6
+ <img alt="Medusa logo" src="https://user-images.githubusercontent.com/59018053/229103726-e5b529a3-9b3f-4970-8a1f-c6af37f087bf.svg">
7
+ </picture>
8
+ </a>
9
+ </p>
10
+
11
+ <h2 align="center">
12
+ Medusa Stats
13
+ </h2>
14
+
15
+ <br>
16
+
17
+ ## Overview
18
+
19
+ `medusa-stats` is a flexible statistics module for Medusa that features:
20
+
21
+ - Pluggable statistics providers
22
+ - Views, options, and chart composition
23
+ - Composite statistics (statistics depending on other statistics)
24
+ - Statistics-triggered configurable alerts
25
+ - UI interface and admin routes for full statistics lifecycle management
26
+ - Caching for optimized performance
27
+
28
+ ## Installation
29
+
30
+ ```bash
31
+ npm install medusa-stats
32
+ # or
33
+ yarn add medusa-stats
34
+ ```
35
+
36
+
37
+
38
+ ## Configuration
39
+
40
+ Add the plugin to your `medusa-config.ts`:
41
+
42
+ ```typescript
43
+ modules: [
44
+ {
45
+ resolve: "medusa-stats/modules/statistics",
46
+ dependencies: [ContainerRegistrationKeys.QUERY], // Query dependency is required in order to be injected into the providers
47
+ options: {
48
+ providers: [
49
+ {
50
+ resolve: "medusa-stats/providers/common",
51
+ },
52
+ ],
53
+ },
54
+ },
55
+ ],
56
+ plugins: [
57
+ `medusa-stats`
58
+ ]
59
+ ```
60
+
61
+ ## Basic Usage
62
+ The plugin provides a framework for defining and calculating statistics as well as an admin interface for their management and visualization. The primary goal of the module is to provide a flexible way to create custom statistics and views that suit your business needs, without being limited to predefined metrics.
63
+
64
+ The module can also be used out-of-the-box with pre-defined statistics providers and the admin interface.
65
+
66
+ ### Views
67
+ Views are collections of related statistics visualizations. They allow you to organize statistics in any way that suits your needs. Every chart in a view can display multiple statistical measurements, each based on a different statistic option.
68
+
69
+ ![Views Image](https://github.com/tax1driver/medusa-stats/blob/bf9e67fccc7fb29c2153e4b06194309b6177c12b/public/view.png)
70
+
71
+ ### Options
72
+ Options are instances of statistics that are calculated with specific parameters. By changing an option's parameters, you can adjust the underlying statistic calculation.
73
+
74
+ ![Options Parameters Image](https://github.com/tax1driver/medusa-stats/blob/bf9e67fccc7fb29c2153e4b06194309b6177c12b/public/params.png)
75
+
76
+ When editing, options can also be configured in terms of their visualization (chart type, dimensions, etc.), cache settings and other parameters.
77
+
78
+ ![Visualization Settings Image](https://github.com/tax1driver/medusa-stats/blob/bf9e67fccc7fb29c2153e4b06194309b6177c12b/public/visualizations.png)
79
+
80
+ ## Providers
81
+
82
+ Providers define what statistics are available and how they are calculated.
83
+
84
+ ### Add provider to project
85
+
86
+ Register your provider in `medusa-config.ts` under the `medusa-stats` module:
87
+
88
+ ```typescript
89
+ modules: [
90
+ {
91
+ resolve: "medusa-stats",
92
+ options: {
93
+ providers: [
94
+ { resolve: "medusa-stats/providers/common" },
95
+ { resolve: "medusa-stats/providers/composite" },
96
+ { resolve: "./src/providers/statistics/my-provider" },
97
+ ],
98
+ },
99
+ },
100
+ ]
101
+ ```
102
+ ### Included providers
103
+
104
+ `medusa-stats` includes two built-in providers:
105
+
106
+ - `medusa-stats/providers/common`
107
+ - General commerce statistics (orders, carts, sales, channels, regions, and related aggregates).
108
+ - `medusa-stats/providers/composite`
109
+ - Composite/stat-transform statistics that consume other statistic outputs.
110
+ - Included statistics: `moving_average`, `rate_of_change`.
111
+
112
+
113
+ ### Creating a Statistics Provider
114
+
115
+ Create a provider class by extending `AbstractStatisticsProvider`, expose available statistics in `getAvailableStatistics`, and implement calculation logic in `calculateStatistic`.
116
+
117
+ Example: a `total_cart_value` statistic (available in the Common Statistics Provider) with filters for currency and cart status.
118
+
119
+ ```typescript
120
+ import { ModuleProvider } from "@medusajs/framework/utils"
121
+ import {
122
+ AbstractStatisticsProvider,
123
+ StatBuilder,
124
+ createTimeSeries,
125
+ sum,
126
+ type AvailableStatistic,
127
+ type CalculateStatisticInput,
128
+ type StatisticResult,
129
+ } from "medusa-stats"
130
+
131
+ class MyStatisticsProvider extends AbstractStatisticsProvider {
132
+ static identifier = "my-statistics"
133
+ static displayName = "My Statistics Provider"
134
+
135
+ async getAvailableStatistics(): Promise<AvailableStatistic[]> {
136
+ return [
137
+ new StatBuilder("total_cart_value", "Total Cart Value")
138
+ .description("Total value of all carts over time")
139
+ .field({
140
+ name: "currency_code",
141
+ label: "Currency",
142
+ description: "Filter by currency code",
143
+ schema: z.string().optional(),
144
+ fieldType: "text",
145
+ placeholder: "USD"
146
+ })
147
+ .chart("line")
148
+ .dimension("time")
149
+ .build(),
150
+ ]
151
+ }
152
+
153
+ async calculateStatistic(input: CalculateStatisticInput): Promise<StatisticResult> {
154
+ const { id, parameters, periodStart, periodEnd, interval } = input;
155
+
156
+ switch(id) {
157
+ case "total_cart_value": {
158
+ const currencyCode = parameters.currency_code;
159
+
160
+ const filters: any = {
161
+ created_at: { $gte: periodStart, $lte: periodEnd }
162
+ };
163
+
164
+ if (currencyCode) {
165
+ filters.currency_code = currencyCode;
166
+ }
167
+
168
+ const { data: carts } = await this.query.graph({
169
+ entity: "cart",
170
+ fields: ["id", "created_at", "total"],
171
+ filters
172
+ });
173
+
174
+ const timeSeries = createTimeSeries( // helper function for time series creation
175
+ carts,
176
+ periodStart,
177
+ periodEnd,
178
+ interval,
179
+ sum('total') // helper accumulator function
180
+ );
181
+
182
+ return {
183
+ value: timeSeries,
184
+ metadata: { totalCarts: carts.length }
185
+ };
186
+ }
187
+ }
188
+ }
189
+ }
190
+
191
+ export default ModuleProvider("statistics", {
192
+ services: [MyStatisticsProvider],
193
+ })
194
+ ```
195
+
196
+
197
+
198
+ ## Composite Statistics
199
+
200
+ Composite statistics allow one statistic option to use another option's output as an input.
201
+
202
+ ### Admin Usage
203
+ A stat option can be configured to receive another statistic's output by settings its value in the Dependecnies section when editing a stat instance in the admin dashboard.
204
+
205
+ ![Dependencies Image](https://github.com/tax1driver/medusa-stats/blob/bf9e67fccc7fb29c2153e4b06194309b6177c12b/public/composite.png)
206
+
207
+ ### Using composite fields in providers
208
+
209
+ To make a statistic composable, define a provider parameter with `fieldType: "stat"`.
210
+ At runtime, that parameter receives dependency output data and can be processed like any other input.
211
+
212
+ ```typescript
213
+ import { z } from "zod"
214
+ import { StatBuilder } from "medusa-stats"
215
+
216
+ new StatBuilder("moving_average", "Moving Average")
217
+ .description("Smooth a time series by averaging values over a rolling window")
218
+ .field(
219
+ {
220
+ name: "input_series",
221
+ label: "Input Series",
222
+ description: "Dependency result to analyze",
223
+ fieldType: "stat",
224
+ schema: TimeSeriesSchema
225
+ }
226
+ )
227
+ .field(
228
+ {
229
+ name: "window_size",
230
+ label: "Window Size",
231
+ fieldType: "number",
232
+ schema: z.number().int().min(2).max(365).default(7),
233
+ },
234
+ 7 // initial value
235
+ )
236
+ .build()
237
+ ```
238
+
239
+ ## Alerts
240
+
241
+ Alerts can be configured per option to trigger when conditions are met.
242
+
243
+ ### Features
244
+
245
+ - Absolute and comparative conditions
246
+ - Alert logs
247
+ - Scheduled evaluation job
248
+ - Event emission for custom handling
249
+
250
+ ### Emitted Events
251
+ To handle the alert triggers, register a subscriber for the `statistics.alert` event type.
252
+
253
+ ```typescript
254
+ type StatisticsAlertEventData = {
255
+ alert_id: string
256
+ alert_name: string
257
+ severity: "info" | "warning" | "critical"
258
+ option_id: string
259
+ current_value: number
260
+ reference_value: number | null
261
+ compare_value: number | [number, number]
262
+ operator: "lt" | "gt" | "lte" | "gte" | "eq" | "neq" | "between"
263
+ comparison_type: string
264
+ }
265
+ ```
266
+
267
+ ## Caching
268
+ In order to optimize performance, statistic results are cached for a configurable amount of time. When a statistic is requested, the module first checks if a valid cached result exists and returns it if available. If not, it calculates the statistic, stores the result in the cache, and then returns it.
269
+
270
+ ### Cache Configuration
271
+ To use caching, `CachingModule` must be enabled in the Medusa project.
272
+
273
+ ```typescript
274
+ {
275
+ resolve: "@medusajs/medusa/caching",
276
+ options: {
277
+ providers: [
278
+ {
279
+ resolve: "@medusajs/caching-redis",
280
+ id: "caching-redis",
281
+ is_default: true,
282
+ options: {
283
+ redisUrl: process.env.CACHE_REDIS_URL,
284
+ },
285
+ },
286
+ ],
287
+ },
288
+ },
289
+ ```
290
+
291
+ The CachingModule's feature flag needs to be enabled as well:
292
+
293
+ *in `.env` file:*
294
+ ```env
295
+ MEDUSA_FF_CACHING=true
296
+ ```
297
+
298
+ or
299
+
300
+ *in `medusa-config.ts`:*
301
+ ```typescript
302
+ featureFlags: {
303
+ caching: true,
304
+ }
305
+ ```
306
+
307
+ ## Planned features and improvements
308
+ - More built-in chart types and visualization options
309
+ - Visualization providers for custom chart types and UI components
310
+ - Visualization on entities' details pages (e.g. product statistics on product page)
package/package.json ADDED
@@ -0,0 +1,87 @@
1
+ {
2
+ "name": "medusa-stats",
3
+ "version": "1.0.5",
4
+ "description": "The most flexible statistics plugin for the most flexible commerce platform.",
5
+ "author": "Maciej (https://github.com/tax1driver)",
6
+ "license": "MIT",
7
+ "files": [
8
+ ".medusa/server"
9
+ ],
10
+ "exports": {
11
+ "./package.json": "./package.json",
12
+ "./workflows": "./.medusa/server/src/workflows/index.js",
13
+ "./.medusa/server/src/modules/*": "./.medusa/server/src/modules/*/index.js",
14
+ "./modules/*": "./.medusa/server/src/modules/*/index.js",
15
+ "./providers/*": "./.medusa/server/src/providers/*/index.js",
16
+ "./*": "./.medusa/server/src/*.js",
17
+ "./admin": {
18
+ "import": "./.medusa/server/src/admin/index.mjs",
19
+ "require": "./.medusa/server/src/admin/index.js",
20
+ "default": "./.medusa/server/src/admin/index.js"
21
+ },
22
+ ".": {
23
+ "import": "./.medusa/server/src/index.mjs",
24
+ "require": "./.medusa/server/src/index.js",
25
+ "default": "./.medusa/server/src/index.js"
26
+ }
27
+ },
28
+ "keywords": [
29
+ "medusa",
30
+ "plugin",
31
+ "medusa-plugin-stats",
32
+ "medusa-plugin-metrics",
33
+ "medusa-plugin",
34
+ "medusa-v2"
35
+ ],
36
+ "scripts": {
37
+ "build": "medusa plugin:build",
38
+ "dev": "medusa plugin:develop",
39
+ "dev:publish": "yarn version patch && medusa plugin:publish",
40
+ "prepublishOnly": "medusa plugin:build",
41
+ "test:integration:http": "TEST_TYPE=integration:http NODE_OPTIONS=--experimental-vm-modules jest --silent=false --runInBand --forceExit",
42
+ "test:integration:modules": "TEST_TYPE=integration:modules NODE_OPTIONS=--experimental-vm-modules jest --silent=false --runInBand --forceExit",
43
+ "test:unit": "TEST_TYPE=unit NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit"
44
+ },
45
+ "devDependencies": {
46
+ "@medusajs/admin-sdk": "2.12.4",
47
+ "@medusajs/cli": "2.12.4",
48
+ "@medusajs/framework": "2.12.4",
49
+ "@medusajs/icons": "2.12.4",
50
+ "@medusajs/medusa": "2.12.4",
51
+ "@medusajs/test-utils": "^2.13.1",
52
+ "@medusajs/ui": "4.0.25",
53
+ "@swc/core": "^1.7.28",
54
+ "@swc/jest": "^0.2.39",
55
+ "@types/jest": "^30.0.0",
56
+ "@types/node": "^20.0.0",
57
+ "@types/react": "^18.3.2",
58
+ "@types/react-dom": "^18.2.25",
59
+ "@uiw/react-json-view": "^2.0.0-alpha.41",
60
+ "date-fns": "^4.1.0",
61
+ "jest": "^30.2.0",
62
+ "prop-types": "^15.8.1",
63
+ "react": "^18.2.0",
64
+ "react-dom": "^18.2.0",
65
+ "ts-node": "^10.9.2",
66
+ "typescript": "^5.6.2",
67
+ "vite": "^5.2.11",
68
+ "yalc": "^1.0.0-pre.53"
69
+ },
70
+ "peerDependencies": {
71
+ "@medusajs/admin-sdk": "^2.12.4",
72
+ "@medusajs/cli": "^2.12.4",
73
+ "@medusajs/framework": "^2.12.4",
74
+ "@medusajs/icons": "^2.12.4",
75
+ "@medusajs/medusa": "^2.12.4",
76
+ "@medusajs/test-utils": "^2.12.4",
77
+ "@medusajs/ui": "^4.0.25"
78
+ },
79
+ "dependencies": {
80
+ "react-colorful": "^5.6.1",
81
+ "recharts": "^3.7.0"
82
+ },
83
+ "engines": {
84
+ "node": ">=20"
85
+ },
86
+ "packageManager": "yarn@4.12.0"
87
+ }