source: SRUAggregator/trunk/src/main/java/eu/clarin/sru/fcs/aggregator/scan/Statistics.java @ 6131

Last change on this file since 6131 was 6131, checked in by emanuel.dima@uni-tuebingen.de, 9 years ago
  1. beta-33: support for slow endpoints; POST external searches; UI errors for corner cases
File size: 5.5 KB
Line 
1package eu.clarin.sru.fcs.aggregator.scan;
2
3import com.fasterxml.jackson.annotation.JsonProperty;
4import java.util.ArrayList;
5import java.util.Collections;
6import java.util.Date;
7import java.util.HashMap;
8import java.util.List;
9import java.util.Map;
10
11/**
12 *
13 * @author edima
14 */
15public class Statistics {
16
17        public static class EndpointStats {
18
19                private final Object lock = new Object();
20
21                @JsonProperty
22                FCSProtocolVersion version = FCSProtocolVersion.LEGACY;
23
24                @JsonProperty
25                List<String> rootCollections = new ArrayList<String>();
26
27                List<Long> queueTimes = Collections.synchronizedList(new ArrayList<Long>());
28                List<Long> executionTimes = Collections.synchronizedList(new ArrayList<Long>());
29
30                @JsonProperty
31                int maxConcurrentRequests;
32
33                public static class DiagPair {
34
35                        public DiagPair(Diagnostic diagnostic, String context, int counter) {
36                                this.diagnostic = diagnostic;
37                                this.context = context;
38                                this.counter = counter;
39                        }
40
41                        @JsonProperty
42                        public Diagnostic diagnostic;
43                        @JsonProperty
44                        String context;
45                        @JsonProperty
46                        public int counter;
47                }
48
49                @JsonProperty
50                Map<String, DiagPair> diagnostics = Collections.synchronizedMap(new HashMap<String, DiagPair>());
51
52                public static class ExcPair {
53
54                        public ExcPair(JsonException exception, String context, int counter) {
55                                this.exception = exception;
56                                this.context = context;
57                                this.counter = counter;
58                        }
59
60                        @JsonProperty
61                        public JsonException exception;
62                        @JsonProperty
63                        String context;
64                        @JsonProperty
65                        public int counter;
66                }
67
68                @JsonProperty
69                Map<String, ExcPair> errors = Collections.synchronizedMap(new HashMap<String, ExcPair>());
70
71                double avg(List<Long> q) {
72                        double sum = 0;
73                        for (long l : q) {
74                                sum += l;
75                        }
76                        return sum / q.size();
77                }
78
79                double max(List<Long> q) {
80                        double max = 0;
81                        for (long l : q) {
82                                max = max > l ? max : l;
83                        }
84                        return max;
85                }
86
87                @JsonProperty
88                public double getAvgQueueTime() {
89                        return avg(queueTimes) / 1000.0;
90                }
91
92                @JsonProperty
93                public double getMaxQueueTime() {
94                        return max(queueTimes) / 1000.0;
95                }
96
97                @JsonProperty
98                public double getAvgExecutionTime() {
99                        return avg(executionTimes) / 1000.0;
100                }
101
102                @JsonProperty
103                public double getMaxExecutionTime() {
104                        return max(executionTimes) / 1000.0;
105                }
106
107                @JsonProperty
108                public int getNumberOfRequests() {
109                        return executionTimes.size();
110                }
111        };
112
113        private final Object lock = new Object();
114
115        Date date = new Date();
116
117        public Date getDate() {
118                return date;
119        }
120
121        // institution to endpoint to statistics_per_endpoint map
122        Map<String, Map<String, EndpointStats>> institutions
123                        = Collections.synchronizedMap(new HashMap<String, Map<String, EndpointStats>>());
124
125        public Map<String, Map<String, EndpointStats>> getInstitutions() {
126                return institutions;
127        }
128
129        public void initEndpoint(Institution institution, Endpoint endpoint, int maxConcurrentRequests) {
130                EndpointStats stats = getEndpointStats(institution, endpoint);
131                synchronized (stats.lock) {
132                        stats.maxConcurrentRequests = maxConcurrentRequests;
133                }
134        }
135
136        public void addEndpointDatapoint(Institution institution, Endpoint endpoint, long enqueuedTime, long executionTime) {
137                EndpointStats stats = getEndpointStats(institution, endpoint);
138                synchronized (stats.lock) {
139                        stats.queueTimes.add(enqueuedTime);
140                        stats.executionTimes.add(executionTime);
141                }
142        }
143
144        public void addEndpointDiagnostic(Institution institution, Endpoint endpoint, Diagnostic diag, String context) {
145                EndpointStats stats = getEndpointStats(institution, endpoint);
146                synchronized (stats.lock) {
147                        if (!stats.diagnostics.containsKey(diag.uri)) {
148                                stats.diagnostics.put(diag.uri, new EndpointStats.DiagPair(diag, context, 1));
149                        } else {
150                                stats.diagnostics.get(diag.uri).counter++;
151                        }
152                }
153        }
154
155        public void addErrorDatapoint(Institution institution, Endpoint endpoint, Exception error, String context) {
156                EndpointStats stats = getEndpointStats(institution, endpoint);
157                JsonException jxc = new JsonException(error);
158                synchronized (stats.lock) {
159                        if (!stats.errors.containsKey(jxc.message)) {
160                                stats.errors.put(jxc.message, new EndpointStats.ExcPair(jxc, context, 1));
161                        } else {
162                                stats.errors.get(jxc.message).counter++;
163                        }
164                }
165        }
166
167        public void upgradeProtocolVersion(Institution institution, Endpoint endpoint) {
168                EndpointStats stats = getEndpointStats(institution, endpoint);
169                synchronized (stats.lock) {
170                        stats.version = FCSProtocolVersion.VERSION_1;
171                }
172        }
173
174        public void addEndpointCollection(Institution institution, Endpoint endpoint, String collectionName) {
175                EndpointStats stats = getEndpointStats(institution, endpoint);
176                synchronized (stats.lock) {
177                        stats.rootCollections.add(collectionName);
178                }
179        }
180
181        public void addEndpointCollections(Institution institution, Endpoint endpoint, List<String> collections) {
182                EndpointStats stats = getEndpointStats(institution, endpoint);
183                synchronized (stats.lock) {
184                        stats.rootCollections.addAll(collections);
185                }
186        }
187
188        private EndpointStats getEndpointStats(Institution institution, Endpoint endpoint) {
189                EndpointStats stats;
190                synchronized (lock) {
191                        if (!institutions.containsKey(institution.getName())) {
192                                institutions.put(institution.getName(),
193                                                Collections.synchronizedMap(new HashMap<String, EndpointStats>()));
194                        }
195                        Map<String, EndpointStats> esmap = institutions.get(institution.getName());
196                        if (!esmap.containsKey(endpoint.getUrl())) {
197                                EndpointStats es = new EndpointStats();
198                                es.version = endpoint.getProtocol();
199                                esmap.put(endpoint.getUrl(), es);
200                        }
201                        stats = esmap.get(endpoint.getUrl());
202                }
203                return stats;
204        }
205
206}
Note: See TracBrowser for help on using the repository browser.