Skip to content

Commit

Permalink
Merge pull request #56 from RADAR-base/release-0.2.1
Browse files Browse the repository at this point in the history
Release 0.2.1
  • Loading branch information
yatharthranjan committed Feb 22, 2018
2 parents f83a284 + b18da3e commit 78fdde5
Show file tree
Hide file tree
Showing 14 changed files with 187 additions and 59 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ smtp.env

/out/
/libs/

# Distribution
/radar-backend-*
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ COPY --from=builder /code/radar-backend-*/lib/* /usr/lib/
# Load topics validator
COPY ./src/main/docker/radar-backend-init /usr/bin

CMD ["radar-backend-init"]
ENTRYPOINT ["radar-backend-init"]
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ plugins {
//---------------------------------------------------------------------------//

group = 'org.radarcns'
version = '0.2.0'
version = '0.2.1'
ext.description = 'Kafka backend for processing device data.'

mainClassName = 'org.radarcns.RadarBackend'
Expand Down
18 changes: 14 additions & 4 deletions radar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,14 @@ rest_proxy:

#======================== Battery level monitor ========================#
battery_monitor:
notify: # Each project can have a number of email addresses
- project_id: s1
email_address:
- [email protected]
- project_id: s2
email_address:
- [email protected]
level: LOW
email_address:
- [email protected]
email_host: localhost
email_port: 25
email_user: [email protected]
Expand All @@ -54,8 +59,13 @@ battery_monitor:

#======================= Disconnection monitor==========================#
disconnect_monitor:
email_address:
- [email protected]
notify:
- project_id: s1
email_address:
- [email protected]
- project_id: s2
email_address:
- [email protected]
email_host: localhost
email_port: 25
email_user: [email protected]
Expand Down
12 changes: 8 additions & 4 deletions src/integrationTest/resources/org/radarcns/kafka/radar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,19 @@ schema_registry:
protocol: http

battery_monitor:
notify:
- project_id: test
email_address:
- notifier@email
level: LOW
email_address:
- notifier@email
topics:
- android_empatica_e4_battery_level

disconnect_monitor:
email_address:
- notifier2@email
notify:
- project_id: test
email_address:
- notifier@email
email_host: localhost
email_port: 25
email_user: sender@email
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/org/radarcns/config/MonitorConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
* POJO representing a monitor configuration
*/
public class MonitorConfig {
@JsonProperty("email_address")
private List<String> emailAddress;
@JsonProperty("notify")
private List<NotifyConfig> notifyConfig;

@JsonProperty("email_host")
private String emailHost;
Expand All @@ -43,12 +43,12 @@ public class MonitorConfig {
@JsonProperty("message")
private String message = null;

public List<String> getEmailAddress() {
return emailAddress;
public List<NotifyConfig> getNotifyConfig() {
return notifyConfig;
}

public void setEmailAddress(List<String> emailAddress) {
this.emailAddress = emailAddress;
public void setNotifyConfig(List<NotifyConfig> notifyConfig) {
this.notifyConfig = notifyConfig;
}

public List<String> getTopics() {
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/org/radarcns/config/NotifyConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.radarcns.config;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;


/**
* POJO to store each email Notification configuration.
*/
public class NotifyConfig {
@JsonProperty("project_id")
private String projectId;

@JsonProperty("email_address")
private List<String> emailAddress;

@JsonCreator
public NotifyConfig(@JsonProperty("project_id") String projectId,
@JsonProperty("email_address") List<String> emailAddress) {
this.projectId = projectId;
this.emailAddress = emailAddress;
}

public String getProjectId() {
return projectId;
}

public void setProjectId(String projectId) {
this.projectId = projectId;
}

public List<String> getEmailAddress() {
return emailAddress;
}

public void setEmailAddress(List<String> emailAddress) {
this.emailAddress = emailAddress;
}
}
12 changes: 8 additions & 4 deletions src/main/java/org/radarcns/monitor/BatteryLevelMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.radarcns.kafka.ObservationKey;
import org.radarcns.monitor.BatteryLevelMonitor.BatteryLevelState;
import org.radarcns.util.EmailSender;
import org.radarcns.util.EmailSenders;
import org.radarcns.util.RadarSingletonFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -43,7 +44,7 @@ public class BatteryLevelMonitor extends
AbstractKafkaMonitor<GenericRecord, GenericRecord, BatteryLevelState> {
private static final Logger logger = LoggerFactory.getLogger(BatteryLevelMonitor.class);

private final EmailSender sender;
private final EmailSenders senders;
private final Status minLevel;
private final long logInterval;
private long messageNumber;
Expand All @@ -52,19 +53,19 @@ public class BatteryLevelMonitor extends
* BatteryLevelMonitor constructor.
* @param radar RADAR properties
* @param topics topics to monitor, each of which has a "batteryLevel" value field
* @param sender email sender for notifications, null if no notifications should be sent.
* @param senders email sender for notifications, null if no notifications should be sent.
* @param minLevel minimum battery level, below which a notification should be sent
* @param logInterval every how many messages to log, 0 for no log messages
*/
public BatteryLevelMonitor(RadarPropertyHandler radar, Collection<String> topics,
EmailSender sender, Status minLevel, long logInterval) {
EmailSenders senders, Status minLevel, long logInterval) {
super(radar, topics, "battery_monitors", "1", new BatteryLevelState());

Properties props = new Properties();
props.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest");
configure(props);

this.sender = sender;
this.senders = senders;
this.minLevel = minLevel == null ? Status.CRITICAL : minLevel;
this.logInterval = logInterval;
}
Expand Down Expand Up @@ -108,6 +109,9 @@ protected void evaluateRecord(ConsumerRecord<GenericRecord, GenericRecord> recor
}

private void updateStatus(ObservationKey key, Status status) {

// Don't report if no email address for this projectId
EmailSender sender = senders.getEmailSenderForProject(key.getProjectId());
if (sender == null) {
return;
}
Expand Down
22 changes: 19 additions & 3 deletions src/main/java/org/radarcns/monitor/DisconnectMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.radarcns.kafka.ObservationKey;
import org.radarcns.monitor.DisconnectMonitor.DisconnectMonitorState;
import org.radarcns.util.EmailSender;
import org.radarcns.util.EmailSenders;
import org.radarcns.util.Monitor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -56,7 +57,7 @@ public class DisconnectMonitor extends AbstractKafkaMonitor<

private final ScheduledExecutorService scheduler;
private final long timeUntilReportedMissing;
private final EmailSender sender;
private final EmailSenders senders;
private final Format dayFormat;
private final int numRepetitions;
private final long repeatInterval;
Expand All @@ -65,9 +66,9 @@ public class DisconnectMonitor extends AbstractKafkaMonitor<
private final String message;

public DisconnectMonitor(RadarPropertyHandler radar, Collection<String> topics, String groupId,
EmailSender sender) {
EmailSenders senders) {
super(radar, topics, groupId, "1", new DisconnectMonitorState());
this.sender = sender;
this.senders = senders;
this.dayFormat = DateFormat.getDateTimeInstance(
DateFormat.MEDIUM, DateFormat.SHORT, Locale.US);
this.scheduler = Executors.newSingleThreadScheduledExecutor();
Expand Down Expand Up @@ -169,6 +170,13 @@ private void scheduleRepetition(final String key, final MissingRecordsReport rep

private void reportMissing(String keyString, MissingRecordsReport report) {
ObservationKey key = getStateStore().stringToKey(keyString);

// Don't report if no email address for this projectId
EmailSender sender = senders.getEmailSenderForProject(key.getProjectId());
if(sender == null) {
return;
}

long timeout = report.getTimeout();
logger.info("Device {} timeout {} (message {} of {}). Reporting it missing.", key,
timeout, report.getMessageNumber(), numRepetitions);
Expand Down Expand Up @@ -202,6 +210,14 @@ private void reportMissing(String keyString, MissingRecordsReport report) {
}

private void reportRecovered(ObservationKey key, long reportedMissingTime) {

// Don't report if no email address for this projectId
EmailSender sender = senders.getEmailSenderForProject(key.getProjectId());
if(sender == null) {
return;
}


logger.info("Device {} seen again. Reporting it recovered.", key);
try {
Date reportedMissingDate = new Date(reportedMissingTime);
Expand Down
27 changes: 11 additions & 16 deletions src/main/java/org/radarcns/monitor/KafkaMonitorFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,9 @@
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
import org.radarcns.config.BatteryMonitorConfig;
import org.radarcns.config.DisconnectMonitorConfig;
import org.radarcns.config.MonitorConfig;
import org.radarcns.config.RadarBackendOptions;
import org.radarcns.config.RadarPropertyHandler;
import org.radarcns.config.SourceStatisticsMonitorConfig;
import org.radarcns.util.EmailSender;

import org.radarcns.config.*;
import org.radarcns.util.EmailSenders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -106,7 +102,7 @@ private KafkaMonitor createBatteryLevelMonitor() throws IOException {
}

BatteryLevelMonitor.Status minLevel = BatteryLevelMonitor.Status.CRITICAL;
EmailSender sender = getSender(config);
EmailSenders senders = getSenders(config);
Collection<String> topics = getTopics(config, "android_empatica_e4_battery_level");

if (config.getLevel() != null) {
Expand All @@ -121,7 +117,7 @@ private KafkaMonitor createBatteryLevelMonitor() throws IOException {
}
long logInterval = config.getLogInterval();

return new BatteryLevelMonitor(properties, topics, sender, minLevel, logInterval);
return new BatteryLevelMonitor(properties, topics, senders, minLevel, logInterval);
}

private KafkaMonitor createDisconnectMonitor()
Expand All @@ -131,16 +127,15 @@ private KafkaMonitor createDisconnectMonitor()
logger.warn("Disconnect monitor is not configured. Cannot start it.");
return null;
}
EmailSender sender = getSender(config);
EmailSenders senders = getSenders(config);
Collection<String> topics = getTopics(config, "android_empatica_e4_temperature");
return new DisconnectMonitor(properties, topics, "disconnect_monitor", sender);
return new DisconnectMonitor(properties, topics, "disconnect_monitor", senders);
}

private EmailSender getSender(MonitorConfig config) throws IOException {
if (config != null && config.getEmailAddress() != null) {
return new EmailSender(config.getEmailHost(), config.getEmailPort(),
config.getEmailUser(),
config.getEmailAddress());

private EmailSenders getSenders(MonitorConfig config) throws IOException {
if (config != null && config.getNotifyConfig() != null) {
return EmailSenders.parseConfig(config);
}
return null;
}
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/org/radarcns/util/EmailSenders.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.radarcns.util;

import org.radarcns.config.MonitorConfig;
import org.radarcns.config.NotifyConfig;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


/**
* Class to store {@link EmailSender} associated with each project.
*/
public class EmailSenders {

private final Map<String,EmailSender> emailSenderMap;

public EmailSenders(Map<String, EmailSender> map) {
this.emailSenderMap = map;
}

/**
*
* Parses the {@link MonitorConfig} to map the corresponding
* {@link EmailSender} to each project. A project can have a list of
* associated email addresses.
*
* @param config Configuration of the Monitor containing project
* and email address mapping
* @throws IOException
*/

public static EmailSenders parseConfig(MonitorConfig config) throws IOException{
Map<String, EmailSender> map = new HashMap<>();
for(NotifyConfig notifyConfig : config.getNotifyConfig()) {
map.put(notifyConfig.getProjectId(),
new EmailSender(config.getEmailHost(), config.getEmailPort(),
config.getEmailUser(), notifyConfig.getEmailAddress()));
}

return new EmailSenders(map);
}

public EmailSender getEmailSenderForProject(String projectId) {
return emailSenderMap.get(projectId);
}

}
Loading

0 comments on commit 78fdde5

Please sign in to comment.