@oneuptime/common 12.0.2 → 12.0.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.
Files changed (228) hide show
  1. package/Models/DatabaseModels/DatabaseBaseModel/DatabaseBaseModel.ts +14 -0
  2. package/Models/DatabaseModels/Index.ts +2 -0
  3. package/Models/DatabaseModels/LogDropFilter.ts +9 -20
  4. package/Models/DatabaseModels/StatusPageMonitorRule.ts +911 -0
  5. package/Models/DatabaseModels/StatusPageResource.ts +73 -0
  6. package/Models/DatabaseModels/TraceDropFilter.ts +9 -20
  7. package/Server/API/BaseAPI.ts +13 -2
  8. package/Server/API/DashboardAPI.ts +39 -0
  9. package/Server/EnvironmentConfig.ts +31 -1
  10. package/Server/Infrastructure/GlobalCache.ts +115 -0
  11. package/Server/Infrastructure/Postgres/DataSourceOptions.ts +16 -0
  12. package/Server/Infrastructure/Postgres/SchemaMigrations/1786005052769-AddStatusPageMonitorRule.ts +123 -0
  13. package/Server/Infrastructure/Postgres/SchemaMigrations/Index.ts +2 -0
  14. package/Server/Services/AlertStateTimelineService.ts +20 -0
  15. package/Server/Services/CephClusterService.ts +26 -86
  16. package/Server/Services/CloudResourceService.ts +23 -83
  17. package/Server/Services/DatabaseService.ts +111 -0
  18. package/Server/Services/DockerHostService.ts +19 -79
  19. package/Server/Services/DockerSwarmClusterService.ts +27 -88
  20. package/Server/Services/HostService.ts +46 -99
  21. package/Server/Services/IncidentStateTimelineService.ts +20 -0
  22. package/Server/Services/Index.ts +2 -0
  23. package/Server/Services/IoTFleetService.ts +19 -79
  24. package/Server/Services/KubernetesClusterService.ts +17 -78
  25. package/Server/Services/LabelService.ts +57 -10
  26. package/Server/Services/MetricTypeService.ts +84 -0
  27. package/Server/Services/MonitorService.ts +89 -14
  28. package/Server/Services/MonitorStatusTimelineService.ts +29 -0
  29. package/Server/Services/MonitorTemplateService.ts +65 -0
  30. package/Server/Services/PodmanHostService.ts +19 -79
  31. package/Server/Services/ProxmoxClusterService.ts +23 -83
  32. package/Server/Services/RumApplicationService.ts +19 -80
  33. package/Server/Services/ScheduledMaintenanceStateTimelineService.ts +21 -0
  34. package/Server/Services/ServerlessFunctionService.ts +24 -85
  35. package/Server/Services/ServiceService.ts +299 -73
  36. package/Server/Services/StatusPageMonitorRuleEngineService.ts +752 -0
  37. package/Server/Services/StatusPageMonitorRuleService.ts +471 -0
  38. package/Server/Services/TelemetryEntityRelationshipService.ts +1 -0
  39. package/Server/Services/TelemetryEntityService.ts +1 -0
  40. package/Server/Utils/Database/ProjectScopedReferenceValidator.ts +170 -39
  41. package/Server/Utils/Monitor/MonitorResource.ts +63 -36
  42. package/Server/Utils/Monitor/MonitorStatusTimeline.ts +36 -0
  43. package/Server/Utils/Monitor/MonitorStepsProjectValidator.ts +148 -127
  44. package/Server/Utils/Monitor/MonitorStepsReferenceExtractor.ts +434 -0
  45. package/Server/Utils/SingleFlight.ts +83 -0
  46. package/Server/Utils/StatusPage/MonitorRulePatternValidator.ts +46 -0
  47. package/Server/Utils/Telemetry/EntityRegistry.ts +107 -26
  48. package/Server/Utils/Telemetry/ResourceHeartbeat.ts +314 -0
  49. package/Server/Utils/Telemetry/Telemetry.ts +351 -122
  50. package/Tests/App/Dashboard/WidgetCatalog.test.ts +2 -0
  51. package/Tests/Models/DatabaseModels/ColumnTransformerInvariants.test.ts +306 -0
  52. package/Tests/Models/DatabaseModels/NumberColumnDeserialization.test.ts +333 -0
  53. package/Tests/Models/DatabaseModels/StatusPageMonitorRuleModel.test.ts +190 -0
  54. package/Tests/Server/API/DashboardPublicResourceListAPI.test.ts +26 -0
  55. package/Tests/Server/Infrastructure/GlobalCache.test.ts +226 -0
  56. package/Tests/Server/Infrastructure/PostgresLockTimeoutOptions.test.ts +181 -0
  57. package/Tests/Server/Services/DatabaseServiceUpdateColumnsIfUnlocked.test.ts +342 -0
  58. package/Tests/Server/Services/DropFilterSaveValidation.test.ts +245 -0
  59. package/Tests/Server/Services/HostServiceUpdateLastSeen.test.ts +163 -15
  60. package/Tests/Server/Services/MetricTypeAttachServices.test.ts +222 -0
  61. package/Tests/Server/Services/MissingReferenceWriteGuard.test.ts +614 -0
  62. package/Tests/Server/Services/MonitorStatusTimelineFastPath.test.ts +23 -0
  63. package/Tests/Server/Services/ResourceUpdateLastSeenLivenessFallback.test.ts +305 -56
  64. package/Tests/Server/Services/ServiceUpdateLastSeenSplitGates.test.ts +705 -0
  65. package/Tests/Server/Services/StatusPageMonitorRuleEngineService.test.ts +1719 -0
  66. package/Tests/Server/Services/StatusPageMonitorRuleHooks.test.ts +1212 -0
  67. package/Tests/Server/Services/StatusPageMonitorRuleMonitorCreateHook.test.ts +228 -0
  68. package/Tests/Server/Utils/APIKey/AccessPermission.test.ts +267 -0
  69. package/Tests/Server/Utils/Database/ProjectScopedReferenceValidator.test.ts +260 -13
  70. package/Tests/Server/Utils/EntityRegistryRowFence.test.ts +302 -0
  71. package/Tests/Server/Utils/MetricTypeIndexConvergence.test.ts +598 -0
  72. package/Tests/Server/Utils/Monitor/MonitorStatusTimelineUnusableStatus.test.ts +154 -0
  73. package/Tests/Server/Utils/Monitor/MonitorStepsProjectValidator.test.ts +638 -92
  74. package/Tests/Server/Utils/Monitor/MonitorStepsReferenceExtractor.test.ts +724 -0
  75. package/Tests/Server/Utils/SingleFlight.test.ts +221 -0
  76. package/Tests/Types/Dashboard/NetworkDashboardTemplate.test.ts +793 -0
  77. package/Tests/Types/Database/BigIntColumnTransformer.test.ts +147 -0
  78. package/Tests/Types/Database/DatabasePropertyTransformer.test.ts +140 -0
  79. package/Tests/Types/Database/NumericColumnValue.test.ts +127 -0
  80. package/Tests/Types/Decimal.test.ts +37 -0
  81. package/Tests/Types/Monitor/SnmpVersionParsing.test.ts +152 -0
  82. package/Tests/Types/Port.test.ts +38 -0
  83. package/Tests/UI/Components/LogsHistogram.test.tsx +276 -0
  84. package/Tests/UI/Components/UseLiveLogsRefresh.test.tsx +489 -0
  85. package/Tests/UI/Components/UseLogsHistogram.test.tsx +500 -0
  86. package/Tests/Utils/Dashboard/Components/DashboardComponentsUtil.test.ts +120 -0
  87. package/Tests/Utils/Dashboard/DashboardNetworkMapComponent.test.ts +327 -0
  88. package/Types/Dashboard/DashboardComponentType.ts +1 -0
  89. package/Types/Dashboard/DashboardComponents/ComponentArgument.ts +1 -0
  90. package/Types/Dashboard/DashboardComponents/DashboardNetworkMapComponent.ts +40 -0
  91. package/Types/Dashboard/DashboardTemplates.ts +397 -0
  92. package/Types/Database/BigIntColumnTransformer.ts +74 -0
  93. package/Types/Database/DatabaseProperty.ts +26 -0
  94. package/Types/Database/NumericColumnValue.ts +128 -0
  95. package/Types/Decimal.ts +16 -4
  96. package/Types/Permission.ts +48 -0
  97. package/Types/Port.ts +16 -4
  98. package/UI/Components/LogsViewer/useLiveLogsRefresh.ts +86 -0
  99. package/UI/Components/LogsViewer/useLogsHistogram.ts +113 -0
  100. package/Utils/Dashboard/Components/DashboardNetworkMapComponent.ts +136 -0
  101. package/Utils/Dashboard/Components/Index.ts +7 -0
  102. package/build/dist/Models/DatabaseModels/DatabaseBaseModel/DatabaseBaseModel.js +12 -0
  103. package/build/dist/Models/DatabaseModels/DatabaseBaseModel/DatabaseBaseModel.js.map +1 -1
  104. package/build/dist/Models/DatabaseModels/Index.js +2 -0
  105. package/build/dist/Models/DatabaseModels/Index.js.map +1 -1
  106. package/build/dist/Models/DatabaseModels/LogDropFilter.js +9 -20
  107. package/build/dist/Models/DatabaseModels/LogDropFilter.js.map +1 -1
  108. package/build/dist/Models/DatabaseModels/StatusPageMonitorRule.js +925 -0
  109. package/build/dist/Models/DatabaseModels/StatusPageMonitorRule.js.map +1 -0
  110. package/build/dist/Models/DatabaseModels/StatusPageResource.js +72 -0
  111. package/build/dist/Models/DatabaseModels/StatusPageResource.js.map +1 -1
  112. package/build/dist/Models/DatabaseModels/TraceDropFilter.js +9 -20
  113. package/build/dist/Models/DatabaseModels/TraceDropFilter.js.map +1 -1
  114. package/build/dist/Server/API/BaseAPI.js +11 -1
  115. package/build/dist/Server/API/BaseAPI.js.map +1 -1
  116. package/build/dist/Server/API/DashboardAPI.js +39 -0
  117. package/build/dist/Server/API/DashboardAPI.js.map +1 -1
  118. package/build/dist/Server/EnvironmentConfig.js +27 -1
  119. package/build/dist/Server/EnvironmentConfig.js.map +1 -1
  120. package/build/dist/Server/Infrastructure/GlobalCache.js +91 -0
  121. package/build/dist/Server/Infrastructure/GlobalCache.js.map +1 -1
  122. package/build/dist/Server/Infrastructure/Postgres/DataSourceOptions.js +5 -3
  123. package/build/dist/Server/Infrastructure/Postgres/DataSourceOptions.js.map +1 -1
  124. package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/1786005052769-AddStatusPageMonitorRule.js +48 -0
  125. package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/1786005052769-AddStatusPageMonitorRule.js.map +1 -0
  126. package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/Index.js +2 -0
  127. package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/Index.js.map +1 -1
  128. package/build/dist/Server/Services/AlertStateTimelineService.js +19 -0
  129. package/build/dist/Server/Services/AlertStateTimelineService.js.map +1 -1
  130. package/build/dist/Server/Services/CephClusterService.js +26 -76
  131. package/build/dist/Server/Services/CephClusterService.js.map +1 -1
  132. package/build/dist/Server/Services/CloudResourceService.js +23 -73
  133. package/build/dist/Server/Services/CloudResourceService.js.map +1 -1
  134. package/build/dist/Server/Services/DatabaseService.js +86 -0
  135. package/build/dist/Server/Services/DatabaseService.js.map +1 -1
  136. package/build/dist/Server/Services/DockerHostService.js +19 -69
  137. package/build/dist/Server/Services/DockerHostService.js.map +1 -1
  138. package/build/dist/Server/Services/DockerSwarmClusterService.js +27 -78
  139. package/build/dist/Server/Services/DockerSwarmClusterService.js.map +1 -1
  140. package/build/dist/Server/Services/HostService.js +58 -102
  141. package/build/dist/Server/Services/HostService.js.map +1 -1
  142. package/build/dist/Server/Services/IncidentStateTimelineService.js +19 -0
  143. package/build/dist/Server/Services/IncidentStateTimelineService.js.map +1 -1
  144. package/build/dist/Server/Services/Index.js +2 -0
  145. package/build/dist/Server/Services/Index.js.map +1 -1
  146. package/build/dist/Server/Services/IoTFleetService.js +19 -69
  147. package/build/dist/Server/Services/IoTFleetService.js.map +1 -1
  148. package/build/dist/Server/Services/KubernetesClusterService.js +17 -68
  149. package/build/dist/Server/Services/KubernetesClusterService.js.map +1 -1
  150. package/build/dist/Server/Services/LabelService.js +42 -7
  151. package/build/dist/Server/Services/LabelService.js.map +1 -1
  152. package/build/dist/Server/Services/MetricTypeService.js +74 -0
  153. package/build/dist/Server/Services/MetricTypeService.js.map +1 -1
  154. package/build/dist/Server/Services/MonitorService.js +81 -9
  155. package/build/dist/Server/Services/MonitorService.js.map +1 -1
  156. package/build/dist/Server/Services/MonitorStatusTimelineService.js +28 -0
  157. package/build/dist/Server/Services/MonitorStatusTimelineService.js.map +1 -1
  158. package/build/dist/Server/Services/MonitorTemplateService.js +60 -0
  159. package/build/dist/Server/Services/MonitorTemplateService.js.map +1 -1
  160. package/build/dist/Server/Services/PodmanHostService.js +19 -69
  161. package/build/dist/Server/Services/PodmanHostService.js.map +1 -1
  162. package/build/dist/Server/Services/ProxmoxClusterService.js +23 -73
  163. package/build/dist/Server/Services/ProxmoxClusterService.js.map +1 -1
  164. package/build/dist/Server/Services/RumApplicationService.js +19 -70
  165. package/build/dist/Server/Services/RumApplicationService.js.map +1 -1
  166. package/build/dist/Server/Services/ScheduledMaintenanceStateTimelineService.js +20 -0
  167. package/build/dist/Server/Services/ScheduledMaintenanceStateTimelineService.js.map +1 -1
  168. package/build/dist/Server/Services/ServerlessFunctionService.js +24 -75
  169. package/build/dist/Server/Services/ServerlessFunctionService.js.map +1 -1
  170. package/build/dist/Server/Services/ServiceService.js +236 -61
  171. package/build/dist/Server/Services/ServiceService.js.map +1 -1
  172. package/build/dist/Server/Services/StatusPageMonitorRuleEngineService.js +603 -0
  173. package/build/dist/Server/Services/StatusPageMonitorRuleEngineService.js.map +1 -0
  174. package/build/dist/Server/Services/StatusPageMonitorRuleService.js +401 -0
  175. package/build/dist/Server/Services/StatusPageMonitorRuleService.js.map +1 -0
  176. package/build/dist/Server/Services/TelemetryEntityRelationshipService.js +1 -0
  177. package/build/dist/Server/Services/TelemetryEntityRelationshipService.js.map +1 -1
  178. package/build/dist/Server/Services/TelemetryEntityService.js +1 -0
  179. package/build/dist/Server/Services/TelemetryEntityService.js.map +1 -1
  180. package/build/dist/Server/Utils/Database/ProjectScopedReferenceValidator.js +105 -29
  181. package/build/dist/Server/Utils/Database/ProjectScopedReferenceValidator.js.map +1 -1
  182. package/build/dist/Server/Utils/Monitor/MonitorResource.js +56 -32
  183. package/build/dist/Server/Utils/Monitor/MonitorResource.js.map +1 -1
  184. package/build/dist/Server/Utils/Monitor/MonitorStatusTimeline.js +30 -3
  185. package/build/dist/Server/Utils/Monitor/MonitorStatusTimeline.js.map +1 -1
  186. package/build/dist/Server/Utils/Monitor/MonitorStepsProjectValidator.js +110 -98
  187. package/build/dist/Server/Utils/Monitor/MonitorStepsProjectValidator.js.map +1 -1
  188. package/build/dist/Server/Utils/Monitor/MonitorStepsReferenceExtractor.js +312 -0
  189. package/build/dist/Server/Utils/Monitor/MonitorStepsReferenceExtractor.js.map +1 -0
  190. package/build/dist/Server/Utils/SingleFlight.js +76 -0
  191. package/build/dist/Server/Utils/SingleFlight.js.map +1 -0
  192. package/build/dist/Server/Utils/StatusPage/MonitorRulePatternValidator.js +29 -0
  193. package/build/dist/Server/Utils/StatusPage/MonitorRulePatternValidator.js.map +1 -0
  194. package/build/dist/Server/Utils/Telemetry/EntityRegistry.js +78 -19
  195. package/build/dist/Server/Utils/Telemetry/EntityRegistry.js.map +1 -1
  196. package/build/dist/Server/Utils/Telemetry/ResourceHeartbeat.js +160 -0
  197. package/build/dist/Server/Utils/Telemetry/ResourceHeartbeat.js.map +1 -0
  198. package/build/dist/Server/Utils/Telemetry/Telemetry.js +279 -104
  199. package/build/dist/Server/Utils/Telemetry/Telemetry.js.map +1 -1
  200. package/build/dist/Types/Dashboard/DashboardComponentType.js +1 -0
  201. package/build/dist/Types/Dashboard/DashboardComponentType.js.map +1 -1
  202. package/build/dist/Types/Dashboard/DashboardComponents/ComponentArgument.js +1 -0
  203. package/build/dist/Types/Dashboard/DashboardComponents/ComponentArgument.js.map +1 -1
  204. package/build/dist/Types/Dashboard/DashboardComponents/DashboardNetworkMapComponent.js +2 -0
  205. package/build/dist/Types/Dashboard/DashboardComponents/DashboardNetworkMapComponent.js.map +1 -0
  206. package/build/dist/Types/Dashboard/DashboardTemplates.js +368 -0
  207. package/build/dist/Types/Dashboard/DashboardTemplates.js.map +1 -1
  208. package/build/dist/Types/Database/BigIntColumnTransformer.js +66 -0
  209. package/build/dist/Types/Database/BigIntColumnTransformer.js.map +1 -0
  210. package/build/dist/Types/Database/DatabaseProperty.js +25 -0
  211. package/build/dist/Types/Database/DatabaseProperty.js.map +1 -1
  212. package/build/dist/Types/Database/NumericColumnValue.js +97 -0
  213. package/build/dist/Types/Database/NumericColumnValue.js.map +1 -0
  214. package/build/dist/Types/Decimal.js +14 -3
  215. package/build/dist/Types/Decimal.js.map +1 -1
  216. package/build/dist/Types/Permission.js +42 -0
  217. package/build/dist/Types/Permission.js.map +1 -1
  218. package/build/dist/Types/Port.js +15 -5
  219. package/build/dist/Types/Port.js.map +1 -1
  220. package/build/dist/UI/Components/LogsViewer/useLiveLogsRefresh.js +44 -0
  221. package/build/dist/UI/Components/LogsViewer/useLiveLogsRefresh.js.map +1 -0
  222. package/build/dist/UI/Components/LogsViewer/useLogsHistogram.js +75 -0
  223. package/build/dist/UI/Components/LogsViewer/useLogsHistogram.js.map +1 -0
  224. package/build/dist/Utils/Dashboard/Components/DashboardNetworkMapComponent.js +114 -0
  225. package/build/dist/Utils/Dashboard/Components/DashboardNetworkMapComponent.js.map +1 -0
  226. package/build/dist/Utils/Dashboard/Components/Index.js +4 -0
  227. package/build/dist/Utils/Dashboard/Components/Index.js.map +1 -1
  228. package/package.json +1 -1
