org/sonews/util/Stats.java
author cli
Fri, 25 Dec 2009 15:42:46 +0100
changeset 25 dd05c3f2fa24
parent 16 5a4a41cfc0a3
child 31 087ef6fe6a1a
permissions -rw-r--r--
Fix for too early disconnects on slow client connections. (#563)
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.util.Calendar;
chris@3
    22
import org.sonews.config.Config;
chris@3
    23
import org.sonews.storage.Channel;
chris@3
    24
import org.sonews.storage.StorageBackendException;
chris@3
    25
import org.sonews.storage.StorageManager;
chris@1
    26
chris@1
    27
/**
chris@1
    28
 * Class that capsulates statistical data gathering.
chris@1
    29
 * @author Christian Lins
chris@1
    30
 * @since sonews/0.5.0
chris@1
    31
 */
chris@1
    32
public final class Stats 
chris@1
    33
{
chris@1
    34
      
chris@1
    35
  public static final byte CONNECTIONS    = 1;
chris@1
    36
  public static final byte POSTED_NEWS    = 2;
chris@1
    37
  public static final byte GATEWAYED_NEWS = 3;
chris@1
    38
  public static final byte FEEDED_NEWS    = 4;
chris@1
    39
  public static final byte MLGW_RUNSTART  = 5;
chris@1
    40
  public static final byte MLGW_RUNEND    = 6;
chris@1
    41
chris@1
    42
  private static Stats instance = new Stats();
chris@1
    43
  
chris@1
    44
  public static Stats getInstance()
chris@1
    45
  {
chris@1
    46
    return Stats.instance;
chris@1
    47
  }
chris@1
    48
  
chris@1
    49
  private Stats() {}
chris@1
    50
  
chris@1
    51
  private volatile int connectedClients = 0;
chris@3
    52
chris@3
    53
  /**
chris@3
    54
   * A generic method that writes event data to the storage backend.
chris@3
    55
   * If event logging is disabled with sonews.eventlog=false this method
chris@3
    56
   * simply does nothing.
chris@3
    57
   * @param type
chris@3
    58
   * @param groupname
chris@3
    59
   */
chris@1
    60
  private void addEvent(byte type, String groupname)
chris@1
    61
  {
chris@3
    62
    if(Config.inst().get(Config.EVENTLOG, true))
chris@1
    63
    {
chris@3
    64
      Channel group = Channel.getByName(groupname);
chris@3
    65
      if(group != null)
chris@1
    66
      {
chris@3
    67
        try
chris@3
    68
        {
chris@3
    69
          StorageManager.current().addEvent(
chris@3
    70
            System.currentTimeMillis(), type, group.getInternalID());
chris@3
    71
        }
chris@3
    72
        catch(StorageBackendException ex)
chris@3
    73
        {
chris@3
    74
          ex.printStackTrace();
chris@3
    75
        }
chris@1
    76
      }
chris@3
    77
      else
chris@1
    78
      {
cli@16
    79
        Log.get().info("Group " + groupname + " does not exist.");
chris@1
    80
      }
chris@1
    81
    }
chris@1
    82
  }
chris@1
    83
  
chris@1
    84
  public void clientConnect()
chris@1
    85
  {
chris@1
    86
    this.connectedClients++;
chris@1
    87
  }
chris@1
    88
  
chris@1
    89
  public void clientDisconnect()
chris@1
    90
  {
chris@1
    91
    this.connectedClients--;
chris@1
    92
  }
chris@1
    93
  
chris@1
    94
  public int connectedClients()
chris@1
    95
  {
chris@1
    96
    return this.connectedClients;
chris@1
    97
  }
chris@1
    98
  
chris@1
    99
  public int getNumberOfGroups()
chris@1
   100
  {
chris@1
   101
    try
chris@1
   102
    {
chris@3
   103
      return StorageManager.current().countGroups();
chris@1
   104
    }
chris@3
   105
    catch(StorageBackendException ex)
chris@1
   106
    {
chris@1
   107
      ex.printStackTrace();
chris@1
   108
      return -1;
chris@1
   109
    }
chris@1
   110
  }
chris@1
   111
  
chris@1
   112
  public int getNumberOfNews()
chris@1
   113
  {
chris@1
   114
    try
chris@1
   115
    {
chris@3
   116
      return StorageManager.current().countArticles();
chris@1
   117
    }
chris@3
   118
    catch(StorageBackendException ex)
chris@1
   119
    {
chris@1
   120
      ex.printStackTrace();
chris@1
   121
      return -1;
chris@1
   122
    }
chris@1
   123
  }
chris@1
   124
  
chris@1
   125
  public int getYesterdaysEvents(final byte eventType, final int hour,
chris@3
   126
    final Channel group)
chris@1
   127
  {
chris@1
   128
    // Determine the timestamp values for yesterday and the given hour
chris@1
   129
    Calendar cal = Calendar.getInstance();
chris@1
   130
    int year  = cal.get(Calendar.YEAR);
chris@1
   131
    int month = cal.get(Calendar.MONTH);
chris@1
   132
    int dayom = cal.get(Calendar.DAY_OF_MONTH) - 1; // Yesterday
chris@1
   133
    
chris@1
   134
    cal.set(year, month, dayom, hour, 0, 0);
chris@1
   135
    long startTimestamp = cal.getTimeInMillis();
chris@1
   136
    
chris@1
   137
    cal.set(year, month, dayom, hour + 1, 0, 0);
chris@1
   138
    long endTimestamp = cal.getTimeInMillis();
chris@1
   139
    
chris@1
   140
    try
chris@1
   141
    {
chris@3
   142
      return StorageManager.current()
chris@1
   143
        .getEventsCount(eventType, startTimestamp, endTimestamp, group);
chris@1
   144
    }
chris@3
   145
    catch(StorageBackendException ex)
chris@1
   146
    {
chris@1
   147
      ex.printStackTrace();
chris@1
   148
      return -1;
chris@1
   149
    }
chris@1
   150
  }
chris@1
   151
  
chris@1
   152
  public void mailPosted(String groupname)
chris@1
   153
  {
chris@1
   154
    addEvent(POSTED_NEWS, groupname);
chris@1
   155
  }
chris@1
   156
  
chris@1
   157
  public void mailGatewayed(String groupname)
chris@1
   158
  {
chris@1
   159
    addEvent(GATEWAYED_NEWS, groupname);
chris@1
   160
  }
chris@1
   161
  
chris@1
   162
  public void mailFeeded(String groupname)
chris@1
   163
  {
chris@1
   164
    addEvent(FEEDED_NEWS, groupname);
chris@1
   165
  }
chris@1
   166
  
chris@1
   167
  public void mlgwRunStart()
chris@1
   168
  {
chris@1
   169
    addEvent(MLGW_RUNSTART, "control");
chris@1
   170
  }
chris@1
   171
  
chris@1
   172
  public void mlgwRunEnd()
chris@1
   173
  {
chris@1
   174
    addEvent(MLGW_RUNEND, "control");
chris@1
   175
  }
chris@1
   176
  
chris@1
   177
  private double perHour(int key, long gid)
chris@1
   178
  {
chris@1
   179
    try
chris@1
   180
    {
chris@3
   181
      return StorageManager.current().getEventsPerHour(key, gid);
chris@1
   182
    }
chris@3
   183
    catch(StorageBackendException ex)
chris@1
   184
    {
chris@1
   185
      ex.printStackTrace();
chris@1
   186
      return -1;
chris@1
   187
    }
chris@1
   188
  }
chris@1
   189
  
chris@1
   190
  public double postedPerHour(long gid)
chris@1
   191
  {
chris@1
   192
    return perHour(POSTED_NEWS, gid);
chris@1
   193
  }
chris@1
   194
  
chris@1
   195
  public double gatewayedPerHour(long gid)
chris@1
   196
  {
chris@1
   197
    return perHour(GATEWAYED_NEWS, gid);
chris@1
   198
  }
chris@1
   199
  
chris@1
   200
  public double feededPerHour(long gid)
chris@1
   201
  {
chris@1
   202
    return perHour(FEEDED_NEWS, gid);
chris@1
   203
  }
chris@1
   204
  
chris@1
   205
}