Refactoring.
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;
41 private static Stats instance = new Stats();
43 public static Stats getInstance()
45 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)
63 if (Config.inst().get(Config.EVENTLOG, true)) {
65 Channel group = Channel.getByName(groupname);
67 StorageManager.current().addEvent(
68 System.currentTimeMillis(), type, group.getInternalID());
71 Log.get().info("Group " + groupname + " does not exist.");
73 } catch (StorageBackendException ex) {
78 public void clientConnect()
80 this.connectedClients++;
83 public void clientDisconnect()
85 this.connectedClients--;
88 public int connectedClients()
90 return this.connectedClients;
93 public int getNumberOfGroups()
96 return StorageManager.current().countGroups();
97 } catch (StorageBackendException ex) {
103 public int getNumberOfNews()
106 return StorageManager.current().countArticles();
107 } catch (StorageBackendException ex) {
108 ex.printStackTrace();
113 public int getYesterdaysEvents(final byte eventType, final int hour,
116 // Determine the timestamp values for yesterday and the given hour
117 Calendar cal = Calendar.getInstance();
118 int year = cal.get(Calendar.YEAR);
119 int month = cal.get(Calendar.MONTH);
120 int dayom = cal.get(Calendar.DAY_OF_MONTH) - 1; // Yesterday
122 cal.set(year, month, dayom, hour, 0, 0);
123 long startTimestamp = cal.getTimeInMillis();
125 cal.set(year, month, dayom, hour + 1, 0, 0);
126 long endTimestamp = cal.getTimeInMillis();
129 return StorageManager.current().getEventsCount(eventType, startTimestamp, endTimestamp, group);
130 } catch (StorageBackendException ex) {
131 ex.printStackTrace();
136 public void mailPosted(String groupname)
138 addEvent(POSTED_NEWS, groupname);
141 public void mailGatewayed(String groupname)
143 addEvent(GATEWAYED_NEWS, groupname);
146 public void mailFeeded(String groupname)
148 addEvent(FEEDED_NEWS, groupname);
151 public void mlgwRunStart()
153 addEvent(MLGW_RUNSTART, "control");
156 public void mlgwRunEnd()
158 addEvent(MLGW_RUNEND, "control");
161 private double perHour(int key, long gid)
164 return StorageManager.current().getEventsPerHour(key, gid);
165 } catch (StorageBackendException ex) {
166 ex.printStackTrace();
171 public double postedPerHour(long gid)
173 return perHour(POSTED_NEWS, gid);
176 public double gatewayedPerHour(long gid)
178 return perHour(GATEWAYED_NEWS, gid);
181 public double feededPerHour(long gid)
183 return perHour(FEEDED_NEWS, gid);