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