Browse Source

add statistics property test case and new statistics items

Jason Rivard 7 years ago
parent
commit
b47bcea274

+ 4 - 0
server/src/main/java/password/pwm/i18n/Admin.java

@@ -66,6 +66,10 @@ public enum Admin implements PwmDisplayBundle {
 
     ;
 
+    public static final String STATISTICS_LABEL_PREFIX = "Statistic_Label.";
+    public static final String STATISTICS_DESCRIPTION_PREFIX =  "Statistic_Description.";
+    public static final String EPS_STATISTICS_LABEL_PREFIX = "EpsStatistic_Label.";
+
     @Override
     public String getKey() {
         return this.toString();

+ 5 - 0
server/src/main/java/password/pwm/svc/event/AuditService.java

@@ -44,6 +44,8 @@ import password.pwm.health.HealthStatus;
 import password.pwm.health.HealthTopic;
 import password.pwm.http.PwmSession;
 import password.pwm.svc.PwmService;
+import password.pwm.svc.stats.Statistic;
+import password.pwm.svc.stats.StatisticsManager;
 import password.pwm.util.LocaleHelper;
 import password.pwm.util.java.JavaHelper;
 import password.pwm.util.java.JsonUtil;
@@ -345,6 +347,9 @@ public class AuditService implements PwmService {
                 lastError = e.getErrorInformation();
             }
         }
+
+        // update statistics
+        StatisticsManager.incrementStat(pwmApplication, Statistic.AUDIT_EVENTS);
     }
 
 

+ 7 - 2
server/src/main/java/password/pwm/svc/event/SyslogAuditService.java

@@ -45,14 +45,16 @@ import password.pwm.error.PwmOperationalException;
 import password.pwm.health.HealthRecord;
 import password.pwm.health.HealthStatus;
 import password.pwm.health.HealthTopic;
+import password.pwm.svc.stats.Statistic;
+import password.pwm.svc.stats.StatisticsManager;
 import password.pwm.util.java.JsonUtil;
 import password.pwm.util.java.TimeDuration;
-import password.pwm.util.localdb.WorkQueueProcessor;
-import password.pwm.util.secure.X509Utils;
 import password.pwm.util.localdb.LocalDB;
 import password.pwm.util.localdb.LocalDBException;
 import password.pwm.util.localdb.LocalDBStoredQueue;
+import password.pwm.util.localdb.WorkQueueProcessor;
 import password.pwm.util.logging.PwmLogger;
+import password.pwm.util.secure.X509Utils;
 
 import javax.net.SocketFactory;
 import javax.net.ssl.SSLContext;
