diff -r ed84c8bdd87b -r 73b21e9f3958 src/org/sonews/util/Stats.java --- a/src/org/sonews/util/Stats.java Sun Aug 29 17:28:58 2010 +0200 +++ b/src/org/sonews/util/Stats.java Mon Aug 30 00:20:06 2010 +0200 @@ -29,178 +29,157 @@ * @author Christian Lins * @since sonews/0.5.0 */ -public final class Stats +public final class Stats { - - public static final byte CONNECTIONS = 1; - public static final byte POSTED_NEWS = 2; - public static final byte GATEWAYED_NEWS = 3; - public static final byte FEEDED_NEWS = 4; - public static final byte MLGW_RUNSTART = 5; - public static final byte MLGW_RUNEND = 6; - private static Stats instance = new Stats(); - - public static Stats getInstance() - { - return Stats.instance; - } - - private Stats() {} - - private volatile int connectedClients = 0; + public static final byte CONNECTIONS = 1; + public static final byte POSTED_NEWS = 2; + public static final byte GATEWAYED_NEWS = 3; + public static final byte FEEDED_NEWS = 4; + public static final byte MLGW_RUNSTART = 5; + public static final byte MLGW_RUNEND = 6; + private static Stats instance = new Stats(); - /** - * A generic method that writes event data to the storage backend. - * If event logging is disabled with sonews.eventlog=false this method - * simply does nothing. - * @param type - * @param groupname - */ - private void addEvent(byte type, String groupname) - { - try - { - if (Config.inst().get(Config.EVENTLOG, true)) - { + public static Stats getInstance() + { + return Stats.instance; + } - Channel group = Channel.getByName(groupname); - if (group != null) - { - StorageManager.current().addEvent( - System.currentTimeMillis(), type, group.getInternalID()); - } - } - else - { - Log.get().info("Group " + groupname + " does not exist."); - } - } - catch (StorageBackendException ex) - { - ex.printStackTrace(); - } - } - - public void clientConnect() - { - this.connectedClients++; - } - - public void clientDisconnect() - { - this.connectedClients--; - } - - public int connectedClients() - { - return this.connectedClients; - } - - public int getNumberOfGroups() - { - try - { - return StorageManager.current().countGroups(); - } - catch(StorageBackendException ex) - { - ex.printStackTrace(); - return -1; - } - } - - public int getNumberOfNews() - { - try - { - return StorageManager.current().countArticles(); - } - catch(StorageBackendException ex) - { - ex.printStackTrace(); - return -1; - } - } - - public int getYesterdaysEvents(final byte eventType, final int hour, - final Channel group) - { - // Determine the timestamp values for yesterday and the given hour - Calendar cal = Calendar.getInstance(); - int year = cal.get(Calendar.YEAR); - int month = cal.get(Calendar.MONTH); - int dayom = cal.get(Calendar.DAY_OF_MONTH) - 1; // Yesterday - - cal.set(year, month, dayom, hour, 0, 0); - long startTimestamp = cal.getTimeInMillis(); - - cal.set(year, month, dayom, hour + 1, 0, 0); - long endTimestamp = cal.getTimeInMillis(); - - try - { - return StorageManager.current() - .getEventsCount(eventType, startTimestamp, endTimestamp, group); - } - catch(StorageBackendException ex) - { - ex.printStackTrace(); - return -1; - } - } - - public void mailPosted(String groupname) - { - addEvent(POSTED_NEWS, groupname); - } - - public void mailGatewayed(String groupname) - { - addEvent(GATEWAYED_NEWS, groupname); - } - - public void mailFeeded(String groupname) - { - addEvent(FEEDED_NEWS, groupname); - } - - public void mlgwRunStart() - { - addEvent(MLGW_RUNSTART, "control"); - } - - public void mlgwRunEnd() - { - addEvent(MLGW_RUNEND, "control"); - } - - private double perHour(int key, long gid) - { - try - { - return StorageManager.current().getEventsPerHour(key, gid); - } - catch(StorageBackendException ex) - { - ex.printStackTrace(); - return -1; - } - } - - public double postedPerHour(long gid) - { - return perHour(POSTED_NEWS, gid); - } - - public double gatewayedPerHour(long gid) - { - return perHour(GATEWAYED_NEWS, gid); - } - - public double feededPerHour(long gid) - { - return perHour(FEEDED_NEWS, gid); - } - + private Stats() + { + } + private volatile int connectedClients = 0; + + /** + * A generic method that writes event data to the storage backend. + * If event logging is disabled with sonews.eventlog=false this method + * simply does nothing. + * @param type + * @param groupname + */ + private void addEvent(byte type, String groupname) + { + try { + if (Config.inst().get(Config.EVENTLOG, true)) { + + Channel group = Channel.getByName(groupname); + if (group != null) { + StorageManager.current().addEvent( + System.currentTimeMillis(), type, group.getInternalID()); + } + } else { + Log.get().info("Group " + groupname + " does not exist."); + } + } catch (StorageBackendException ex) { + ex.printStackTrace(); + } + } + + public void clientConnect() + { + this.connectedClients++; + } + + public void clientDisconnect() + { + this.connectedClients--; + } + + public int connectedClients() + { + return this.connectedClients; + } + + public int getNumberOfGroups() + { + try { + return StorageManager.current().countGroups(); + } catch (StorageBackendException ex) { + ex.printStackTrace(); + return -1; + } + } + + public int getNumberOfNews() + { + try { + return StorageManager.current().countArticles(); + } catch (StorageBackendException ex) { + ex.printStackTrace(); + return -1; + } + } + + public int getYesterdaysEvents(final byte eventType, final int hour, + final Channel group) + { + // Determine the timestamp values for yesterday and the given hour + Calendar cal = Calendar.getInstance(); + int year = cal.get(Calendar.YEAR); + int month = cal.get(Calendar.MONTH); + int dayom = cal.get(Calendar.DAY_OF_MONTH) - 1; // Yesterday + + cal.set(year, month, dayom, hour, 0, 0); + long startTimestamp = cal.getTimeInMillis(); + + cal.set(year, month, dayom, hour + 1, 0, 0); + long endTimestamp = cal.getTimeInMillis(); + + try { + return StorageManager.current().getEventsCount(eventType, startTimestamp, endTimestamp, group); + } catch (StorageBackendException ex) { + ex.printStackTrace(); + return -1; + } + } + + public void mailPosted(String groupname) + { + addEvent(POSTED_NEWS, groupname); + } + + public void mailGatewayed(String groupname) + { + addEvent(GATEWAYED_NEWS, groupname); + } + + public void mailFeeded(String groupname) + { + addEvent(FEEDED_NEWS, groupname); + } + + public void mlgwRunStart() + { + addEvent(MLGW_RUNSTART, "control"); + } + + public void mlgwRunEnd() + { + addEvent(MLGW_RUNEND, "control"); + } + + private double perHour(int key, long gid) + { + try { + return StorageManager.current().getEventsPerHour(key, gid); + } catch (StorageBackendException ex) { + ex.printStackTrace(); + return -1; + } + } + + public double postedPerHour(long gid) + { + return perHour(POSTED_NEWS, gid); + } + + public double gatewayedPerHour(long gid) + { + return perHour(GATEWAYED_NEWS, gid); + } + + public double feededPerHour(long gid) + { + return perHour(FEEDED_NEWS, gid); + } }