1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
43
44 @Override
45 public int getSuccessfulUploadOperations() {
46 return successful;
47 }
48
49
50
51
52 @Override
53 public int getFailedUploadOperations() {
54 return failed;
55 }
56
57
58
59
60 @Override
61 public long getTotalBytes() {
62 return totalNumBytesRead;
63 }
64
65
66
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
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
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 }