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
050 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
051 *   for proprietary functionality or for external specifications that are not
052 *   considered stable or mature enough to be guaranteed to work in an
053 *   interoperable way with 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 = new TreeMap<>();
111    final Entry e = entry.getEntry();
112    for (final Attribute a : e.getAttributes())
113    {
114      try
115      {
116        final String lowerName = StaticUtils.toLowerCase(a.getName());
117        if (lowerName.startsWith(rcPrefix) && lowerName.endsWith("-name"))
118        {
119          final String name = a.getValue();
120          final int intValue = Integer.parseInt(lowerName.substring(
121               rcPrefix.length(), (lowerName.length() - 5)));
122          final long count = entry.getLong(rcPrefix + intValue + "-count");
123          final double percent = entry.getDouble(
124               rcPrefix + intValue + "-percent");
125          final double totalResponseTimeMillis = entry.getDouble(
126               rcPrefix + intValue + "-total-response-time-millis");
127          final double averageResponseTimeMillis = entry.getDouble(
128               rcPrefix + intValue + "-average-response-time-millis");
129          rcMap.put(intValue,
130               new ResultCodeInfo(intValue, name, operationType, count, percent,
131                    totalResponseTimeMillis, averageResponseTimeMillis));
132        }
133      }
134      catch (final Exception ex)
135      {
136        Debug.debugException(ex);
137      }
138    }
139
140    resultCodeInfoMap = Collections.unmodifiableMap(rcMap);
141  }
142
143
144
145  /**
146   * Retrieves the type of operation with which this result code information is
147   * associated, if appropriate.
148   *
149   * @return  The type of operation with which this result code information is
150   *          associated, or {@code null} if this information applies to all
151   *          types of operations.
152   */
153  public OperationType getOperationType()
154  {
155    return operationType;
156  }
157
158
159
160  /**
161   * Retrieves the total number of operations of the associated type that have
162   * been processed, if available.
163   *
164   * @return  The total number of operations of the associated type that have
165   *          been processed, or {@code null} if this information was not in the
166   *          monitor entry.
167   */
168  public Long getTotalCount()
169  {
170    return totalCount;
171  }
172
173
174
175  /**
176   * Retrieves the number of operations of the associated type that resulted in
177   * failure, if available.
178   *
179   * @return  The number of operations of the associated type that resulted
180   *          in failure, or {@code null} if this information was not in the
181   *          monitor entry.
182   */
183  public Long getFailedCount()
184  {
185    return failedCount;
186  }
187
188
189
190  /**
191   * Retrieves the percent of operations of the associated type that resulted in
192   * failure, if available.
193   *
194   * @return  The percent of operations of the associated type that resulted
195   *          in failure, or {@code null} if this information was not in the
196   *          monitor entry.
197   */
198  public Double getFailedPercent()
199  {
200    return failedPercent;
201  }
202
203
204
205  /**
206   * Retrieves a map with information about the result codes that have been
207   * returned for operations of the associated type, indexed by the result
208   * code's integer value.
209   *
210   * @return  A map with information about the result codes that have been
211   *          returned for operations of the associated type.
212   */
213  public Map<Integer,ResultCodeInfo> getResultCodeInfoMap()
214  {
215    return resultCodeInfoMap;
216  }
217}