|
@@ -1,138 +1,138 @@
|
|
|
-package cz.senslog.watchdog.app;
|
|
|
|
|
-
|
|
|
|
|
-import cz.senslog.watchdog.config.AllMonitoredObjects;
|
|
|
|
|
-import cz.senslog.watchdog.config.ExecutableGroup;
|
|
|
|
|
-import cz.senslog.watchdog.config.MonitoredObject;
|
|
|
|
|
-import cz.senslog.watchdog.core.adt.Node;
|
|
|
|
|
-import cz.senslog.watchdog.domain.*;
|
|
|
|
|
-import cz.senslog.watchdog.messagebroker.MessageBroker;
|
|
|
|
|
-import cz.senslog.watchdog.provider.DataProvider;
|
|
|
|
|
-import cz.senslog.watchdog.provider.ProvidedData;
|
|
|
|
|
-import cz.senslog.watchdog.provider.ProvidedObject;
|
|
|
|
|
-import org.apache.logging.log4j.LogManager;
|
|
|
|
|
-import org.apache.logging.log4j.Logger;
|
|
|
|
|
-
|
|
|
|
|
-import java.time.Instant;
|
|
|
|
|
-import java.time.ZoneId;
|
|
|
|
|
-import java.util.*;
|
|
|
|
|
-
|
|
|
|
|
-import static cz.senslog.watchdog.domain.StatusReport.*;
|
|
|
|
|
-import static java.time.LocalDateTime.ofInstant;
|
|
|
|
|
-import static java.util.Collections.emptyList;
|
|
|
|
|
-import static java.util.Collections.singletonList;
|
|
|
|
|
-
|
|
|
|
|
-public class Watcher {
|
|
|
|
|
-
|
|
|
|
|
- private static final Logger logger = LogManager.getLogger(Watcher.class);
|
|
|
|
|
-
|
|
|
|
|
- private final ExecutableGroup group;
|
|
|
|
|
- private final DataProvider dataProvider;
|
|
|
|
|
- private final MessageBroker messageBroker;
|
|
|
|
|
-
|
|
|
|
|
- public static Watcher create(ExecutableGroup group, DataProvider dataProvider, MessageBroker messageBroker) {
|
|
|
|
|
- return new Watcher(group, dataProvider, messageBroker);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private Watcher(ExecutableGroup group, DataProvider dataProvider, MessageBroker messageBroker) {
|
|
|
|
|
- this.group = group;
|
|
|
|
|
- this.dataProvider = dataProvider;
|
|
|
|
|
- this.messageBroker = messageBroker;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public void check() {
|
|
|
|
|
-
|
|
|
|
|
- Instant now = Instant.now();
|
|
|
|
|
- ProvidedData data = dataProvider.getLastData();
|
|
|
|
|
-
|
|
|
|
|
- List<SimpleReport> reports = new ArrayList<>();
|
|
|
|
|
- List<String> reportedMessages = new ArrayList<>(data.getErrorMessages());
|
|
|
|
|
-
|
|
|
|
|
- if (!data.isEmpty()) {
|
|
|
|
|
- for (String unitId : group.getKeys()) {
|
|
|
|
|
- Node<String, MonitoredObject> mUnitNode = group.getNode(unitId);
|
|
|
|
|
- if (mUnitNode.getValue() instanceof AllMonitoredObjects) {
|
|
|
|
|
- // ALL UNITS
|
|
|
|
|
- int period = mUnitNode.getValue().getPeriod();
|
|
|
|
|
- Instant minTimestamp = now.minusSeconds(period);
|
|
|
|
|
- for (String pUnitId : data.getKeys()) {
|
|
|
|
|
- Node<String, ProvidedObject> pUnitNode = data.getNode(pUnitId);
|
|
|
|
|
- String unitName = pUnitNode.getValue().getName();
|
|
|
|
|
- for (ProvidedObject pObject : pUnitNode.getValues()) {
|
|
|
|
|
- SimpleReport simpleReport = new SimpleReport(new ObservationInfo(
|
|
|
|
|
- new Unit(Long.parseLong(pUnitId), unitName),
|
|
|
|
|
- new Sensor(Long.parseLong(pObject.getId()), pObject.getName()),
|
|
|
|
|
- pObject.getTimestamp()
|
|
|
|
|
- ),
|
|
|
|
|
- pObject.getTimestamp().toInstant().isBefore(minTimestamp) ? FAIL : OK
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- if (isAllowedToReport(simpleReport)) {
|
|
|
|
|
- reports.add(simpleReport);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- } else if (!data.containsNode(unitId)) {
|
|
|
|
|
- reportedMessages.add(String.format("No data for: unit_id - %s", unitId));
|
|
|
|
|
- } else {
|
|
|
|
|
- Node<String, ProvidedObject> pUnitNode = data.getNode(unitId);
|
|
|
|
|
- String unitName = pUnitNode.getValue().getName();
|
|
|
|
|
- for (MonitoredObject mSensor : mUnitNode.getValues()) {
|
|
|
|
|
- int period = mSensor.getPeriod();
|
|
|
|
|
- Instant minTimestamp = now.minusSeconds(period);
|
|
|
|
|
- Collection<ProvidedObject> providedObjects = mSensor instanceof AllMonitoredObjects ?
|
|
|
|
|
- pUnitNode.getValues() : pUnitNode.containsKey(mSensor.getId()) ?
|
|
|
|
|
- singletonList(pUnitNode.getNode(mSensor.getId()).getValue()) : emptyList();
|
|
|
|
|
-
|
|
|
|
|
- if (providedObjects.isEmpty()) {
|
|
|
|
|
- reports.add(new SimpleReport(new ObservationInfo(
|
|
|
|
|
- new Unit(Long.parseLong(unitId), unitName),
|
|
|
|
|
- new Sensor(Long.parseLong(mSensor.getId()), "unknown"),
|
|
|
|
|
- null
|
|
|
|
|
- ),
|
|
|
|
|
- NO_DATA
|
|
|
|
|
- ));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- for (ProvidedObject pObject : providedObjects) {
|
|
|
|
|
- SimpleReport simpleReport = new SimpleReport(new ObservationInfo(
|
|
|
|
|
- new Unit(Long.parseLong(unitId), unitName),
|
|
|
|
|
- new Sensor(Long.parseLong(pObject.getId()), pObject.getName()),
|
|
|
|
|
- pObject.getTimestamp()
|
|
|
|
|
- ),
|
|
|
|
|
- pObject.getTimestamp().toInstant().isBefore(minTimestamp) ? FAIL : OK
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- if (isAllowedToReport(simpleReport)) {
|
|
|
|
|
- reports.add(simpleReport);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- } else {
|
|
|
|
|
- reportedMessages.add("An error has occurred. No data to check.");
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- Map<String, String> operationProperties = new HashMap<>();
|
|
|
|
|
- operationProperties.put("Group name", group.getConfig().getName());
|
|
|
|
|
-
|
|
|
|
|
- messageBroker.send(new Report(ofInstant(now, ZoneId.systemDefault()), reports, reportedMessages, operationProperties), status -> {
|
|
|
|
|
- String brokerType = messageBroker.getType().name().toLowerCase();
|
|
|
|
|
- if (status.isSuccess()) {
|
|
|
|
|
- logger.info("The report at '{}' was send via '{}' broker successfully for the group's name '{}'.",
|
|
|
|
|
- status.getReport().getCreated(), brokerType, group.getConfig().getName()
|
|
|
|
|
- );
|
|
|
|
|
- } else {
|
|
|
|
|
- logger.error("Can not send a message '{}' via '{}' broker because of '{}'.",
|
|
|
|
|
- status.getReport().getCreated(), brokerType, status.getError()
|
|
|
|
|
- );
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- private boolean isAllowedToReport(SimpleReport report) {
|
|
|
|
|
- if (report == null) { return false; }
|
|
|
|
|
- return report.getStatus().ordinal() >= group.getConfig().getResultType().ordinal();
|
|
|
|
|
- }
|
|
|
|
|
|
|
+package cz.senslog.watchdog.core;
|
|
|
|
|
+
|
|
|
|
|
+import cz.senslog.watchdog.config.AllMonitoredObjects;
|
|
|
|
|
+import cz.senslog.watchdog.config.ExecutableGroup;
|
|
|
|
|
+import cz.senslog.watchdog.config.MonitoredObject;
|
|
|
|
|
+import cz.senslog.watchdog.core.adt.Node;
|
|
|
|
|
+import cz.senslog.watchdog.domain.*;
|
|
|
|
|
+import cz.senslog.watchdog.messagebroker.MessageBroker;
|
|
|
|
|
+import cz.senslog.watchdog.provider.DataProvider;
|
|
|
|
|
+import cz.senslog.watchdog.provider.ProvidedData;
|
|
|
|
|
+import cz.senslog.watchdog.provider.ProvidedObject;
|
|
|
|
|
+import org.apache.logging.log4j.LogManager;
|
|
|
|
|
+import org.apache.logging.log4j.Logger;
|
|
|
|
|
+
|
|
|
|
|
+import java.time.Instant;
|
|
|
|
|
+import java.time.ZoneId;
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
+
|
|
|
|
|
+import static cz.senslog.watchdog.domain.StatusReport.*;
|
|
|
|
|
+import static java.time.LocalDateTime.ofInstant;
|
|
|
|
|
+import static java.util.Collections.emptyList;
|
|
|
|
|
+import static java.util.Collections.singletonList;
|
|
|
|
|
+
|
|
|
|
|
+public class Watcher {
|
|
|
|
|
+
|
|
|
|
|
+ private static final Logger logger = LogManager.getLogger(Watcher.class);
|
|
|
|
|
+
|
|
|
|
|
+ private final ExecutableGroup group;
|
|
|
|
|
+ private final DataProvider dataProvider;
|
|
|
|
|
+ private final MessageBroker messageBroker;
|
|
|
|
|
+
|
|
|
|
|
+ public static Watcher create(ExecutableGroup group, DataProvider dataProvider, MessageBroker messageBroker) {
|
|
|
|
|
+ return new Watcher(group, dataProvider, messageBroker);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Watcher(ExecutableGroup group, DataProvider dataProvider, MessageBroker messageBroker) {
|
|
|
|
|
+ this.group = group;
|
|
|
|
|
+ this.dataProvider = dataProvider;
|
|
|
|
|
+ this.messageBroker = messageBroker;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void check() {
|
|
|
|
|
+
|
|
|
|
|
+ Instant now = Instant.now();
|
|
|
|
|
+ ProvidedData data = dataProvider.getLastData();
|
|
|
|
|
+
|
|
|
|
|
+ List<SimpleReport> reports = new ArrayList<>();
|
|
|
|
|
+ List<String> reportedMessages = new ArrayList<>(data.getErrorMessages());
|
|
|
|
|
+
|
|
|
|
|
+ if (!data.isEmpty()) {
|
|
|
|
|
+ for (String unitId : group.getKeys()) {
|
|
|
|
|
+ Node<String, MonitoredObject> mUnitNode = group.getNode(unitId);
|
|
|
|
|
+ if (mUnitNode.getValue() instanceof AllMonitoredObjects) {
|
|
|
|
|
+ // ALL UNITS
|
|
|
|
|
+ int period = mUnitNode.getValue().getPeriod();
|
|
|
|
|
+ Instant minTimestamp = now.minusSeconds(period);
|
|
|
|
|
+ for (String pUnitId : data.getKeys()) {
|
|
|
|
|
+ Node<String, ProvidedObject> pUnitNode = data.getNode(pUnitId);
|
|
|
|
|
+ String unitName = pUnitNode.getValue().getName();
|
|
|
|
|
+ for (ProvidedObject pObject : pUnitNode.getValues()) {
|
|
|
|
|
+ SimpleReport simpleReport = new SimpleReport(new ObservationInfo(
|
|
|
|
|
+ new Unit(Long.parseLong(pUnitId), unitName),
|
|
|
|
|
+ new Sensor(Long.parseLong(pObject.getId()), pObject.getName()),
|
|
|
|
|
+ pObject.getTimestamp()
|
|
|
|
|
+ ),
|
|
|
|
|
+ pObject.getTimestamp().toInstant().isBefore(minTimestamp) ? FAIL : OK
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ if (isAllowedToReport(simpleReport)) {
|
|
|
|
|
+ reports.add(simpleReport);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } else if (!data.containsNode(unitId)) {
|
|
|
|
|
+ reportedMessages.add(String.format("No data for: unit_id - %s", unitId));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ Node<String, ProvidedObject> pUnitNode = data.getNode(unitId);
|
|
|
|
|
+ String unitName = pUnitNode.getValue().getName();
|
|
|
|
|
+ for (MonitoredObject mSensor : mUnitNode.getValues()) {
|
|
|
|
|
+ int period = mSensor.getPeriod();
|
|
|
|
|
+ Instant minTimestamp = now.minusSeconds(period);
|
|
|
|
|
+ Collection<ProvidedObject> providedObjects = mSensor instanceof AllMonitoredObjects ?
|
|
|
|
|
+ pUnitNode.getValues() : pUnitNode.containsKey(mSensor.getId()) ?
|
|
|
|
|
+ singletonList(pUnitNode.getNode(mSensor.getId()).getValue()) : emptyList();
|
|
|
|
|
+
|
|
|
|
|
+ if (providedObjects.isEmpty()) {
|
|
|
|
|
+ reports.add(new SimpleReport(new ObservationInfo(
|
|
|
|
|
+ new Unit(Long.parseLong(unitId), unitName),
|
|
|
|
|
+ new Sensor(Long.parseLong(mSensor.getId()), "unknown"),
|
|
|
|
|
+ null
|
|
|
|
|
+ ),
|
|
|
|
|
+ NO_DATA
|
|
|
|
|
+ ));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ for (ProvidedObject pObject : providedObjects) {
|
|
|
|
|
+ SimpleReport simpleReport = new SimpleReport(new ObservationInfo(
|
|
|
|
|
+ new Unit(Long.parseLong(unitId), unitName),
|
|
|
|
|
+ new Sensor(Long.parseLong(pObject.getId()), pObject.getName()),
|
|
|
|
|
+ pObject.getTimestamp()
|
|
|
|
|
+ ),
|
|
|
|
|
+ pObject.getTimestamp().toInstant().isBefore(minTimestamp) ? FAIL : OK
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ if (isAllowedToReport(simpleReport)) {
|
|
|
|
|
+ reports.add(simpleReport);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ reportedMessages.add("An error has occurred. No data to check.");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, String> operationProperties = new HashMap<>();
|
|
|
|
|
+ operationProperties.put("Group name", group.getConfig().getName());
|
|
|
|
|
+
|
|
|
|
|
+ messageBroker.send(new Report(ofInstant(now, ZoneId.systemDefault()), reports, reportedMessages, operationProperties), status -> {
|
|
|
|
|
+ String brokerType = messageBroker.getType().name().toLowerCase();
|
|
|
|
|
+ if (status.isSuccess()) {
|
|
|
|
|
+ logger.info("The report at '{}' was send via '{}' broker successfully for the group's name '{}'.",
|
|
|
|
|
+ status.getReport().getCreated(), brokerType, group.getConfig().getName()
|
|
|
|
|
+ );
|
|
|
|
|
+ } else {
|
|
|
|
|
+ logger.error("Can not send a message '{}' via '{}' broker because of '{}'.",
|
|
|
|
|
+ status.getReport().getCreated(), brokerType, status.getError()
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ private boolean isAllowedToReport(SimpleReport report) {
|
|
|
|
|
+ if (report == null) { return false; }
|
|
|
|
|
+ return report.getStatus().ordinal() >= group.getConfig().getResultType().ordinal();
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|