1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/org/sonews/util/Stats.java Wed Jul 01 10:48:22 2009 +0200
1.3 @@ -0,0 +1,194 @@
1.4 +/*
1.5 + * SONEWS News Server
1.6 + * see AUTHORS for the list of contributors
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, either version 3 of the License, or
1.11 + * (at your option) any later version.
1.12 + *
1.13 + * This program is distributed in the hope that it will be useful,
1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.16 + * GNU General Public License for more details.
1.17 + *
1.18 + * You should have received a copy of the GNU General Public License
1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.20 + */
1.21 +
1.22 +package org.sonews.util;
1.23 +
1.24 +import java.sql.SQLException;
1.25 +import java.util.Calendar;
1.26 +import org.sonews.daemon.storage.Database;
1.27 +import org.sonews.daemon.storage.Group;
1.28 +
1.29 +/**
1.30 + * Class that capsulates statistical data gathering.
1.31 + * @author Christian Lins
1.32 + * @since sonews/0.5.0
1.33 + */
1.34 +public final class Stats
1.35 +{
1.36 +
1.37 + public static final byte CONNECTIONS = 1;
1.38 + public static final byte POSTED_NEWS = 2;
1.39 + public static final byte GATEWAYED_NEWS = 3;
1.40 + public static final byte FEEDED_NEWS = 4;
1.41 + public static final byte MLGW_RUNSTART = 5;
1.42 + public static final byte MLGW_RUNEND = 6;
1.43 +
1.44 + private static Stats instance = new Stats();
1.45 +
1.46 + public static Stats getInstance()
1.47 + {
1.48 + return Stats.instance;
1.49 + }
1.50 +
1.51 + private Stats() {}
1.52 +
1.53 + private volatile int connectedClients = 0;
1.54 +
1.55 + private void addEvent(byte type, String groupname)
1.56 + {
1.57 + Group group = Group.getByName(groupname);
1.58 + if(group != null)
1.59 + {
1.60 + try
1.61 + {
1.62 + Database.getInstance().addEvent(
1.63 + System.currentTimeMillis(), type, group.getID());
1.64 + }
1.65 + catch(SQLException ex)
1.66 + {
1.67 + ex.printStackTrace();
1.68 + }
1.69 + }
1.70 + else
1.71 + {
1.72 + Log.msg("Group " + groupname + " does not exist.", true);
1.73 + }
1.74 + }
1.75 +
1.76 + public void clientConnect()
1.77 + {
1.78 + this.connectedClients++;
1.79 + }
1.80 +
1.81 + public void clientDisconnect()
1.82 + {
1.83 + this.connectedClients--;
1.84 + }
1.85 +
1.86 + public int connectedClients()
1.87 + {
1.88 + return this.connectedClients;
1.89 + }
1.90 +
1.91 + public int getNumberOfGroups()
1.92 + {
1.93 + try
1.94 + {
1.95 + return Database.getInstance().countGroups();
1.96 + }
1.97 + catch(SQLException ex)
1.98 + {
1.99 + ex.printStackTrace();
1.100 + return -1;
1.101 + }
1.102 + }
1.103 +
1.104 + public int getNumberOfNews()
1.105 + {
1.106 + try
1.107 + {
1.108 + return Database.getInstance().countArticles();
1.109 + }
1.110 + catch(SQLException ex)
1.111 + {
1.112 + ex.printStackTrace();
1.113 + return -1;
1.114 + }
1.115 + }
1.116 +
1.117 + public int getYesterdaysEvents(final byte eventType, final int hour,
1.118 + final Group group)
1.119 + {
1.120 + // Determine the timestamp values for yesterday and the given hour
1.121 + Calendar cal = Calendar.getInstance();
1.122 + int year = cal.get(Calendar.YEAR);
1.123 + int month = cal.get(Calendar.MONTH);
1.124 + int dayom = cal.get(Calendar.DAY_OF_MONTH) - 1; // Yesterday
1.125 +
1.126 + cal.set(year, month, dayom, hour, 0, 0);
1.127 + long startTimestamp = cal.getTimeInMillis();
1.128 +
1.129 + cal.set(year, month, dayom, hour + 1, 0, 0);
1.130 + long endTimestamp = cal.getTimeInMillis();
1.131 +
1.132 + try
1.133 + {
1.134 + return Database.getInstance()
1.135 + .getEventsCount(eventType, startTimestamp, endTimestamp, group);
1.136 + }
1.137 + catch(SQLException ex)
1.138 + {
1.139 + ex.printStackTrace();
1.140 + return -1;
1.141 + }
1.142 + }
1.143 +
1.144 + public void mailPosted(String groupname)
1.145 + {
1.146 + addEvent(POSTED_NEWS, groupname);
1.147 + }
1.148 +
1.149 + public void mailGatewayed(String groupname)
1.150 + {
1.151 + addEvent(GATEWAYED_NEWS, groupname);
1.152 + }
1.153 +
1.154 + public void mailFeeded(String groupname)
1.155 + {
1.156 + addEvent(FEEDED_NEWS, groupname);
1.157 + }
1.158 +
1.159 + public void mlgwRunStart()
1.160 + {
1.161 + addEvent(MLGW_RUNSTART, "control");
1.162 + }
1.163 +
1.164 + public void mlgwRunEnd()
1.165 + {
1.166 + addEvent(MLGW_RUNEND, "control");
1.167 + }
1.168 +
1.169 + private double perHour(int key, long gid)
1.170 + {
1.171 + try
1.172 + {
1.173 + return Database.getInstance().getNumberOfEventsPerHour(key, gid);
1.174 + }
1.175 + catch(SQLException ex)
1.176 + {
1.177 + ex.printStackTrace();
1.178 + return -1;
1.179 + }
1.180 + }
1.181 +
1.182 + public double postedPerHour(long gid)
1.183 + {
1.184 + return perHour(POSTED_NEWS, gid);
1.185 + }
1.186 +
1.187 + public double gatewayedPerHour(long gid)
1.188 + {
1.189 + return perHour(GATEWAYED_NEWS, gid);
1.190 + }
1.191 +
1.192 + public double feededPerHour(long gid)
1.193 + {
1.194 + return perHour(FEEDED_NEWS, gid);
1.195 + }
1.196 +
1.197 +}