org/sonews/util/Stats.java
author cli
Wed May 12 11:18:02 2010 +0200 (2010-05-12)
changeset 31 087ef6fe6a1a
parent 16 5a4a41cfc0a3
permissions -rw-r--r--
Group.getByName() removed. Channel.getByName() does no longer catch StorageBackendExceptions but throw them further.
     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 
    42   private static Stats instance = new Stats();
    43   
    44   public static Stats getInstance()
    45   {
    46     return Stats.instance;
    47   }
    48   
    49   private Stats() {}
    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     {
    64       if (Config.inst().get(Config.EVENTLOG, true))
    65       {
    66 
    67         Channel group = Channel.getByName(groupname);
    68         if (group != null)
    69         {
    70           StorageManager.current().addEvent(
    71                   System.currentTimeMillis(), type, group.getInternalID());
    72         }
    73       } 
    74       else
    75       {
    76         Log.get().info("Group " + groupname + " does not exist.");
    77       }
    78     } 
    79     catch (StorageBackendException ex)
    80     {
    81       ex.printStackTrace();
    82     }
    83   }
    84   
    85   public void clientConnect()
    86   {
    87     this.connectedClients++;
    88   }
    89   
    90   public void clientDisconnect()
    91   {
    92     this.connectedClients--;
    93   }
    94   
    95   public int connectedClients()
    96   {
    97     return this.connectedClients;
    98   }
    99   
   100   public int getNumberOfGroups()
   101   {
   102     try
   103     {
   104       return StorageManager.current().countGroups();
   105     }
   106     catch(StorageBackendException ex)
   107     {
   108       ex.printStackTrace();
   109       return -1;
   110     }
   111   }
   112   
   113   public int getNumberOfNews()
   114   {
   115     try
   116     {
   117       return StorageManager.current().countArticles();
   118     }
   119     catch(StorageBackendException ex)
   120     {
   121       ex.printStackTrace();
   122       return -1;
   123     }
   124   }
   125   
   126   public int getYesterdaysEvents(final byte eventType, final int hour,
   127     final Channel group)
   128   {
   129     // Determine the timestamp values for yesterday and the given hour
   130     Calendar cal = Calendar.getInstance();
   131     int year  = cal.get(Calendar.YEAR);
   132     int month = cal.get(Calendar.MONTH);
   133     int dayom = cal.get(Calendar.DAY_OF_MONTH) - 1; // Yesterday
   134     
   135     cal.set(year, month, dayom, hour, 0, 0);
   136     long startTimestamp = cal.getTimeInMillis();
   137     
   138     cal.set(year, month, dayom, hour + 1, 0, 0);
   139     long endTimestamp = cal.getTimeInMillis();
   140     
   141     try
   142     {
   143       return StorageManager.current()
   144         .getEventsCount(eventType, startTimestamp, endTimestamp, group);
   145     }
   146     catch(StorageBackendException ex)
   147     {
   148       ex.printStackTrace();
   149       return -1;
   150     }
   151   }
   152   
   153   public void mailPosted(String groupname)
   154   {
   155     addEvent(POSTED_NEWS, groupname);
   156   }
   157   
   158   public void mailGatewayed(String groupname)
   159   {
   160     addEvent(GATEWAYED_NEWS, groupname);
   161   }
   162   
   163   public void mailFeeded(String groupname)
   164   {
   165     addEvent(FEEDED_NEWS, groupname);
   166   }
   167   
   168   public void mlgwRunStart()
   169   {
   170     addEvent(MLGW_RUNSTART, "control");
   171   }
   172   
   173   public void mlgwRunEnd()
   174   {
   175     addEvent(MLGW_RUNEND, "control");
   176   }
   177   
   178   private double perHour(int key, long gid)
   179   {
   180     try
   181     {
   182       return StorageManager.current().getEventsPerHour(key, gid);
   183     }
   184     catch(StorageBackendException ex)
   185     {
   186       ex.printStackTrace();
   187       return -1;
   188     }
   189   }
   190   
   191   public double postedPerHour(long gid)
   192   {
   193     return perHour(POSTED_NEWS, gid);
   194   }
   195   
   196   public double gatewayedPerHour(long gid)
   197   {
   198     return perHour(GATEWAYED_NEWS, gid);
   199   }
   200   
   201   public double feededPerHour(long gid)
   202   {
   203     return perHour(FEEDED_NEWS, gid);
   204   }
   205   
   206 }