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