001    /*
002     * Copyright 2014-2015 UnboundID Corp.
003     * All Rights Reserved.
004     */
005    /*
006     * Copyright (C) 2015 UnboundID Corp.
007     *
008     * This program is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU General Public License (GPLv2 only)
010     * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011     * as published by the Free Software Foundation.
012     *
013     * This program is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program; if not, see <http://www.gnu.org/licenses>.
020     */
021    package com.unboundid.ldap.sdk.unboundidds.monitors;
022    
023    
024    
025    import java.io.Serializable;
026    import java.util.Collections;
027    import java.util.Map;
028    import java.util.TreeMap;
029    
030    import com.unboundid.ldap.sdk.Attribute;
031    import com.unboundid.ldap.sdk.Entry;
032    import com.unboundid.ldap.sdk.OperationType;
033    import com.unboundid.util.Debug;
034    import com.unboundid.util.NotMutable;
035    import com.unboundid.util.StaticUtils;
036    import com.unboundid.util.ThreadSafety;
037    import com.unboundid.util.ThreadSafetyLevel;
038    
039    
040    
041    /**
042     * <BLOCKQUOTE>
043     *   <B>NOTE:</B>  This class is part of the Commercial Edition of the UnboundID
044     *   LDAP SDK for Java.  It is not available for use in applications that
045     *   include only the Standard Edition of the LDAP SDK, and is not supported for
046     *   use in conjunction with non-UnboundID products.
047     * </BLOCKQUOTE>
048     * This class provides a data structure that provides information about the
049     * result codes associated with a particular type of operation (or across all
050     * types of operations, if the associated operation type is {@code null}).
051     */
052    @NotMutable()
053    @ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
054    public final class OperationResultCodeInfo
055           implements Serializable
056    {
057      /**
058       * The serial version UID for this serializable class.
059       */
060      private static final long serialVersionUID = 4688688688915878084L;
061    
062    
063    
064      // The percentage of operations of the associated type that failed.
065      private final Double failedPercent;
066    
067      // The total number of operations of the associated type that failed.
068      private final Long failedCount;
069    
070      // The total number of operations of the associated type.
071      private final Long totalCount;
072    
073      // Information about each result code returned for the associated operation
074      // type, indexed by the result code's integer value.
075      private final Map<Integer,ResultCodeInfo> resultCodeInfoMap;
076    
077      // The associated operation type.  It may be null if this structure provides
078      // information about all operation types.
079      private final OperationType operationType;
080    
081    
082    
083      /**
084       * Creates a new operation result code information object from the provided
085       * information.
086       *
087       * @param  entry             The monitor entry to use to obtain the result
088       *                           code information.
089       * @param  operationType     The operation type for this object.  It may be
090       *                           {@code null} if the information applies to all
091       *                           types of operations.
092       * @param  opTypeAttrPrefix  The prefix that will be used for information
093       *                           about
094       */
095      OperationResultCodeInfo(final MonitorEntry entry,
096                              final OperationType operationType,
097                              final String opTypeAttrPrefix)
098      {
099        this.operationType = operationType;
100    
101        totalCount = entry.getLong(opTypeAttrPrefix + "total-count");
102        failedCount = entry.getLong(opTypeAttrPrefix + "failed-count");
103        failedPercent = entry.getDouble(opTypeAttrPrefix + "failed-percent");
104    
105        final String rcPrefix = opTypeAttrPrefix + "result-";
106        final TreeMap<Integer,ResultCodeInfo> rcMap =
107             new TreeMap<Integer,ResultCodeInfo>();
108        final Entry e = entry.getEntry();
109        for (final Attribute a : e.getAttributes())
110        {
111          try
112          {
113            final String lowerName = StaticUtils.toLowerCase(a.getName());
114            if (lowerName.startsWith(rcPrefix) && lowerName.endsWith("-name"))
115            {
116              final String name = a.getValue();
117              final int intValue = Integer.parseInt(lowerName.substring(
118                   rcPrefix.length(), (lowerName.length() - 5)));
119              final long count = entry.getLong(rcPrefix + intValue + "-count");
120              final double percent = entry.getDouble(
121                   rcPrefix + intValue + "-percent");
122              final double totalResponseTimeMillis = entry.getDouble(
123                   rcPrefix + intValue + "-total-response-time-millis");
124              final double averageResponseTimeMillis = entry.getDouble(
125                   rcPrefix + intValue + "-average-response-time-millis");
126              rcMap.put(intValue,
127                   new ResultCodeInfo(intValue, name, operationType, count, percent,
128                        totalResponseTimeMillis, averageResponseTimeMillis));
129            }
130          }
131          catch (final Exception ex)
132          {
133            Debug.debugException(ex);
134          }
135        }
136    
137        resultCodeInfoMap = Collections.unmodifiableMap(rcMap);
138      }
139    
140    
141    
142      /**
143       * Retrieves the type of operation with which this result code information is
144       * associated, if appropriate.
145       *
146       * @return  The type of operation with which this result code information is
147       *          associated, or {@code null} if this information applies to all
148       *          types of operations.
149       */
150      public OperationType getOperationType()
151      {
152        return operationType;
153      }
154    
155    
156    
157      /**
158       * Retrieves the total number of operations of the associated type that have
159       * been processed, if available.
160       *
161       * @return  The total number of operations of the associated type that have
162       *          been processed, or {@code null} if this information was not in the
163       *          monitor entry.
164       */
165      public Long getTotalCount()
166      {
167        return totalCount;
168      }
169    
170    
171    
172      /**
173       * Retrieves the number of operations of the associated type that resulted in
174       * failure, if available.
175       *
176       * @return  The number of operations of the associated type that resulted
177       *          in failure, or {@code null} if this information was not in the
178       *          monitor entry.
179       */
180      public Long getFailedCount()
181      {
182        return failedCount;
183      }
184    
185    
186    
187      /**
188       * Retrieves the percent of operations of the associated type that resulted in
189       * failure, if available.
190       *
191       * @return  The percent of operations of the associated type that resulted
192       *          in failure, or {@code null} if this information was not in the
193       *          monitor entry.
194       */
195      public Double getFailedPercent()
196      {
197        return failedPercent;
198      }
199    
200    
201    
202      /**
203       * Retrieves a map with information about the result codes that have been
204       * returned for operations of the associated type, indexed by the result
205       * code's integer value.
206       *
207       * @return  A map with information about the result codes that have been
208       *          returned for operations of the associated type.
209       */
210      public Map<Integer,ResultCodeInfo> getResultCodeInfoMap()
211      {
212        return resultCodeInfoMap;
213      }
214    }