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.util.ArrayList;
041import java.util.Collections;
042import java.util.LinkedHashMap;
043import java.util.List;
044import java.util.Map;
045import java.util.StringTokenizer;
046
047import com.unboundid.ldap.sdk.Entry;
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
056import static com.unboundid.ldap.sdk.unboundidds.monitors.MonitorMessages.*;
057
058
059
060/**
061 * This class defines a numeric gauge monitor entry, which obtains its
062 * information from a numeric value in a monitor entry.
063 * <BR>
064 * <BLOCKQUOTE>
065 *   <B>NOTE:</B>  This class, and other classes within the
066 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
067 *   supported for use against Ping Identity, UnboundID, and
068 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
069 *   for proprietary functionality or for external specifications that are not
070 *   considered stable or mature enough to be guaranteed to work in an
071 *   interoperable way with other types of LDAP servers.
072 * </BLOCKQUOTE>
073 */
074@NotMutable()
075@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
076public final class NumericGaugeMonitorEntry
077       extends GaugeMonitorEntry
078{
079  /**
080   * The structural object class used in gauge monitor entries.
081   */
082  @NotNull static final String NUMERIC_GAUGE_MONITOR_OC =
083       "ds-numeric-gauge-monitor-entry";
084
085
086
087  /**
088   * The serial version UID for this serializable class.
089   */
090  private static final long serialVersionUID = 2049893927290436280L;
091
092
093
094  // The current value for the gauge.
095  @Nullable private final Double currentValue;
096
097  // The maximum value observed for the gauge.
098  @Nullable private final Double maximumValue;
099
100  // The minimum value observed for the gauge.
101  @Nullable private final Double minimumValue;
102
103  // The current value for the gauge.
104  @Nullable private final Double previousValue;
105
106  // The set of observed values for the gauge.
107  @NotNull private final List<Double> observedValues;
108
109
110
111  /**
112   * Creates a new numeric gauge monitor entry from the provided entry.
113   *
114   * @param  entry  The entry to be parsed as a numeric gauge monitor entry.  It
115   *                must not be {@code null}.
116   */
117  public NumericGaugeMonitorEntry(@NotNull final Entry entry)
118  {
119    super(entry);
120
121    currentValue = getDouble("value");
122    previousValue = getDouble("previous-value");
123    minimumValue = getDouble("value-minimum");
124    maximumValue = getDouble("value-maximum");
125
126    final String observedStr = getString("observed-values");
127    if ((observedStr == null) || observedStr.isEmpty())
128    {
129      observedValues = Collections.emptyList();
130    }
131    else
132    {
133      final ArrayList<Double> values = new ArrayList<>(10);
134      try
135      {
136        final StringTokenizer tokenizer = new StringTokenizer(observedStr, ",");
137        while (tokenizer.hasMoreTokens())
138        {
139          values.add(Double.parseDouble(tokenizer.nextToken()));
140        }
141      }
142      catch (final Exception e)
143      {
144        Debug.debugException(e);
145        values.clear();
146      }
147
148      observedValues = Collections.unmodifiableList(values);
149    }
150  }
151
152
153
154  /**
155   * Retrieves the current value for the gauge, if available.
156   *
157   * @return  The current value for the gauge, or {@code null} if it was not
158   *          included in the monitor entry.
159   */
160  @Nullable()
161  public Double getCurrentValue()
162  {
163    return currentValue;
164  }
165
166
167
168  /**
169   * Retrieves the previous value for the gauge, if available.
170   *
171   * @return  The previous value for the gauge, or {@code null} if it was not
172   *          included in the monitor entry.
173   */
174  @Nullable()
175  public Double getPreviousValue()
176  {
177    return previousValue;
178  }
179
180
181
182  /**
183   * Retrieves the minimum value observed for the gauge, if available.
184   *
185   * @return  The minimum value observed for the gauge, or {@code null} if it
186   *          was not included in the monitor entry.
187   */
188  @Nullable()
189  public Double getMinimumValue()
190  {
191    return minimumValue;
192  }
193
194
195
196  /**
197   * Retrieves the maximum value observed for the gauge, if available.
198   *
199   * @return  The maximum value observed for the gauge, or {@code null} if it
200   *          was not included in the monitor entry.
201   */
202  @Nullable()
203  public Double getMaximumValue()
204  {
205    return maximumValue;
206  }
207
208
209
210  /**
211   * Retrieves the set of observed values for the gauge, if available.
212   *
213   * @return  The set of observed values for the gauge, or {@code null} if it
214   *          was not included in the monitor entry.
215   */
216  @NotNull()
217  public List<Double> getObservedValues()
218  {
219    return observedValues;
220  }
221
222
223
224  /**
225   * {@inheritDoc}
226   */
227  @Override()
228  @NotNull()
229  public String getMonitorDisplayName()
230  {
231    return INFO_NUMERIC_GAUGE_MONITOR_DISPNAME.get();
232  }
233
234
235
236  /**
237   * {@inheritDoc}
238   */
239  @Override()
240  @NotNull()
241  public String getMonitorDescription()
242  {
243    return INFO_NUMERIC_GAUGE_MONITOR_DESC.get();
244  }
245
246
247
248  /**
249   * {@inheritDoc}
250   */
251  @Override()
252  @NotNull()
253  public Map<String,MonitorAttribute> getMonitorAttributes()
254  {
255    final Map<String,MonitorAttribute> superAttributes =
256         super.getMonitorAttributes();
257
258    final LinkedHashMap<String,MonitorAttribute> attrs = new LinkedHashMap<>(
259         StaticUtils.computeMapCapacity(superAttributes.size() + 5));
260    attrs.putAll(superAttributes);
261
262    if (currentValue != null)
263    {
264      addMonitorAttribute(attrs,
265           "value",
266           INFO_NUMERIC_GAUGE_DISPNAME_CURRENT_VALUE.get(),
267           INFO_NUMERIC_GAUGE_DESC_CURRENT_VALUE.get(),
268           currentValue);
269    }
270
271    if (previousValue != null)
272    {
273      addMonitorAttribute(attrs,
274           "previous-value",
275           INFO_NUMERIC_GAUGE_DISPNAME_PREVIOUS_VALUE.get(),
276           INFO_NUMERIC_GAUGE_DESC_PREVIOUS_VALUE.get(),
277           previousValue);
278    }
279
280    if (minimumValue != null)
281    {
282      addMonitorAttribute(attrs,
283           "value-minimum",
284           INFO_NUMERIC_GAUGE_DISPNAME_MINIMUM_VALUE.get(),
285           INFO_NUMERIC_GAUGE_DESC_MINIMUM_VALUE.get(),
286           minimumValue);
287    }
288
289    if (maximumValue != null)
290    {
291      addMonitorAttribute(attrs,
292           "value-maximum",
293           INFO_NUMERIC_GAUGE_DISPNAME_MAXIMUM_VALUE.get(),
294           INFO_NUMERIC_GAUGE_DESC_MAXIMUM_VALUE.get(),
295           maximumValue);
296    }
297
298    if (! observedValues.isEmpty())
299    {
300      final Double[] values = new Double[observedValues.size()];
301      observedValues.toArray(values);
302
303      attrs.put("observed-values",
304           new MonitorAttribute("observed-values",
305                INFO_NUMERIC_GAUGE_DISPNAME_OBSERVED_VALUES.get(),
306                INFO_NUMERIC_GAUGE_DESC_OBSERVED_VALUES.get(),
307                values));
308    }
309
310    return Collections.unmodifiableMap(attrs);
311  }
312}