Drupal: bez dědičnosti, implementujeme rovnou rozhraní (nelze dědit).
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 java.util.logging.Level;
22 import org.sonews.config.Config;
23 import org.sonews.storage.Group;
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 {
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;
40 private static Stats instance = new Stats();
42 public static Stats getInstance() {
43 return Stats.instance;
46 private volatile int connectedClients = 0;
52 * A generic method that writes event data to the storage backend.
53 * If event logging is disabled with sonews.eventlog=false this method
54 * simply does nothing.
58 private void addEvent(byte type, String groupname) {
60 if (Config.inst().get(Config.EVENTLOG, true)) {
61 Group group = StorageManager.current().getGroup(groupname);
63 StorageManager.current().addEvent(
64 System.currentTimeMillis(), type, group.getInternalID());
67 StringBuilder strBuf = new StringBuilder();
68 strBuf.append("Group ");
69 strBuf.append(groupname);
70 strBuf.append(" does not exist.");
71 Log.get().info(strBuf.toString());
73 } catch (StorageBackendException ex) {
74 Log.get().log(Level.SEVERE, ex.getLocalizedMessage(), ex);
78 public void clientConnect() {
79 this.connectedClients++;
82 public void clientDisconnect() {
83 this.connectedClients--;
86 public int connectedClients() {
87 return this.connectedClients;
90 public int getNumberOfGroups() {
92 return StorageManager.current().countGroups();
93 } catch (StorageBackendException ex) {
94 Log.get().log(Level.SEVERE, ex.getLocalizedMessage(), ex);
99 public int getNumberOfNews() {
101 return StorageManager.current().countArticles();
102 } catch (StorageBackendException ex) {
103 Log.get().log(Level.SEVERE, ex.getLocalizedMessage(), ex);
108 public int getYesterdaysEvents(final byte eventType, final int hour,
110 // Determine the timestamp values for yesterday and the given hour
111 Calendar cal = Calendar.getInstance();
112 int year = cal.get(Calendar.YEAR);
113 int month = cal.get(Calendar.MONTH);
114 int dayom = cal.get(Calendar.DAY_OF_MONTH) - 1; // Yesterday
116 cal.set(year, month, dayom, hour, 0, 0);
117 long startTimestamp = cal.getTimeInMillis();
119 cal.set(year, month, dayom, hour + 1, 0, 0);
120 long endTimestamp = cal.getTimeInMillis();
123 return StorageManager.current().getEventsCount(eventType, startTimestamp, endTimestamp, group);
124 } catch (StorageBackendException ex) {
125 Log.get().log(Level.SEVERE, ex.getLocalizedMessage(), ex);
130 public void mailPosted(String groupname) {
131 addEvent(POSTED_NEWS, groupname);
134 public void mailGatewayed(String groupname) {
135 addEvent(GATEWAYED_NEWS, groupname);
138 public void mailFeeded(String groupname) {
139 addEvent(FEEDED_NEWS, groupname);
142 public void mlgwRunStart() {
143 addEvent(MLGW_RUNSTART, "control");
146 public void mlgwRunEnd() {
147 addEvent(MLGW_RUNEND, "control");
150 private double perHour(int key, long gid) {
152 return StorageManager.current().getEventsPerHour(key, gid);
153 } catch (StorageBackendException ex) {
154 Log.get().log(Level.SEVERE, ex.getLocalizedMessage(), ex);
159 public double postedPerHour(long gid) {
160 return perHour(POSTED_NEWS, gid);
163 public double gatewayedPerHour(long gid) {
164 return perHour(GATEWAYED_NEWS, gid);
167 public double feededPerHour(long gid) {
168 return perHour(FEEDED_NEWS, gid);