Design Alert Monitoring System
Design an alert monitoring system that monitors sensor readings from multiple machines. A machine may contain different sensor types, such as Temperature and Pressure.
Each sensor has an inclusive lower threshold and upper threshold. When a recorded value is less than the lower threshold or greater than the upper threshold, the system must create and store an alert.
An alert identifier is supplied with every sensor reading. The identifier is used only when the reading generates an alert. Otherwise, it is discarded. Every supplied alert identifier must be unique.
Users can view all generated alerts and manually change an alert's state to ACKNOWLEDGED, RESOLVED, or IGNORED.
Alert Monitoring System
Implement the following methods in the AlertMonitoringSystem class.
Add Sensor
void addSensor(String machineId, String sensorType, double lowerThreshold, double upperThreshold)
Add a sensor to a machine and define its valid inclusive threshold range. A machine can have multiple sensors, but each sensor type is added only once to a particular machine.
Record Sensor Value
String recordSensorValue(int alertId, String machineId, String sensorType, double value)
Record a value for the specified sensor. If the value is outside the sensor's threshold range, create an alert using the supplied alertId. A newly created alert has the initial state TRIGGERED.
When an alert is created, return it using the following format:
alertId,machineId,sensorType,value,state
If the value is within the allowed range, return an empty string. In this case, the supplied alertId is discarded and no alert is stored for it.
View Alerts
List<String> viewAlerts()
Return every generated alert in increasing order of alert identifier. Represent each alert using the following format:
alertId,machineId,sensorType,value,state
Return an empty list when no alerts have been generated.
Update Alert State
boolean updateAlertState(int alertId, String newState)
Manually update the state of an existing alert. Return true after the state is updated successfully. Return false if no generated alert has the specified identifier.
Behavior Requirements
- A value equal to either threshold is within the permitted range.
- Every out-of-range reading creates a separate alert.
- The supplied
alertId is used only if the reading creates an alert.
- If a reading does not create an alert, its supplied
alertId is discarded.
- Every
alertId supplied to recordSensorValue must be unique.
- Alert identifiers do not need to be consecutive or supplied in sorted order.
- A newly generated alert always has the state
TRIGGERED.
- Updating an alert changes only its state.
- Alerts remain available after their states are updated.
viewAlerts() returns alerts in increasing order of alert identifier.
Constraints
1 <= machineId.length() <= 50
1 <= sensorType.length() <= 50
-1,000,000,000 <= lowerThreshold < upperThreshold <= 1,000,000,000
-1,000,000,000 <= value <= 1,000,000,000
1 <= alertId <= 1,000,000,000
machineId and sensorType contain only letters, digits, hyphens, and underscores.
- Every
alertId passed to recordSensorValue is globally unique.
- At most
100,000 sensors will be added.
- At most
100,000 sensor values will be recorded.
- Each
machineId and sensorType used by recordSensorValue identifies an added sensor.
newState is ACKNOWLEDGED, RESOLVED, or IGNORED.
Examples
Example 1
Method calls:
addSensor(machineId = "Machine-A", sensorType = "Temperature", lowerThreshold = 10.0, upperThreshold = 40.0)
recordSensorValue(alertId = 45, machineId = "Machine-A", sensorType = "Temperature", value = 28.5)
Output:
""
The value is inside the inclusive range, so no alert is generated. Alert identifier 45 is discarded.
Example 2
Method calls:
addSensor(machineId = "Machine-B", sensorType = "Pressure", lowerThreshold = 20.0, upperThreshold = 75.0)
recordSensorValue(alertId = 72, machineId = "Machine-B", sensorType = "Pressure", value = 81.0)
Output:
"72,Machine-B,Pressure,81.0,TRIGGERED"
The value is greater than the upper threshold, so an alert is created using the supplied alert identifier 72.
Example 3
Assume that the system contains these two alerts:
"72,Machine-B,Pressure,81.0,TRIGGERED"
"18,Machine-C,Temperature,-6.0,TRIGGERED"
Method call:
updateAlertState(alertId = 72, newState = "ACKNOWLEDGED")
Output:
true
Method call:
viewAlerts()
Output:
["18,Machine-C,Temperature,-6.0,TRIGGERED", "72,Machine-B,Pressure,81.0,ACKNOWLEDGED"]
The first alert's state is updated, and the alerts are returned in increasing order of alert identifier.
Example 4
Method call:
updateAlertState(alertId = 45, newState = "RESOLVED")
Output:
false
Alert identifier 45 was discarded because its reading did not generate an alert, so there is no stored alert to update.