View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.beans.stats.collectors;
12  
13  import jakarta.inject.Inject;
14  
15  import javax.naming.NamingException;
16  
17  import org.slf4j.Logger;
18  import org.slf4j.LoggerFactory;
19  import org.springframework.beans.factory.annotation.Value;
20  
21  import psiprobe.beans.ContainerWrapperBean;
22  import psiprobe.model.ApplicationResource;
23  import psiprobe.model.DataSourceInfo;
24  import psiprobe.tools.TimeExpression;
25  
26  /**
27   * The Class DatasourceStatsCollectorBean.
28   */
29  public class DatasourceStatsCollectorBean extends AbstractStatsCollectorBean {
30  
31    /** The Constant PREFIX_ESTABLISHED. */
32    private static final String PREFIX_ESTABLISHED = "ds.est.";
33  
34    /** The Constant PREFIX_BUSY. */
35    private static final String PREFIX_BUSY = "ds.busy.";
36  
37    /** The Constant logger. */
38    private static final Logger logger = LoggerFactory.getLogger(DatasourceStatsCollectorBean.class);
39  
40    /** The container wrapper. */
41    @Inject
42    private ContainerWrapperBean containerWrapper;
43  
44    /**
45     * Gets the container wrapper.
46     *
47     * @return the container wrapper
48     */
49    public ContainerWrapperBean getContainerWrapper() {
50      return containerWrapper;
51    }
52  
53    /**
54     * Sets the container wrapper.
55     *
56     * @param containerWrapper the new container wrapper
57     */
58    public void setContainerWrapper(ContainerWrapperBean containerWrapper) {
59      this.containerWrapper = containerWrapper;
60    }
61  
62    @Override
63    public void collect() throws NamingException, InterruptedException {
64      long currentTime = System.currentTimeMillis();
65      if (containerWrapper == null) {
66        logger.error("Cannot collect data source stats. Container wrapper is not set.");
67      } else {
68        for (ApplicationResource ds : getContainerWrapper().getDataSources()) {
69          String appName = ds.getApplicationName();
70          String name = (appName == null ? "" : appName) + '/' + ds.getName();
71          DataSourceInfo dsi = ds.getDataSourceInfo();
72          int numEstablished = dsi.getEstablishedConnections();
73          int numBusy = dsi.getBusyConnections();
74          logger.trace("Collecting stats for datasource: {}", name);
75          buildAbsoluteStats(PREFIX_ESTABLISHED + name, numEstablished, currentTime);
76          buildAbsoluteStats(PREFIX_BUSY + name, numBusy, currentTime);
77        }
78        logger.debug("datasource stats collected in {}ms", System.currentTimeMillis() - currentTime);
79      }
80    }
81  
82    /**
83     * Reset.
84     *
85     * @throws NamingException the naming exception
86     */
87    public void reset() throws NamingException {
88      if (containerWrapper == null) {
89        logger.error("Cannot reset application stats. Container wrapper is not set.");
90      } else {
91        for (ApplicationResource ds : getContainerWrapper().getDataSources()) {
92          reset(ds.getName());
93        }
94      }
95    }
96  
97    /**
98     * Reset.
99     *
100    * @param name the name
101    */
102   public void reset(String name) {
103     resetStats(PREFIX_ESTABLISHED + name);
104     resetStats(PREFIX_BUSY + name);
105   }
106 
107   /**
108    * Sets the max series expression.
109    *
110    * @param period the period
111    * @param span the span
112    */
113   public void setMaxSeries(
114       @Value("${psiprobe.beans.stats.collectors.datasource.period}") long period,
115       @Value("${psiprobe.beans.stats.collectors.datasource.span}") long span) {
116     super.setMaxSeries((int) TimeExpression.dataPoints(period, span));
117   }
118 
119 }