Refactor logging to fix NullPointerException.
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)
62 if(Config.inst().get(Config.EVENTLOG, true))
64 Channel group = Channel.getByName(groupname);
69 StorageManager.current().addEvent(
70 System.currentTimeMillis(), type, group.getInternalID());
72 catch(StorageBackendException ex)
79 Log.get().info("Group " + groupname + " does not exist.");
84 public void clientConnect()
86 this.connectedClients++;
89 public void clientDisconnect()
91 this.connectedClients--;
94 public int connectedClients()
96 return this.connectedClients;
99 public int getNumberOfGroups()
103 return StorageManager.current().countGroups();
105 catch(StorageBackendException ex)
107 ex.printStackTrace();
112 public int getNumberOfNews()
116 return StorageManager.current().countArticles();
118 catch(StorageBackendException ex)
120 ex.printStackTrace();
125 public int getYesterdaysEvents(final byte eventType, final int hour,
128 // Determine the timestamp values for yesterday and the given hour
129 Calendar cal = Calendar.getInstance();
130 int year = cal.get(Calendar.YEAR);
131 int month = cal.get(Calendar.MONTH);
132 int dayom = cal.get(Calendar.DAY_OF_MONTH) - 1; // Yesterday
134 cal.set(year, month, dayom, hour, 0, 0);
135 long startTimestamp = cal.getTimeInMillis();
137 cal.set(year, month, dayom, hour + 1, 0, 0);
138 long endTimestamp = cal.getTimeInMillis();
142 return StorageManager.current()
143 .getEventsCount(eventType, startTimestamp, endTimestamp, group);
145 catch(StorageBackendException ex)
147 ex.printStackTrace();
152 public void mailPosted(String groupname)
154 addEvent(POSTED_NEWS, groupname);
157 public void mailGatewayed(String groupname)
159 addEvent(GATEWAYED_NEWS, groupname);
162 public void mailFeeded(String groupname)
164 addEvent(FEEDED_NEWS, groupname);
167 public void mlgwRunStart()
169 addEvent(MLGW_RUNSTART, "control");
172 public void mlgwRunEnd()
174 addEvent(MLGW_RUNEND, "control");
177 private double perHour(int key, long gid)
181 return StorageManager.current().getEventsPerHour(key, gid);
183 catch(StorageBackendException ex)
185 ex.printStackTrace();
190 public double postedPerHour(long gid)
192 return perHour(POSTED_NEWS, gid);
195 public double gatewayedPerHour(long gid)
197 return perHour(GATEWAYED_NEWS, gid);
200 public double feededPerHour(long gid)
202 return perHour(FEEDED_NEWS, gid);