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
chris@1
     1
/*
chris@1
     2
 *   SONEWS News Server
chris@1
     3
 *   see AUTHORS for the list of contributors
chris@1
     4
 *
chris@1
     5
 *   This program is free software: you can redistribute it and/or modify
chris@1
     6
 *   it under the terms of the GNU General Public License as published by
chris@1
     7
 *   the Free Software Foundation, either version 3 of the License, or
chris@1
     8
 *   (at your option) any later version.
chris@1
     9
 *
chris@1
    10
 *   This program is distributed in the hope that it will be useful,
chris@1
    11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@1
    12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
chris@1
    13
 *   GNU General Public License for more details.
chris@1
    14
 *
chris@1
    15
 *   You should have received a copy of the GNU General Public License
chris@1
    16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
chris@1
    17
 */
chris@1
    18
chris@1
    19
package org.sonews.util;
chris@1
    20
chris@1
    21
import java.sql.SQLException;
chris@1
    22
import java.util.Calendar;
chris@1
    23
import org.sonews.daemon.storage.Database;
chris@1
    24
import org.sonews.daemon.storage.Group;
chris@1
    25
chris@1
    26
/**
chris@1
    27
 * Class that capsulates statistical data gathering.
chris@1
    28
 * @author Christian Lins
chris@1
    29
 * @since sonews/0.5.0
chris@1
    30
 */
chris@1
    31
