View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.staticfiles.jmx;
23  
24  import com.google.common.cache.Cache;
25  import com.google.common.cache.CacheBuilder;
26  
27  import org.joda.time.DateTime;
28  
29  import java.util.ArrayList;
30  import java.util.Collections;
31  import java.util.List;
32  import java.util.concurrent.TimeUnit;
33  
34  public class UploadStatistics implements UploadStatisticsMXBean {
35  
36    private long totalNumBytesRead = 0L;
37    private int successful = 0;
38    private int failed = 0;
39    private Cache<Long, Long> bytesCounter = CacheBuilder.newBuilder().expireAfterWrite(15, TimeUnit.MINUTES).build();
40  
41    /**
42     * @see org.opencastproject.staticfiles.jmx.UploadStatisticsMXBean#getSuccessfulUploadOperations()
43     */
44    @Override
45    public int getSuccessfulUploadOperations() {
46      return successful;
47    }
48  
49    /**
50     * @see org.opencastproject.staticfiles.jmx.UploadStatisticsMXBean#getFailedUploadOperations()
51     */
52    @Override
53    public int getFailedUploadOperations() {
54      return failed;
55    }
56  
57    /**
58     * @see org.opencastproject.staticfiles.jmx.UploadStatisticsMXBean#getTotalBytes()
59     */
60    @Override
61    public long getTotalBytes() {
62      return totalNumBytesRead;
63    }
64  
65    /**
66     * @see org.opencastproject.staticfiles.jmx.UploadStatisticsMXBean#getBytesInLastMinute()
67     */
68    @Override
69    public long getBytesInLastMinute() {
70      long key = getKeyByTime(new DateTime().minusMinutes(1).getMillis());
71      return key != 0 ? totalNumBytesRead - bytesCounter.getIfPresent(key) : 0;
72    }
73  
74    /**
75     * @see org.opencastproject.staticfiles.jmx.UploadStatisticsMXBean#getBytesInLastFiveMinutes()
76     */
77    @Override
78    public long getBytesInLastFiveMinutes() {
79      long key = getKeyByTime(new DateTime().minusMinutes(5).getMillis());
80      return key != 0 ? totalNumBytesRead - bytesCounter.getIfPresent(key) : 0;
81    }
82  
83    /**
84     * @see org.opencastproject.staticfiles.jmx.UploadStatisticsMXBean#getBytesInLastFifteenMinutes()
85     */
86    @Override
87    public long getBytesInLastFifteenMinutes() {
88      long key = getKeyByTime(new DateTime().minusMinutes(15).getMillis());
89      return key != 0 ? totalNumBytesRead - bytesCounter.getIfPresent(key) : 0;
90    }
91  
92    private long getKeyByTime(long timeBeforeFiveMinute) {
93      long key = 0L;
94      List<Long> bytes = new ArrayList<Long>(bytesCounter.asMap().keySet());
95      Collections.sort(bytes);
96      for (Long milis : bytes) {
97        if (milis > timeBeforeFiveMinute) {
98          key = milis;
99          break;
100       }
101     }
102     return key;
103   }
104 
105   public void add(long bytes) {
106     if (totalNumBytesRead == 0) {
107       bytesCounter.put(System.currentTimeMillis(), 0L);
108     }
109     totalNumBytesRead += bytes;
110     bytesCounter.put(System.currentTimeMillis(), totalNumBytesRead);
111   }
112 
113   public void successful() {
114     successful++;
115   }
116 
117   public void failed() {
118     failed++;
119   }
120 
121 }