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