public final class Stats 
chris@1
    32
{
chris@1
    33
      
chris@1
    34
  public static final byte CONNECTIONS    = 1;
chris@1
    35
  public static final byte POSTED_NEWS    = 2;
chris@1
    36
  public static final byte GATEWAYED_NEWS = 3;
chris@1
    37
  public static final byte FEEDED_NEWS    = 4;
chris@1
    38
  public static final byte MLGW_RUNSTART  = 5;
chris@1
    39
  public static final byte MLGW_RUNEND    = 6;
chris@1
    40
chris@1
    41
  private static Stats instance = new Stats();
chris@1
    42
  
chris@1
    43
  public static Stats getInstance()
chris@1
    44
  {
chris@1
    45
    return Stats.instance;
chris@1
    46
  }
chris@1
    47
  
chris@1
    48
  private Stats() {}
chris@1
    49
  
chris@1
    50
  private volatile int connectedClients = 0;
chris@1
    51
  
chris@1
    52
  private void addEvent(byte type, String groupname)
chris@1
    53
  {
chris@1
    54
    Group group = Group.getByName(groupname);
chris@1
    55
    if(group != null)
chris@1
    56
    {
chris@1
    57
      try
chris@1
    58
      {
chris@1
    59
        Database.getInstance().addEvent(
chris@1
    60
          System.currentTimeMillis(), type, group.getID());
chris@1
    61
      }
chris@1
    62
      catch(SQLException ex)
chris@1
    63
      {
chris@1
    64
        ex.printStackTrace();
chris@1
    65
      }
chris@1
    66
    }
chris@1
    67
    else
chris@1
    68
    {
chris@1
    69
      Log.msg("Group " + groupname + " does not exist.", true);
chris@1
    70
    }
chris@1
    71
  }
chris@1
    72
  
chris@1
    73
  public void clientConnect()
chris@1
    74
  {
chris@1
    75
    this.connectedClients++;
chris@1
    76
  }
chris@1
    77
  
chris@1
    78
  public void clientDisconnect()
chris@1
    79
  {
chris@1
    80
    this.connectedClients--;
chris@1
    81
  }
chris@1
    82
  
chris@1
    83
  public int connectedClients()
chris@1
    84
  {
chris@1
    85
    return this.connectedClients;
chris@1
    86
  }
chris@1
    87
  
chris@1
    88
  public int getNumberOfGroups()
chris@1
    89
  {
chris@1
    90
    try
chris@1
    91
    {
chris@1
    92
      return Database.getInstance().countGroups();
chris@1
    93
    }
chris@1
    94
    catch(SQLException ex)
chris@1
    95
    {
chris@1
    96
      ex.printStackTrace();
chris@1
    97
      return -1;
chris@1
    98
    }
chris@1
    99
  }
chris@1
   100
  
chris@1
   101
  public int getNumberOfNews()
chris@1
   102
  {
chris@1
   103
    try
chris@1
   104
    {
chris@1
   105
      return Database.getInstance().countArticles();
chris@1
   106
    }
chris@1
   107
    catch(SQLException ex)
chris@1
   108
    {
chris@1
   109
      ex.printStackTrace();
chris@1
   110
      return -1;
chris@1
   111
    }
chris@1
   112
  }
chris@1
   113
  
chris@1
   114
  public int getYesterdaysEvents(final byte eventType, final int hour,
chris@1
   115
    final Group group)
chris@1
   116
  {
chris@1
   117
    // Determine the timestamp values for yesterday and the given hour
chris@1
   118
    Calendar cal = Calendar.getInstance();
chris@1
   119
    int year  = cal.get(Calendar.YEAR);
chris@1
   120
    int month = cal.get(Calendar.MONTH);
chris@1
   121
    int dayom = cal.get(Calendar.DAY_OF_MONTH) - 1; // Yesterday
chris@1
   122
    
chris@1
   123
    cal.set(year, month, dayom, hour, 0, 0);
chris@1
   124
    long startTimestamp = cal.getTimeInMillis();
chris@1
   125
    
chris@1
   126
    cal.set(year, month, dayom, hour + 1, 0, 0);
chris@1
   127
    long endTimestamp = cal.getTimeInMillis();
chris@1
   128
    
chris@1
   129
    try
chris@1
   130
    {
chris@1
   131
      return Database.getInstance()
chris@1
   132
        .getEventsCount(eventType, startTimestamp, endTimestamp, group);
chris@1
   133
    }
chris@1
   134
    catch(SQLException ex)
chris@1
   135
    {
chris@1
   136
      ex.printStackTrace();
chris@1
   137
      return -1;
chris@1
   138
    }
chris@1
   139
  }
chris@1
   140
  
chris@1
   141
  public void mailPosted(String groupname)
chris@1
   142
  {
chris@1
   143
    addEvent(POSTED_NEWS, groupname);
chris@1
   144
  }
chris@1
   145
  
chris@1
   146
  public void mailGatewayed(String groupname)
chris@1
   147
  {
chris@1
   148
    addEvent(GATEWAYED_NEWS, groupname);
chris@1
   149
  }
chris@1
   150
  
chris@1
   151
  public void mailFeeded(String groupname)
chris@1
   152
  {
chris@1
   153
    addEvent(FEEDED_NEWS, groupname);
chris@1
   154
  }
chris@1
   155
  
chris@1
   156
  public void mlgwRunStart()
chris@1
   157
  {
chris@1
   158
    addEvent(MLGW_RUNSTART, "control");
chris@1
   159
  }
chris@1
   160
  
chris@1
   161
  public void mlgwRunEnd()
chris@1
   162
  {
chris@1
   163
    addEvent(MLGW_RUNEND, "control");
chris@1
   164
  }
chris@1
   165
  
chris@1
   166
  private double perHour(int key, long gid)
chris@1
   167
  {
chris@1
   168
    try
chris@1
   169
    {
chris@1
   170
      return Database.getInstance().getNumberOfEventsPerHour(key, gid);
chris@1
   171
    }
chris@1
   172
    catch(SQLException ex)
chris@1
   173
    {
chris@1
   174
      ex.printStackTrace();
chris@1
   175
      return -1;
chris@1
   176
    }
chris@1
   177
  }
chris@1
   178
  
chris@1
   179
  public double postedPerHour(long gid)
chris@1
   180
  {
chris@1
   181
    return perHour(POSTED_NEWS, gid);
chris@1
   182
  }
chris@1
   183
  
chris@1
   184
  public double gatewayedPerHour(long gid)
chris@1
   185
  {
chris@1
   186
    return perHour(GATEWAYED_NEWS, gid);
chris@1
   187
  }
chris@1
   188
  
chris@1
   189
  public double feededPerHour(long gid)
chris@1
   190
  {
chris@1
   191
    return perHour(FEEDED_NEWS, gid);
chris@1
   192
  }
chris@1
   193
  
chris@1
   194
}