@@ -82,11 +84,13 @@ public class SyslogAuditService {
 
 
     private final Configuration configuration;
+    private final PwmApplication pwmApplication;
     private List<SyslogIF> syslogInstances = new ArrayList<>();
 
     public SyslogAuditService(final PwmApplication pwmApplication)
             throws LocalDBException
     {
+        this.pwmApplication = pwmApplication;
         this.configuration = pwmApplication.getConfig();
         this.certificates = configuration.readSettingAsCertificate(PwmSetting.AUDIT_SYSLOG_CERTIFICATES);
 
@@ -197,6 +201,7 @@ public class SyslogAuditService {
                 syslogInstance.info(auditRecord);
                 LOGGER.trace("delivered syslog audit event: " + auditRecord);
                 lastError = null;
+                StatisticsManager.incrementStat(this.pwmApplication, Statistic.SYSLOG_MESSAGES_SENT);
                 return WorkQueueProcessor.ProcessResult.SUCCESS;
             } catch (Exception e) {
                 final String errorMsg = "error while sending syslog message to remote service: " + e.getMessage();

+ 1 - 1
server/src/main/java/password/pwm/svc/stats/EpsStatistic.java

@@ -28,7 +28,7 @@ public enum EpsStatistic {
     }
 
     public String getLabel(final Locale locale) {
-        final String keyName = "Statistic_Label." + EpsStatistic.class.getSimpleName() + "_" + this.name();
+        final String keyName = Admin.EPS_STATISTICS_LABEL_PREFIX + this.name();
         return LocaleHelper.getLocalizedMessage(locale, keyName, null, Admin.class);
     }
 }

+ 5 - 7
server/src/main/java/password/pwm/svc/stats/Statistic.java

@@ -37,6 +37,7 @@ import java.util.SortedSet;
 import java.util.TreeSet;
 
 public enum Statistic {
+    AUDIT_EVENTS                        (Type.INCREMENTOR, "AuditEvents", null),
     AUTHENTICATIONS                     (Type.INCREMENTOR, "Authentications", null),
     AUTHENTICATION_FAILURES             (Type.INCREMENTOR, "AuthenticationFailures", null),
     AUTHENTICATION_EXPIRED              (Type.INCREMENTOR, "Authentications_Expired", null),
@@ -106,6 +107,7 @@ public enum Statistic {
     INTRUDER_ATTEMPTS                   (Type.INCREMENTOR, "IntruderAttempts", null),
     FOREIGN_SESSIONS_ACCEPTED           (Type.INCREMENTOR, "ForeignSessionsAccepted", null),
     OBSOLETE_URL_REQUESTS               (Type.INCREMENTOR, "ObsoleteUrlRequests", null),
+    SYSLOG_MESSAGES_SENT                (Type.INCREMENTOR, "SyslogMessagesSent", null),
 
     AVG_PASSWORD_SYNC_TIME              (Type.AVERAGE, "AvgPasswordSyncTime", null),
     AVG_AUTHENTICATION_TIME             (Type.AVERAGE, "AvgAuthenticationTime", null),
@@ -145,11 +147,7 @@ public enum Statistic {
     }
 
     public static SortedSet<Statistic> sortedValues(final Locale locale) {
-        final Comparator<Statistic> comparator = new Comparator<Statistic>() {
-            public int compare(final Statistic o1, final Statistic o2) {
-                return o1.getLabel(locale).compareTo(o2.getLabel(locale));
-            }
-        };
+        final Comparator<Statistic> comparator = Comparator.comparing(o -> o.getLabel(locale));
         final TreeSet<Statistic> set = new TreeSet<>(comparator);
         set.addAll(Arrays.asList(values()));
         return set;
@@ -162,7 +160,7 @@ public enum Statistic {
 
     public String getLabel(final Locale locale) {
         try {
-            final String keyName = "Statistic_Label." + this.getKey();
+            final String keyName = Admin.STATISTICS_LABEL_PREFIX + this.getKey();
             return LocaleHelper.getLocalizedMessage(locale, keyName, null, Admin.class);
         } catch (MissingResourceException e) {
             return "MISSING STATISTIC LABEL for " + this.getKey();
@@ -170,7 +168,7 @@ public enum Statistic {
     }
 
     public String getDescription(final Locale locale) {
-        final String keyName = "Statistic_Description." + this.getKey();
+        final String keyName = Admin.STATISTICS_DESCRIPTION_PREFIX + this.getKey();
         try {
             return LocaleHelper.getLocalizedMessage(locale, keyName, null, Admin.class);
         } catch (Exception e) {

+ 13 - 9
server/src/main/resources/password/pwm/i18n/Admin.properties

@@ -126,6 +126,8 @@ Confirm_Report_Clear=Do you wish clear the cached report data?  <br/><br/>This w
 Display_Report_Action_Start=Sending request to start report engine.
 Display_Report_Action_Stop=Sending request to stop the report engine.
 Display_Report_Action_Clear=Sending request to clear the cached report data.
+Statistic_Label.AuditEvents=Audit Events
+Statistic_Description.AuditEvents=Number of audit events generated and stored locally.
 Statistic_Label.Authentications=Authentications
 Statistic_Description.Authentications=Number of successful user authentications that have occurred.
 Statistic_Label.AuthenticationFailures=Authentication Failures
@@ -272,15 +274,17 @@ Statistic_Label.ForeignSessionsAccepted=Foreign Sessions Accepted
 Statistic_Description.ForeignSessionsAccepted=Number of sessions generated on a foreign or server or previous instance of this server and accepted as valid authentication.
 Statistic_Label.ObsoleteUrlRequests=Obsolete URL Requests
 Statistic_Description.ObsoleteUrlRequests=Number of web requests to obsolete URLs.
-Statistic_Label.EpsStatistic_REQUESTS=Requests
-Statistic_Label.EpsStatistic_SESSIONS=Sessions
-Statistic_Label.EpsStatistic_PASSWORD_CHANGES=Password Changes
-Statistic_Label.EpsStatistic_AUTHENTICATION=Authentications
-Statistic_Label.EpsStatistic_INTRUDER_ATTEMPTS=Intruder Attempts
-Statistic_Label.EpsStatistic_PWMDB_READS=LocalDB Reads
-Statistic_Label.EpsStatistic_PWMDB_WRITES=LocalDB Writes
-Statistic_Label.EpsStatistic_DB_READS=Database Reads
-Statistic_Label.EpsStatistic_DB_WRITES=Database Writes
+Statistic_Label.SyslogMessagesSent=Syslog Messages Sent
+Statistic_Description.SyslogMessagesSent=Number of successfully sent syslog messages.
+EpsStatistic_Label.REQUESTS=Requests
+EpsStatistic_Label.SESSIONS=Sessions
+EpsStatistic_Label.PASSWORD_CHANGES=Password Changes
+EpsStatistic_Label.AUTHENTICATION=Authentications
+EpsStatistic_Label.INTRUDER_ATTEMPTS=Intruder Attempts
+EpsStatistic_Label.PWMDB_READS=LocalDB Reads
+EpsStatistic_Label.PWMDB_WRITES=LocalDB Writes
+EpsStatistic_Label.DB_READS=Database Reads
+EpsStatistic_Label.DB_WRITES=Database Writes
 Title_About=About
 Title_DirectoryReporting=Directory Reporting
 Title_DataViewer=Data Viewer

+ 70 - 0
server/src/test/java/password/pwm/i18n/AdminPropertyKeysTest.java

@@ -0,0 +1,70 @@
+package password.pwm.i18n;
+
+import org.junit.Assert;
+import org.junit.Test;
+import password.pwm.PwmConstants;
+import password.pwm.svc.stats.EpsStatistic;
+import password.pwm.svc.stats.Statistic;
+
+import java.util.HashSet;
+import java.util.ResourceBundle;
+import java.util.Set;
+
+public class AdminPropertyKeysTest {
+
+    @Test
+    public void testStatisticsLabelKeys() {
+        final ResourceBundle resourceBundle = ResourceBundle.getBundle(password.pwm.i18n.Admin.class.getName(), PwmConstants.DEFAULT_LOCALE);
+
+        final Set<String> expectedKeys = new HashSet<>();
+
+        for (final Statistic statistic : Statistic.values()) {
+            final String[] keys = new String[]{
+                    password.pwm.i18n.Admin.STATISTICS_DESCRIPTION_PREFIX + statistic.getKey(),
+                    password.pwm.i18n.Admin.STATISTICS_LABEL_PREFIX + statistic.getKey(),
+            };
+            for (final String key : keys) {
+                expectedKeys.add(key);
+                Assert.assertTrue(
+                        "Admin.properties missing record for " + key,
+                        resourceBundle.containsKey(key));
+            }
+        }
+
+        final Set<String> extraKeys = new HashSet<>(resourceBundle.keySet());
+        extraKeys.removeAll(expectedKeys);
+
+        for (final String key : extraKeys) {
+            if (key.startsWith( password.pwm.i18n.Admin.STATISTICS_DESCRIPTION_PREFIX)
+                    || key.startsWith( password.pwm.i18n.Admin.STATISTICS_LABEL_PREFIX)) {
+
+                Assert.fail("unexpected key in Admin.properties file: " + key);
+            }
+        }
+    }
+
+
+    @Test
+    public void testDpsStatisticsLabelKeys() {
+        final ResourceBundle resourceBundle = ResourceBundle.getBundle(password.pwm.i18n.Admin.class.getName(), PwmConstants.DEFAULT_LOCALE);
+
+        final Set<String> expectedKeys = new HashSet<>();
+
+        for (final EpsStatistic statistic : EpsStatistic.values()) {
+            final String key = Admin.EPS_STATISTICS_LABEL_PREFIX + statistic.name();
+            expectedKeys.add(key);
+            Assert.assertTrue(
+                    "Admin.properties missing record for " + key,
+                    resourceBundle.containsKey(key));
+        }
+
+        final Set<String> extraKeys = new HashSet<>(resourceBundle.keySet());
+        extraKeys.removeAll(expectedKeys);
+
+        for (final String key : extraKeys) {
+            if (key.startsWith( password.pwm.i18n.Admin.EPS_STATISTICS_LABEL_PREFIX)) {
+                Assert.fail("unexpected key in Admin.properties file: " + key);
+            }
+        }
+    }
+}