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