1 package net.sf.appstatus.services;
2
3 import java.util.concurrent.atomic.AtomicLong;
4
5 import net.sf.appstatus.core.services.IService;
6
7 import org.apache.commons.lang3.ObjectUtils;
8
9 public class Service implements IService {
10 protected AtomicLong cacheHits = new AtomicLong();
11 protected AtomicLong hits = new AtomicLong();
12 protected AtomicLong running = new AtomicLong();
13 protected String name;
14 protected String group;
15 private CachedCallStatistics totalStats;
16 private CachedCallStatistics windowStats;
17 private CachedCallStatistics windowPrevisiousStats;
18 private long windowSize = 2000;
19 private long windowStart = 0;
20 private int minMaxDelay;
21
22 public Service(int minMaxDelay) {
23 this.minMaxDelay = minMaxDelay;
24 totalStats = new CachedCallStatistics(minMaxDelay);
25 windowStats = new CachedCallStatistics(minMaxDelay);
26 windowPrevisiousStats = new CachedCallStatistics(minMaxDelay);
27 }
28
29 public long getRunning() {
30
31 while (true) {
32 long runningCalls = running.get();
33
34 if (runningCalls < 0) {
35
36 running.compareAndSet(runningCalls, 0);
37 } else
38 return runningCalls;
39 }
40
41 }
42
43
44
45
46
47
48
49
50
51
52 public void addCall(Long executionTime, boolean cacheHit, boolean failure, boolean error, int nestedCalls) {
53 totalStats.addCall(executionTime, cacheHit, failure, error, nestedCalls);
54
55 updateWindows();
56 windowStats.addCall(executionTime, cacheHit, failure, error, nestedCalls);
57 }
58
59
60
61
62
63 private void updateWindows() {
64 long currentTime = System.currentTimeMillis();
65 if (currentTime - windowStart > windowSize) {
66 synchronized (this) {
67 if (currentTime - windowStart > windowSize) {
68
69 if (currentTime - windowStart <= 2 * windowSize) {
70 windowPrevisiousStats = windowStats;
71 } else {
72 windowPrevisiousStats = new CachedCallStatistics(minMaxDelay);
73 }
74 windowStats = new CachedCallStatistics(minMaxDelay);
75 windowStart = currentTime;
76 }
77
78 }
79 }
80 }
81
82 public String getName() {
83 return name;
84 }
85
86 public void setName(String name) {
87 this.name = name;
88 }
89
90 public String getGroup() {
91 return group;
92 }
93
94 public void setGroup(String group) {
95 this.group = group;
96 }
97
98 public Double getAvgResponseTime() {
99 return totalStats.getDirectStatistics().getAvgResponseTime();
100 }
101
102 public long getCacheHits() {
103 return cacheHits.get();
104 }
105
106 public long getHits() {
107 return hits.get();
108 }
109
110 public long getFailures() {
111 return totalStats.getFailures();
112 }
113
114 public long getErrors() {
115 return totalStats.getErrors();
116 }
117
118 public Long getMaxResponseTime() {
119 return totalStats.getDirectStatistics().getMaxResponseTime();
120 }
121
122 public Long getMinResponseTime() {
123 return totalStats.getDirectStatistics().getMinResponseTime();
124 }
125
126 public Double getAvgResponseTimeWithCache() {
127 return totalStats.getCacheStatistics().getAvgResponseTime();
128 }
129
130 public Long getMaxResponseTimeWithCache() {
131 return totalStats.getCacheStatistics().getMaxResponseTime();
132 }
133
134 public Long getMinResponseTimeWithCache() {
135 return totalStats.getCacheStatistics().getMinResponseTime();
136 }
137
138 public double getAvgNestedCalls() {
139 return totalStats.getDirectStatistics().getAvgNestedCalls();
140 }
141
142 public double getAvgNestedCallsWithCache() {
143 return totalStats.getCacheStatistics().getAvgNestedCalls();
144 }
145
146 public int compareTo(IService otherService) {
147 int groupCompare = ObjectUtils.compare(group, otherService.getGroup());
148 if (groupCompare != 0) {
149 return groupCompare;
150 }
151
152 return ObjectUtils.compare(name, otherService.getName());
153 }
154
155 public double getCurrentRate() {
156 updateWindows();
157 return (windowPrevisiousStats.getTotalHits() * 1000) / (double) windowSize;
158 }
159
160 }