src/org/sonews/util/Stats.java
author František Kučera <franta-hg@frantovo.cz>
Thu Oct 20 10:50:58 2011 +0200 (2011-10-20)
changeset 103 a788bf0e1080
parent 48 b78e77619152
permissions -rwxr-xr-x
Drupal: zpráva od uživatele se před uložením prožene přes XSLT případně Tidy.
     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 java.util.logging.Level;
    22 import org.sonews.config.Config;
    23 import org.sonews.storage.Group;
    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 	public static final byte CONNECTIONS = 1;
    35 	public static final byte POSTED_NEWS = 2;
    36 	public static final byte GATEWAYED_NEWS = 3;
    37 	public static final byte FEEDED_NEWS = 4;
    38 	public static final byte MLGW_RUNSTART = 5;
    39 	public static final byte MLGW_RUNEND = 6;
    40 	private static Stats instance = new Stats();
    41 
    42 	public static Stats getInstance() {
    43 		return Stats.instance;
    44 	}
    45 
    46 	private volatile int connectedClients = 0;
    47 
    48 	private Stats() {
    49 	}
    50 
    51 	/**
    52 	 * A generic method that writes event data to the storage backend.
    53 	 * If event logging is disabled with sonews.eventlog=false this method
    54 	 * simply does nothing.
    55 	 * @param type
    56 	 * @param groupname
    57 	 */
    58 	private void addEvent(byte type, String groupname) {
    59 		try {
    60 			if (Config.inst().get(Config.EVENTLOG, true)) {
    61 				Group group = StorageManager.current().getGroup(groupname);
    62 				if (group != null) {
    63 					StorageManager.current().addEvent(
    64 							System.currentTimeMillis(), type, group.getInternalID());
    65 				}
    66 			} else {
    67 				StringBuilder strBuf = new StringBuilder();
    68 				strBuf.append("Group ");
    69 				strBuf.append(groupname);
    70 				strBuf.append(" does not exist.");
    71 				Log.get().info(strBuf.toString());
    72 			}
    73 		} catch (StorageBackendException ex) {
    74 			Log.get().log(Level.SEVERE, ex.getLocalizedMessage(), ex);
    75 		}
    76 	}
    77 
    78 	public void clientConnect() {
    79 		this.connectedClients++;
    80 	}
    81 
    82 	public void clientDisconnect() {
    83 		this.connectedClients--;
    84 	}
    85 
    86 	public int connectedClients() {
    87 		return this.connectedClients;
    88 	}
    89 
    90 	public int getNumberOfGroups() {
    91 		try {
    92 			return StorageManager.current().countGroups();
    93 		} catch (StorageBackendException ex) {
    94 			Log.get().log(Level.SEVERE, ex.getLocalizedMessage(), ex);
    95 			return -1;
    96 		}
    97 	}
    98 
    99 	public int getNumberOfNews() {
   100 		try {
   101 			return StorageManager.current().countArticles();
   102 		} catch (StorageBackendException ex) {
   103 			Log.get().log(Level.SEVERE, ex.getLocalizedMessage(), ex);
   104 			return -1;
   105 		}
   106 	}
   107 
   108 	public int getYesterdaysEvents(final byte eventType, final int hour,
   109 			final Group group) {
   110 		// Determine the timestamp values for yesterday and the given hour
   111 		Calendar cal = Calendar.getInstance();
   112 		int year = cal.get(Calendar.YEAR);
   113 		int month = cal.get(Calendar.MONTH);
   114 		int dayom = cal.get(Calendar.DAY_OF_MONTH) - 1; // Yesterday
   115 
   116 		cal.set(year, month, dayom, hour, 0, 0);
   117 		long startTimestamp = cal.getTimeInMillis();
   118 
   119 		cal.set(year, month, dayom, hour + 1, 0, 0);
   120 		long endTimestamp = cal.getTimeInMillis();
   121 
   122 		try {
   123 			return StorageManager.current().getEventsCount(eventType, startTimestamp, endTimestamp, group);
   124 		} catch (StorageBackendException ex) {
   125 			Log.get().log(Level.SEVERE, ex.getLocalizedMessage(), ex);
   126 			return -1;
   127 		}
   128 	}
   129 
   130 	public void mailPosted(String groupname) {
   131 		addEvent(POSTED_NEWS, groupname);
   132 	}
   133 
   134 	public void mailGatewayed(String groupname) {
   135 		addEvent(GATEWAYED_NEWS, groupname);
   136 	}
   137 
   138 	public void mailFeeded(String groupname) {
   139 		addEvent(FEEDED_NEWS, groupname);
   140 	}
   141 
   142 	public void mlgwRunStart() {
   143 		addEvent(MLGW_RUNSTART, "control");
   144 	}
   145 
   146 	public void mlgwRunEnd() {
   147 		addEvent(MLGW_RUNEND, "control");
   148 	}
   149 
   150 	private double perHour(int key, long gid) {
   151 		try {
   152 			return StorageManager.current().getEventsPerHour(key, gid);
   153 		} catch (StorageBackendException ex) {
   154 			Log.get().log(Level.SEVERE, ex.getLocalizedMessage(), ex);
   155 			return -1;
   156 		}
   157 	}
   158 
   159 	public double postedPerHour(long gid) {
   160 		return perHour(POSTED_NEWS, gid);
   161 	}
   162 
   163 	public double gatewayedPerHour(long gid) {
   164 		return perHour(GATEWAYED_NEWS, gid);
   165 	}
   166 
   167 	public double feededPerHour(long gid) {
   168 		return perHour(FEEDED_NEWS, gid);
   169 	}
   170 }