Skip to content

Commit

Permalink
Backend: Common+Metadata: Refactor name of AlertingSetting to UserAle…
Browse files Browse the repository at this point in the history
…rtingSettings (#2414)

* Refactored Name of AlertingSetting to UserAlertingSettings

This is just a refactor of the Name. This does not provide any functionality, but will improve the readability of the SumStateAlert PR in the future.
This commit is one of many prepare PRs / commits for a bigger PR:
SumStateAlert #2260
By Seperating the big #2260 into smaller commits we would like to make the review easier.

Co-authored-by: Kai Jeschek <[email protected]>
  • Loading branch information
DerStoecki and da-Kai authored Dec 6, 2023
1 parent a6c6a3b commit 9cdb86c
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import io.openems.backend.alerting.scheduler.MessageSchedulerService;
import io.openems.backend.alerting.scheduler.TimedExecutor;
import io.openems.backend.alerting.scheduler.TimedExecutor.TimedTask;
import io.openems.backend.common.metadata.AlertingSetting;
import io.openems.backend.common.metadata.UserAlertingSettings;
import io.openems.backend.common.metadata.Edge;
import io.openems.backend.common.metadata.Mailer;
import io.openems.backend.common.metadata.Metadata;
Expand Down Expand Up @@ -175,7 +175,7 @@ protected OfflineEdgeMessage getEdgeMessage(Edge edge) {
return null;
}

private boolean shouldReceiveMail(Edge edge, AlertingSetting setting) {
private boolean shouldReceiveMail(Edge edge, UserAlertingSettings setting) {
final var lastMailRecievedAt = setting.getLastNotification();
final var edgeOfflineSince = edge.getLastmessage();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
import com.google.gson.JsonPrimitive;

import io.openems.backend.alerting.Message;
import io.openems.backend.common.metadata.AlertingSetting;
import io.openems.backend.common.metadata.UserAlertingSettings;
import io.openems.common.utils.JsonUtils;

public class OfflineEdgeMessage extends Message {

public static final String TEMPLATE = "alerting_email";

private final ZonedDateTime offlineAt;
private final TreeMap<Integer, List<AlertingSetting>> recipients;
private final TreeMap<Integer, List<UserAlertingSettings>> recipients;

private OfflineEdgeMessage(String edgeId, ZonedDateTime offlineAt, TreeMap<Integer, List<AlertingSetting>> map) {
private OfflineEdgeMessage(String edgeId, ZonedDateTime offlineAt, TreeMap<Integer, List<UserAlertingSettings>> map) {
super(edgeId);
this.offlineAt = offlineAt;
this.recipients = map;
Expand All @@ -41,13 +41,13 @@ public ZonedDateTime getNotifyStamp() {
*
* @param setting of user to whom to send the mail to
*/
public void addRecipient(AlertingSetting setting) {
public void addRecipient(UserAlertingSettings setting) {
this.recipients.putIfAbsent(setting.getDelayTime(), new ArrayList<>());
var settings = this.recipients.get(setting.getDelayTime());
settings.add(setting);
}

public List<AlertingSetting> getCurrentRecipients() {
public List<UserAlertingSettings> getCurrentRecipients() {
return this.recipients.get(this.recipients.firstKey());
}

Expand Down Expand Up @@ -84,7 +84,7 @@ public JsonObject getParams() {

@Override
public String toString() {
var rec = this.getCurrentRecipients().stream().map(AlertingSetting::getUserId).collect(Collectors.joining(","));
var rec = this.getCurrentRecipients().stream().map(UserAlertingSettings::getUserId).collect(Collectors.joining(","));
return "OfflineEdgeMessage{for=" + this.getEdgeId() + ", to=[" + rec + "], at=" + this.getNotifyStamp() + "}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import io.openems.backend.alerting.scheduler.MessageScheduler;
import io.openems.backend.alerting.scheduler.MessageSchedulerService;
import io.openems.backend.alerting.scheduler.MinuteTimer;
import io.openems.backend.common.metadata.AlertingSetting;
import io.openems.backend.common.metadata.UserAlertingSettings;
import io.openems.backend.common.metadata.Edge;
import io.openems.backend.common.metadata.Mailer;
import io.openems.backend.common.test.DummyMetadata;
Expand Down Expand Up @@ -83,7 +83,7 @@ public EventAdmin getEventAdmin() {

public static class AlertingMetadataImpl extends SimpleMetadataImpl {
private Collection<Edge> edges;
private Map<String, List<AlertingSetting>> alertingSettings;
private Map<String, List<UserAlertingSettings>> alertingSettings;
private Map<String, Level> sumStates = new HashMap<>(10);

/**
Expand All @@ -92,7 +92,7 @@ public static class AlertingMetadataImpl extends SimpleMetadataImpl {
* @param edges to add
* @param settings to add
*/
public void initialize(Collection<Edge> edges, Map<String, List<AlertingSetting>> settings) {
public void initialize(Collection<Edge> edges, Map<String, List<UserAlertingSettings>> settings) {
this.edges = edges;
this.alertingSettings = settings;
}
Expand All @@ -113,7 +113,7 @@ public Collection<Edge> getAllOfflineEdges() {
}

@Override
public List<AlertingSetting> getUserAlertingSettings(String edgeId) {
public List<UserAlertingSettings> getUserAlertingSettings(String edgeId) {
return this.alertingSettings.get(edgeId);
}

Expand All @@ -125,7 +125,7 @@ public Collection<Edge> getEdges() {
return this.edges;
}

public Map<String, List<AlertingSetting>> getAlertingSettings() {
public Map<String, List<UserAlertingSettings>> getAlertingSettings() {
return this.alertingSettings;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import io.openems.backend.alerting.Dummy.MailerImpl;
import io.openems.backend.alerting.Dummy.TimeLeapMinuteTimer;
import io.openems.backend.alerting.scheduler.Scheduler;
import io.openems.backend.common.metadata.AlertingSetting;
import io.openems.backend.common.metadata.UserAlertingSettings;
import io.openems.backend.common.metadata.Edge;
import io.openems.common.event.EventBuilder;

Expand All @@ -36,7 +36,7 @@ record SimpleAlertingSetting(String user, int delay) {
private Scheduler scheduler;

private HashMap<Integer, Edge> edges;
private Map<String, List<AlertingSetting>> settings;
private Map<String, List<UserAlertingSettings>> settings;

public TestEnvironment() {
final var instant = Instant.now();
Expand Down Expand Up @@ -86,9 +86,9 @@ public void createEdge(int id, boolean online, ZonedDateTime lastMessage, Simple
edge.setOnline(online);
this.edges.put(id, edge);

var list = new ArrayList<AlertingSetting>(5);
var list = new ArrayList<UserAlertingSettings>(5);
for (var set : settings) {
list.add(new AlertingSetting(id, set.user, null, lastMessage, set.delay));
list.add(new UserAlertingSettings(id, set.user, null, lastMessage, set.delay));
}

this.settings.put(edge.getId(), list);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import io.openems.backend.alerting.Dummy.SimpleMetadataImpl;
import io.openems.backend.alerting.message.OfflineEdgeMessage;
import io.openems.backend.alerting.scheduler.MinuteTimer;
import io.openems.backend.common.metadata.AlertingSetting;
import io.openems.backend.common.metadata.UserAlertingSettings;
import io.openems.backend.common.metadata.Edge;
import io.openems.backend.common.metadata.Metadata;
import io.openems.backend.common.test.DummyMetadata;
Expand Down Expand Up @@ -56,12 +56,12 @@ public void send() {
final var timer = new MinuteTimer(Clock.systemUTC());
final var handler = new OfflineEdgeHandler(msgsch, timer, mailer, Utility.getTestMetadata(), 1);
final var msg_1 = new OfflineEdgeMessage("1", ZonedDateTime.now().minusSeconds(1));
msg_1.addRecipient(new AlertingSetting(1, "user1", null, null, 1));
msg_1.addRecipient(new AlertingSetting(1, "user2", null, null, 2));
msg_1.addRecipient(new UserAlertingSettings(1, "user1", null, null, 1));
msg_1.addRecipient(new UserAlertingSettings(1, "user2", null, null, 2));

final var msg_2 = new OfflineEdgeMessage("Fail", ZonedDateTime.now().minusSeconds(2));
msg_2.addRecipient(new AlertingSetting(2, "user1", null, null, 1));
msg_2.addRecipient(new AlertingSetting(2, "user2", null, null, 2));
msg_2.addRecipient(new UserAlertingSettings(2, "user1", null, null, 1));
msg_2.addRecipient(new UserAlertingSettings(2, "user2", null, null, 2));

assertEquals(mailer.sentMails.size(), 0);
var msgs = new ArrayList<>(List.of(msg_1, msg_2));
Expand Down Expand Up @@ -180,16 +180,16 @@ private static AlertingMetadataImpl getTestMetadata() {
Utility.getTestEdge(metadata, "5", Utility.now.minusMonths(1), false), //
Utility.getTestEdge(metadata, "6", null, false)); //

final Map<String, List<AlertingSetting>> settings = Map.of(//
final Map<String, List<UserAlertingSettings>> settings = Map.of(//
"1", List.of(//
new AlertingSetting(1, "user1", null, Utility.now.minusDays(1), 1)), //
new UserAlertingSettings(1, "user1", null, Utility.now.minusDays(1), 1)), //
"2", List.of(), //
"3", List.of(//
new AlertingSetting(3, "user1", null, Utility.now.plusDays(1), 1), //
new AlertingSetting(3, "user2", null, Utility.now, 0)), //
new UserAlertingSettings(3, "user1", null, Utility.now.plusDays(1), 1), //
new UserAlertingSettings(3, "user2", null, Utility.now, 0)), //
"4", List.of(//
new AlertingSetting(4, "edge2", null, Utility.now.minusDays(1), 1), //
new AlertingSetting(4, "edge3", null, Utility.now.minusDays(1), 2)) //
new UserAlertingSettings(4, "edge2", null, Utility.now.minusDays(1), 1), //
new UserAlertingSettings(4, "edge3", null, Utility.now.minusDays(1), 2)) //
);

metadata.initialize(edges, settings);
Expand All @@ -206,13 +206,13 @@ private static class ToManyMsgsMetadata extends DummyMetadata {
private final ZonedDateTime now = ZonedDateTime.now();
private final ZonedDateTime yesterday = this.now.minusDays(1);

private List<AlertingSetting> userList = List.of(//
new AlertingSetting(1, "user0", null, this.yesterday, 60), //
new AlertingSetting(1, "user1", null, this.yesterday, 15), //
new AlertingSetting(1, "user2", null, this.yesterday, 10), //
new AlertingSetting(1, "user3", null, this.yesterday, 30), //
new AlertingSetting(1, "user4", null, this.yesterday, 30), //
new AlertingSetting(1, "user5", null, this.yesterday, 1440));
private List<UserAlertingSettings> userList = List.of(//
new UserAlertingSettings(1, "user0", null, this.yesterday, 60), //
new UserAlertingSettings(1, "user1", null, this.yesterday, 15), //
new UserAlertingSettings(1, "user2", null, this.yesterday, 10), //
new UserAlertingSettings(1, "user3", null, this.yesterday, 30), //
new UserAlertingSettings(1, "user4", null, this.yesterday, 30), //
new UserAlertingSettings(1, "user5", null, this.yesterday, 1440));

@Override
public boolean isInitialized() {
Expand All @@ -231,7 +231,7 @@ public Collection<Edge> getAllOfflineEdges() {
}

@Override
public List<AlertingSetting> getUserAlertingSettings(String edgeId) {
public List<UserAlertingSettings> getUserAlertingSettings(String edgeId) {
return this.userList;
}
}
Expand All @@ -256,11 +256,11 @@ public Collection<Edge> getAllOfflineEdges() {
}

@Override
public List<AlertingSetting> getUserAlertingSettings(String edgeId) {
public List<UserAlertingSettings> getUserAlertingSettings(String edgeId) {
return List.of(//
new AlertingSetting(0, "user0", null, this.yesterday, 60), //
new AlertingSetting(0, "user1", null, this.yesterday, 15), //
new AlertingSetting(0, "user2", null, this.yesterday, 0) //
new UserAlertingSettings(0, "user0", null, this.yesterday, 60), //
new UserAlertingSettings(0, "user1", null, this.yesterday, 15), //
new UserAlertingSettings(0, "user2", null, this.yesterday, 0) //
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import io.openems.backend.common.metadata.AlertingSetting;
import io.openems.backend.common.metadata.UserAlertingSettings;
import io.openems.common.exceptions.OpenemsError.OpenemsNamedException;
import io.openems.common.jsonrpc.base.JsonrpcRequest;
import io.openems.common.utils.JsonUtils;
Expand Down Expand Up @@ -47,7 +47,7 @@ public static SetUserAlertingConfigsRequest from(JsonrpcRequest request) throws
}

private final String edgeId;
private final List<AlertingSetting> userSettings = new ArrayList<>();
private final List<UserAlertingSettings> userSettings = new ArrayList<>();

private SetUserAlertingConfigsRequest(JsonrpcRequest request) throws OpenemsNamedException {
super(request, SetUserAlertingConfigsRequest.METHOD);
Expand All @@ -60,7 +60,7 @@ private SetUserAlertingConfigsRequest(JsonrpcRequest request) throws OpenemsName
var userId = JsonUtils.getAsString(userJsonObject, "userId");
var timeToWait = JsonUtils.getAsInt(userJsonObject, "delayTime");

this.userSettings.add(new AlertingSetting(userId, timeToWait));
this.userSettings.add(new UserAlertingSettings(userId, timeToWait));
} catch (OpenemsNamedException e) {
e.printStackTrace();
}
Expand All @@ -81,7 +81,7 @@ public String getEdgeId() {
*
* @return list of {@link UserAlertingSetting}
*/
public List<AlertingSetting> getUserSettings() {
public List<UserAlertingSettings> getUserSettings() {
return this.userSettings;
}

Expand All @@ -93,7 +93,7 @@ public JsonObject getParams() {
.build();
}

private JsonElement toJson(AlertingSetting setting) {
private JsonElement toJson(UserAlertingSettings setting) {
return JsonUtils.buildJsonObject() //
.addProperty("userId", setting.getUserId()) //
.addProperty("delayTime", setting.getDelayTime()) //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import io.openems.backend.common.metadata.AlertingSetting;
import io.openems.backend.common.metadata.UserAlertingSettings;
import io.openems.common.jsonrpc.base.JsonrpcResponseSuccess;
import io.openems.common.session.Role;
import io.openems.common.utils.JsonUtils;
Expand All @@ -32,9 +32,9 @@
*/
public class GetUserAlertingConfigsResponse extends JsonrpcResponseSuccess {

private final List<AlertingSetting> settings;
private final List<UserAlertingSettings> settings;

public GetUserAlertingConfigsResponse(UUID id, List<AlertingSetting> settings) {
public GetUserAlertingConfigsResponse(UUID id, List<UserAlertingSettings> settings) {
super(id);
this.settings = settings;
}
Expand All @@ -46,7 +46,7 @@ public JsonObject getResult() {
.build();
}

private JsonElement toJson(AlertingSetting setting) {
private JsonElement toJson(UserAlertingSettings setting) {
return JsonUtils.buildJsonObject() //
.addProperty("userId", setting.getUserId()) //
.add("role", setting.getUserRole().asJson()) //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,10 @@ public static String activeStateChannelsToString(
*
*
* @param edgeId the Edge ID
* @return List of {@link AlertingSetting}
* @return List of {@link UserAlertingSettings}
* @throws OpenemsException on error
*/
public List<AlertingSetting> getUserAlertingSettings(String edgeId) throws OpenemsException;
public List<UserAlertingSettings> getUserAlertingSettings(String edgeId) throws OpenemsException;

/**
* Gets the alerting settings for given edge id and userId.
Expand All @@ -308,7 +308,7 @@ public static String activeStateChannelsToString(
* @return List of {@link UserRoleDelayTime}
* @throws OpenemsException on error
*/
public AlertingSetting getUserAlertingSettings(String edgeId, String userId) throws OpenemsException;
public UserAlertingSettings getUserAlertingSettings(String edgeId, String userId) throws OpenemsException;

/**
* Sets the alerting settings for the given list of users.
Expand All @@ -318,7 +318,7 @@ public static String activeStateChannelsToString(
* @param users list of users to update
* @throws OpenemsException on error
*/
public void setUserAlertingSettings(User user, String edgeId, List<AlertingSetting> users) throws OpenemsException;
public void setUserAlertingSettings(User user, String edgeId, List<UserAlertingSettings> users) throws OpenemsException;

/**
* Returns an EventAdmin, used by Edge objects.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@

import io.openems.common.session.Role;

public class AlertingSetting {
public class UserAlertingSettings {
private final int id;
private final String userId;
private final Role userRole;
private final ZonedDateTime lastNotification;
private final int delayTime;

public AlertingSetting(String userId, int delayTime) {
public UserAlertingSettings(String userId, int delayTime) {
this(0, userId, null, null, delayTime);
}

public AlertingSetting(int id, String userId, Role userRole, ZonedDateTime lastNotification, int delayTime) {
public UserAlertingSettings(int id, String userId, Role userRole, ZonedDateTime lastNotification, int delayTime) {
this.id = id;
this.userId = userId;
this.userRole = userRole;
Expand Down Expand Up @@ -57,7 +57,7 @@ public boolean equals(Object obj) {
if (obj == null || this.getClass() != obj.getClass()) {
return false;
}
var other = (AlertingSetting) obj;
var other = (UserAlertingSettings) obj;
return this.id == other.id;
}
}
Loading

0 comments on commit 9cdb86c

Please sign in to comment.