chris@1: /* chris@1: * SONEWS News Server chris@1: * see AUTHORS for the list of contributors chris@1: * chris@1: * This program is free software: you can redistribute it and/or modify chris@1: * it under the terms of the GNU General Public License as published by chris@1: * the Free Software Foundation, either version 3 of the License, or chris@1: * (at your option) any later version. chris@1: * chris@1: * This program is distributed in the hope that it will be useful, chris@1: * but WITHOUT ANY WARRANTY; without even the implied warranty of chris@1: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the chris@1: * GNU General Public License for more details. chris@1: * chris@1: * You should have received a copy of the GNU General Public License chris@1: * along with this program. If not, see . chris@1: */ chris@1: chris@1: package org.sonews.util; chris@1: chris@1: import java.util.Calendar; chris@3: import org.sonews.config.Config; chris@3: import org.sonews.storage.Channel; chris@3: import org.sonews.storage.StorageBackendException; chris@3: import org.sonews.storage.StorageManager; chris@1: chris@1: /** chris@1: * Class that capsulates statistical data gathering. chris@1: * @author Christian Lins chris@1: * @since sonews/0.5.0 chris@1: */ cli@37: public final class Stats chris@1: { chris@1: cli@37: public static final byte CONNECTIONS = 1; cli@37: public static final byte POSTED_NEWS = 2; cli@37: public static final byte GATEWAYED_NEWS = 3; cli@37: public static final byte FEEDED_NEWS = 4; cli@37: public static final byte MLGW_RUNSTART = 5; cli@37: public static final byte MLGW_RUNEND = 6; cli@37: private static Stats instance = new Stats(); chris@3: cli@37: public static Stats getInstance() cli@37: { cli@37: return Stats.instance; cli@37: } cli@31: cli@37: private Stats() cli@37: { cli@37: } cli@37: private volatile int connectedClients = 0; cli@37: cli@37: /** cli@37: * A generic method that writes event data to the storage backend. cli@37: * If event logging is disabled with sonews.eventlog=false this method cli@37: * simply does nothing. cli@37: * @param type cli@37: * @param groupname cli@37: */ cli@37: private void addEvent(byte type, String groupname) cli@37: { cli@37: try { cli@37: if (Config.inst().get(Config.EVENTLOG, true)) { cli@37: cli@37: Channel group = Channel.getByName(groupname); cli@37: if (group != null) { cli@37: StorageManager.current().addEvent( cli@37: System.currentTimeMillis(), type, group.getInternalID()); cli@37: } cli@37: } else { cli@37: Log.get().info("Group " + groupname + " does not exist."); cli@37: } cli@37: } catch (StorageBackendException ex) { cli@37: ex.printStackTrace(); cli@37: } cli@37: } cli@37: cli@37: public void clientConnect() cli@37: { cli@37: this.connectedClients++; cli@37: } cli@37: cli@37: public void clientDisconnect() cli@37: { cli@37: this.connectedClients--; cli@37: } cli@37: cli@37: public int connectedClients() cli@37: { cli@37: return this.connectedClients; cli@37: } cli@37: cli@37: public int getNumberOfGroups() cli@37: { cli@37: try { cli@37: return StorageManager.current().countGroups(); cli@37: } catch (StorageBackendException ex) { cli@37: ex.printStackTrace(); cli@37: return -1; cli@37: } cli@37: } cli@37: cli@37: public int getNumberOfNews() cli@37: { cli@37: try { cli@37: return StorageManager.current().countArticles(); cli@37: } catch (StorageBackendException ex) { cli@37: ex.printStackTrace(); cli@37: return -1; cli@37: } cli@37: } cli@37: cli@37: public int getYesterdaysEvents(final byte eventType, final int hour, cli@37: final Channel group) cli@37: { cli@37: // Determine the timestamp values for yesterday and the given hour cli@37: Calendar cal = Calendar.getInstance(); cli@37: int year = cal.get(Calendar.YEAR); cli@37: int month = cal.get(Calendar.MONTH); cli@37: int dayom = cal.get(Calendar.DAY_OF_MONTH) - 1; // Yesterday cli@37: cli@37: cal.set(year, month, dayom, hour, 0, 0); cli@37: long startTimestamp = cal.getTimeInMillis(); cli@37: cli@37: cal.set(year, month, dayom, hour + 1, 0, 0); cli@37: long endTimestamp = cal.getTimeInMillis(); cli@37: cli@37: try { cli@37: return StorageManager.current().getEventsCount(eventType, startTimestamp, endTimestamp, group); cli@37: } catch (StorageBackendException ex) { cli@37: ex.printStackTrace(); cli@37: return -1; cli@37: } cli@37: } cli@37: cli@37: public void mailPosted(String groupname) cli@37: { cli@37: addEvent(POSTED_NEWS, groupname); cli@37: } cli@37: cli@37: public void mailGatewayed(String groupname) cli@37: { cli@37: addEvent(GATEWAYED_NEWS, groupname); cli@37: } cli@37: cli@37: public void mailFeeded(String groupname) cli@37: { cli@37: addEvent(FEEDED_NEWS, groupname); cli@37: } cli@37: cli@37: public void mlgwRunStart() cli@37: { cli@37: addEvent(MLGW_RUNSTART, "control"); cli@37: } cli@37: cli@37: public void mlgwRunEnd() cli@37: { cli@37: addEvent(MLGW_RUNEND, "control"); cli@37: } cli@37: cli@37: private double perHour(int key, long gid) cli@37: { cli@37: try { cli@37: return StorageManager.current().getEventsPerHour(key, gid); cli@37: } catch (StorageBackendException ex) { cli@37: ex.printStackTrace(); cli@37: return -1; cli@37: } cli@37: } cli@37: cli@37: public double postedPerHour(long gid) cli@37: { cli@37: return perHour(POSTED_NEWS, gid); cli@37: } cli@37: cli@37: public double gatewayedPerHour(long gid) cli@37: { cli@37: return perHour(GATEWAYED_NEWS, gid); cli@37: } cli@37: cli@37: public double feededPerHour(long gid) cli@37: { cli@37: return perHour(FEEDED_NEWS, gid); cli@37: } chris@1: }