@@ -0,0 +1,471 @@
1
+ import DatabaseService from "./DatabaseService";
2
+ import StatusPageMonitorRuleEngineService from "./StatusPageMonitorRuleEngineService";
3
+ import StatusPageGroupService from "./StatusPageGroupService";
4
+ import StatusPageResourceService from "./StatusPageResourceService";
5
+ import StatusPageService from "./StatusPageService";
6
+ import Model from "../../Models/DatabaseModels/StatusPageMonitorRule";
7
+ import StatusPageGroup from "../../Models/DatabaseModels/StatusPageGroup";
8
+ import StatusPageResource from "../../Models/DatabaseModels/StatusPageResource";
9
+ import CreateBy from "../Types/Database/CreateBy";
10
+ import DeleteBy from "../Types/Database/DeleteBy";
11
+ import UpdateBy from "../Types/Database/UpdateBy";
12
+ import { OnCreate, OnDelete, OnUpdate } from "../Types/Database/Hooks";
13
+ import { LIMIT_PER_PROJECT } from "../../Types/Database/LimitMax";
14
+ import BadDataException from "../../Types/Exception/BadDataException";
15
+ import ObjectID from "../../Types/ObjectID";
16
+ import CaptureSpan from "../Utils/Telemetry/CaptureSpan";
17
+ import ProjectScopedReferenceValidator, {
18
+ resolveReferenceId,
19
+ } from "../Utils/Database/ProjectScopedReferenceValidator";
20
+ import StatusPageMonitorRulePatternValidator from "../Utils/StatusPage/MonitorRulePatternValidator";
21
+ import logger, { LogAttributes } from "../Utils/Logger";
22
+
23
+ export class Service extends DatabaseService<Model> {
24
+ public constructor() {
25
+ super(Model);
26
+ }
27
+
28
+ @CaptureSpan()
29
+ protected override async onBeforeCreate(
30
+ createBy: CreateBy<Model>,
31
+ ): Promise<OnCreate<Model>> {
32
+ if (!createBy.data.statusPageId) {
33
+ throw new BadDataException(
34
+ "Status Page ID is required to create a status page monitor rule.",
35
+ );
36
+ }
37
+
38
+ this.assertHasMatchCriteria({
39
+ monitorLabelCount: (createBy.data.monitorLabels || []).length,
40
+ monitorNamePattern: createBy.data.monitorNamePattern,
41
+ monitorDescriptionPattern: createBy.data.monitorDescriptionPattern,
42
+ });
43
+
44
+ StatusPageMonitorRulePatternValidator.validate({
45
+ namePattern: createBy.data.monitorNamePattern,
46
+ descriptionPattern: createBy.data.monitorDescriptionPattern,
47
+ });
48
+
49
+ await this.assertReferencesAreInScope({
50
+ projectId: createBy.props.tenantId || createBy.data.projectId,
51
+ statusPageId: resolveReferenceId(
52
+ createBy.data.statusPageId || createBy.data.statusPage,
53
+ ),
54
+ statusPageGroupId: resolveReferenceId(
55
+ createBy.data.statusPageGroupId || createBy.data.statusPageGroup,
56
+ ),
57
+ });
58
+
59
+ return {
60
+ createBy: createBy,
61
+ carryForward: null,
62
+ };
63
+ }
64
+
65
+ @CaptureSpan()
66
+ protected override async onCreateSuccess(
67
+ _onCreate: OnCreate<Model>,
68
+ createdItem: Model,
69
+ ): Promise<Model> {
70
+ /*
71
+ * Backfill: a rule the user just wrote should populate the page with the
72
+ * monitors that already match, not only with ones created after it.
73
+ */
74
+ if (createdItem.id) {
75
+ await this.syncQuietly(createdItem.id);
76
+ }
77
+
78
+ return createdItem;
79
+ }
80
+
81
+ @CaptureSpan()
82
+ protected override async onBeforeUpdate(
83
+ updateBy: UpdateBy<Model>,
84
+ ): Promise<OnUpdate<Model>> {
85
+ StatusPageMonitorRulePatternValidator.validate({
86
+ namePattern: updateBy.data.monitorNamePattern as string | undefined,
87
+ descriptionPattern: updateBy.data.monitorDescriptionPattern as
88
+ | string
89
+ | undefined,
90
+ });
91
+
92
+ /*
93
+ * An edit can empty a rule out just as easily as a create can, and the
94
+ * result is the same useless rule. The merged view is what matters, so
95
+ * fields the edit does not mention are read back off the stored row -
96
+ * clearing only the labels on a rule that also has a name pattern is
97
+ * perfectly fine, and must not be refused.
98
+ */
99
+ await this.assertUpdateKeepsMatchCriteria(updateBy);
100
+
101
+ /*
102
+ * Re-pointing a rule at a different group is an ordinary edit, so the same
103
+ * scope checks the create path makes apply here. statusPageId is
104
+ * create-only in the column ACL, so only the group can move.
105
+ */
106
+ const nextGroupId: ObjectID | string | undefined = resolveReferenceId(
107
+ (updateBy.data as Record<string, unknown>)["statusPageGroupId"] ||
108
+ (updateBy.data as Record<string, unknown>)["statusPageGroup"],
109
+ );
110
+
111
+ if (nextGroupId) {
112
+ for (const rule of await this.findRulesForQuery(updateBy)) {
113
+ await this.assertReferencesAreInScope({
114
+ projectId: updateBy.props.tenantId || rule.projectId,
115
+ statusPageId: rule.statusPageId,
116
+ statusPageGroupId: nextGroupId,
117
+ });
118
+ }
119
+ }
120
+
121
+ return { updateBy, carryForward: null };
122
+ }
123
+
124
+ @CaptureSpan()
125
+ protected override async onUpdateSuccess(
126
+ onUpdate: OnUpdate<Model>,
127
+ updatedItemIds: Array<ObjectID>,
128
+ ): Promise<OnUpdate<Model>> {
129
+ /*
130
+ * Every editable field on this model changes what the rule matches or how
131
+ * the resources it owns are rendered, so any edit re-runs the rule. The
132
+ * sync is idempotent, so re-running it after a no-op edit costs a read.
133
+ */
134
+ for (const id of updatedItemIds) {
135
+ await this.syncQuietly(id);
136
+ }
137
+
138
+ return onUpdate;
139
+ }
140
+
141
+ /**
142
+ * Deleting a rule undoes it: the resources it added go too, and the monitors
143
+ * it was publishing are offered to the rules that survive.
144
+ *
145
+ * Every destructive step lives here rather than in onBeforeDelete, because
146
+ * DatabaseService applies checkDeleteQueryPermission *after* onBeforeDelete.
147
+ * A hook there sees the caller's raw, un-tenant-scoped query, so a
148
+ * root-privileged write from it would let a caller destroy another project's
149
+ * status page resources by naming that project's rule id, with the
150
+ * permission check only rejecting the (already moot) rule delete afterwards.
151
+ * `itemIdsBeforeDelete` is the permission-checked set, so acting on it is
152
+ * safe.
153
+ *
154
+ * By this point the StatusPageResource -> StatusPageMonitorRule foreign key
155
+ * has usually already cascaded the resources away, which makes the removal
156
+ * pass belt-and-braces rather than the primary mechanism. It is kept so the
157
+ * behaviour does not depend on the cascade staying as it is — and it is also
158
+ * why the monitor ids come from carryForward: after the cascade there is
159
+ * nothing left to read them off.
160
+ */
161
+ @CaptureSpan()
162
+ protected override async onDeleteSuccess(
163
+ onDelete: OnDelete<Model>,
164
+ itemIdsBeforeDelete: Array<ObjectID>,
165
+ ): Promise<OnDelete<Model>> {
166
+ const monitorIdsByRuleId: Record<string, Array<string>> = (
167
+ onDelete.carryForward as {
168
+ monitorIdsByRuleId?: Record<string, Array<string>>;
169
+ } | null
170
+ )?.monitorIdsByRuleId || {};
171
+
172
+ for (const statusPageMonitorRuleId of itemIdsBeforeDelete) {
173
+ try {
174
+ await StatusPageMonitorRuleEngineService.removeResourcesAddedByRule({
175
+ statusPageMonitorRuleId: statusPageMonitorRuleId,
176
+ });
177
+ } catch (error) {
178
+ logger.error(
179
+ `Error removing resources added by status page monitor rule ${statusPageMonitorRuleId.toString()}: ${error}`,
180
+ {
181
+ statusPageMonitorRuleId: statusPageMonitorRuleId.toString(),
182
+ } as LogAttributes,
183
+ );
184
+ }
185
+
186
+ /*
187
+ * Deleting a rule must behave like disabling one. Disabling routes
188
+ * through syncResourcesForRule, which hands each released monitor to the
189
+ * other rules on the page; without this, deleting would instead drop a
190
+ * monitor that a sibling rule still claims — the same rule, switched off
191
+ * two different ways, giving two different pages.
192
+ */
193
+ for (const monitorId of monitorIdsByRuleId[
194
+ statusPageMonitorRuleId.toString()
195
+ ] || []) {
196
+ try {
197
+ await StatusPageMonitorRuleEngineService.syncRulesForMonitor({
198
+ monitorId: new ObjectID(monitorId),
199
+ projectId: onDelete.deleteBy.props.tenantId as ObjectID | undefined,
200
+ });
201
+ } catch (error) {
202
+ logger.error(
203
+ `Error re-homing monitor ${monitorId} after status page monitor rule ${statusPageMonitorRuleId.toString()} was deleted: ${error}`,
204
+ { monitorId: monitorId } as LogAttributes,
205
+ );
206
+ }
207
+ }
208
+ }
209
+
210
+ return onDelete;
211
+ }
212
+
213
+ /**
214
+ * Notes down which monitors each doomed rule is publishing, so
215
+ * onDeleteSuccess can offer them to the rules that survive.
216
+ *
217
+ * This has to happen before the delete because the StatusPageResource ->
218
+ * StatusPageMonitorRule foreign key is ON DELETE CASCADE: by the time the
219
+ * rule row is gone, so are the resources that named the monitors.
220
+ *
221
+ * It is READ ONLY, and reads through the caller's own props rather than as
222
+ * root. onBeforeDelete runs before checkDeleteQueryPermission, so anything
223
+ * here sees the caller's raw query — a root-privileged write from this hook
224
+ * would let a caller destroy another project's status page resources by
225
+ * naming that project's rule id.
226
+ */
227
+ @CaptureSpan()
228
+ protected override async onBeforeDelete(
229
+ deleteBy: DeleteBy<Model>,
230
+ ): Promise<OnDelete<Model>> {
231
+ const monitorIdsByRuleId: Record<string, Array<string>> = {};
232
+
233
+ try {
234
+ const rules: Array<Model> = await this.findBy({
235
+ query: deleteBy.query,
236
+ select: {
237
+ _id: true,
238
+ },
239
+ limit: LIMIT_PER_PROJECT,
240
+ skip: 0,
241
+ props: deleteBy.props,
242
+ });
243
+
244
+ for (const rule of rules) {
245
+ if (!rule.id) {
246
+ continue;
247
+ }
248
+
249
+ const resources: Array<StatusPageResource> =
250
+ await StatusPageResourceService.findBy({
251
+ query: {
252
+ statusPageMonitorRuleId: rule.id,
253
+ },
254
+ select: {
255
+ monitorId: true,
256
+ },
257
+ limit: LIMIT_PER_PROJECT,
258
+ skip: 0,
259
+ props: {
260
+ isRoot: true,
261
+ },
262
+ });
263
+
264
+ monitorIdsByRuleId[rule.id.toString()] = resources
265
+ .map((resource: StatusPageResource) => {
266
+ return resource.monitorId?.toString() || "";
267
+ })
268
+ .filter((monitorId: string) => {
269
+ return monitorId !== "";
270
+ });
271
+ }
272
+ } catch (error) {
273
+ /*
274
+ * Best effort. Failing to note the monitors down must not block the
275
+ * delete; the worst case is that a sibling rule re-adopts them on its
276
+ * next run instead of immediately.
277
+ */
278
+ logger.error(
279
+ `Error collecting monitors published by status page monitor rules before delete: ${error}`,
280
+ );
281
+ }
282
+
283
+ return { deleteBy, carryForward: { monitorIdsByRuleId } };
284
+ }
285
+
286
+ /**
287
+ * A rule points at a status page, and optionally at a group on that page.
288
+ * Neither is checked by the framework, and both decide what ends up on a
289
+ * PUBLIC page, so both are checked here:
290
+ *
291
+ * - a status page from another project would let one tenant publish its
292
+ * monitors on another tenant's status page;
293
+ * - a group belonging to a different status page would produce resources
294
+ * that render nowhere, because the page only draws groups that are its
295
+ * own — a rule that silently adds invisible monitors.
296
+ */
297
+ private async assertReferencesAreInScope(data: {
298
+ projectId: ObjectID | undefined;
299
+ statusPageId: ObjectID | string | undefined;
300
+ statusPageGroupId: ObjectID | string | undefined;
301
+ }): Promise<void> {
302
+ await ProjectScopedReferenceValidator.validateReferencesBelongToProject({
303
+ projectId: data.projectId,
304
+ subject: "status page monitor rule",
305
+ references: [
306
+ {
307
+ modelName: "Status Page",
308
+ id: data.statusPageId,
309
+ service: StatusPageService,
310
+ },
311
+ {
312
+ modelName: "Status Page Group",
313
+ id: data.statusPageGroupId,
314
+ service: StatusPageGroupService,
315
+ },
316
+ ],
317
+ });
318
+
319
+ if (!data.statusPageGroupId || !data.statusPageId) {
320
+ return;
321
+ }
322
+
323
+ const group: StatusPageGroup | null =
324
+ await StatusPageGroupService.findOneById({
325
+ id: new ObjectID(data.statusPageGroupId.toString()),
326
+ select: {
327
+ _id: true,
328
+ statusPageId: true,
329
+ },
330
+ props: {
331
+ isRoot: true,
332
+ },
333
+ });
334
+
335
+ /*
336
+ * A group id that matches no row at all is left to the foreign key to
337
+ * report, the same call the project-scope validator above makes.
338
+ */
339
+ if (!group) {
340
+ return;
341
+ }
342
+
343
+ if (group.statusPageId?.toString() !== data.statusPageId.toString()) {
344
+ throw new BadDataException(
345
+ "The group this rule adds monitors to belongs to a different status page. Pick a group on the status page the rule is for.",
346
+ );
347
+ }
348
+ }
349
+
350
+ /**
351
+ * A rule with no criteria would claim every monitor in the project. An empty
352
+ * form is much more likely to be an unfinished one, and the cost of guessing
353
+ * wrong is a private monitor on a public page — so say so instead.
354
+ */
355
+ private assertHasMatchCriteria(data: {
356
+ monitorLabelCount: number;
357
+ monitorNamePattern?: string | undefined;
358
+ monitorDescriptionPattern?: string | undefined;
359
+ }): void {
360
+ if (
361
+ data.monitorLabelCount === 0 &&
362
+ !data.monitorNamePattern &&
363
+ !data.monitorDescriptionPattern
364
+ ) {
365
+ throw new BadDataException(
366
+ "A status page monitor rule needs at least one match criterion: monitor labels, a monitor name pattern, or a monitor description pattern. Use .* as the name pattern to match every monitor.",
367
+ );
368
+ }
369
+ }
370
+
371
+ /**
372
+ * The rules an update actually touches, read for validation.
373
+ *
374
+ * onBeforeUpdate runs before DatabaseService applies the update query's
375
+ * permission check, so the query here is still the caller's raw one. The
376
+ * tenant is pinned onto it when the caller has one, so a validation read can
377
+ * never reach across projects and answer questions about another tenant's
378
+ * rules. Genuinely root callers (the engines, migrations) have no tenantId
379
+ * and are trusted.
380
+ */
381
+ private async findRulesForQuery(
382
+ updateBy: UpdateBy<Model>,
383
+ ): Promise<Array<Model>> {
384
+ const query: Record<string, unknown> = {
385
+ ...(updateBy.query as Record<string, unknown>),
386
+ };
387
+
388
+ if (updateBy.props.tenantId) {
389
+ query["projectId"] = updateBy.props.tenantId;
390
+ }
391
+
392
+ return await this.findBy({
393
+ query: query as UpdateBy<Model>["query"],
394
+ select: {
395
+ _id: true,
396
+ projectId: true,
397
+ statusPageId: true,
398
+ monitorLabels: {
399
+ _id: true,
400
+ },
401
+ monitorNamePattern: true,
402
+ monitorDescriptionPattern: true,
403
+ },
404
+ limit: LIMIT_PER_PROJECT,
405
+ skip: 0,
406
+ props: {
407
+ isRoot: true,
408
+ },
409
+ });
410
+ }
411
+
412
+ /**
413
+ * The create-time criteria check, applied to what the rule will look like
414
+ * after this edit. Every rule the edit touches has to survive it.
415
+ */
416
+ private async assertUpdateKeepsMatchCriteria(
417
+ updateBy: UpdateBy<Model>,
418
+ ): Promise<void> {
419
+ const touchesCriteria: boolean =
420
+ updateBy.data.monitorLabels !== undefined ||
421
+ updateBy.data.monitorNamePattern !== undefined ||
422
+ updateBy.data.monitorDescriptionPattern !== undefined;
423
+
424
+ if (!touchesCriteria) {
425
+ return;
426
+ }
427
+
428
+ const rules: Array<Model> = await this.findRulesForQuery(updateBy);
429
+
430
+ const nextLabels: Array<unknown> | undefined = updateBy.data
431
+ .monitorLabels as Array<unknown> | undefined;
432
+
433
+ for (const rule of rules) {
434
+ this.assertHasMatchCriteria({
435
+ monitorLabelCount: (nextLabels === undefined
436
+ ? rule.monitorLabels || []
437
+ : nextLabels
438
+ ).length,
439
+ monitorNamePattern:
440
+ updateBy.data.monitorNamePattern === undefined
441
+ ? rule.monitorNamePattern
442
+ : (updateBy.data.monitorNamePattern as string | undefined),
443
+ monitorDescriptionPattern:
444
+ updateBy.data.monitorDescriptionPattern === undefined
445
+ ? rule.monitorDescriptionPattern
446
+ : (updateBy.data.monitorDescriptionPattern as string | undefined),
447
+ });
448
+ }
449
+ }
450
+
451
+ /**
452
+ * Rule CRUD must not fail because the sync did. The rule is already saved
453
+ * and the next monitor change (or the next edit) re-runs it.
454
+ */
455
+ private async syncQuietly(statusPageMonitorRuleId: ObjectID): Promise<void> {
456
+ try {
457
+ await StatusPageMonitorRuleEngineService.syncResourcesForRule({
458
+ statusPageMonitorRuleId: statusPageMonitorRuleId,
459
+ });
460
+ } catch (error) {
461
+ logger.error(
462
+ `Error syncing status page monitor rule ${statusPageMonitorRuleId.toString()}: ${error}`,
463
+ {
464
+ statusPageMonitorRuleId: statusPageMonitorRuleId.toString(),
465
+ } as LogAttributes,
466
+ );
467
+ }
468
+ }
469
+ }
470
+
471
+ export default new Service();
@@ -55,6 +55,7 @@ export class TelemetryEntityRelationshipService extends DatabaseService<Model> {
55
55
  },
56
56
  lastSeenAt: now,
57
57
  describe: `edge ${edge.fromEntityKey}-[${edge.relationshipType}]->${edge.toEntityKey}`,
58
+ rowFenceId: `${projectId.toString()}:${edge.fromEntityKey}:${edge.toEntityKey}:${edge.relationshipType}`,
58
59
  buildModel: () => {
59
60
  const model: Model = new Model();
60
61
  model.projectId = projectId;
@@ -137,6 +137,7 @@ export class TelemetryEntityService extends DatabaseService<Model> {
137
137
  },
138
138
  lastSeenAt: now,
139
139
  describe: `entity ${entity.entityType}/${entity.entityKey}`,
140
+ rowFenceId: `${projectId.toString()}:${entity.entityType}:${entity.entityKey}`,
140
141
  select: { descriptiveAttributes: true, labels: true, resourceId: true },
141
142
  buildUpdate: (existing: Model): QueryDeepPartialEntity<Model> => {
142
143
  const update: QueryDeepPartialEntity<Model> =