@oneuptime/common 14.0.95 → 14.0.97

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 (253) hide show
  1. package/Models/AnalyticsModels/NetworkFlow.d.mts +4 -0
  2. package/Models/AnalyticsModels/NetworkFlow.d.ts +4 -0
  3. package/Models/AnalyticsModels/NetworkFlow.js +50 -0
  4. package/Models/DatabaseModels/Alert.d.mts +2 -0
  5. package/Models/DatabaseModels/Alert.d.ts +2 -0
  6. package/Models/DatabaseModels/Alert.js +63 -0
  7. package/Models/DatabaseModels/DatabaseBaseModel/DatabaseBaseModel.js +14 -37
  8. package/Models/DatabaseModels/DetectionRuleException.d.mts +25 -0
  9. package/Models/DatabaseModels/DetectionRuleException.d.ts +25 -0
  10. package/Models/DatabaseModels/DetectionRuleException.js +412 -0
  11. package/Models/DatabaseModels/DetectionRuleException.mjs +2 -0
  12. package/Models/DatabaseModels/Incident.d.mts +2 -0
  13. package/Models/DatabaseModels/Incident.d.ts +2 -0
  14. package/Models/DatabaseModels/Incident.js +63 -0
  15. package/Models/DatabaseModels/Index.js +12 -0
  16. package/Models/DatabaseModels/NetworkAddressName.d.mts +14 -0
  17. package/Models/DatabaseModels/NetworkAddressName.d.ts +14 -0
  18. package/Models/DatabaseModels/NetworkAddressName.js +300 -0
  19. package/Models/DatabaseModels/NetworkAddressName.mjs +2 -0
  20. package/Models/DatabaseModels/NetworkDevice.d.mts +1 -0
  21. package/Models/DatabaseModels/NetworkDevice.d.ts +1 -0
  22. package/Models/DatabaseModels/NetworkDevice.js +38 -0
  23. package/Models/DatabaseModels/SecurityCase.d.mts +8 -0
  24. package/Models/DatabaseModels/SecurityCase.d.ts +8 -0
  25. package/Models/DatabaseModels/SecurityCase.js +145 -0
  26. package/Models/DatabaseModels/SecurityCaseEvidence.d.mts +28 -0
  27. package/Models/DatabaseModels/SecurityCaseEvidence.d.ts +28 -0
  28. package/Models/DatabaseModels/SecurityCaseEvidence.js +489 -0
  29. package/Models/DatabaseModels/SecurityCaseEvidence.mjs +2 -0
  30. package/Models/DatabaseModels/SecurityCaseFeed.d.mts +4 -1
  31. package/Models/DatabaseModels/SecurityCaseFeed.d.ts +4 -1
  32. package/Models/DatabaseModels/SecurityCaseFeed.js +6 -0
  33. package/Models/DatabaseModels/SecurityCasePriority.d.mts +2 -0
  34. package/Models/DatabaseModels/SecurityCasePriority.d.ts +2 -0
  35. package/Models/DatabaseModels/SecurityCasePriority.js +44 -0
  36. package/Models/DatabaseModels/SecurityCaseTask.d.mts +22 -0
  37. package/Models/DatabaseModels/SecurityCaseTask.d.ts +22 -0
  38. package/Models/DatabaseModels/SecurityCaseTask.js +467 -0
  39. package/Models/DatabaseModels/SecurityCaseTask.mjs +2 -0
  40. package/Models/DatabaseModels/SecurityWatchlist.d.mts +22 -0
  41. package/Models/DatabaseModels/SecurityWatchlist.d.ts +22 -0
  42. package/Models/DatabaseModels/SecurityWatchlist.js +411 -0
  43. package/Models/DatabaseModels/SecurityWatchlist.mjs +2 -0
  44. package/Models/DatabaseModels/SecurityWatchlistItem.d.mts +17 -0
  45. package/Models/DatabaseModels/SecurityWatchlistItem.d.ts +17 -0
  46. package/Models/DatabaseModels/SecurityWatchlistItem.js +321 -0
  47. package/Models/DatabaseModels/SecurityWatchlistItem.mjs +2 -0
  48. package/Types/AI/AiAffectedResources.d.mts +2 -1
  49. package/Types/AI/AiAffectedResources.d.ts +2 -1
  50. package/Types/AI/AiAffectedResources.js +12 -1
  51. package/Types/AffectedResources/AffectedResourceSearch.d.mts +53 -0
  52. package/Types/AffectedResources/AffectedResourceSearch.d.ts +53 -0
  53. package/Types/AffectedResources/AffectedResourceSearch.js +224 -0
  54. package/Types/AffectedResources/AffectedResourceSearch.mjs +11 -0
  55. package/Types/ArchivableValue/ArchivableValueUsage.d.mts +21 -2
  56. package/Types/ArchivableValue/ArchivableValueUsage.d.ts +21 -2
  57. package/Types/ArchivableValue/ArchivableValueUsage.js +91 -12
  58. package/Types/ArchivableValue/ArchivableValueUsage.mjs +1 -0
  59. package/Types/Color.d.mts +0 -2
  60. package/Types/Color.d.ts +0 -2
  61. package/Types/Color.js +0 -14
  62. package/Types/Database/DatabaseProperty.d.mts +0 -2
  63. package/Types/Database/DatabaseProperty.d.ts +0 -2
  64. package/Types/Database/DatabaseProperty.js +0 -3
  65. package/Types/Database/TableColumn.d.mts +8 -0
  66. package/Types/Database/TableColumn.d.ts +8 -0
  67. package/Types/Database/TableColumn.js +69 -23
  68. package/Types/Database/TableColumn.mjs +1 -0
  69. package/Types/Date.d.mts +0 -2
  70. package/Types/Date.d.ts +0 -2
  71. package/Types/Date.js +0 -23
  72. package/Types/Domain.d.mts +0 -2
  73. package/Types/Domain.d.ts +0 -2
  74. package/Types/Domain.js +0 -14
  75. package/Types/Email/EmailTemplateType.d.mts +2 -0
  76. package/Types/Email/EmailTemplateType.d.ts +2 -0
  77. package/Types/Email/EmailTemplateType.js +2 -0
  78. package/Types/Email.d.mts +0 -2
  79. package/Types/Email.d.ts +0 -2
  80. package/Types/Email.js +0 -15
  81. package/Types/IP/IP.d.mts +0 -2
  82. package/Types/IP/IP.d.ts +0 -2
  83. package/Types/IP/IP.js +0 -14
  84. package/Types/JSONFunctions.js +16 -1
  85. package/Types/Monitor/CriteriaAlert.d.mts +0 -2
  86. package/Types/Monitor/CriteriaAlert.d.ts +0 -2
  87. package/Types/Monitor/CriteriaAlert.js +0 -18
  88. package/Types/Monitor/CriteriaAlert.mjs +1 -2
  89. package/Types/Monitor/CriteriaFilter.d.mts +0 -2
  90. package/Types/Monitor/CriteriaFilter.d.ts +0 -2
  91. package/Types/Monitor/CriteriaFilter.js +1 -42
  92. package/Types/Monitor/CriteriaFilter.mjs +0 -1
  93. package/Types/Monitor/CriteriaIncident.d.mts +0 -2
  94. package/Types/Monitor/CriteriaIncident.d.ts +0 -2
  95. package/Types/Monitor/CriteriaIncident.js +0 -23
  96. package/Types/Monitor/CriteriaIncident.mjs +1 -2
  97. package/Types/Monitor/IncomingMonitor/IncidentGroupingConfig.d.mts +0 -2
  98. package/Types/Monitor/IncomingMonitor/IncidentGroupingConfig.d.ts +0 -2
  99. package/Types/Monitor/IncomingMonitor/IncidentGroupingConfig.js +0 -11
  100. package/Types/Monitor/IncomingMonitor/IncidentGroupingConfig.mjs +1 -2
  101. package/Types/Monitor/MonitorCriteria.d.mts +0 -2
  102. package/Types/Monitor/MonitorCriteria.d.ts +0 -2
  103. package/Types/Monitor/MonitorCriteria.js +0 -21
  104. package/Types/Monitor/MonitorCriteriaInstance.d.mts +0 -2
  105. package/Types/Monitor/MonitorCriteriaInstance.d.ts +0 -2
  106. package/Types/Monitor/MonitorCriteriaInstance.js +0 -54
  107. package/Types/Monitor/MonitorStep.d.mts +0 -2
  108. package/Types/Monitor/MonitorStep.d.ts +0 -2
  109. package/Types/Monitor/MonitorStep.js +0 -80
  110. package/Types/Monitor/MonitorSteps.d.mts +0 -2
  111. package/Types/Monitor/MonitorSteps.d.ts +0 -2
  112. package/Types/Monitor/MonitorSteps.js +0 -31
  113. package/Types/Monitor/MonitorType.js +5 -4
  114. package/Types/Monitor/TelemetryAlertRule.d.mts +111 -0
  115. package/Types/Monitor/TelemetryAlertRule.d.ts +111 -0
  116. package/Types/Monitor/TelemetryAlertRule.js +886 -0
  117. package/Types/Monitor/TelemetryAlertRule.mjs +5 -0
  118. package/Types/Monitor/TelemetryRulePreview.d.mts +57 -0
  119. package/Types/Monitor/TelemetryRulePreview.d.ts +57 -0
  120. package/Types/Monitor/TelemetryRulePreview.js +190 -0
  121. package/Types/Monitor/TelemetryRulePreview.mjs +6 -0
  122. package/Types/Name.d.mts +0 -2
  123. package/Types/Name.d.ts +0 -2
  124. package/Types/Name.js +0 -14
  125. package/Types/NetFlow/NetworkAddressName.d.mts +30 -0
  126. package/Types/NetFlow/NetworkAddressName.d.ts +30 -0
  127. package/Types/NetFlow/NetworkAddressName.js +149 -0
  128. package/Types/NetFlow/NetworkAddressName.mjs +6 -0
  129. package/Types/NetFlow/NetworkTraffic.d.mts +20 -0
  130. package/Types/NetFlow/NetworkTraffic.d.ts +20 -0
  131. package/Types/NetFlow/NetworkTraffic.js +5 -1
  132. package/Types/NetFlow/NetworkTraffic.mjs +2 -0
  133. package/Types/NetworkController/NetworkControllerReadSummary.d.mts +2 -0
  134. package/Types/NetworkController/NetworkControllerReadSummary.d.ts +2 -0
  135. package/Types/NetworkController/NetworkControllerReadSummary.js +7 -1
  136. package/Types/NetworkDevice/NetworkDeviceDiscoveredDetails.d.mts +47 -1
  137. package/Types/NetworkDevice/NetworkDeviceDiscoveredDetails.d.ts +47 -1
  138. package/Types/NetworkDevice/NetworkDeviceDiscoveredDetails.js +173 -8
  139. package/Types/NetworkDevice/NetworkDeviceDiscoveredDetails.mjs +7 -0
  140. package/Types/NotificationSetting/NotificationEmailRollupCategory.js +3 -0
  141. package/Types/NotificationSetting/NotificationEmailRollupPolicy.js +6 -0
  142. package/Types/NotificationSetting/NotificationSettingEventType.d.mts +2 -0
  143. package/Types/NotificationSetting/NotificationSettingEventType.d.ts +2 -0
  144. package/Types/NotificationSetting/NotificationSettingEventType.js +9 -0
  145. package/Types/ObjectID.d.mts +0 -1
  146. package/Types/ObjectID.d.ts +0 -1
  147. package/Types/ObjectID.js +0 -9
  148. package/Types/OnCallDutyPolicy/EveryNewEventRules.d.mts +23 -0
  149. package/Types/OnCallDutyPolicy/EveryNewEventRules.d.ts +23 -0
  150. package/Types/OnCallDutyPolicy/EveryNewEventRules.js +105 -0
  151. package/Types/OnCallDutyPolicy/EveryNewEventRules.mjs +9 -0
  152. package/Types/OnCallDutyPolicy/OnCallDutyPolicyRunEnding.d.mts +4 -0
  153. package/Types/OnCallDutyPolicy/OnCallDutyPolicyRunEnding.d.ts +4 -0
  154. package/Types/OnCallDutyPolicy/OnCallDutyPolicyRunEnding.js +25 -1
  155. package/Types/OnCallDutyPolicy/OnCallDutyPolicyRunEnding.mjs +4 -0
  156. package/Types/OnCallDutyPolicy/OnCallEmptyLevel.d.mts +15 -0
  157. package/Types/OnCallDutyPolicy/OnCallEmptyLevel.d.ts +15 -0
  158. package/Types/OnCallDutyPolicy/OnCallEmptyLevel.js +118 -0
  159. package/Types/OnCallDutyPolicy/OnCallEmptyLevel.mjs +9 -0
  160. package/Types/Permission.d.mts +8 -0
  161. package/Types/Permission.d.ts +8 -0
  162. package/Types/Permission.js +87 -3
  163. package/Types/Phone.d.mts +0 -2
  164. package/Types/Phone.d.ts +0 -2
  165. package/Types/Phone.js +0 -14
  166. package/Types/Port.d.mts +0 -2
  167. package/Types/Port.d.ts +0 -2
  168. package/Types/Port.js +0 -14
  169. package/Types/ProviderLogo/ProviderLogo.d.mts +24 -1
  170. package/Types/ProviderLogo/ProviderLogo.d.ts +24 -1
  171. package/Types/ProviderLogo/ProviderLogo.js +47 -0
  172. package/Types/SecurityCase/SecurityCaseEvidenceKind.d.mts +7 -0
  173. package/Types/SecurityCase/SecurityCaseEvidenceKind.d.ts +7 -0
  174. package/Types/SecurityCase/SecurityCaseEvidenceKind.js +24 -0
  175. package/Types/SecurityCase/SecurityCaseEvidenceKind.mjs +4 -0
  176. package/Types/SecurityEvent/DetectionRetrohunt.d.mts +38 -0
  177. package/Types/SecurityEvent/DetectionRetrohunt.d.ts +38 -0
  178. package/Types/SecurityEvent/DetectionRetrohunt.js +147 -0
  179. package/Types/SecurityEvent/DetectionRetrohunt.mjs +10 -0
  180. package/Types/SecurityEvent/DetectionRuleException.d.mts +65 -0
  181. package/Types/SecurityEvent/DetectionRuleException.d.ts +65 -0
  182. package/Types/SecurityEvent/DetectionRuleException.js +441 -0
  183. package/Types/SecurityEvent/DetectionRuleException.mjs +24 -0
  184. package/Types/SecurityEvent/SecurityWatchlist.d.mts +167 -0
  185. package/Types/SecurityEvent/SecurityWatchlist.d.ts +167 -0
  186. package/Types/SecurityEvent/SecurityWatchlist.js +648 -0
  187. package/Types/SecurityEvent/SecurityWatchlist.mjs +51 -0
  188. package/Types/SecurityEvent/SigmaRule.d.mts +2 -0
  189. package/Types/SecurityEvent/SigmaRule.d.ts +2 -0
  190. package/Types/SecurityEvent/SigmaRule.js +5 -0
  191. package/Types/Telemetry/OpenTelemetryGuides.d.mts +52 -0
  192. package/Types/Telemetry/OpenTelemetryGuides.d.ts +52 -0
  193. package/Types/Telemetry/OpenTelemetryGuides.js +591 -0
  194. package/Types/Telemetry/OpenTelemetryGuides.mjs +17 -0
  195. package/Types/Version.d.mts +0 -2
  196. package/Types/Version.d.ts +0 -2
  197. package/Types/Version.js +0 -14
  198. package/Types/Workflow/AffectedResourcesSetting.d.mts +13 -0
  199. package/Types/Workflow/AffectedResourcesSetting.d.ts +13 -0
  200. package/Types/Workflow/AffectedResourcesSetting.js +120 -0
  201. package/Types/Workflow/AffectedResourcesSetting.mjs +8 -0
  202. package/Types/Workflow/Components/BaseModel.js +15 -3
  203. package/Types/Workflow/Documentation/DatabaseDocumentation.js +36 -0
  204. package/Utils/Inventory/AssetFactSource.d.mts +6 -2
  205. package/Utils/Inventory/AssetFactSource.d.ts +6 -2
  206. package/Utils/Inventory/AssetFactSource.js +18 -1
  207. package/Utils/Inventory/AssetFactSource.mjs +1 -0
  208. package/Utils/Markdown/FeedLead.d.mts +10 -0
  209. package/Utils/Markdown/FeedLead.d.ts +10 -0
  210. package/Utils/Markdown/FeedLead.js +198 -0
  211. package/Utils/Markdown/FeedLead.mjs +8 -0
  212. package/Utils/NetworkDevice/NetworkDeviceAssetFacts.d.mts +9 -1
  213. package/Utils/NetworkDevice/NetworkDeviceAssetFacts.d.ts +9 -1
  214. package/Utils/NetworkDevice/NetworkDeviceAssetFacts.js +139 -18
  215. package/Utils/NetworkDevice/NetworkDeviceAssetFacts.mjs +1 -0
  216. package/Utils/NetworkDevice/NetworkDeviceRefresh.d.mts +88 -0
  217. package/Utils/NetworkDevice/NetworkDeviceRefresh.d.ts +88 -0
  218. package/Utils/NetworkDevice/NetworkDeviceRefresh.js +232 -0
  219. package/Utils/NetworkDevice/NetworkDeviceRefresh.mjs +16 -0
  220. package/Utils/NetworkTraffic/NetworkAddressBlock.d.mts +20 -0
  221. package/Utils/NetworkTraffic/NetworkAddressBlock.d.ts +20 -0
  222. package/Utils/NetworkTraffic/NetworkAddressBlock.js +264 -0
  223. package/Utils/NetworkTraffic/NetworkAddressBlock.mjs +3 -0
  224. package/Utils/Schema/MonitorSchemas.d.mts +9 -0
  225. package/Utils/Schema/MonitorSchemas.d.ts +9 -0
  226. package/Utils/Schema/MonitorSchemas.js +291 -0
  227. package/Utils/Schema/MonitorSchemas.mjs +9 -0
  228. package/Utils/Schema/OpenApiMetadataRegistry.d.mts +4 -0
  229. package/Utils/Schema/OpenApiMetadataRegistry.d.ts +4 -0
  230. package/Utils/Schema/OpenApiMetadataRegistry.js +20 -0
  231. package/Utils/Schema/OpenApiMetadataRegistry.mjs +2 -0
  232. package/Utils/Schema/TypeSchemas.d.mts +12 -0
  233. package/Utils/Schema/TypeSchemas.d.ts +12 -0
  234. package/Utils/Schema/TypeSchemas.js +176 -0
  235. package/Utils/Schema/TypeSchemas.mjs +11 -0
  236. package/Utils/Schema/Zod.js +23 -17
  237. package/Utils/SecurityCase/SecurityCaseMarkdown.d.mts +34 -0
  238. package/Utils/SecurityCase/SecurityCaseMarkdown.d.ts +34 -0
  239. package/Utils/SecurityCase/SecurityCaseMarkdown.js +69 -0
  240. package/Utils/SecurityCase/SecurityCaseMarkdown.mjs +9 -0
  241. package/Utils/SecurityCase/SecurityCasePriorities.d.mts +2 -0
  242. package/Utils/SecurityCase/SecurityCasePriorities.d.ts +2 -0
  243. package/Utils/SecurityCase/SecurityCasePriorities.js +15 -1
  244. package/Utils/SecurityCase/SecurityCaseTargets.d.mts +128 -0
  245. package/Utils/SecurityCase/SecurityCaseTargets.d.ts +128 -0
  246. package/Utils/SecurityCase/SecurityCaseTargets.js +482 -0
  247. package/Utils/SecurityCase/SecurityCaseTargets.mjs +28 -0
  248. package/Utils/SecurityEvent/Sigma/SigmaRuleParser.js +22 -0
  249. package/Utils/SecurityEvent/Sigma/SigmaWatchlists.d.mts +24 -0
  250. package/Utils/SecurityEvent/Sigma/SigmaWatchlists.d.ts +24 -0
  251. package/Utils/SecurityEvent/Sigma/SigmaWatchlists.js +183 -0
  252. package/Utils/SecurityEvent/Sigma/SigmaWatchlists.mjs +9 -0
  253. package/package.json +1 -1
