001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.monitoring.statistics.portlet;
016    
017    import com.liferay.portal.kernel.monitoring.MonitoringException;
018    import com.liferay.portal.kernel.monitoring.statistics.RequestStatistics;
019    
020    import java.util.Set;
021    
022    /**
023     * @author Michael C. Han
024     * @author Brian Wing Shun Chan
025     */
026    public class EventRequestSummaryStatistics implements PortletSummaryStatistics {
027    
028            @Override
029            public long getAverageTime() {
030                    long averageTime = 0;
031    
032                    long count = 0;
033    
034                    for (CompanyStatistics companyStatistics :
035                                    _serverStatistics.getCompanyStatisticsSet()) {
036    
037                            for (RequestStatistics requestStatistics :
038                                            companyStatistics.getEventRequestStatisticsSet()) {
039    
040                                    averageTime += requestStatistics.getAverageTime();
041    
042                                    count++;
043                            }
044                    }
045    
046                    return averageTime / count;
047            }
048    
049            @Override
050            public long getAverageTimeByCompany(long companyId)
051                    throws MonitoringException {
052    
053                    CompanyStatistics companyStatistics =
054                            _serverStatistics.getCompanyStatistics(companyId);
055    
056                    return getAverageTimeByCompany(companyStatistics);
057            }
058    
059            @Override
060            public long getAverageTimeByCompany(String webId)
061                    throws MonitoringException {
062    
063                    CompanyStatistics companyStatistics =
064                            _serverStatistics.getCompanyStatistics(webId);
065    
066                    return getAverageTimeByCompany(companyStatistics);
067            }
068    
069            @Override
070            public long getAverageTimeByPortlet(String portletId)
071                    throws MonitoringException {
072    
073                    long averageTime = 0;
074    
075                    Set<CompanyStatistics> companyStatisticsSet =
076                            _serverStatistics.getCompanyStatisticsSet();
077    
078                    for (CompanyStatistics companyStatistics : companyStatisticsSet) {
079                            RequestStatistics requestStatistics =
080                                    companyStatistics.getEventRequestStatistics(portletId);
081    
082                            averageTime += requestStatistics.getAverageTime();
083                    }
084    
085                    return averageTime / companyStatisticsSet.size();
086            }
087    
088            @Override
089            public long getAverageTimeByPortlet(String portletId, long companyId)
090                    throws MonitoringException {
091    
092                    CompanyStatistics companyStatistics =
093                            _serverStatistics.getCompanyStatistics(companyId);
094    
095                    RequestStatistics requestStatistics =
096                            companyStatistics.getEventRequestStatistics(portletId);
097    
098                    return requestStatistics.getAverageTime();
099            }
100    
101            @Override
102            public long getAverageTimeByPortlet(String portletId, String webId)
103                    throws MonitoringException {
104    
105                    CompanyStatistics companyStatistics =
106                            _serverStatistics.getCompanyStatistics(webId);
107    
108                    RequestStatistics requestStatistics =
109                            companyStatistics.getEventRequestStatistics(portletId);
110    
111                    return requestStatistics.getAverageTime();
112            }
113    
114            @Override
115            public long getErrorCount() {
116                    long errorCount = 0;
117    
118                    for (CompanyStatistics companyStatistics :
119                                    _serverStatistics.getCompanyStatisticsSet()) {
120    
121                            errorCount += getErrorCountByCompany(companyStatistics);
122                    }
123    
124                    return errorCount;
125            }
126    
127            @Override
128            public long getErrorCountByCompany(long companyId)
129                    throws MonitoringException {
130    
131                    CompanyStatistics companyStatistics =
132                            _serverStatistics.getCompanyStatistics(companyId);
133    
134                    return getErrorCountByCompany(companyStatistics);
135            }
136    
137            @Override
138            public long getErrorCountByCompany(String webId)
139                    throws MonitoringException {
140    
141                    CompanyStatistics companyStatistics =
142                            _serverStatistics.getCompanyStatistics(webId);
143    
144                    return getErrorCountByCompany(companyStatistics);
145            }
146    
147            @Override
148            public long getErrorCountByPortlet(String portletId)
149                    throws MonitoringException {
150    
151                    long errorCount = 0;
152    
153                    for (CompanyStatistics companyStatistics :
154                                    _serverStatistics.getCompanyStatisticsSet()) {
155    
156                            errorCount += getErrorCountByPortlet(portletId, companyStatistics);
157                    }
158    
159                    return errorCount;
160            }
161    
162            @Override
163            public long getErrorCountByPortlet(String portletId, long companyId)
164                    throws MonitoringException {
165    
166                    CompanyStatistics companyStatistics =
167                            _serverStatistics.getCompanyStatistics(companyId);
168    
169                    return getErrorCountByPortlet(portletId, companyStatistics);
170            }
171    
172            @Override
173            public long getErrorCountByPortlet(String portletId, String webId)
174                    throws MonitoringException {
175    
176                    CompanyStatistics companyStatistics =
177                            _serverStatistics.getCompanyStatistics(webId);
178    
179                    return getErrorCountByPortlet(portletId, companyStatistics);
180            }
181    
182            @Override
183            public long getMaxTime() {
184                    long maxTime = 0;
185    
186                    for (CompanyStatistics companyStatistics :
187                                    _serverStatistics.getCompanyStatisticsSet()) {
188    
189                            for (RequestStatistics requestStatistics :
190                                            companyStatistics.getEventRequestStatisticsSet()) {
191    
192                                    if (requestStatistics.getMaxTime() > maxTime) {
193                                            maxTime = requestStatistics.getMaxTime();
194                                    }
195                            }
196                    }
197    
198                    return maxTime;
199            }
200    
201            @Override
202            public long getMaxTimeByCompany(long companyId) throws MonitoringException {
203                    CompanyStatistics companyStatistics =
204                            _serverStatistics.getCompanyStatistics(companyId);
205    
206                    return companyStatistics.getMaxTime();
207            }
208    
209            @Override
210            public long getMaxTimeByCompany(String webId) throws MonitoringException {
211                    CompanyStatistics companyStatistics =
212                            _serverStatistics.getCompanyStatistics(webId);
213    
214                    return companyStatistics.getMaxTime();
215            }
216    
217            @Override
218            public long getMaxTimeByPortlet(String portletId)
219                    throws MonitoringException {
220    
221                    long maxTime = 0;
222    
223                    for (CompanyStatistics companyStatistics :
224                                    _serverStatistics.getCompanyStatisticsSet()) {
225    
226                            long curMaxTime = getMaxTimeByPortlet(portletId, companyStatistics);
227    
228                            if (curMaxTime > maxTime) {
229                                    maxTime = curMaxTime;
230                            }
231                    }
232    
233                    return maxTime;
234            }
235    
236            @Override
237            public long getMaxTimeByPortlet(String portletId, long companyId)
238                    throws MonitoringException {
239    
240                    CompanyStatistics companyStatistics =
241                            _serverStatistics.getCompanyStatistics(companyId);
242    
243                    return getMaxTimeByPortlet(portletId, companyStatistics);
244            }
245    
246            @Override
247            public long getMaxTimeByPortlet(String portletId, String webId)
248                    throws MonitoringException {
249    
250                    CompanyStatistics companyStatistics =
251                            _serverStatistics.getCompanyStatistics(webId);
252    
253                    return getMaxTimeByPortlet(portletId, companyStatistics);
254            }
255    
256            @Override
257            public long getMinTime() {
258                    long minTime = 0;
259    
260                    for (CompanyStatistics companyStatistics :
261                                    _serverStatistics.getCompanyStatisticsSet()) {
262    
263                            for (RequestStatistics requestStatistics :
264                                            companyStatistics.getEventRequestStatisticsSet()) {
265    
266                                    if (requestStatistics.getMinTime() < minTime) {
267                                            minTime = requestStatistics.getMinTime();
268                                    }
269                            }
270                    }
271    
272                    return minTime;
273            }
274    
275            @Override
276            public long getMinTimeByCompany(long companyId) throws MonitoringException {
277                    CompanyStatistics companyStatistics =
278                            _serverStatistics.getCompanyStatistics(companyId);
279    
280                    return companyStatistics.getMinTime();
281            }
282    
283            @Override
284            public long getMinTimeByCompany(String webId) throws MonitoringException {
285                    CompanyStatistics companyStatistics =
286                            _serverStatistics.getCompanyStatistics(webId);
287    
288                    return companyStatistics.getMinTime();
289            }
290    
291            @Override
292            public long getMinTimeByPortlet(String portletId)
293                    throws MonitoringException {
294    
295                    long minTime = 0;
296    
297                    for (CompanyStatistics companyStatistics :
298                                    _serverStatistics.getCompanyStatisticsSet()) {
299    
300                            long curMinTime = getMinTimeByPortlet(portletId, companyStatistics);
301    
302                            if (curMinTime < minTime) {
303                                    minTime = curMinTime;
304                            }
305                    }
306    
307                    return minTime;
308            }
309    
310            @Override
311            public long getMinTimeByPortlet(String portletId, long companyId)
312                    throws MonitoringException {
313    
314                    CompanyStatistics companyStatistics =
315                            _serverStatistics.getCompanyStatistics(companyId);
316    
317                    return getMinTimeByPortlet(portletId, companyStatistics);
318            }
319    
320            @Override
321            public long getMinTimeByPortlet(String portletId, String webId)
322                    throws MonitoringException {
323    
324                    CompanyStatistics companyStatistics =
325                            _serverStatistics.getCompanyStatistics(webId);
326    
327                    return getMinTimeByPortlet(portletId, companyStatistics);
328            }
329    
330            @Override
331            public long getRequestCount() {
332                    long requestCount = 0;
333    
334                    for (CompanyStatistics companyStatistics :
335                                    _serverStatistics.getCompanyStatisticsSet()) {
336    
337                            requestCount += getRequestCountByCompany(companyStatistics);
338                    }
339    
340                    return requestCount;
341            }
342    
343            @Override
344            public long getRequestCountByCompany(long companyId)
345                    throws MonitoringException {
346    
347                    CompanyStatistics companyStatistics =
348                            _serverStatistics.getCompanyStatistics(companyId);
349    
350                    return getRequestCountByCompany(companyStatistics);
351            }
352    
353            @Override
354            public long getRequestCountByCompany(String webId)
355                    throws MonitoringException {
356    
357                    CompanyStatistics companyStatistics =
358                            _serverStatistics.getCompanyStatistics(webId);
359    
360                    return getRequestCountByCompany(companyStatistics);
361            }
362    
363            @Override
364            public long getRequestCountByPortlet(String portletId)
365                    throws MonitoringException {
366    
367                    long requestCount = 0;
368    
369                    for (CompanyStatistics companyStatistics :
370                                    _serverStatistics.getCompanyStatisticsSet()) {
371    
372                            requestCount += getRequestCountByPortlet(
373                                    portletId, companyStatistics);
374                    }
375    
376                    return requestCount;
377            }
378    
379            @Override
380            public long getRequestCountByPortlet(String portletId, long companyId)
381                    throws MonitoringException {
382    
383                    CompanyStatistics companyStatistics =
384                            _serverStatistics.getCompanyStatistics(companyId);
385    
386                    return getRequestCountByPortlet(portletId, companyStatistics);
387            }
388    
389            @Override
390            public long getRequestCountByPortlet(String portletId, String webId)
391                    throws MonitoringException {
392    
393                    CompanyStatistics companyStatistics =
394                            _serverStatistics.getCompanyStatistics(webId);
395    
396                    return getRequestCountByPortlet(portletId, companyStatistics);
397            }
398    
399            @Override
400            public long getSuccessCount() {
401                    long successCount = 0;
402    
403                    for (CompanyStatistics companyStatistics :
404                                    _serverStatistics.getCompanyStatisticsSet()) {
405    
406                            successCount += getSuccessCountByCompany(companyStatistics);
407                    }
408    
409                    return successCount;
410            }
411    
412            @Override
413            public long getSuccessCountByCompany(long companyId)
414                    throws MonitoringException {
415    
416                    CompanyStatistics companyStatistics =
417                            _serverStatistics.getCompanyStatistics(companyId);
418    
419                    return getSuccessCountByCompany(companyStatistics);
420            }
421    
422            @Override
423            public long getSuccessCountByCompany(String webId)
424                    throws MonitoringException {
425    
426                    CompanyStatistics companyStatistics =
427                            _serverStatistics.getCompanyStatistics(webId);
428    
429                    return getSuccessCountByCompany(companyStatistics);
430            }
431    
432            @Override
433            public long getSuccessCountByPortlet(String portletId)
434                    throws MonitoringException {
435    
436                    long successCount = 0;
437    
438                    for (CompanyStatistics companyStatistics :
439                                    _serverStatistics.getCompanyStatisticsSet()) {
440    
441                            successCount += getSuccessCountByPortlet(
442                                    portletId, companyStatistics);
443                    }
444    
445                    return successCount;
446            }
447    
448            @Override
449            public long getSuccessCountByPortlet(String portletId, long companyId)
450                    throws MonitoringException {
451    
452                    CompanyStatistics companyStatistics =
453                            _serverStatistics.getCompanyStatistics(companyId);
454    
455                    return getSuccessCountByPortlet(portletId, companyStatistics);
456            }
457    
458            @Override
459            public long getSuccessCountByPortlet(String portletId, String webId)
460                    throws MonitoringException {
461    
462                    CompanyStatistics companyStatistics =
463                            _serverStatistics.getCompanyStatistics(webId);
464    
465                    return getSuccessCountByPortlet(portletId, companyStatistics);
466            }
467    
468            @Override
469            public long getTimeoutCount() {
470                    long timeoutCount = 0;
471    
472                    for (CompanyStatistics companyStatistics :
473                                    _serverStatistics.getCompanyStatisticsSet()) {
474    
475                            timeoutCount += getTimeoutCountByCompany(companyStatistics);
476                    }
477    
478                    return timeoutCount;
479            }
480    
481            @Override
482            public long getTimeoutCountByCompany(long companyId)
483                    throws MonitoringException {
484    
485                    CompanyStatistics companyStatistics =
486                            _serverStatistics.getCompanyStatistics(companyId);
487    
488                    return getTimeoutCountByCompany(companyStatistics);
489            }
490    
491            @Override
492            public long getTimeoutCountByCompany(String webId)
493                    throws MonitoringException {
494    
495                    CompanyStatistics companyStatistics =
496                            _serverStatistics.getCompanyStatistics(webId);
497    
498                    return getTimeoutCountByCompany(companyStatistics);
499            }
500    
501            @Override
502            public long getTimeoutCountByPortlet(String portletId)
503                    throws MonitoringException {
504    
505                    long timeoutCount = 0;
506    
507                    for (CompanyStatistics companyStatistics :
508                                    _serverStatistics.getCompanyStatisticsSet()) {
509    
510                            timeoutCount += getTimeoutCountByPortlet(
511                                    portletId, companyStatistics);
512                    }
513    
514                    return timeoutCount;
515            }
516    
517            @Override
518            public long getTimeoutCountByPortlet(String portletId, long companyId)
519                    throws MonitoringException {
520    
521                    CompanyStatistics companyStatistics =
522                            _serverStatistics.getCompanyStatistics(companyId);
523    
524                    return getTimeoutCountByPortlet(portletId, companyStatistics);
525            }
526    
527            @Override
528            public long getTimeoutCountByPortlet(String portletId, String webId)
529                    throws MonitoringException {
530    
531                    CompanyStatistics companyStatistics =
532                            _serverStatistics.getCompanyStatistics(webId);
533    
534                    return getTimeoutCountByPortlet(portletId, companyStatistics);
535            }
536    
537            public void setServerStatistics(ServerStatistics serverStatistics) {
538                    _serverStatistics = serverStatistics;
539            }
540    
541            protected long getAverageTimeByCompany(
542                    CompanyStatistics companyStatistics) {
543    
544                    long averageTime = 0;
545    
546                    Set<RequestStatistics> requestStatisticsSet =
547                            companyStatistics.getEventRequestStatisticsSet();
548    
549                    for (RequestStatistics requestStatistics : requestStatisticsSet) {
550                            averageTime += requestStatistics.getAverageTime();
551                    }
552    
553                    return averageTime / requestStatisticsSet.size();
554            }
555    
556            protected long getErrorCountByCompany(CompanyStatistics companyStatistics) {
557                    long errorCount = 0;
558    
559                    for (RequestStatistics requestStatistics :
560                                    companyStatistics.getEventRequestStatisticsSet()) {
561    
562                            errorCount += requestStatistics.getErrorCount();
563                    }
564    
565                    return errorCount;
566            }
567    
568            protected long getErrorCountByPortlet(
569                            String portletId, CompanyStatistics companyStatistics)
570                    throws MonitoringException {
571    
572                    RequestStatistics requestStatistics =
573                            companyStatistics.getEventRequestStatistics(portletId);
574    
575                    return requestStatistics.getErrorCount();
576            }
577    
578            protected long getMaxTimeByPortlet(
579                            String portletId, CompanyStatistics companyStatistics)
580                    throws MonitoringException {
581    
582                    long maxTime = 0;
583    
584                    RequestStatistics requestStatistics =
585                            companyStatistics.getEventRequestStatistics(portletId);
586    
587                    if (requestStatistics.getMaxTime() > maxTime) {
588                            maxTime = requestStatistics.getMaxTime();
589                    }
590    
591                    return maxTime;
592            }
593    
594            protected long getMinTimeByPortlet(
595                            String portletId, CompanyStatistics companyStatistics)
596                    throws MonitoringException {
597    
598                    long minTime = 0;
599    
600                    RequestStatistics requestStatistics =
601                            companyStatistics.getEventRequestStatistics(portletId);
602    
603                    if (requestStatistics.getMinTime() < minTime) {
604                            minTime = requestStatistics.getMinTime();
605                    }
606    
607                    return minTime;
608            }
609    
610            protected long getRequestCountByCompany(
611                    CompanyStatistics companyStatistics) {
612    
613                    long requestCount = 0;
614    
615                    for (RequestStatistics requestStatistics :
616                                    companyStatistics.getEventRequestStatisticsSet()) {
617    
618                            requestCount += requestStatistics.getRequestCount();
619                    }
620    
621                    return requestCount;
622            }
623    
624            protected long getRequestCountByPortlet(
625                            String portletId, CompanyStatistics companyStatistics)
626                    throws MonitoringException {
627    
628                    RequestStatistics requestStatistics =
629                            companyStatistics.getEventRequestStatistics(portletId);
630    
631                    return requestStatistics.getRequestCount();
632            }
633    
634            protected long getSuccessCountByCompany(
635                    CompanyStatistics companyStatistics) {
636    
637                    long successCount = 0;
638    
639                    for (RequestStatistics requestStatistics :
640                                    companyStatistics.getEventRequestStatisticsSet()) {
641    
642                            successCount += requestStatistics.getSuccessCount();
643                    }
644    
645                    return successCount;
646            }
647    
648            protected long getSuccessCountByPortlet(
649                            String portletId, CompanyStatistics companyStatistics)
650                    throws MonitoringException {
651    
652                    RequestStatistics requestStatistics =
653                            companyStatistics.getEventRequestStatistics(portletId);
654    
655                    return requestStatistics.getSuccessCount();
656            }
657    
658            protected long getTimeoutCountByCompany(
659                    CompanyStatistics companyStatistics) {
660    
661                    long timeoutCount = 0;
662    
663                    for (RequestStatistics requestStatistics :
664                                    companyStatistics.getEventRequestStatisticsSet()) {
665    
666                            timeoutCount += requestStatistics.getTimeoutCount();
667                    }
668    
669                    return timeoutCount;
670            }
671    
672            protected long getTimeoutCountByPortlet(
673                            String portletId, CompanyStatistics companyStatistics)
674                    throws MonitoringException {
675    
676                    RequestStatistics requestStatistics =
677                            companyStatistics.getEventRequestStatistics(portletId);
678    
679                    return requestStatistics.getTimeoutCount();
680            }
681    
682            private ServerStatistics _serverStatistics;
683    
684    }