src/org/sonews/util/Stats.java
changeset 39 73b21e9f3958
parent 35 ed84c8bdd87b
child 48 b78e77619152
     1.1 --- a/src/org/sonews/util/Stats.java	Sun Aug 29 17:28:58 2010 +0200
     1.2 +++ b/src/org/sonews/util/Stats.java	Mon Aug 30 00:20:06 2010 +0200
     1.3 @@ -29,178 +29,157 @@
     1.4   * @author Christian Lins
     1.5   * @since sonews/0.5.0
     1.6   */
     1.7 -public final class Stats 
     1.8 +public final class Stats
     1.9  {
    1.10 -      
    1.11 -  public static final byte CONNECTIONS    = 1;
    1.12 -  public static final byte POSTED_NEWS    = 2;
    1.13 -  public static final byte GATEWAYED_NEWS = 3;
    1.14 -  public static final byte FEEDED_NEWS    = 4;
    1.15 -  public static final byte MLGW_RUNSTART  = 5;
    1.16 -  public static final byte MLGW_RUNEND    = 6;
    1.17  
    1.18 -  private static Stats instance = new Stats();
    1.19 -  
    1.20 -  public static Stats getInstance()
    1.21 -  {
    1.22 -    return Stats.instance;
    1.23 -  }
    1.24 -  
    1.25 -  private Stats() {}
    1.26 -  
    1.27 -  private volatile int connectedClients = 0;
    1.28 +	public static final byte CONNECTIONS = 1;
    1.29 +	public static final byte POSTED_NEWS = 2;
    1.30 +	public static final byte GATEWAYED_NEWS = 3;
    1.31 +	public static final byte FEEDED_NEWS = 4;
    1.32 +	public static final byte MLGW_RUNSTART = 5;
    1.33 +	public static final byte MLGW_RUNEND = 6;
    1.34 +	private static Stats instance = new Stats();
    1.35  
    1.36 -  /**
    1.37 -   * A generic method that writes event data to the storage backend.
    1.38 -   * If event logging is disabled with sonews.eventlog=false this method
    1.39 -   * simply does nothing.
    1.40 -   * @param type
    1.41 -   * @param groupname
    1.42 -   */
    1.43 -  private void addEvent(byte type, String groupname)
    1.44 -  {
    1.45 -    try
    1.46 -    {
    1.47 -      if (Config.inst().get(Config.EVENTLOG, true))
    1.48 -      {
    1.49 +	public static Stats getInstance()
    1.50 +	{
    1.51 +		return Stats.instance;
    1.52 +	}
    1.53  
    1.54 -        Channel group = Channel.getByName(groupname);
    1.55 -        if (group != null)
    1.56 -        {
    1.57 -          StorageManager.current().addEvent(
    1.58 -                  System.currentTimeMillis(), type, group.getInternalID());
    1.59 -        }
    1.60 -      } 
    1.61 -      else
    1.62 -      {
    1.63 -        Log.get().info("Group " + groupname + " does not exist.");
    1.64 -      }
    1.65 -    } 
    1.66 -    catch (StorageBackendException ex)
    1.67 -    {
    1.68 -      ex.printStackTrace();
    1.69 -    }
    1.70 -  }
    1.71 -  
    1.72 -  public void clientConnect()
    1.73 -  {
    1.74 -    this.connectedClients++;
    1.75 -  }
    1.76 -  
    1.77 -  public void clientDisconnect()
    1.78 -  {
    1.79 -    this.connectedClients--;
    1.80 -  }
    1.81 -  
    1.82 -  public int connectedClients()
    1.83 -  {
    1.84 -    return this.connectedClients;
    1.85 -  }
    1.86 -  
    1.87 -  public int getNumberOfGroups()
    1.88 -  {
    1.89 -    try
    1.90 -    {
    1.91 -      return StorageManager.current().countGroups();
    1.92 -    }
    1.93 -    catch(StorageBackendException ex)
    1.94 -    {
    1.95 -      ex.printStackTrace();
    1.96 -      return -1;
    1.97 -    }
    1.98 -  }
    1.99 -  
   1.100 -  public int getNumberOfNews()
   1.101 -  {
   1.102 -    try
   1.103 -    {
   1.104 -      return StorageManager.current().countArticles();
   1.105 -    }
   1.106 -    catch(StorageBackendException ex)
   1.107 -    {
   1.108 -      ex.printStackTrace();
   1.109 -      return -1;
   1.110 -    }
   1.111 -  }
   1.112 -  
   1.113 -  public int getYesterdaysEvents(final byte eventType, final int hour,
   1.114 -    final Channel group)
   1.115 -  {
   1.116 -    // Determine the timestamp values for yesterday and the given hour
   1.117 -    Calendar cal = Calendar.getInstance();
   1.118 -    int year  = cal.get(Calendar.YEAR);
   1.119 -    int month = cal.get(Calendar.MONTH);
   1.120 -    int dayom = cal.get(Calendar.DAY_OF_MONTH) - 1; // Yesterday
   1.121 -    
   1.122 -    cal.set(year, month, dayom, hour, 0, 0);
   1.123 -    long startTimestamp = cal.getTimeInMillis();
   1.124 -    
   1.125 -    cal.set(year, month, dayom, hour + 1, 0, 0);
   1.126 -    long endTimestamp = cal.getTimeInMillis();
   1.127 -    
   1.128 -    try
   1.129 -    {
   1.130 -      return StorageManager.current()
   1.131 -        .getEventsCount(eventType, startTimestamp, endTimestamp, group);
   1.132 -    }
   1.133 -    catch(StorageBackendException ex)
   1.134 -    {
   1.135 -      ex.printStackTrace();
   1.136 -      return -1;
   1.137 -    }
   1.138 -  }
   1.139 -  
   1.140 -  public void mailPosted(String groupname)
   1.141 -  {
   1.142 -    addEvent(POSTED_NEWS, groupname);
   1.143 -  }
   1.144 -  
   1.145 -  public void mailGatewayed(String groupname)
   1.146 -  {
   1.147 -    addEvent(GATEWAYED_NEWS, groupname);
   1.148 -  }
   1.149 -  
   1.150 -  public void mailFeeded(String groupname)
   1.151 -  {
   1.152 -    addEvent(FEEDED_NEWS, groupname);
   1.153 -  }
   1.154 -  
   1.155 -  public void mlgwRunStart()
   1.156 -  {
   1.157 -    addEvent(MLGW_RUNSTART, "control");
   1.158 -  }
   1.159 -  
   1.160 -  public void mlgwRunEnd()
   1.161 -  {
   1.162 -    addEvent(MLGW_RUNEND, "control");
   1.163 -  }
   1.164 -  
   1.165 -  private double perHour(int key, long gid)
   1.166 -  {
   1.167 -    try
   1.168 -    {
   1.169 -      return StorageManager.current().getEventsPerHour(key, gid);
   1.170 -    }
   1.171 -    catch(StorageBackendException ex)
   1.172 -    {
   1.173 -      ex.printStackTrace();
   1.174 -      return -1;
   1.175 -    }
   1.176 -  }
   1.177 -  
   1.178 -  public double postedPerHour(long gid)
   1.179 -  {
   1.180 -    return perHour(POSTED_NEWS, gid);
   1.181 -  }
   1.182 -  
   1.183 -  public double gatewayedPerHour(long gid)
   1.184 -  {
   1.185 -    return perHour(GATEWAYED_NEWS, gid);
   1.186 -  }
   1.187 -  
   1.188 -  public double feededPerHour(long gid)
   1.189 -  {
   1.190 -    return perHour(FEEDED_NEWS, gid);
   1.191 -  }
   1.192 -  
   1.193 +	private Stats()
   1.194 +	{
   1.195 +	}
   1.196 +	private volatile int connectedClients = 0;
   1.197 +
   1.198 +	/**
   1.199 +	 * A generic method that writes event data to the storage backend.
   1.200 +	 * If event logging is disabled with sonews.eventlog=false this method
   1.201 +	 * simply does nothing.
   1.202 +	 * @param type
   1.203 +	 * @param groupname
   1.204 +	 */
   1.205 +	private void addEvent(byte type, String groupname)
   1.206 +	{
   1.207 +		try {
   1.208 +			if (Config.inst().get(Config.EVENTLOG, true)) {
   1.209 +
   1.210 +				Channel group = Channel.getByName(groupname);
   1.211 +				if (group != null) {
   1.212 +					StorageManager.current().addEvent(
   1.213 +						System.currentTimeMillis(), type, group.getInternalID());
   1.214 +				}
   1.215 +			} else {
   1.216 +				Log.get().info("Group " + groupname + " does not exist.");
   1.217 +			}
   1.218 +		} catch (StorageBackendException ex) {
   1.219 +			ex.printStackTrace();
   1.220 +		}
   1.221 +	}
   1.222 +
   1.223 +	public void clientConnect()
   1.224 +	{
   1.225 +		this.connectedClients++;
   1.226 +	}
   1.227 +
   1.228 +	public void clientDisconnect()
   1.229 +	{
   1.230 +		this.connectedClients--;
   1.231 +	}
   1.232 +
   1.233 +	public int connectedClients()
   1.234 +	{
   1.235 +		return this.connectedClients;
   1.236 +	}
   1.237 +
   1.238 +	public int getNumberOfGroups()
   1.239 +	{
   1.240 +		try {
   1.241 +			return StorageManager.current().countGroups();
   1.242 +		} catch (StorageBackendException ex) {
   1.243 +			ex.printStackTrace();
   1.244 +			return -1;
   1.245 +		}
   1.246 +	}
   1.247 +
   1.248 +	public int getNumberOfNews()
   1.249 +	{
   1.250 +		try {
   1.251 +			return StorageManager.current().countArticles();
   1.252 +		} catch (StorageBackendException ex) {
   1.253 +			ex.printStackTrace();
   1.254 +			return -1;
   1.255 +		}
   1.256 +	}
   1.257 +
   1.258 +	public int getYesterdaysEvents(final byte eventType, final int hour,
   1.259 +		final Channel group)
   1.260 +	{
   1.261 +		// Determine the timestamp values for yesterday and the given hour
   1.262 +		Calendar cal = Calendar.getInstance();
   1.263 +		int year = cal.get(Calendar.YEAR);
   1.264 +		int month = cal.get(Calendar.MONTH);
   1.265 +		int dayom = cal.get(Calendar.DAY_OF_MONTH) - 1; // Yesterday
   1.266 +
   1.267 +		cal.set(year, month, dayom, hour, 0, 0);
   1.268 +		long startTimestamp = cal.getTimeInMillis();
   1.269 +
   1.270 +		cal.set(year, month, dayom, hour + 1, 0, 0);
   1.271 +		long endTimestamp = cal.getTimeInMillis();
   1.272 +
   1.273 +		try {
   1.274 +			return StorageManager.current().getEventsCount(eventType, startTimestamp, endTimestamp, group);
   1.275 +		} catch (StorageBackendException ex) {
   1.276 +			ex.printStackTrace();
   1.277 +			return -1;
   1.278 +		}
   1.279 +	}
   1.280 +
   1.281 +	public void mailPosted(String groupname)
   1.282 +	{
   1.283 +		addEvent(POSTED_NEWS, groupname);
   1.284 +	}
   1.285 +
   1.286 +	public void mailGatewayed(String groupname)
   1.287 +	{
   1.288 +		addEvent(GATEWAYED_NEWS, groupname);
   1.289 +	}
   1.290 +
   1.291 +	public void mailFeeded(String groupname)
   1.292 +	{
   1.293 +		addEvent(FEEDED_NEWS, groupname);
   1.294 +	}
   1.295 +
   1.296 +	public void mlgwRunStart()
   1.297 +	{
   1.298 +		addEvent(MLGW_RUNSTART, "control");
   1.299 +	}
   1.300 +
   1.301 +	public void mlgwRunEnd()
   1.302 +	{
   1.303 +		addEvent(MLGW_RUNEND, "control");
   1.304 +	}
   1.305 +
   1.306 +	private double perHour(int key, long gid)
   1.307 +	{
   1.308 +		try {
   1.309 +			return StorageManager.current().getEventsPerHour(key, gid);
   1.310 +		} catch (StorageBackendException ex) {
   1.311 +			ex.printStackTrace();
   1.312 +			return -1;
   1.313 +		}
   1.314 +	}
   1.315 +
   1.316 +	public double postedPerHour(long gid)
   1.317 +	{
   1.318 +		return perHour(POSTED_NEWS, gid);
   1.319 +	}
   1.320 +
   1.321 +	public double gatewayedPerHour(long gid)
   1.322 +	{
   1.323 +		return perHour(GATEWAYED_NEWS, gid);
   1.324 +	}
   1.325 +
   1.326 +	public double feededPerHour(long gid)
   1.327 +	{
   1.328 +		return perHour(FEEDED_NEWS, gid);
   1.329 +	}
   1.330  }