-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminAlertsNotificationRevamp.java
More file actions
152 lines (127 loc) · 6.66 KB
/
AdminAlertsNotificationRevamp.java
File metadata and controls
152 lines (127 loc) · 6.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package org.labkey.wnprc_ehr.notification;
import org.labkey.api.data.CompareType;
import org.labkey.api.data.Container;
import org.labkey.api.data.SimpleFilter;
import org.labkey.api.data.Sort;
import org.labkey.api.module.Module;
import org.labkey.api.security.User;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
public class AdminAlertsNotificationRevamp extends AbstractEHRNotification {
//Class Variables
NotificationToolkit notificationToolkit = new NotificationToolkit();
NotificationToolkit.StyleToolkit styleToolkit = new NotificationToolkit.StyleToolkit();
NotificationToolkit.DateToolkit dateToolkit = new NotificationToolkit.DateToolkit();
//Constructors
/**
* This constructor is used to register the notification in WNPRC_EHRModule.java.
* @param owner
*/
public AdminAlertsNotificationRevamp(Module owner) {super(owner);}
//Notification Details
@Override
public String getName() {
return "Admin Alerts Revamp";
}
@Override
public String getDescription() {
return "This email contains a series of automatic alerts about the WNPRC colony. It was run on: " + dateToolkit.getCurrentTime();
}
@Override
public String getEmailSubject(Container c) {
return "Daily Admin Alerts: " + dateToolkit.getCurrentTime();
}
@Override
public String getScheduleDescription() {
return "Daily at 11AM";
}
@Override
public String getCronString() {
return notificationToolkit.createCronString("0", "11", "*");
}
@Override
public String getCategory() {
return "Revamped Notifications";
}
//Message Creation
@Override
public String getMessageBodyHTML(Container c, User u) {
// Gets AdminAlertsNotificationRevampObject.
AdminAlertsNotificationObject myAdminAlertsNotificationObject = new AdminAlertsNotificationObject(c, u);
Boolean messageIsEmpty = true;
// Begins message body.
StringBuilder messageBody = new StringBuilder();
// Creates CSS.
messageBody.append(styleToolkit.beginStyle());
messageBody.append(styleToolkit.setBasicTableStyle());
messageBody.append(styleToolkit.setHeaderRowBackgroundColor("#d9d9d9"));
messageBody.append(styleToolkit.endStyle());
// Begins message info.
messageBody.append("<p>This email contains a series of automatic alerts about the WNPRC colony. It was run on: " + dateToolkit.getCurrentTime() + ".</p>");
// Summarizes site logins in the past 7 days.
messageBody.append("<p>Site Logins In The Past 7 Days:</p>");
if (!myAdminAlertsNotificationObject.siteUsageLastSevenDays.isEmpty()) {
// Creates table.
String[] myTableColumns = new String[]{"Day of Week", "Date", "Logins"};
NotificationToolkit.NotificationRevampTable myTable = new NotificationToolkit.NotificationRevampTable(myTableColumns, myAdminAlertsNotificationObject.siteUsageLastSevenDays);
messageBody.append(myTable.createBasicHTMLTable());
}
// Lists number of client errors since yesterday.
messageBody.append("<br>\n<p><b>WARNING: There were " + myAdminAlertsNotificationObject.numClientErrorsSinceYesterday + " client errors since yesterday (" + dateToolkit.getDateXDaysFromNow(-1) + "):</b></p>");
messageBody.append(notificationToolkit.createHyperlink("<p>Click here to view them</p>", myAdminAlertsNotificationObject.numClientErrorsSinceYesterdayURLView));
return messageBody.toString();
}
public static class AdminAlertsNotificationObject {
// Set up.
NotificationToolkit notificationToolkit = new NotificationToolkit();
NotificationToolkit.DateToolkit dateToolkit = new NotificationToolkit.DateToolkit();
Container currentContainer;
User currentUser;
// Data to retrieve.
ArrayList<String[]> siteUsageLastSevenDays = new ArrayList<>();
Long numClientErrorsSinceYesterday = Long.valueOf(0);
String numClientErrorsSinceYesterdayURLView = "";
// Creates constructor.
AdminAlertsNotificationObject(Container c, User u) {
// Sets variables.
this.currentContainer = c;
this.currentUser = u;
// Gets data.
getSiteUsageSummaryLastSevenDays();
getNumClientErrorsSinceYesterday();
}
// 1. Summarizes site usage in the past 7 days (gets list of logins).
void getSiteUsageSummaryLastSevenDays() {
// Creates filter.
Date dateSevenDaysAgo = dateToolkit.getDateXDaysFromNow(-50);
SimpleFilter myFilter = new SimpleFilter("date", dateSevenDaysAgo, CompareType.DATE_GTE);
// Creates sort.
Sort mySort = new Sort("+date");
// Creates columns to retrieve.
String[] targetColumns = new String[]{"dayOfWeek", "date", "Logins"};
// Runs query.
ArrayList<HashMap<String, String>> returnArray = notificationToolkit.getTableMultiRowMultiColumnWithFieldKeys(currentContainer, currentUser, "core", "SiteUsageByDay", myFilter, mySort, targetColumns);
// Formats return data into ArrayList<String[]> to pass in as table data.
for (HashMap<String, String> currentSiteUsage : returnArray) {
String currentDayOfWeek = currentSiteUsage.get("dayOfWeek");
String currentDate = currentSiteUsage.get("date");
String currentNumLogins = currentSiteUsage.get("Logins");
String[] myTableDataRow = new String[]{currentDayOfWeek, currentDate, currentNumLogins};
this.siteUsageLastSevenDays.add(myTableDataRow);
}
}
// 2. Gets number of client errors since yesterday.
void getNumClientErrorsSinceYesterday() {
// Creates filter.
Date dateYesterday = dateToolkit.getDateXDaysFromNow(-1);
SimpleFilter myFilter = new SimpleFilter("date", dateYesterday, CompareType.DATE_GTE);
myFilter.addCondition("SubType", "LabKey Server Backup", CompareType.NEQ_OR_NULL);
// Runs query.
ArrayList<String> returnArray = notificationToolkit.getTableMultiRowSingleColumn(currentContainer, currentUser, "auditlog", "Client API Actions", myFilter, null, "RowId", null);
this.numClientErrorsSinceYesterday = Long.valueOf(returnArray.size());
// Creates a URL to view number of client errors since yesterday.
this.numClientErrorsSinceYesterdayURLView = notificationToolkit.createQueryURL(currentContainer, "execute", "auditlog", "Client API Actions", myFilter);
}
}
}