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