org/sonews/util/Stats.java
author chris <chris@marvin>
Wed Jul 01 10:48:22 2009 +0200 (2009-07-01)
changeset 2 1090e2141798
child 3 2fdc9cc89502
permissions -rw-r--r--
sonews/0.5.1 fixes merged
     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.sql.SQLException;
    22 import java.util.Calendar;
    23 import org.sonews.daemon.storage.Database;
    24 import org.sonews.daemon.storage.Group;
    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       
    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 
    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   private volatile int connectedClients = 0;
    51   
    52   private void addEvent(byte type, String groupname)
    53   {
    54     Group group = Group.getByName(groupname);
    55     if(group != null)
    56     {
    57       try
    58       {
    59         Database.getInstance().addEvent(
    60           System.currentTimeMillis(), type, group.getID());
    61       }
    62       catch(SQLException ex)
    63       {
    64         ex.printStackTrace();
    65       }
    66     }
    67     else
    68     {
    69       Log.msg("Group " + groupname + " does not exist.", true);
    70     }
    71   }
    72   
    73   public void clientConnect()
    74   {
    75     this.connectedClients++;
    76   }
    77   
    78   public void clientDisconnect()
    79   {
    80     this.connectedClients--;
    81   }
    82   
    83   public int connectedClients()
    84   {
    85     return this.connectedClients;
    86   }
    87   
    88   public int getNumberOfGroups()
    89   {
    90     try
    91     {
    92       return Database.getInstance().countGroups();
    93     }
    94     catch(SQLException ex)
    95     {
    96       ex.printStackTrace();
    97       return -1;
    98     }
    99   }
   100   
   101   public int getNumberOfNews()
   102   {
   103     try
   104     {
   105       return Database.getInstance().countArticles();
   106     }
   107     catch(SQLException ex)
   108     {
   109       ex.printStackTrace();
   110       return -1;
   111     }
   112   }
   113   
   114   public int getYesterdaysEvents(final byte eventType, final int hour,
   115     final Group group)
   116   {
   117     // Determine the timestamp values for yesterday and the given hour
   118     Calendar cal = Calendar.getInstance();
   119     int year  = cal.get(Calendar.YEAR);
   120     int month = cal.get(Calendar.MONTH);
   121     int dayom = cal.get(Calendar.DAY_OF_MONTH) - 1; // Yesterday
   122     
   123     cal.set(year, month, dayom, hour, 0, 0);
   124     long startTimestamp = cal.getTimeInMillis();
   125     
   126     cal.set(year, month, dayom, hour + 1, 0, 0);
   127     long endTimestamp = cal.getTimeInMillis();
   128     
   129     try
   130     {
   131       return Database.getInstance()
   132         .getEventsCount(eventType, startTimestamp, endTimestamp, group);
   133     }
   134     catch(SQLException ex)
   135     {
   136       ex.printStackTrace();
   137       return -1;
   138     }
   139   }
   140   
   141   public void mailPosted(String groupname)
   142   {
   143     addEvent(POSTED_NEWS, groupname);
   144   }
   145   
   146   public void mailGatewayed(String groupname)
   147   {
   148     addEvent(GATEWAYED_NEWS, groupname);
   149   }
   150   
   151   public void mailFeeded(String groupname)
   152   {
   153     addEvent(FEEDED_NEWS, groupname);
   154   }
   155   
   156   public void mlgwRunStart()
   157   {
   158     addEvent(MLGW_RUNSTART, "control");
   159   }
   160   
   161   public void mlgwRunEnd()
   162   {
   163     addEvent(MLGW_RUNEND, "control");
   164   }
   165   
   166   private double perHour(int key, long gid)
   167   {
   168     try
   169     {
   170       return Database.getInstance().getNumberOfEventsPerHour(key, gid);
   171     }
   172     catch(SQLException ex)
   173     {
   174       ex.printStackTrace();
   175       return -1;
   176     }
   177   }
   178   
   179   public double postedPerHour(long gid)
   180   {
   181     return perHour(POSTED_NEWS, gid);
   182   }
   183   
   184   public double gatewayedPerHour(long gid)
   185   {
   186     return perHour(GATEWAYED_NEWS, gid);
   187   }
   188   
   189   public double feededPerHour(long gid)
   190   {
   191     return perHour(FEEDED_NEWS, gid);
   192   }
   193   
   194 }