Moving source files into src/-subdir.
3 * see AUTHORS for the list of contributors
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package org.sonews.util;
21 import java.util.Calendar;
22 import org.sonews.config.Config;
23 import org.sonews.storage.Channel;
24 import org.sonews.storage.StorageBackendException;
25 import org.sonews.storage.StorageManager;
28 * Class that capsulates statistical data gathering.
29 * @author Christian Lins
32 public final class Stats
35 public static final byte CONNECTIONS = 1;
36 public static final byte POSTED_NEWS = 2;
37 public static final byte GATEWAYED_NEWS = 3;
38 public static final byte FEEDED_NEWS = 4;
39 public static final byte MLGW_RUNSTART = 5;
40 public static final byte MLGW_RUNEND = 6;
42 private static Stats instance = new Stats();
44 public static Stats getInstance()
46 return Stats.instance;
51 private volatile int connectedClients = 0;
54 * A generic method that writes event data to the storage backend.
55 * If event logging is disabled with sonews.eventlog=false this method
56 * simply does nothing.
60 private void addEvent(byte type, String groupname)
64 if (Config.inst().get(Config.EVENTLOG, true))
67 Channel group = Channel.getByName(groupname);
70 StorageManager.current().addEvent(
71 System.currentTimeMillis(), type, group.getInternalID());
76 Log.get().info("Group " + groupname + " does not exist.");
79 catch (StorageBackendException ex)
85 public void clientConnect()
87 this.connectedClients++;
90 public void clientDisconnect()
92 this.connectedClients--;
95 public int connectedClients()
97 return this.connectedClients;
100 public int getNumberOfGroups()
104 return StorageManager.current().countGroups();
106 catch(StorageBackendException ex)
108 ex.printStackTrace();
113 public int getNumberOfNews()
117 return StorageManager.current().countArticles();
119 catch(StorageBackendException ex)
121 ex.printStackTrace();
126 public int getYesterdaysEvents(final byte eventType, final int hour,
129 // Determine the timestamp values for yesterday and the given hour
130 Calendar cal = Calendar.getInstance();
131 int year = cal.get(Calendar.YEAR);
132 int month = cal.get(Calendar.MONTH);
133 int dayom = cal.get(Calendar.DAY_OF_MONTH) - 1; // Yesterday
135 cal.set(year, month, dayom, hour, 0, 0);
136 long startTimestamp = cal.getTimeInMillis();
138 cal.set(year, month, dayom, hour + 1, 0, 0);
139 long endTimestamp = cal.getTimeInMillis();
143 return StorageManager.current()
144 .getEventsCount(eventType, startTimestamp, endTimestamp, group);
146 catch(StorageBackendException ex)
148 ex.printStackTrace();
153 public void mailPosted(String groupname)
155 addEvent(POSTED_NEWS, groupname);
158 public void mailGatewayed(String groupname)
160 addEvent(GATEWAYED_NEWS, groupname);
163 public void mailFeeded(String groupname)
165 addEvent(FEEDED_NEWS, groupname);
168 public void mlgwRunStart()
170 addEvent(MLGW_RUNSTART, "control");
173 public void mlgwRunEnd()
175 addEvent(MLGW_RUNEND, "control");
178 private double perHour(int key, long gid)
182 return StorageManager.current().getEventsPerHour(key, gid);
184 catch(StorageBackendException ex)
186 ex.printStackTrace();
191 public double postedPerHour(long gid)
193 return perHour(POSTED_NEWS, gid);
196 public double gatewayedPerHour(long gid)
198 return perHour(GATEWAYED_NEWS, gid);
201 public double feededPerHour(long gid)
203 return perHour(FEEDED_NEWS, gid);