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