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 |
|
chris@1
|
19 |
package org.sonews.util;
|
chris@1
|
20 |
|
chris@1
|
21 |
import java.util.Calendar;
|
chris@3
|
22 |
import org.sonews.config.Config;
|
chris@3
|
23 |
import org.sonews.storage.Channel;
|
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@37
|
32 |
public final class Stats
|
chris@1
|
33 |
{
|
chris@1
|
34 |
|
cli@37
|
35 |
public static final byte CONNECTIONS = 1;
|
cli@37
|
36 |
public static final byte POSTED_NEWS = 2;
|
cli@37
|
37 |
public static final byte GATEWAYED_NEWS = 3;
|
cli@37
|
38 |
public static final byte FEEDED_NEWS = 4;
|
cli@37
|
39 |
public static final byte MLGW_RUNSTART = 5;
|
cli@37
|
40 |
public static final byte MLGW_RUNEND = 6;
|
cli@37
|
41 |
private static Stats instance = new Stats();
|
chris@3
|
42 |
|
cli@37
|
43 |
public static Stats getInstance()
|
cli@37
|
44 |
{
|
cli@37
|
45 |
return Stats.instance;
|
cli@37
|
46 |
}
|
cli@31
|
47 |
|
cli@37
|
48 |
private Stats()
|
cli@37
|
49 |
{
|
cli@37
|
50 |
}
|
cli@37
|
51 |
private volatile int connectedClients = 0;
|
cli@37
|
52 |
|
cli@37
|
53 |
/**
|
cli@37
|
54 |
* A generic method that writes event data to the storage backend.
|
cli@37
|
55 |
* If event logging is disabled with sonews.eventlog=false this method
|
cli@37
|
56 |
* simply does nothing.
|
cli@37
|
57 |
* @param type
|
cli@37
|
58 |
* @param groupname
|
cli@37
|
59 |
*/
|
cli@37
|
60 |
private void addEvent(byte type, String groupname)
|
cli@37
|
61 |
{
|
cli@37
|
62 |
try {
|
cli@37
|
63 |
if (Config.inst().get(Config.EVENTLOG, true)) {
|
cli@37
|
64 |
|
cli@37
|
65 |
Channel group = Channel.getByName(groupname);
|
cli@37
|
66 |
if (group != null) {
|
cli@37
|
67 |
StorageManager.current().addEvent(
|
cli@37
|
68 |
System.currentTimeMillis(), type, group.getInternalID());
|
cli@37
|
69 |
}
|
cli@37
|
70 |
} else {
|
cli@37
|
71 |
Log.get().info("Group " + groupname + " does not exist.");
|
cli@37
|
72 |
}
|
cli@37
|
73 |
} catch (StorageBackendException ex) {
|
cli@37
|
74 |
ex.printStackTrace();
|
cli@37
|
75 |
}
|
cli@37
|
76 |
}
|
cli@37
|
77 |
|
cli@37
|
78 |
public void clientConnect()
|
cli@37
|
79 |
{
|
cli@37
|
80 |
this.connectedClients++;
|
cli@37
|
81 |
}
|
cli@37
|
82 |
|
cli@37
|
83 |
public void clientDisconnect()
|
cli@37
|
84 |
{
|
cli@37
|
85 |
this.connectedClients--;
|
cli@37
|
86 |
}
|
cli@37
|
87 |
|
cli@37
|
88 |
public int connectedClients()
|
cli@37
|
89 |
{
|
cli@37
|
90 |
return this.connectedClients;
|
cli@37
|
91 |
}
|
cli@37
|
92 |
|
cli@37
|
93 |
public int getNumberOfGroups()
|
cli@37
|
94 |
{
|
cli@37
|
95 |
try {
|
cli@37
|
96 |
return StorageManager.current().countGroups();
|
cli@37
|
97 |
} catch (StorageBackendException ex) {
|
cli@37
|
98 |
ex.printStackTrace();
|
cli@37
|
99 |
return -1;
|
cli@37
|
100 |
}
|
cli@37
|
101 |
}
|
cli@37
|
102 |
|
cli@37
|
103 |
public int getNumberOfNews()
|
cli@37
|
104 |
{
|
cli@37
|
105 |
try {
|
cli@37
|
106 |
return StorageManager.current().countArticles();
|
cli@37
|
107 |
} catch (StorageBackendException ex) {
|
cli@37
|
108 |
ex.printStackTrace();
|
cli@37
|
109 |
return -1;
|
cli@37
|
110 |
}
|
cli@37
|
111 |
}
|
cli@37
|
112 |
|
cli@37
|
113 |
public int getYesterdaysEvents(final byte eventType, final int hour,
|
cli@37
|
114 |
final Channel group)
|
cli@37
|
115 |
{
|
cli@37
|
116 |
// Determine the timestamp values for yesterday and the given hour
|
cli@37
|
117 |
Calendar cal = Calendar.getInstance();
|
cli@37
|
118 |
int year = cal.get(Calendar.YEAR);
|
cli@37
|
119 |
int month = cal.get(Calendar.MONTH);
|
cli@37
|
120 |
int dayom = cal.get(Calendar.DAY_OF_MONTH) - 1; // Yesterday
|
cli@37
|
121 |
|
cli@37
|
122 |
cal.set(year, month, dayom, hour, 0, 0);
|
cli@37
|
123 |
long startTimestamp = cal.getTimeInMillis();
|
cli@37
|
124 |
|
cli@37
|
125 |
cal.set(year, month, dayom, hour + 1, 0, 0);
|
cli@37
|
126 |
long endTimestamp = cal.getTimeInMillis();
|
cli@37
|
127 |
|
cli@37
|
128 |
try {
|
cli@37
|
129 |
return StorageManager.current().getEventsCount(eventType, startTimestamp, endTimestamp, group);
|
cli@37
|
130 |
} catch (StorageBackendException ex) {
|
cli@37
|
131 |
ex.printStackTrace();
|
cli@37
|
132 |
return -1;
|
cli@37
|
133 |
}
|
cli@37
|
134 |
}
|
cli@37
|
135 |
|
cli@37
|
136 |
public void mailPosted(String groupname)
|
cli@37
|
137 |
{
|
cli@37
|
138 |
addEvent(POSTED_NEWS, groupname);
|
cli@37
|
139 |
}
|
cli@37
|
140 |
|
cli@37
|
141 |
public void mailGatewayed(String groupname)
|
cli@37
|
142 |
{
|
cli@37
|
143 |
addEvent(GATEWAYED_NEWS, groupname);
|
cli@37
|
144 |
}
|
cli@37
|
145 |
|
cli@37
|
146 |
public void mailFeeded(String groupname)
|
cli@37
|
147 |
{
|
cli@37
|
148 |
addEvent(FEEDED_NEWS, groupname);
|
cli@37
|
149 |
}
|
cli@37
|
150 |
|
cli@37
|
151 |
public void mlgwRunStart()
|
cli@37
|
152 |
{
|
cli@37
|
153 |
addEvent(MLGW_RUNSTART, "control");
|
cli@37
|
154 |
}
|
cli@37
|
155 |
|
cli@37
|
156 |
public void mlgwRunEnd()
|
cli@37
|
157 |
{
|
cli@37
|
158 |
addEvent(MLGW_RUNEND, "control");
|
cli@37
|
159 |
}
|
cli@37
|
160 |
|
cli@37
|
161 |
private double perHour(int key, long gid)
|
cli@37
|
162 |
{
|
cli@37
|
163 |
try {
|
cli@37
|
164 |
return StorageManager.current().getEventsPerHour(key, gid);
|
cli@37
|
165 |
} catch (StorageBackendException ex) {
|
cli@37
|
166 |
ex.printStackTrace();
|
cli@37
|
167 |
return -1;
|
cli@37
|
168 |
}
|
cli@37
|
169 |
}
|
cli@37
|
170 |
|
cli@37
|
171 |
public double postedPerHour(long gid)
|
cli@37
|
172 |
{
|
cli@37
|
173 |
return perHour(POSTED_NEWS, gid);
|
cli@37
|
174 |
}
|
cli@37
|
175 |
|
cli@37
|
176 |
public double gatewayedPerHour(long gid)
|
cli@37
|
177 |
{
|
cli@37
|
178 |
return perHour(GATEWAYED_NEWS, gid);
|
cli@37
|
179 |
}
|
cli@37
|
180 |
|
cli@37
|
181 |
public double feededPerHour(long gid)
|
cli@37
|
182 |
{
|
cli@37
|
183 |
return perHour(FEEDED_NEWS, gid);
|
cli@37
|
184 |
}
|
chris@1
|
185 |
}
|