@@ -0,0 +1,886 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.TelemetryAlertRuleShapeProblem = exports.TelemetryAlertRuleRecovery = exports.TelemetryAlertRuleAction = void 0;
7
+ const FilterCondition_1 = __importDefault(require("../Filter/FilterCondition"));
8
+ const ObjectID_1 = __importDefault(require("../ObjectID"));
9
+ const RollingTime_1 = __importDefault(require("../RollingTime/RollingTime"));
10
+ const CriteriaFilter_1 = require("./CriteriaFilter");
11
+ const MonitorCriteria_1 = __importDefault(require("./MonitorCriteria"));
12
+ const MonitorCriteriaInstance_1 = __importDefault(require("./MonitorCriteriaInstance"));
13
+ const MonitorStep_1 = __importDefault(require("./MonitorStep"));
14
+ const MonitorStepExceptionMonitor_1 = require("./MonitorStepExceptionMonitor");
15
+ const MonitorStepLogMonitor_1 = require("./MonitorStepLogMonitor");
16
+ const MonitorStepMetricMonitor_1 = require("./MonitorStepMetricMonitor");
17
+ const MonitorStepTraceMonitor_1 = require("./MonitorStepTraceMonitor");
18
+ const MonitorType_1 = __importDefault(require("./MonitorType"));
19
+ const RecommendationCriteriaBuilder_1 = require("./Recommendation/RecommendationCriteriaBuilder");
20
+ /*
21
+ * A telemetry monitor's criteria, read as the one rule most people mean by
22
+ * them: "alert when there are more than 10 matching logs in 5 minutes",
23
+ * "alert when the average of a metric over 5 minutes is above 80".
24
+ *
25
+ * Telemetry monitors (Logs, Traces, Exceptions, Metrics) store their
26
+ * criteria in the same shape as every other monitor: criteria checked top
27
+ * to bottom, each with filters, a status to change to, and incidents and
28
+ * alerts to open. Most of them are one rule, often with its opposite beside
29
+ * it to set the healthy status again - which is how the out-of-the-box
30
+ * criteria, the recommendations, the AI insight monitors and the templates
31
+ * all write them. The monitor form reads such criteria as a rule, and edits
32
+ * the rule in plain words: a condition, a value, a window, and what
33
+ * happens. Criteria that are more than that (several rules, several
34
+ * filters, a disabled rule, grouped incidents) are not a rule: the form
35
+ * shows them in the full criteria editor, unchanged.
36
+ *
37
+ * Nothing new is stored. A rule is read from the criteria and written back
38
+ * onto them: every field the rule does not show (descriptions, owners,
39
+ * labels, roles, remediation notes, a retained incident row of a switched
40
+ * off action, the healthy criteria's name) is kept as it was, so reading a
41
+ * monitor and saving it unchanged saves exactly what was there.
42
+ */
43
+ var TelemetryAlertRuleAction;
44
+ (function (TelemetryAlertRuleAction) {
45
+ TelemetryAlertRuleAction["Alert"] = "Alert";
46
+ TelemetryAlertRuleAction["Incident"] = "Incident";
47
+ TelemetryAlertRuleAction["AlertAndIncident"] = "AlertAndIncident";
48
+ // Only the monitor's status changes: nothing is opened.
49
+ TelemetryAlertRuleAction["StatusOnly"] = "StatusOnly";
50
+ })(TelemetryAlertRuleAction || (exports.TelemetryAlertRuleAction = TelemetryAlertRuleAction = {}));
51
+ /*
52
+ * How the monitor goes back to healthy once the rule stops matching.
53
+ */
54
+ var TelemetryAlertRuleRecovery;
55
+ (function (TelemetryAlertRuleRecovery) {
56
+ /*
57
+ * No criteria of its own: when no criteria matches, the monitor returns to
58
+ * its default status, and the rule's incidents and alerts resolve.
59
+ */
60
+ TelemetryAlertRuleRecovery["DefaultStatus"] = "DefaultStatus";
61
+ // A second criteria, the rule's exact opposite, sets the healthy status.
62
+ TelemetryAlertRuleRecovery["Opposite"] = "Opposite";
63
+ /*
64
+ * The opposite, a little past the threshold, as the recommendations write
65
+ * it (DefaultRecoveryMarginFraction), so a value sitting at the threshold
66
+ * does not flip the status on every check.
67
+ */
68
+ TelemetryAlertRuleRecovery["OppositeWithMargin"] = "OppositeWithMargin";
69
+ /*
70
+ * A second criteria that sets the healthy status for some of the values
71
+ * the rule does not match, and never for one it does: the out-of-the-box
72
+ * Metrics criteria open an incident at exactly 0 and call anything above 0
73
+ * healthy. The values in neither return to the default status, which is
74
+ * the same healthy status, so it reads as the rule's recovery. A changed
75
+ * rule writes it as the rule's exact opposite.
76
+ */
77
+ TelemetryAlertRuleRecovery["Disjoint"] = "Disjoint";
78
+ })(TelemetryAlertRuleRecovery || (exports.TelemetryAlertRuleRecovery = TelemetryAlertRuleRecovery = {}));
79
+ /*
80
+ * Why criteria are not read as a rule. Said to the reader above the full
81
+ * criteria editor, and pinned by tests.
82
+ */
83
+ var TelemetryAlertRuleShapeProblem;
84
+ (function (TelemetryAlertRuleShapeProblem) {
85
+ TelemetryAlertRuleShapeProblem["NotARuleMonitorType"] = "NotARuleMonitorType";
86
+ TelemetryAlertRuleShapeProblem["NoCriteria"] = "NoCriteria";
87
+ TelemetryAlertRuleShapeProblem["SeveralRules"] = "SeveralRules";
88
+ TelemetryAlertRuleShapeProblem["DisabledRule"] = "DisabledRule";
89
+ TelemetryAlertRuleShapeProblem["SeveralFilters"] = "SeveralFilters";
90
+ TelemetryAlertRuleShapeProblem["OtherCheck"] = "OtherCheck";
91
+ TelemetryAlertRuleShapeProblem["UnsupportedCondition"] = "UnsupportedCondition";
92
+ TelemetryAlertRuleShapeProblem["SeveralIncidentsOrAlerts"] = "SeveralIncidentsOrAlerts";
93
+ TelemetryAlertRuleShapeProblem["GroupedIncidents"] = "GroupedIncidents";
94
+ TelemetryAlertRuleShapeProblem["UnknownMetric"] = "UnknownMetric";
95
+ })(TelemetryAlertRuleShapeProblem || (exports.TelemetryAlertRuleShapeProblem = TelemetryAlertRuleShapeProblem = {}));
96
+ /*
97
+ * The comparisons the rule editor offers, in the order it offers them. The
98
+ * same set the full criteria editor offers for these checks
99
+ * (CriteriaFilterUtil.getFilterTypeOptionsByCheckOn in the Dashboard), plus
100
+ * Not Equal To, which only an older or hand-written criteria holds: read,
101
+ * and offered back to such a rule, so it keeps meaning what it meant.
102
+ */
103
+ const STATIC_COMPARISONS = [
104
+ CriteriaFilter_1.FilterType.GreaterThan,
105
+ CriteriaFilter_1.FilterType.GreaterThanOrEqualTo,
106
+ CriteriaFilter_1.FilterType.LessThan,
107
+ CriteriaFilter_1.FilterType.LessThanOrEqualTo,
108
+ CriteriaFilter_1.FilterType.EqualTo,
109
+ ];
110
+ const ANOMALY_COMPARISONS = [
111
+ CriteriaFilter_1.FilterType.AnomalouslyHigh,
112
+ CriteriaFilter_1.FilterType.AnomalouslyLow,
113
+ CriteriaFilter_1.FilterType.Anomalous,
114
+ ];
115
+ const READ_ONLY_COMPARISONS = [CriteriaFilter_1.FilterType.NotEqualTo];
116
+ /*
117
+ * A comparison of a count holds or not for every whole number. Read as the
118
+ * set of counts it matches, two filters are opposites when every count
119
+ * matches exactly one of them: "Equal To 0" and "Greater Than 0" are, as the
120
+ * out-of-the-box Logs criteria pair them, though Greater Than is not the
121
+ * formal opposite of Equal To.
122
+ */
123
+ const LARGE_COUNT = 1000000000;
124
+ class TelemetryAlertRuleUtil {
125
+ static isRuleMonitorType(monitorType) {
126
+ return Boolean(monitorType &&
127
+ TelemetryAlertRuleUtil.RULE_MONITOR_TYPES.includes(monitorType));
128
+ }
129
+ // What a rule of this monitor type compares.
130
+ static getMeasureCheckOn(monitorType) {
131
+ switch (monitorType) {
132
+ case MonitorType_1.default.Logs:
133
+ return CriteriaFilter_1.CheckOn.LogCount;
134
+ case MonitorType_1.default.Traces:
135
+ return CriteriaFilter_1.CheckOn.SpanCount;
136
+ case MonitorType_1.default.Exceptions:
137
+ return CriteriaFilter_1.CheckOn.ExceptionCount;
138
+ case MonitorType_1.default.Metrics:
139
+ return CriteriaFilter_1.CheckOn.MetricValue;
140
+ default:
141
+ return null;
142
+ }
143
+ }
144
+ // Whether the measure is a count of things in the window (not a metric).
145
+ static isCountMonitorType(monitorType) {
146
+ return (monitorType === MonitorType_1.default.Logs ||
147
+ monitorType === MonitorType_1.default.Traces ||
148
+ monitorType === MonitorType_1.default.Exceptions);
149
+ }
150
+ /*
151
+ * The anomaly conditions are offered where the server judges them: log
152
+ * and span counts against their hourly baselines, metrics against theirs.
153
+ * Exceptions have no baseline.
154
+ */
155
+ static supportsAnomaly(monitorType) {
156
+ return (monitorType === MonitorType_1.default.Logs ||
157
+ monitorType === MonitorType_1.default.Traces ||
158
+ monitorType === MonitorType_1.default.Metrics);
159
+ }
160
+ /*
161
+ * The comparisons to offer for a rule of this type. A comparison the rule
162
+ * already holds is always offered, so it can be kept.
163
+ */
164
+ static getComparisons(data) {
165
+ const comparisons = [...STATIC_COMPARISONS];
166
+ if (TelemetryAlertRuleUtil.supportsAnomaly(data.monitorType)) {
167
+ comparisons.push(...ANOMALY_COMPARISONS);
168
+ }
169
+ if (data.current && !comparisons.includes(data.current)) {
170
+ if (TelemetryAlertRuleUtil.isReadableComparison(data)) {
171
+ comparisons.push(data.current);
172
+ }
173
+ }
174
+ return comparisons;
175
+ }
176
+ static isAnomaly(filterType) {
177
+ return CriteriaFilter_1.CriteriaFilterUtil.isAnomalyFilterType(filterType);
178
+ }
179
+ // The window of a count monitor, in seconds, as its step holds it.
180
+ static getWindowInSeconds(data) {
181
+ var _a, _b, _c, _d, _e, _f;
182
+ switch (data.monitorType) {
183
+ case MonitorType_1.default.Logs:
184
+ return (((_b = (_a = data.monitorStep.data) === null || _a === void 0 ? void 0 : _a.logMonitor) === null || _b === void 0 ? void 0 : _b.lastXSecondsOfLogs) ||
185
+ MonitorStepLogMonitor_1.MonitorStepLogMonitorUtil.getDefault().lastXSecondsOfLogs);
186
+ case MonitorType_1.default.Traces:
187
+ return (((_d = (_c = data.monitorStep.data) === null || _c === void 0 ? void 0 : _c.traceMonitor) === null || _d === void 0 ? void 0 : _d.lastXSecondsOfSpans) ||
188
+ MonitorStepTraceMonitor_1.MonitorStepTraceMonitorUtil.getDefault().lastXSecondsOfSpans);
189
+ case MonitorType_1.default.Exceptions:
190
+ return (((_f = (_e = data.monitorStep.data) === null || _e === void 0 ? void 0 : _e.exceptionMonitor) === null || _f === void 0 ? void 0 : _f.lastXSecondsOfExceptions) ||
191
+ MonitorStepExceptionMonitor_1.MonitorStepExceptionMonitorUtil.getDefault().lastXSecondsOfExceptions);
192
+ default:
193
+ return undefined;
194
+ }
195
+ }
196
+ static getRollingTime(monitorStep) {
197
+ var _a, _b;
198
+ return (((_b = (_a = monitorStep.data) === null || _a === void 0 ? void 0 : _a.metricMonitor) === null || _b === void 0 ? void 0 : _b.rollingTime) || RollingTime_1.default.Past1Minute);
199
+ }
200
+ /*
201
+ * The aliases a metric rule can compare: every query's and formula's
202
+ * variable, queries first - the order the evaluator resolves them in.
203
+ */
204
+ static getMetricAliases(monitorStep) {
205
+ var _a, _b, _c;
206
+ const config = (_a = monitorStep.data) === null || _a === void 0 ? void 0 : _a.metricMonitor;
207
+ const viewConfig = MonitorStepMetricMonitor_1.MonitorStepMetricMonitorUtil.getMetricViewConfig(config);
208
+ const aliases = [];
209
+ for (const queryConfig of viewConfig.queryConfigs) {
210
+ const alias = (_b = queryConfig.metricAliasData) === null || _b === void 0 ? void 0 : _b.metricVariable;
211
+ if (alias && !aliases.includes(alias)) {
212
+ aliases.push(alias);
213
+ }
214
+ }
215
+ for (const formulaConfig of viewConfig.formulaConfigs) {
216
+ const alias = (_c = formulaConfig.metricAliasData) === null || _c === void 0 ? void 0 : _c.metricVariable;
217
+ if (alias && !aliases.includes(alias)) {
218
+ aliases.push(alias);
219
+ }
220
+ }
221
+ return aliases;
222
+ }
223
+ /*
224
+ * Read the rule out of a step's criteria, or say why they are not one.
225
+ * `defaultMonitorStatusId` is the status the monitor shows when no
226
+ * criteria matches (MonitorSteps.defaultMonitorStatusId): a healthy
227
+ * criteria is only the rule's recovery when it sets that same status, so
228
+ * dropping it (when the rule becomes an anomaly) changes nothing.
229
+ */
230
+ static read(data) {
231
+ var _a, _b, _c, _d, _e;
232
+ const measure = TelemetryAlertRuleUtil.getMeasureCheckOn(data.monitorType);
233
+ if (!measure) {
234
+ return {
235
+ rule: null,
236
+ problem: TelemetryAlertRuleShapeProblem.NotARuleMonitorType,
237
+ };
238
+ }
239
+ const instances = ((_c = (_b = (_a = data.monitorStep.data) === null || _a === void 0 ? void 0 : _a.monitorCriteria) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.monitorCriteriaInstanceArray) || [];
240
+ if (instances.length === 0) {
241
+ return { rule: null, problem: TelemetryAlertRuleShapeProblem.NoCriteria };
242
+ }
243
+ if (instances.length > 2) {
244
+ return {
245
+ rule: null,
246
+ problem: TelemetryAlertRuleShapeProblem.SeveralRules,
247
+ };
248
+ }
249
+ let ruleInstance = undefined;
250
+ let recoveryInstance = undefined;
251
+ if (instances.length === 1) {
252
+ ruleInstance = instances[0];
253
+ }
254
+ else {
255
+ const recoveryCandidates = instances.filter((instance) => {
256
+ return TelemetryAlertRuleUtil.isRecoveryCandidate({
257
+ instance,
258
+ defaultMonitorStatusId: data.defaultMonitorStatusId,
259
+ });
260
+ });
261
+ if (recoveryCandidates.length !== 1) {
262
+ return {
263
+ rule: null,
264
+ problem: TelemetryAlertRuleShapeProblem.SeveralRules,
265
+ };
266
+ }
267
+ recoveryInstance = recoveryCandidates[0];
268
+ ruleInstance = instances.find((instance) => {
269
+ return instance !== recoveryInstance;
270
+ });
271
+ }
272
+ if (!(ruleInstance === null || ruleInstance === void 0 ? void 0 : ruleInstance.data)) {
273
+ return { rule: null, problem: TelemetryAlertRuleShapeProblem.NoCriteria };
274
+ }
275
+ const problem = TelemetryAlertRuleUtil.getRuleInstanceProblem({
276
+ monitorType: data.monitorType,
277
+ monitorStep: data.monitorStep,
278
+ instance: ruleInstance,
279
+ measure,
280
+ });
281
+ if (problem) {
282
+ return { rule: null, problem };
283
+ }
284
+ const ruleFilter = ruleInstance.data.filters[0];
285
+ let recovery = TelemetryAlertRuleRecovery.DefaultStatus;
286
+ if (recoveryInstance) {
287
+ const recoveryFilter = ((_d = recoveryInstance.data) === null || _d === void 0 ? void 0 : _d.filters.length) === 1
288
+ ? recoveryInstance.data.filters[0]
289
+ : undefined;
290
+ const matchedRecovery = recoveryFilter && ((_e = recoveryInstance.data) === null || _e === void 0 ? void 0 : _e.isEnabled) !== false
291
+ ? TelemetryAlertRuleUtil.getRecoveryKind({
292
+ monitorType: data.monitorType,
293
+ ruleFilter,
294
+ recoveryFilter,
295
+ })
296
+ : null;
297
+ if (!matchedRecovery) {
298
+ return {
299
+ rule: null,
300
+ problem: TelemetryAlertRuleShapeProblem.SeveralRules,
301
+ };
302
+ }
303
+ recovery = matchedRecovery;
304
+ }
305
+ const ruleData = ruleInstance.data;
306
+ const isMetric = data.monitorType === MonitorType_1.default.Metrics;
307
+ const metricOptions = ruleFilter.metricMonitorOptions;
308
+ const rule = {
309
+ filterType: ruleFilter.filterType,
310
+ threshold: TelemetryAlertRuleUtil.isAnomaly(ruleFilter.filterType)
311
+ ? undefined
312
+ : TelemetryAlertRuleUtil.toNumber(ruleFilter.value),
313
+ windowInSeconds: TelemetryAlertRuleUtil.getWindowInSeconds({
314
+ monitorType: data.monitorType,
315
+ monitorStep: data.monitorStep,
316
+ }),
317
+ rollingTime: isMetric
318
+ ? TelemetryAlertRuleUtil.getRollingTime(data.monitorStep)
319
+ : undefined,
320
+ metricAlias: isMetric ? metricOptions === null || metricOptions === void 0 ? void 0 : metricOptions.metricAlias : undefined,
321
+ metricAggregationType: isMetric
322
+ ? metricOptions === null || metricOptions === void 0 ? void 0 : metricOptions.metricAggregationType
323
+ : undefined,
324
+ onNoDataPolicy: isMetric ? metricOptions === null || metricOptions === void 0 ? void 0 : metricOptions.onNoDataPolicy : undefined,
325
+ thresholdUnit: isMetric ? metricOptions === null || metricOptions === void 0 ? void 0 : metricOptions.thresholdUnit : undefined,
326
+ anomalyDetection: metricOptions === null || metricOptions === void 0 ? void 0 : metricOptions.anomalyDetection,
327
+ action: TelemetryAlertRuleUtil.getAction(ruleInstance),
328
+ monitorStatusId: ruleData.changeMonitorStatus
329
+ ? ruleData.monitorStatusId
330
+ : undefined,
331
+ incident: ruleData.incidents[0],
332
+ alert: ruleData.alerts[0],
333
+ name: ruleData.name || "",
334
+ ruleCriteriaId: ruleData.id,
335
+ recovery,
336
+ };
337
+ return { rule, problem: null };
338
+ }
339
+ /*
340
+ * Write a rule back onto a step: the rule criteria, its recovery criteria
341
+ * (kept the opposite of the rule) and the window. Returns a new step; the
342
+ * one handed in is not changed.
343
+ */
344
+ static write(data) {
345
+ var _a, _b, _c, _d;
346
+ const { monitorType, rule } = data;
347
+ const step = MonitorStep_1.default.clone(data.monitorStep);
348
+ const measure = TelemetryAlertRuleUtil.getMeasureCheckOn(monitorType);
349
+ if (!measure || !step.data) {
350
+ return step;
351
+ }
352
+ TelemetryAlertRuleUtil.writeWindow({
353
+ monitorType,
354
+ monitorStep: step,
355
+ rule,
356
+ });
357
+ const instances = (((_b = (_a = step.data.monitorCriteria) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.monitorCriteriaInstanceArray) || []).map((instance) => {
358
+ return MonitorCriteriaInstance_1.default.clone(instance);
359
+ });
360
+ const ruleInstance = instances.find((instance) => {
361
+ var _a;
362
+ return ((_a = instance.data) === null || _a === void 0 ? void 0 : _a.id) === rule.ruleCriteriaId;
363
+ });
364
+ /*
365
+ * A rule that is not one of these criteria is a new one (a recipe): it
366
+ * takes the criteria's place.
367
+ */
368
+ if (!ruleInstance) {
369
+ step.setMonitorCriteria(TelemetryAlertRuleUtil.buildCriteria({ monitorType, rule }));
370
+ return step;
371
+ }
372
+ const previousRuleFilter = (_c = ruleInstance.data) === null || _c === void 0 ? void 0 : _c.filters[0];
373
+ const ruleFilter = TelemetryAlertRuleUtil.buildRuleFilter({
374
+ monitorType,
375
+ measure,
376
+ rule,
377
+ previous: previousRuleFilter,
378
+ });
379
+ TelemetryAlertRuleUtil.applyRuleToInstance({
380
+ instance: ruleInstance,
381
+ rule,
382
+ filter: ruleFilter,
383
+ });
384
+ const recoveryIndex = instances.findIndex((instance) => {
385
+ return instance !== ruleInstance;
386
+ });
387
+ if (recoveryIndex >= 0) {
388
+ const recoveryInstance = instances[recoveryIndex];
389
+ /*
390
+ * An anomaly has no opposite to write: the monitor is healthy again
391
+ * when no criteria matches, and its default status is the one this
392
+ * criteria set (read() requires that), so dropping it changes nothing.
393
+ */
394
+ if (TelemetryAlertRuleUtil.isAnomaly(rule.filterType) ||
395
+ rule.recovery === TelemetryAlertRuleRecovery.DefaultStatus) {
396
+ instances.splice(recoveryIndex, 1);
397
+ }
398
+ else {
399
+ const ruleChanged = !TelemetryAlertRuleUtil.isSameComparison(previousRuleFilter, ruleFilter);
400
+ const previousRecoveryFilter = (_d = recoveryInstance.data) === null || _d === void 0 ? void 0 : _d.filters[0];
401
+ if (recoveryInstance.data && previousRecoveryFilter) {
402
+ recoveryInstance.data.filters = [
403
+ TelemetryAlertRuleUtil.buildRecoveryFilter({
404
+ monitorType,
405
+ ruleFilter,
406
+ previousRecoveryFilter,
407
+ recovery: rule.recovery,
408
+ ruleChanged,
409
+ }),
410
+ ];
411
+ }
412
+ }
413
+ }
414
+ const criteria = new MonitorCriteria_1.default();
415
+ criteria.data = { monitorCriteriaInstanceArray: instances };
416
+ step.setMonitorCriteria(criteria);
417
+ return step;
418
+ }
419
+ /*
420
+ * The criteria for a new rule: the rule alone. When it stops matching, no
421
+ * criteria matches, and the monitor returns to its default status and
422
+ * resolves what the rule opened (MonitorResource, "No criteria met").
423
+ */
424
+ static buildCriteria(data) {
425
+ const measure = TelemetryAlertRuleUtil.getMeasureCheckOn(data.monitorType);
426
+ const instance = TelemetryAlertRuleUtil.newRuleInstance(data.rule);
427
+ if (measure) {
428
+ TelemetryAlertRuleUtil.applyRuleToInstance({
429
+ instance,
430
+ rule: data.rule,
431
+ filter: TelemetryAlertRuleUtil.buildRuleFilter({
432
+ monitorType: data.monitorType,
433
+ measure,
434
+ rule: data.rule,
435
+ previous: undefined,
436
+ }),
437
+ });
438
+ }
439
+ const criteria = new MonitorCriteria_1.default();
440
+ criteria.data = { monitorCriteriaInstanceArray: [instance] };
441
+ return criteria;
442
+ }
443
+ /*
444
+ * The window, written where the step's sub-configuration keeps it. A step
445
+ * without the sub-configuration gets its type's defaults with the window.
446
+ */
447
+ static writeWindow(data) {
448
+ const { monitorType, monitorStep, rule } = data;
449
+ if (!monitorStep.data) {
450
+ return;
451
+ }
452
+ if (monitorType === MonitorType_1.default.Logs && rule.windowInSeconds) {
453
+ const logMonitor = monitorStep.data.logMonitor || MonitorStepLogMonitor_1.MonitorStepLogMonitorUtil.getDefault();
454
+ monitorStep.setLogMonitor(Object.assign(Object.assign({}, logMonitor), { lastXSecondsOfLogs: rule.windowInSeconds }));
455
+ }
456
+ if (monitorType === MonitorType_1.default.Traces && rule.windowInSeconds) {
457
+ const traceMonitor = monitorStep.data.traceMonitor ||
458
+ MonitorStepTraceMonitor_1.MonitorStepTraceMonitorUtil.getDefault();
459
+ monitorStep.setTraceMonitor(Object.assign(Object.assign({}, traceMonitor), { lastXSecondsOfSpans: rule.windowInSeconds }));
460
+ }
461
+ if (monitorType === MonitorType_1.default.Exceptions && rule.windowInSeconds) {
462
+ const exceptionMonitor = monitorStep.data.exceptionMonitor ||
463
+ MonitorStepExceptionMonitor_1.MonitorStepExceptionMonitorUtil.getDefault();
464
+ monitorStep.setExceptionMonitor(Object.assign(Object.assign({}, exceptionMonitor), { lastXSecondsOfExceptions: rule.windowInSeconds }));
465
+ }
466
+ if (monitorType === MonitorType_1.default.Metrics && rule.rollingTime) {
467
+ const metricMonitor = monitorStep.data.metricMonitor ||
468
+ MonitorStepMetricMonitor_1.MonitorStepMetricMonitorUtil.getDefault();
469
+ monitorStep.setMetricMonitor(Object.assign(Object.assign({}, metricMonitor), { rollingTime: rule.rollingTime }));
470
+ }
471
+ }
472
+ /*
473
+ * Whether a value matches a comparison, the way the server compares one
474
+ * number (CompareCriteria.compareCriteriaNumbers). The rule editor's
475
+ * preview marks the windows a rule would have matched with it.
476
+ */
477
+ static matches(data) {
478
+ switch (data.filterType) {
479
+ case CriteriaFilter_1.FilterType.GreaterThan:
480
+ return data.value > data.threshold;
481
+ case CriteriaFilter_1.FilterType.GreaterThanOrEqualTo:
482
+ return data.value >= data.threshold;
483
+ case CriteriaFilter_1.FilterType.LessThan:
484
+ return data.value < data.threshold;
485
+ case CriteriaFilter_1.FilterType.LessThanOrEqualTo:
486
+ return data.value <= data.threshold;
487
+ case CriteriaFilter_1.FilterType.EqualTo:
488
+ return data.value === data.threshold;
489
+ case CriteriaFilter_1.FilterType.NotEqualTo:
490
+ return data.value !== data.threshold;
491
+ default:
492
+ return false;
493
+ }
494
+ }
495
+ // ---- private ----
496
+ static isReadableComparison(data) {
497
+ if (!data.current) {
498
+ return false;
499
+ }
500
+ if (STATIC_COMPARISONS.includes(data.current)) {
501
+ return true;
502
+ }
503
+ if (READ_ONLY_COMPARISONS.includes(data.current)) {
504
+ return true;
505
+ }
506
+ return (ANOMALY_COMPARISONS.includes(data.current) &&
507
+ TelemetryAlertRuleUtil.supportsAnomaly(data.monitorType));
508
+ }
509
+ static getRuleInstanceProblem(data) {
510
+ var _a;
511
+ const instanceData = data.instance.data;
512
+ if (!instanceData) {
513
+ return TelemetryAlertRuleShapeProblem.NoCriteria;
514
+ }
515
+ if (instanceData.isEnabled === false) {
516
+ return TelemetryAlertRuleShapeProblem.DisabledRule;
517
+ }
518
+ if (instanceData.filters.length !== 1) {
519
+ return TelemetryAlertRuleShapeProblem.SeveralFilters;
520
+ }
521
+ const filter = instanceData.filters[0];
522
+ if (filter.checkOn !== data.measure) {
523
+ return TelemetryAlertRuleShapeProblem.OtherCheck;
524
+ }
525
+ if (filter.evaluateOverTime ||
526
+ !TelemetryAlertRuleUtil.isReadableComparison({
527
+ monitorType: data.monitorType,
528
+ current: filter.filterType,
529
+ })) {
530
+ return TelemetryAlertRuleShapeProblem.UnsupportedCondition;
531
+ }
532
+ /*
533
+ * A rule without a value is still a rule: a new Metrics monitor starts
534
+ * on "above a value" and asks for the value, and the criteria cannot be
535
+ * saved until it has one (MonitorCriteriaInstance.getValidationError).
536
+ */
537
+ if (instanceData.incidents.length > 1 || instanceData.alerts.length > 1) {
538
+ return TelemetryAlertRuleShapeProblem.SeveralIncidentsOrAlerts;
539
+ }
540
+ if ((instanceData.createIncidents && instanceData.incidents.length !== 1) ||
541
+ (instanceData.createAlerts && instanceData.alerts.length !== 1)) {
542
+ return TelemetryAlertRuleShapeProblem.SeveralIncidentsOrAlerts;
543
+ }
544
+ if (instanceData.incidentGrouping || instanceData.openForEveryEmail) {
545
+ return TelemetryAlertRuleShapeProblem.GroupedIncidents;
546
+ }
547
+ if (data.monitorType === MonitorType_1.default.Metrics) {
548
+ const alias = (_a = filter.metricMonitorOptions) === null || _a === void 0 ? void 0 : _a.metricAlias;
549
+ if (alias &&
550
+ !TelemetryAlertRuleUtil.getMetricAliases(data.monitorStep).includes(alias)) {
551
+ return TelemetryAlertRuleShapeProblem.UnknownMetric;
552
+ }
553
+ }
554
+ return null;
555
+ }
556
+ /*
557
+ * A criteria that can be the rule's recovery: on, opening nothing, and
558
+ * setting the monitor's default status.
559
+ */
560
+ static isRecoveryCandidate(data) {
561
+ var _a;
562
+ const instanceData = data.instance.data;
563
+ if (!instanceData || !data.defaultMonitorStatusId) {
564
+ return false;
565
+ }
566
+ return (instanceData.isEnabled !== false &&
567
+ !instanceData.createIncidents &&
568
+ !instanceData.createAlerts &&
569
+ Boolean(instanceData.changeMonitorStatus) &&
570
+ ((_a = instanceData.monitorStatusId) === null || _a === void 0 ? void 0 : _a.toString()) ===
571
+ data.defaultMonitorStatusId.toString() &&
572
+ !instanceData.incidentGrouping &&
573
+ !instanceData.openForEveryEmail);
574
+ }
575
+ static getRecoveryKind(data) {
576
+ const { ruleFilter, recoveryFilter } = data;
577
+ if (recoveryFilter.checkOn !== ruleFilter.checkOn ||
578
+ recoveryFilter.evaluateOverTime ||
579
+ TelemetryAlertRuleUtil.isAnomaly(ruleFilter.filterType) ||
580
+ TelemetryAlertRuleUtil.isAnomaly(recoveryFilter.filterType)) {
581
+ return null;
582
+ }
583
+ const ruleValue = TelemetryAlertRuleUtil.toNumber(ruleFilter.value);
584
+ const recoveryValue = TelemetryAlertRuleUtil.toNumber(recoveryFilter.value);
585
+ if (ruleValue === undefined ||
586
+ recoveryValue === undefined ||
587
+ !ruleFilter.filterType ||
588
+ !recoveryFilter.filterType) {
589
+ return null;
590
+ }
591
+ const isMetric = data.monitorType === MonitorType_1.default.Metrics;
592
+ /*
593
+ * A metric's recovery must read the same number the rule reads: the
594
+ * same query, in the same unit, taken from its window the same way.
595
+ */
596
+ if (isMetric) {
597
+ const ruleOptions = ruleFilter.metricMonitorOptions;
598
+ const recoveryOptions = recoveryFilter.metricMonitorOptions;
599
+ if (((ruleOptions === null || ruleOptions === void 0 ? void 0 : ruleOptions.metricAlias) || "") !==
600
+ ((recoveryOptions === null || recoveryOptions === void 0 ? void 0 : recoveryOptions.metricAlias) || "") ||
601
+ ((ruleOptions === null || ruleOptions === void 0 ? void 0 : ruleOptions.thresholdUnit) || "") !==
602
+ ((recoveryOptions === null || recoveryOptions === void 0 ? void 0 : recoveryOptions.thresholdUnit) || "") ||
603
+ ((ruleOptions === null || ruleOptions === void 0 ? void 0 : ruleOptions.metricAggregationType) || "") !==
604
+ ((recoveryOptions === null || recoveryOptions === void 0 ? void 0 : recoveryOptions.metricAggregationType) || "")) {
605
+ return null;
606
+ }
607
+ }
608
+ const comparison = TelemetryAlertRuleUtil.compareFilterSets({
609
+ isCount: !isMetric,
610
+ rule: { filterType: ruleFilter.filterType, value: ruleValue },
611
+ recovery: {
612
+ filterType: recoveryFilter.filterType,
613
+ value: recoveryValue,
614
+ },
615
+ });
616
+ if (comparison.isOpposite) {
617
+ return TelemetryAlertRuleRecovery.Opposite;
618
+ }
619
+ if (comparison.overlaps) {
620
+ return null;
621
+ }
622
+ if (isMetric &&
623
+ recoveryFilter.filterType === (0, RecommendationCriteriaBuilder_1.getRecoveryFilterType)(ruleFilter.filterType)) {
624
+ const withMargin = (0, RecommendationCriteriaBuilder_1.getRecoveryThreshold)({
625
+ filterType: ruleFilter.filterType,
626
+ value: ruleValue,
627
+ });
628
+ if (withMargin !== undefined &&
629
+ TelemetryAlertRuleUtil.isSameNumber(withMargin, recoveryValue)) {
630
+ return TelemetryAlertRuleRecovery.OppositeWithMargin;
631
+ }
632
+ }
633
+ return TelemetryAlertRuleRecovery.Disjoint;
634
+ }
635
+ /*
636
+ * How the values two comparisons match relate: whether every value
637
+ * matches exactly one of them (opposites), and whether any value matches
638
+ * both. A comparison changes its answer only at its own value, so a few
639
+ * values on each side of the two thresholds, and far beyond them, decide
640
+ * it: whole numbers from 0 for a count, any number for a metric.
641
+ */
642
+ static compareFilterSets(data) {
643
+ const thresholds = [data.rule.value, data.recovery.value];
644
+ const candidates = [];
645
+ if (data.isCount) {
646
+ candidates.push(0, LARGE_COUNT);
647
+ for (const threshold of thresholds) {
648
+ for (const near of [
649
+ Math.floor(threshold) - 1,
650
+ Math.floor(threshold),
651
+ Math.ceil(threshold),
652
+ Math.ceil(threshold) + 1,
653
+ ]) {
654
+ if (near >= 0) {
655
+ candidates.push(near);
656
+ }
657
+ }
658
+ }
659
+ }
660
+ else {
661
+ const lowest = Math.min(...thresholds);
662
+ const highest = Math.max(...thresholds);
663
+ const gap = highest - lowest;
664
+ const step = gap > 0 ? gap / 4 : Math.max(1, Math.abs(lowest)) * 1e-6;
665
+ candidates.push(-LARGE_COUNT, LARGE_COUNT, (lowest + highest) / 2);
666
+ for (const threshold of thresholds) {
667
+ candidates.push(threshold - step, threshold, threshold + step);
668
+ }
669
+ }
670
+ let isOpposite = true;
671
+ let overlaps = false;
672
+ for (const candidate of candidates) {
673
+ const ruleMatches = TelemetryAlertRuleUtil.matches({
674
+ filterType: data.rule.filterType,
675
+ threshold: data.rule.value,
676
+ value: candidate,
677
+ });
678
+ const recoveryMatches = TelemetryAlertRuleUtil.matches({
679
+ filterType: data.recovery.filterType,
680
+ threshold: data.recovery.value,
681
+ value: candidate,
682
+ });
683
+ if (ruleMatches === recoveryMatches) {
684
+ isOpposite = false;
685
+ }
686
+ if (ruleMatches && recoveryMatches) {
687
+ overlaps = true;
688
+ }
689
+ }
690
+ return { isOpposite, overlaps };
691
+ }
692
+ static getAction(instance) {
693
+ var _a, _b;
694
+ const createIncidents = Boolean((_a = instance.data) === null || _a === void 0 ? void 0 : _a.createIncidents);
695
+ const createAlerts = Boolean((_b = instance.data) === null || _b === void 0 ? void 0 : _b.createAlerts);
696
+ if (createIncidents && createAlerts) {
697
+ return TelemetryAlertRuleAction.AlertAndIncident;
698
+ }
699
+ if (createIncidents) {
700
+ return TelemetryAlertRuleAction.Incident;
701
+ }
702
+ if (createAlerts) {
703
+ return TelemetryAlertRuleAction.Alert;
704
+ }
705
+ return TelemetryAlertRuleAction.StatusOnly;
706
+ }
707
+ static newRuleInstance(rule) {
708
+ const instance = new MonitorCriteriaInstance_1.default();
709
+ instance.data = {
710
+ id: rule.ruleCriteriaId || ObjectID_1.default.generate().toString(),
711
+ monitorStatusId: undefined,
712
+ filterCondition: FilterCondition_1.default.Any,
713
+ filters: [],
714
+ incidents: [],
715
+ alerts: [],
716
+ changeMonitorStatus: false,
717
+ createIncidents: false,
718
+ createAlerts: false,
719
+ isEnabled: true,
720
+ name: "",
721
+ description: "",
722
+ };
723
+ return instance;
724
+ }
725
+ static buildRuleFilter(data) {
726
+ var _a, _b, _c;
727
+ const { rule } = data;
728
+ const isAnomaly = TelemetryAlertRuleUtil.isAnomaly(rule.filterType);
729
+ /*
730
+ * The value as it was stored when it is still the same number: the API
731
+ * and Terraform may have written it as text ("10"), and a rule saved
732
+ * unchanged must save what was there.
733
+ */
734
+ const previousValue = (_a = data.previous) === null || _a === void 0 ? void 0 : _a.value;
735
+ const value = isAnomaly
736
+ ? undefined
737
+ : previousValue !== undefined &&
738
+ TelemetryAlertRuleUtil.toNumber(previousValue) === rule.threshold
739
+ ? previousValue
740
+ : rule.threshold;
741
+ const filter = Object.assign(Object.assign({}, (data.previous || {})), { checkOn: data.measure, filterType: rule.filterType, value: value });
742
+ if (data.monitorType === MonitorType_1.default.Metrics) {
743
+ const options = Object.assign({}, (((_b = data.previous) === null || _b === void 0 ? void 0 : _b.metricMonitorOptions) || {}));
744
+ TelemetryAlertRuleUtil.setOrDelete(options, "metricAlias", rule.metricAlias);
745
+ TelemetryAlertRuleUtil.setOrDelete(options, "metricAggregationType", rule.metricAggregationType);
746
+ TelemetryAlertRuleUtil.setOrDelete(options, "onNoDataPolicy", rule.onNoDataPolicy);
747
+ TelemetryAlertRuleUtil.setOrDelete(options, "thresholdUnit", rule.thresholdUnit);
748
+ /*
749
+ * Kept whichever comparison the rule makes, as the full editor keeps
750
+ * it: a threshold condition ignores it, and a rule switched back to an
751
+ * anomaly finds its sensitivity again.
752
+ */
753
+ TelemetryAlertRuleUtil.setOrDelete(options, "anomalyDetection", rule.anomalyDetection);
754
+ filter.metricMonitorOptions = options;
755
+ }
756
+ else {
757
+ /*
758
+ * A count's anomaly conditions read their sensitivity and baseline
759
+ * from the same options a metric's do (LogMonitorCriteria,
760
+ * TraceMonitorCriteria).
761
+ */
762
+ const options = Object.assign({}, (((_c = data.previous) === null || _c === void 0 ? void 0 : _c.metricMonitorOptions) || {}));
763
+ TelemetryAlertRuleUtil.setOrDelete(options, "anomalyDetection", rule.anomalyDetection);
764
+ if (Object.keys(options).length > 0) {
765
+ filter.metricMonitorOptions = options;
766
+ }
767
+ else {
768
+ delete filter.metricMonitorOptions;
769
+ }
770
+ }
771
+ return filter;
772
+ }
773
+ static buildRecoveryFilter(data) {
774
+ var _a, _b, _c, _d;
775
+ const { ruleFilter, previousRecoveryFilter } = data;
776
+ const recoveryFilter = Object.assign({}, previousRecoveryFilter);
777
+ /*
778
+ * A metric's recovery reads the same query, unit and aggregation as the
779
+ * rule; the other options (a no-data policy of its own) stay its own.
780
+ */
781
+ if (data.monitorType === MonitorType_1.default.Metrics) {
782
+ const options = Object.assign({}, (previousRecoveryFilter.metricMonitorOptions || {}));
783
+ TelemetryAlertRuleUtil.setOrDelete(options, "metricAlias", (_a = ruleFilter.metricMonitorOptions) === null || _a === void 0 ? void 0 : _a.metricAlias);
784
+ TelemetryAlertRuleUtil.setOrDelete(options, "metricAggregationType", (_b = ruleFilter.metricMonitorOptions) === null || _b === void 0 ? void 0 : _b.metricAggregationType);
785
+ TelemetryAlertRuleUtil.setOrDelete(options, "thresholdUnit", (_c = ruleFilter.metricMonitorOptions) === null || _c === void 0 ? void 0 : _c.thresholdUnit);
786
+ recoveryFilter.metricMonitorOptions = options;
787
+ }
788
+ // Unchanged comparison: the recovery keeps its own wording of it.
789
+ if (!data.ruleChanged) {
790
+ return recoveryFilter;
791
+ }
792
+ const ruleValue = TelemetryAlertRuleUtil.toNumber(ruleFilter.value);
793
+ recoveryFilter.filterType = TelemetryAlertRuleUtil.getOppositeFilterType({
794
+ monitorType: data.monitorType,
795
+ filterType: ruleFilter.filterType,
796
+ value: ruleValue,
797
+ });
798
+ recoveryFilter.value =
799
+ data.recovery === TelemetryAlertRuleRecovery.OppositeWithMargin
800
+ ? (_d = (0, RecommendationCriteriaBuilder_1.getRecoveryThreshold)({
801
+ filterType: ruleFilter.filterType,
802
+ value: ruleValue,
803
+ })) !== null && _d !== void 0 ? _d : ruleValue
804
+ : ruleValue;
805
+ return recoveryFilter;
806
+ }
807
+ /*
808
+ * The opposite of a comparison. For a count, "is 0" is written as its
809
+ * opposite "more than 0", as the out-of-the-box criteria write it.
810
+ */
811
+ static getOppositeFilterType(data) {
812
+ if (TelemetryAlertRuleUtil.isCountMonitorType(data.monitorType) &&
813
+ data.filterType === CriteriaFilter_1.FilterType.EqualTo &&
814
+ data.value === 0) {
815
+ return CriteriaFilter_1.FilterType.GreaterThan;
816
+ }
817
+ return (0, RecommendationCriteriaBuilder_1.getRecoveryFilterType)(data.filterType);
818
+ }
819
+ static applyRuleToInstance(data) {
820
+ const { instance, rule } = data;
821
+ if (!instance.data) {
822
+ return;
823
+ }
824
+ instance.data.filters = [data.filter];
825
+ instance.data.name = rule.name;
826
+ const opensIncident = rule.action === TelemetryAlertRuleAction.Incident ||
827
+ rule.action === TelemetryAlertRuleAction.AlertAndIncident;
828
+ const opensAlert = rule.action === TelemetryAlertRuleAction.Alert ||
829
+ rule.action === TelemetryAlertRuleAction.AlertAndIncident;
830
+ instance.data.createIncidents = opensIncident;
831
+ instance.data.createAlerts = opensAlert;
832
+ /*
833
+ * The rows of an action that is off stay, as the full editor keeps them
834
+ * (MonitorCriteriaInstance.getValidationError): turning it back on
835
+ * brings back what was typed. Only the first row is the rule's.
836
+ */
837
+ if (rule.incident) {
838
+ instance.data.incidents = [
839
+ rule.incident,
840
+ ...instance.data.incidents.slice(1),
841
+ ];
842
+ }
843
+ if (rule.alert) {
844
+ instance.data.alerts = [rule.alert, ...instance.data.alerts.slice(1)];
845
+ }
846
+ instance.data.changeMonitorStatus = Boolean(rule.monitorStatusId);
847
+ instance.data.monitorStatusId = rule.monitorStatusId;
848
+ }
849
+ static isSameComparison(previous, next) {
850
+ if (!previous) {
851
+ return false;
852
+ }
853
+ return (previous.filterType === next.filterType &&
854
+ TelemetryAlertRuleUtil.toNumber(previous.value) ===
855
+ TelemetryAlertRuleUtil.toNumber(next.value));
856
+ }
857
+ // A stored value as a number: numbers, and numeric text (the API's).
858
+ static toNumber(value) {
859
+ if (typeof value === "number") {
860
+ return Number.isFinite(value) ? value : undefined;
861
+ }
862
+ if (typeof value === "string" && value.trim().length > 0) {
863
+ const parsed = Number(value.trim());
864
+ return Number.isFinite(parsed) ? parsed : undefined;
865
+ }
866
+ return undefined;
867
+ }
868
+ static isSameNumber(a, b) {
869
+ const scale = Math.max(1, Math.abs(a), Math.abs(b));
870
+ return Math.abs(a - b) <= scale * 1e-9;
871
+ }
872
+ static setOrDelete(target, key, value) {
873
+ if (value === undefined) {
874
+ delete target[key];
875
+ return;
876
+ }
877
+ target[key] = value;
878
+ }
879
+ }
880
+ TelemetryAlertRuleUtil.RULE_MONITOR_TYPES = [
881
+ MonitorType_1.default.Logs,
882
+ MonitorType_1.default.Traces,
883
+ MonitorType_1.default.Exceptions,
884
+ MonitorType_1.default.Metrics,
885
+ ];
886
+ exports.default = TelemetryAlertRuleUtil;