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.sql.SQLException;
22 import java.util.Calendar;
23 import org.sonews.daemon.storage.Database;
24 import org.sonews.daemon.storage.Group;
27 * Class that capsulates statistical data gathering.
28 * @author Christian Lins
31 public final class Stats
34 public static final byte CONNECTIONS = 1;
35 public static final byte POSTED_NEWS = 2;
36 public static final byte GATEWAYED_NEWS = 3;
37 public static final byte FEEDED_NEWS = 4;
38 public static final byte MLGW_RUNSTART = 5;
39 public static final byte MLGW_RUNEND = 6;
41 private static Stats instance = new Stats();
43 public static Stats getInstance()
45 return Stats.instance;
50 private volatile int connectedClients = 0;
52 private void addEvent(byte type, String groupname)
54 Group group = Group.getByName(groupname);
59 Database.getInstance().addEvent(
60 System.currentTimeMillis(), type, group.getID());
62 catch(SQLException ex)
69 Log.msg("Group " + groupname + " does not exist.", true);
73 public void clientConnect()
75 this.connectedClients++;
78 public void clientDisconnect()
80 this.connectedClients--;
83 public int connectedClients()
85 return this.connectedClients;
88 public int getNumberOfGroups()
92 return Database.getInstance().countGroups();
94 catch(SQLException ex)
101 public int getNumberOfNews()
105 return Database.getInstance().countArticles();
107 catch(SQLException ex)
109 ex.printStackTrace();
114 public int getYesterdaysEvents(final byte eventType, final int hour,
117 // Determine the timestamp values for yesterday and the given hour
118 Calendar cal = Calendar.getInstance();
119 int year = cal.get(Calendar.YEAR);
120 int month = cal.get(Calendar.MONTH);
121 int dayom = cal.get(Calendar.DAY_OF_MONTH) - 1; // Yesterday
123 cal.set(year, month, dayom, hour, 0, 0);
124 long startTimestamp = cal.getTimeInMillis();
126 cal.set(year, month, dayom, hour + 1, 0, 0);
127 long endTimestamp = cal.getTimeInMillis();
131 return Database.getInstance()
132 .getEventsCount(eventType, startTimestamp, endTimestamp, group);
134 catch(SQLException ex)
136 ex.printStackTrace();
141 public void mailPosted(String groupname)
143 addEvent(POSTED_NEWS, groupname);
146 public void mailGatewayed(String groupname)
148 addEvent(GATEWAYED_NEWS, groupname);
151 public void mailFeeded(String groupname)
153 addEvent(FEEDED_NEWS, groupname);
156 public void mlgwRunStart()
158 addEvent(MLGW_RUNSTART, "control");
161 public void mlgwRunEnd()
163 addEvent(MLGW_RUNEND, "control");
166 private double perHour(int key, long gid)
170 return Database.getInstance().getNumberOfEventsPerHour(key, gid);
172 catch(SQLException ex)
174 ex.printStackTrace();
179 public double postedPerHour(long gid)
181 return perHour(POSTED_NEWS, gid);
184 public double gatewayedPerHour(long gid)
186 return perHour(GATEWAYED_NEWS, gid);
189 public double feededPerHour(long gid)
191 return perHour(FEEDED_NEWS, gid);