chris@1
|
1 |
/*
|
chris@1
|
2 |
* SONEWS News Server
|
chris@1
|
3 |
* see AUTHORS for the list of contributors
|
chris@1
|
4 |
*
|
chris@1
|
5 |
* This program is free software: you can redistribute it and/or modify
|
chris@1
|
6 |
* it under the terms of the GNU General Public License as published by
|
chris@1
|
7 |
* the Free Software Foundation, either version 3 of the License, or
|
chris@1
|
8 |
* (at your option) any later version.
|
chris@1
|
9 |
*
|
chris@1
|
10 |
* This program is distributed in the hope that it will be useful,
|
chris@1
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
chris@1
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
chris@1
|
13 |
* GNU General Public License for more details.
|
chris@1
|
14 |
*
|
chris@1
|
15 |
* You should have received a copy of the GNU General Public License
|
chris@1
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
chris@1
|
17 |
*/
|
chris@1
|
18 |
package org.sonews.util;
|
chris@1
|
19 |
|
chris@1
|
20 |
import java.util.Calendar;
|
cli@49
|
21 |
import java.util.logging.Level;
|
chris@3
|
22 |
import org.sonews.config.Config;
|
cli@48
|
23 |
import org.sonews.storage.Group;
|
chris@3
|
24 |
import org.sonews.storage.StorageBackendException;
|
chris@3
|
25 |
import org.sonews.storage.StorageManager;
|
chris@1
|
26 |
|
chris@1
|
27 |
/**
|
chris@1
|
28 |
* Class that capsulates statistical data gathering.
|
chris@1
|
29 |
* @author Christian Lins
|
chris@1
|
30 |
* @since sonews/0.5.0
|
chris@1
|
31 |
*/
|
cli@48
|
32 |
public final class Stats {
|
chris@1
|
33 |
|
cli@37
|
34 |
public static final byte CONNECTIONS = 1;
|
cli@37
|
35 |
public static final byte POSTED_NEWS = 2;
|
cli@37
|
36 |
public static final byte GATEWAYED_NEWS = 3;
|
cli@37
|
37 |
public static final byte FEEDED_NEWS = 4;
|
cli@37
|
38 |
public static final byte MLGW_RUNSTART = 5;
|
cli@37
|
39 |
public static final byte MLGW_RUNEND = 6;
|
cli@37
|
40 |
private static Stats instance = new Stats();
|
chris@3
|
41 |
|
cli@48
|
42 |
public static Stats getInstance() {
|
cli@37
|
43 |
return Stats.instance;
|
cli@37
|
44 |
}
|
cli@31
|
45 |
|
cli@48
|
46 |
private volatile int connectedClients = 0;
|
cli@48
|
47 |
|
cli@48
|
48 |
private Stats() {
|
cli@37
|
49 |
}
|
cli@37
|
50 |
|
cli@37
|
51 |
/**
|
cli@37
|
52 |
* A generic method that writes event data to the storage backend.
|
cli@37
|
53 |
* If event logging is disabled with sonews.eventlog=false this method
|
cli@37
|
54 |
* simply does nothing.
|
cli@37
|
55 |
* @param type
|
cli@37
|
56 |
* @param groupname
|
cli@37
|
57 |
*/
|
cli@48
|
58 |
private void addEvent(byte type, String groupname) {
|
cli@37
|
59 |
try {
|
cli@37
|
60 |
if (Config.inst().get(Config.EVENTLOG, true)) {
|
cli@48
|
61 |
Group group = StorageManager.current().getGroup(groupname);
|
cli@37
|
62 |
if (group != null) {
|
cli@37
|
63 |
StorageManager.current().addEvent(
|
cli@48
|
64 |
System.currentTimeMillis(), type, group.getInternalID());
|
cli@37
|
65 |
}
|
cli@37
|
66 |
} else {
|
cli@49
|
67 |
StringBuilder strBuf = new StringBuilder();
|
cli@49
|
68 |
strBuf.append("Group ");
|
cli@49
|
69 |
strBuf.append(groupname);
|
cli@49
|
70 |
strBuf.append(" does not exist.");
|
cli@49
|
71 |
Log.get().info(strBuf.toString());
|
cli@37
|
72 |
}
|
cli@37
|
73 |
} catch (StorageBackendException ex) {
|
cli@49
|
74 |
Log.get().log(Level.SEVERE, ex.getLocalizedMessage(), ex);
|
cli@37
|
75 |
}
|
cli@37
|
76 |
}
|
cli@37
|
77 |
|
cli@48
|
78 |
public void clientConnect() {
|
cli@37
|
79 |
this.connectedClients++;
|
cli@37
|
80 |
}
|
cli@37
|
81 |
|
cli@48
|
82 |
public void clientDisconnect() {
|
cli@37
|
83 |
this.connectedClients--;
|
cli@37
|
84 |
}
|
cli@37
|
85 |
|
cli@48
|
86 |
public int connectedClients() {
|
cli@37
|
87 |
return this.connectedClients;
|
cli@37
|
88 |
}
|
cli@37
|
89 |
|
cli@48
|
90 |
public int getNumberOfGroups() {
|
cli@37
|
91 |
try {
|
cli@37
|
92 |
return StorageManager.current().countGroups();
|
cli@37
|
93 |
} catch (StorageBackendException ex) {
|
cli@49
|
94 |
Log.get().log(Level.SEVERE, ex.getLocalizedMessage(), ex);
|
cli@37
|
95 |
return -1;
|
cli@37
|
96 |
}
|
cli@37
|
97 |
}
|
cli@37
|
98 |
|
cli@48
|
99 |
public int getNumberOfNews() {
|
cli@37
|
100 |
try {
|
cli@37
|
101 |
return StorageManager.current().countArticles();
|
cli@37
|
102 |
} catch (StorageBackendException ex) {
|
cli@49
|
103 |
Log.get().log(Level.SEVERE, ex.getLocalizedMessage(), ex);
|
cli@37
|
104 |
return -1;
|
cli@37
|
105 |
}
|
cli@37
|
106 |
}
|
cli@37
|
107 |
|
cli@37
|
108 |
public int getYesterdaysEvents(final byte eventType, final int hour,
|
cli@48
|
109 |
final Group group) {
|
cli@37
|
110 |
// Determine the timestamp values for yesterday and the given hour
|
cli@37
|
111 |
Calendar cal = Calendar.getInstance();
|
cli@37
|
112 |
int year = cal.get(Calendar.YEAR);
|
cli@37
|
113 |
int month = cal.get(Calendar.MONTH);
|
cli@37
|
114 |
int dayom = cal.get(Calendar.DAY_OF_MONTH) - 1; // Yesterday
|
cli@37
|
115 |
|
cli@37
|
116 |
cal.set(year, month, dayom, hour, 0, 0);
|
cli@37
|
117 |
long startTimestamp = cal.getTimeInMillis();
|
cli@37
|
118 |
|
cli@37
|
119 |
cal.set(year, month, dayom, hour + 1, 0, 0);
|
cli@37
|
120 |
long endTimestamp = cal.getTimeInMillis();
|
cli@37
|
121 |
|
cli@37
|
122 |
try {
|
cli@37
|
123 |
return StorageManager.current().getEventsCount(eventType, startTimestamp, endTimestamp, group);
|
cli@37
|
124 |
} catch (StorageBackendException ex) {
|
cli@49
|
125 |
Log.get().log(Level.SEVERE, ex.getLocalizedMessage(), ex);
|
cli@37
|
126 |
return -1;
|
cli@37
|
127 |
}
|
cli@37
|
128 |
}
|
cli@37
|
129 |
|
cli@48
|
130 |
public void mailPosted(String groupname) {
|
cli@37
|
131 |
addEvent(POSTED_NEWS, groupname);
|
cli@37
|
132 |
}
|
cli@37
|
133 |
|
cli@48
|
134 |
public void mailGatewayed(String groupname) {
|
cli@37
|
135 |
addEvent(GATEWAYED_NEWS, groupname);
|
cli@37
|
136 |
}
|
cli@37
|
137 |
|
cli@48
|
138 |
public void mailFeeded(String groupname) {
|
cli@37
|
139 |
addEvent(FEEDED_NEWS, groupname);
|
cli@37
|
140 |
}
|
cli@37
|
141 |
|
cli@48
|
142 |
public void mlgwRunStart() {
|
cli@37
|
143 |
addEvent(MLGW_RUNSTART, "control");
|
cli@37
|
144 |
}
|
cli@37
|
145 |
|
cli@48
|
146 |
public void mlgwRunEnd() {
|
cli@37
|
147 |
addEvent(MLGW_RUNEND, "control");
|
cli@37
|
148 |
}
|
cli@37
|
149 |
|
cli@48
|
150 |
private double perHour(int key, long gid) {
|
cli@37
|
151 |
try {
|
cli@37
|
152 |
return StorageManager.current().getEventsPerHour(key, gid);
|
cli@37
|
153 |
} catch (StorageBackendException ex) {
|
cli@49
|
154 |
Log.get().log(Level.SEVERE, ex.getLocalizedMessage(), ex);
|
cli@37
|
155 |
return -1;
|
cli@37
|
156 |
}
|
cli@37
|
157 |
}
|
cli@37
|
158 |
|
cli@48
|
159 |
public double postedPerHour(long gid) {
|
cli@37
|
160 |
return perHour(POSTED_NEWS, gid);
|
cli@37
|
161 |
}
|
cli@37
|
162 |
|
cli@48
|
163 |
public double gatewayedPerHour(long gid) {
|
cli@37
|
164 |
return perHour(GATEWAYED_NEWS, gid);
|
cli@37
|
165 |
}
|
cli@37
|
166 |
|
cli@48
|
167 |
public double feededPerHour(long gid) {
|
cli@37
|
168 |
return perHour(FEEDED_NEWS, gid);
|
cli@37
|
169 |
}
|
chris@1
|
170 |
}
|