1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/org/sonews/util/Stats.java Sun Aug 29 17:43:58 2010 +0200
1.3 @@ -0,0 +1,206 @@
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.util.Calendar;
1.25 +import org.sonews.config.Config;
1.26 +import org.sonews.storage.Channel;
1.27 +import org.sonews.storage.StorageBackendException;
1.28 +import org.sonews.storage.StorageManager;
1.29 +
1.30 +/**
1.31 + * Class that capsulates statistical data gathering.
1.32 + * @author Christian Lins
1.33 + * @since sonews/0.5.0
1.34 + */
1.35 +public final class Stats
1.36 +{
1.37 +
1.38 + public static final byte CONNECTIONS = 1;
1.39 + public static final byte POSTED_NEWS = 2;
1.40 + public static final byte GATEWAYED_NEWS = 3;
1.41 + public static final byte FEEDED_NEWS = 4;
1.42 + public static final byte MLGW_RUNSTART = 5;
1.43 + public static final byte MLGW_RUNEND = 6;
1.44 +
1.45 + private static Stats instance = new Stats();
1.46 +
1.47 + public static Stats getInstance()
1.48 + {
1.49 + return Stats.instance;
1.50 + }
1.51 +
1.52 + private Stats() {}
1.53 +
1.54 + private volatile int connectedClients = 0;
1.55 +
1.56 + /**
1.57 + * A generic method that writes event data to the storage backend.
1.58 + * If event logging is disabled with sonews.eventlog=false this method
1.59 + * simply does nothing.
1.60 + * @param type
1.61 + * @param groupname
1.62 + */
1.63 + private void addEvent(byte type, String groupname)
1.64 + {
1.65 + try
1.66 + {
1.67 + if (Config.inst().get(Config.EVENTLOG, true))
1.68 + {
1.69 +
1.70 + Channel group = Channel.getByName(groupname);
1.71 + if (group != null)
1.72 + {
1.73 + StorageManager.current().addEvent(
1.74 + System.currentTimeMillis(), type, group.getInternalID());
1.75 + }
1.76 + }
1.77 + else
1.78 + {
1.79 + Log.get().info("Group " + groupname + " does not exist.");
1.80 + }
1.81 + }
1.82 + catch (StorageBackendException ex)
1.83 + {
1.84 + ex.printStackTrace();
1.85 + }
1.86 + }
1.87 +
1.88 + public void clientConnect()
1.89 + {
1.90 + this.connectedClients++;
1.91 + }
1.92 +
1.93 + public void clientDisconnect()
1.94 + {
1.95 + this.connectedClients--;
1.96 + }
1.97 +
1.98 + public int connectedClients()
1.99 + {
1.100 + return this.connectedClients;
1.101 + }
1.102 +
1.103 + public int getNumberOfGroups()
1.104 + {
1.105 + try
1.106 + {
1.107 + return StorageManager.current().countGroups();
1.108 + }
1.109 + catch(StorageBackendException ex)
1.110 + {
1.111 + ex.printStackTrace();
1.112 + return -1;
1.113 + }
1.114 + }
1.115 +
1.116 + public int getNumberOfNews()
1.117 + {
1.118 + try
1.119 + {
1.120 + return StorageManager.current().countArticles();
1.121 + }
1.122 + catch(StorageBackendException ex)
1.123 + {
1.124 + ex.printStackTrace();
1.125 + return -1;
1.126 + }
1.127 + }
1.128 +
1.129 + public int getYesterdaysEvents(final byte eventType, final int hour,
1.130 + final Channel group)
1.131 + {
1.132 + // Determine the timestamp values for yesterday and the given hour
1.133 + Calendar cal = Calendar.getInstance();
1.134 + int year = cal.get(Calendar.YEAR);
1.135 + int month = cal.get(Calendar.MONTH);
1.136 + int dayom = cal.get(Calendar.DAY_OF_MONTH) - 1; // Yesterday
1.137 +
1.138 + cal.set(year, month, dayom, hour, 0, 0);
1.139 + long startTimestamp = cal.getTimeInMillis();
1.140 +
1.141 + cal.set(year, month, dayom, hour + 1, 0, 0);
1.142 + long endTimestamp = cal.getTimeInMillis();
1.143 +
1.144 + try
1.145 + {
1.146 + return StorageManager.current()
1.147 + .getEventsCount(eventType, startTimestamp, endTimestamp, group);
1.148 + }
1.149 + catch(StorageBackendException ex)
1.150 + {
1.151 + ex.printStackTrace();
1.152 + return -1;
1.153 + }
1.154 + }
1.155 +
1.156 + public void mailPosted(String groupname)
1.157 + {
1.158 + addEvent(POSTED_NEWS, groupname);
1.159 + }
1.160 +
1.161 + public void mailGatewayed(String groupname)
1.162 + {
1.163 + addEvent(GATEWAYED_NEWS, groupname);
1.164 + }
1.165 +
1.166 + public void mailFeeded(String groupname)
1.167 + {
1.168 + addEvent(FEEDED_NEWS, groupname);
1.169 + }
1.170 +
1.171 + public void mlgwRunStart()
1.172 + {
1.173 + addEvent(MLGW_RUNSTART, "control");
1.174 + }
1.175 +
1.176 + public void mlgwRunEnd()
1.177 + {
1.178 + addEvent(MLGW_RUNEND, "control");
1.179 + }
1.180 +
1.181 + private double perHour(int key, long gid)
1.182 + {
1.183 + try
1.184 + {
1.185 + return StorageManager.current().getEventsPerHour(key, gid);
1.186 + }
1.187 + catch(StorageBackendException ex)
1.188 + {
1.189 + ex.printStackTrace();
1.190 + return -1;
1.191 + }
1.192 + }
1.193 +
1.194 + public double postedPerHour(long gid)
1.195 + {
1.196 + return perHour(POSTED_NEWS, gid);
1.197 + }
1.198 +
1.199 + public double gatewayedPerHour(long gid)
1.200 + {
1.201 + return perHour(GATEWAYED_NEWS, gid);
1.202 + }
1.203 +
1.204 + public double feededPerHour(long gid)
1.205 + {
1.206 + return perHour(FEEDED_NEWS, gid);
1.207 + }
1.208 +
1.209 +}