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.sql.SQLException; chris@1: import java.util.Calendar; chris@1: import org.sonews.daemon.storage.Database; chris@1: import org.sonews.daemon.storage.Group; 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@1: chris@1: private void addEvent(byte type, String groupname) chris@1: { chris@1: Group group = Group.getByName(groupname); chris@1: if(group != null) chris@1: { chris@1: try chris@1: { chris@1: Database.getInstance().addEvent( chris@1: System.currentTimeMillis(), type, group.getID()); chris@1: } chris@1: catch(SQLException ex) chris@1: { chris@1: ex.printStackTrace(); chris@1: } chris@1: } chris@1: else chris@1: { chris@1: Log.msg("Group " + groupname + " does not exist.", true); 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@1: return Database.getInstance().countGroups(); chris@1: } chris@1: catch(SQLException 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@1: return Database.getInstance().countArticles(); chris@1: } chris@1: catch(SQLException 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@1: final Group 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@1: return Database.getInstance() chris@1: .getEventsCount(eventType, startTimestamp, endTimestamp, group); chris@1: } chris@1: catch(SQLException 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@1: return Database.getInstance().getNumberOfEventsPerHour(key, gid); chris@1: } chris@1: catch(SQLException 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: }