Merge Channel and Group classes.
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/>.
18 package org.sonews.util;
20 import java.util.Calendar;
21 import org.sonews.config.Config;
22 import org.sonews.storage.Group;
23 import org.sonews.storage.StorageBackendException;
24 import org.sonews.storage.StorageManager;
27 * Class that capsulates statistical data gathering.
28 * @author Christian Lins
31 public final class Stats {
33 public static final byte CONNECTIONS = 1;
34 public static final byte POSTED_NEWS = 2;
35 public static final byte GATEWAYED_NEWS = 3;
36 public static final byte FEEDED_NEWS = 4;
37 public static final byte MLGW_RUNSTART = 5;
38 public static final byte MLGW_RUNEND = 6;
39 private static Stats instance = new Stats();
41 public static Stats getInstance() {
42 return Stats.instance;
45 private volatile int connectedClients = 0;
51 * A generic method that writes event data to the storage backend.
52 * If event logging is disabled with sonews.eventlog=false this method
53 * simply does nothing.
57 private void addEvent(byte type, String groupname) {
59 if (Config.inst().get(Config.EVENTLOG, true)) {
60 Group group = StorageManager.current().getGroup(groupname);
62 StorageManager.current().addEvent(
63 System.currentTimeMillis(), type, group.getInternalID());
66 Log.get().info("Group " + groupname + " does not exist.");
68 } catch (StorageBackendException ex) {
73 public void clientConnect() {
74 this.connectedClients++;
77 public void clientDisconnect() {
78 this.connectedClients--;
81 public int connectedClients() {
82 return this.connectedClients;
85 public int getNumberOfGroups() {
87 return StorageManager.current().countGroups();
88 } catch (StorageBackendException ex) {
94 public int getNumberOfNews() {
96 return StorageManager.current().countArticles();
97 } catch (StorageBackendException ex) {
103 public int getYesterdaysEvents(final byte eventType, final int hour,
105 // Determine the timestamp values for yesterday and the given hour
106 Calendar cal = Calendar.getInstance();
107 int year = cal.get(Calendar.YEAR);
108 int month = cal.get(Calendar.MONTH);
109 int dayom = cal.get(Calendar.DAY_OF_MONTH) - 1; // Yesterday
111 cal.set(year, month, dayom, hour, 0, 0);
112 long startTimestamp = cal.getTimeInMillis();
114 cal.set(year, month, dayom, hour + 1, 0, 0);
115 long endTimestamp = cal.getTimeInMillis();
118 return StorageManager.current().getEventsCount(eventType, startTimestamp, endTimestamp, group);
119 } catch (StorageBackendException ex) {
120 ex.printStackTrace();
125 public void mailPosted(String groupname) {
126 addEvent(POSTED_NEWS, groupname);
129 public void mailGatewayed(String groupname) {
130 addEvent(GATEWAYED_NEWS, groupname);
133 public void mailFeeded(String groupname) {
134 addEvent(FEEDED_NEWS, groupname);
137 public void mlgwRunStart() {
138 addEvent(MLGW_RUNSTART, "control");
141 public void mlgwRunEnd() {
142 addEvent(MLGW_RUNEND, "control");
145 private double perHour(int key, long gid) {
147 return StorageManager.current().getEventsPerHour(key, gid);
148 } catch (StorageBackendException ex) {
149 ex.printStackTrace();
154 public double postedPerHour(long gid) {
155 return perHour(POSTED_NEWS, gid);
158 public double gatewayedPerHour(long gid) {
159 return perHour(GATEWAYED_NEWS, gid);
162 public double feededPerHour(long gid) {
163 return perHour(FEEDED_NEWS, gid);