001/*
002 * Copyright 2014-2018 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2015-2018 Ping Identity Corporation
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 */
021package com.unboundid.ldap.sdk.unboundidds.monitors;
022
023
024
025import java.io.Serializable;
026import java.util.Collections;
027import java.util.Map;
028import java.util.TreeMap;
029
030import com.unboundid.ldap.sdk.Attribute;
031import com.unboundid.ldap.sdk.Entry;
032import com.unboundid.ldap.sdk.OperationType;
033import com.unboundid.util.Debug;
034import com.unboundid.util.NotMutable;
035import com.unboundid.util.StaticUtils;
036import com.unboundid.util.ThreadSafety;
037import com.unboundid.util.ThreadSafetyLevel;
038
039
040
041/**
042 * This class provides a data structure that provides information about the
043 * result codes associated with a particular type of operation (or across all
044 * types of operations, if the associated operation type is {@code null}).
045 * <BR>
046 * <BLOCKQUOTE>
047 *   <B>NOTE:</B>  This class, and other classes within the
048 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
049 *   supported for use against Ping Identity, UnboundID, and Alcatel-Lucent 8661
050 *   server products.  These classes provide support for proprietary
051 *   functionality or for external specifications that are not considered stable
052 *   or mature enough to be guaranteed to work in an interoperable way with
053 *   other types of LDAP servers.
054 * </BLOCKQUOTE>
055 */
056@NotMutable()
057@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
058public final class OperationResultCodeInfo
059       implements Serializable
060{
061  /**
062   * The serial version UID for this serializable class.
063   */
064  private static final long serialVersionUID = 4688688688915878084L;
065
066
067
068  // The percentage of operations of the associated type that failed.
069  private final Double failedPercent;
070
071  // The total number of operations of the associated type that failed.
072  private final Long failedCount;
073
074  // The total number of operations of the associated type.
075  private final Long totalCount;
076
077  // Information about each result code returned for the associated operation
078  // type, indexed by the result code's integer value.
079  private final Map<Integer,ResultCodeInfo> resultCodeInfoMap;
080
081  // The associated operation type.  It may be null if this structure provides
082  // information about all operation types.
083  private final OperationType operationType;
084
085
086
087  /**
088   * Creates a new operation result code information object from the provided
089   * information.
090   *
091   * @param  entry             The monitor entry to use to obtain the result
092   *                           code information.
093   * @param  operationType     The operation type for this object.  It may be
094   *                           {@code null} if the information applies to all
095   *                           types of operations.
096   * @param  opTypeAttrPrefix  The prefix that will be used for information
097   *                           about
098   */
099  OperationResultCodeInfo(final MonitorEntry entry,
100                          final OperationType operationType,
101                          final String opTypeAttrPrefix)
102  {
103    this.operationType = operationType;
104
105    totalCount = entry.getLong(opTypeAttrPrefix + "total-count");
106    failedCount = entry.getLong(opTypeAttrPrefix + "failed-count");
107    failedPercent = entry.getDouble(opTypeAttrPrefix + "failed-percent");
108
109    final String rcPrefix = opTypeAttrPrefix + "result-";
110    final TreeMap<Integer,ResultCodeInfo> rcMap =
111         new TreeMap<Integer,ResultCodeInfo>();
112    final Entry e = entry.getEntry();
113    for (final Attribute a : e.getAttributes())
114    {
115      try
116      {
117        final String lowerName = StaticUtils.toLowerCase(a.getName());
118        if (lowerName.startsWith(rcPrefix) && lowerName.endsWith("-name"))
119        {
120          final String name = a.getValue();
121          final int intValue = Integer.parseInt(lowerName.substring(
122               rcPrefix.length(), (lowerName.length() - 5)));
123          final long count = entry.getLong(rcPrefix + intValue + "-count");
124          final double percent = entry.getDouble(
125               rcPrefix + intValue + "-percent");
126          final double totalResponseTimeMillis = entry.getDouble(
127               rcPrefix + intValue + "-total-response-time-millis");
128          final double averageResponseTimeMillis = entry.getDouble(
129               rcPrefix + intValue + "-average-response-time-millis");
130          rcMap.put(intValue,
131               new ResultCodeInfo(intValue, name, operationType, count, percent,
132                    totalResponseTimeMillis, averageResponseTimeMillis));
133        }
134      }
135      catch (final Exception ex)
136      {
137        Debug.debugException(ex);
138      }
139    }
140
141    resultCodeInfoMap = Collections.unmodifiableMap(rcMap);
142  }
143
144
145
146  /**
147   * Retrieves the type of operation with which this result code information is
148   * associated, if appropriate.
149   *
150   * @return  The type of operation with which this result code information is
151   *          associated, or {@code null} if this information applies to all
152   *          types of operations.
153   */
154  public OperationType getOperationType()
155  {
156    return operationType;
157  }
158
159
160
161  /**
162   * Retrieves the total number of operations of the associated type that have
163   * been processed, if available.
164   *
165   * @return  The total number of operations of the associated type that have
166   *          been processed, or {@code null} if this information was not in the
167   *          monitor entry.
168   */
169  public Long getTotalCount()
170  {
171    return totalCount;
172  }
173
174
175
176  /**
177   * Retrieves the number of operations of the associated type that resulted in
178   * failure, if available.
179   *
180   * @return  The number of operations of the associated type that resulted
181   *          in failure, or {@code null} if this information was not in the
182   *          monitor entry.
183   */
184  public Long getFailedCount()
185  {
186    return failedCount;
187  }
188
189
190
191  /**
192   * Retrieves the percent of operations of the associated type that resulted in
193   * failure, if available.
194   *
195   * @return  The percent of operations of the associated type that resulted
196   *          in failure, or {@code null} if this information was not in the
197   *          monitor entry.
198   */
199  public Double getFailedPercent()
200  {
201    return failedPercent;
202  }
203
204
205
206  /**
207   * Retrieves a map with information about the result codes that have been
208   * returned for operations of the associated type, indexed by the result
209   * code's integer value.
210   *
211   * @return  A map with information about the result codes that have been
212   *          returned for operations of the associated type.
213   */
214  public Map<Integer,ResultCodeInfo> getResultCodeInfoMap()
215  {
216    return resultCodeInfoMap;
217  }
218}