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: */ chris@1: public final class Stats chris@1: { chris@1: chris@1: public static final byte CONNECTIONS = 1; chris@1: public static final byte POSTED_NEWS = 2; chris@1: public static final byte GATEWAYED_NEWS = 3; chris@1: public static final byte FEEDED_NEWS = 4; chris@1: public static final byte MLGW_RUNSTART = 5; chris@1: public static final byte MLGW_RUNEND = 6; chris@1: chris@1: private static Stats instance = new Stats(); chris@1: chris@1: public static Stats getInstance() chris@1: { chris@1: return Stats.instance; chris@1: } chris@1: chris@1: private Stats() {} chris@1: chris@1: private volatile int connectedClients = 0; chris@3: chris@3: /** chris@3: * A generic method that writes event data to the storage backend. chris@3: * If event logging is disabled with sonews.eventlog=false this method chris@3: * simply does nothing. chris@3: * @param type chris@3: * @param groupname chris@3: */ chris@1: private void addEvent(byte type, String groupname) chris@1: { chris@3: if(Config.inst().get(Config.EVENTLOG, true)) chris@1: { chris@3: Channel group = Channel.getByName(groupname); chris@3: if(group != null) chris@1: { chris@3: try chris@3: { chris@3: StorageManager.current().addEvent( chris@3: System.currentTimeMillis(), type, group.getInternalID()); chris@3: } chris@3: catch(StorageBackendException ex) chris@3: { chris@3: ex.printStackTrace(); chris@3: } chris@1: } chris@3: else chris@1: { cli@16: Log.get().info("Group " + groupname + " does not exist."); chris@1: } chris@1: } chris@1: } chris@1: chris@1: public void clientConnect() chris@1: { chris@1: this.connectedClients++; chris@1: } chris@1: chris@1: public void clientDisconnect() chris@1: { chris@1: this.connectedClients--; chris@1: } chris@1: chris@1: public int connectedClients() chris@1: { chris@1: return this.connectedClients; chris@1: } chris@1: chris@1: public int getNumberOfGroups() chris@1: { chris@1: try chris@1: { chris@3: return StorageManager.current().countGroups(); chris@1: } chris@3: catch(StorageBackendException ex) chris@1: { chris@1: ex.printStackTrace(); chris@1: return -1; chris@1: } chris@1: } chris@1: chris@1: public int getNumberOfNews() chris@1: { chris@1: try chris@1: { chris@3: return StorageManager.current().countArticles(); chris@1: } chris@3: catch(StorageBackendException ex) chris@1: { chris@1: ex.printStackTrace(); chris@1: return -1; chris@1: } chris@1: } chris@1: chris@1: public int getYesterdaysEvents(final byte eventType, final int hour, chris@3: final Channel group) chris@1: { chris@1: // Determine the timestamp values for yesterday and the given hour chris@1: Calendar cal = Calendar.getInstance(); chris@1: int year = cal.get(Calendar.YEAR); chris@1: int month = cal.get(Calendar.MONTH); chris@1: int dayom = cal.get(Calendar.DAY_OF_MONTH) - 1; // Yesterday chris@1: chris@1: cal.set(year, month, dayom, hour, 0, 0); chris@1: long startTimestamp = cal.getTimeInMillis(); chris@1: chris@1: cal.set(year, month, dayom, hour + 1, 0, 0); chris@1: long endTimestamp = cal.getTimeInMillis(); chris@1: chris@1: try chris@1: { chris@3: return StorageManager.current() chris@1: .getEventsCount(eventType, startTimestamp, endTimestamp, group); chris@1: } chris@3: catch(StorageBackendException ex) chris@1: { chris@1: ex.printStackTrace(); chris@1: return -1; chris@1: } chris@1: } chris@1: chris@1: public void mailPosted(String groupname) chris@1: { chris@1: addEvent(POSTED_NEWS, groupname); chris@1: } chris@1: chris@1: public void mailGatewayed(String groupname) chris@1: { chris@1: addEvent(GATEWAYED_NEWS, groupname); chris@1: } chris@1: chris@1: public void mailFeeded(String groupname) chris@1: { chris@1: addEvent(FEEDED_NEWS, groupname); chris@1: } chris@1: chris@1: public void mlgwRunStart() chris@1: { chris@1: addEvent(MLGW_RUNSTART, "control"); chris@1: } chris@1: chris@1: public void mlgwRunEnd() chris@1: { chris@1: addEvent(MLGW_RUNEND, "control"); chris@1: } chris@1: chris@1: private double perHour(int key, long gid) chris@1: { chris@1: try chris@1: { chris@3: return StorageManager.current().getEventsPerHour(key, gid); chris@1: } chris@3: catch(StorageBackendException ex) chris@1: { chris@1: ex.printStackTrace(); chris@1: return -1; chris@1: } chris@1: } chris@1: chris@1: public double postedPerHour(long gid) chris@1: { chris@1: return perHour(POSTED_NEWS, gid); chris@1: } chris@1: chris@1: public double gatewayedPerHour(long gid) chris@1: { chris@1: return perHour(GATEWAYED_NEWS, gid); chris@1: } chris@1: chris@1: public double feededPerHour(long gid) chris@1: { chris@1: return perHour(FEEDED_NEWS, gid); chris@1: } chris@1: chris@1: }