1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * The contents of this file are subject to the terms of either the Universal Permissive License 7 * v 1.0 as shown at http://oss.oracle.com/licenses/upl 8 * 9 * or the following license: 10 * 11 * Redistribution and use in source and binary forms, with or without modification, are permitted 12 * provided that the following conditions are met: 13 * 14 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions 15 * and the following disclaimer. 16 * 17 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of 18 * conditions and the following disclaimer in the documentation and/or other materials provided with 19 * the distribution. 20 * 21 * 3. Neither the name of the copyright holder nor the names of its contributors may be used to 22 * endorse or promote products derived from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 26 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 31 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 package org.openjdk.jmc.flightrecorder.rules.jdk.general; 34 35 import static org.openjdk.jmc.common.unit.UnitLookup.NUMBER; 36 import static org.openjdk.jmc.common.unit.UnitLookup.NUMBER_UNITY; 37 38 import java.text.MessageFormat; 39 import java.util.Arrays; 40 import java.util.Collection; 41 import java.util.List; 42 import java.util.concurrent.Callable; 43 import java.util.concurrent.FutureTask; 44 import java.util.concurrent.RunnableFuture; 45 46 import org.openjdk.jmc.common.item.Aggregators; 47 import org.openjdk.jmc.common.item.IAggregator; 48 import org.openjdk.jmc.common.item.IItemCollection; 49 import org.openjdk.jmc.common.item.IItemFilter; 50 import org.openjdk.jmc.common.item.IItemQuery; 51 import org.openjdk.jmc.common.item.ItemFilters; 52 import org.openjdk.jmc.common.item.ItemQueryBuilder; 53 import org.openjdk.jmc.common.unit.IQuantity; 54 import org.openjdk.jmc.common.util.IPreferenceValueProvider; 55 import org.openjdk.jmc.common.util.TypedPreference; 56 import org.openjdk.jmc.flightrecorder.rules.IRule; 57 import org.openjdk.jmc.flightrecorder.rules.Result; 58 import org.openjdk.jmc.flightrecorder.rules.jdk.messages.internal.Messages; 59 import org.openjdk.jmc.flightrecorder.rules.util.RulesToolkit; 60 import org.openjdk.jmc.flightrecorder.rules.util.RulesToolkit.EventAvailability; 61 62 public class DMSIncidentRule implements IRule { 63 64 private static final String RESULT_ID = "DMSIncident"; //$NON-NLS-1$ 65 private static final String DMS_PATH = "http://www.oracle.com/dms/dfw/dms/dfw/DFW_Incident/DFW_Incident_state"; //$NON-NLS-1$ 66 private static final IItemFilter FILTER = ItemFilters.type(DMS_PATH); 67 public static final IAggregator<IQuantity, ?> INCIDENTS_COUNT = Aggregators.count( 68 Messages.getString(Messages.DMSIncidentRule_AGGR_INCIDENTS_COUNT), 69 Messages.getString(Messages.DMSIncidentRule_AGGR_INCIDENTS_COUNT_DESC), FILTER); 70 71 private static final TypedPreference<IQuantity> WARNING_LIMIT = new TypedPreference<>("dmsincident.warning.limit", //$NON-NLS-1$ 72 Messages.getString(Messages.DMSIncidentRule_CONFIG_WARNING_LIMIT), 73 Messages.getString(Messages.DMSIncidentRule_CONFIG_WARNING_LIMIT_LONG), NUMBER, NUMBER_UNITY.quantity(1)); 74 private static final List<TypedPreference<?>> CONFIG_ATTRIBUTES = Arrays.<TypedPreference<?>> asList(WARNING_LIMIT); 75 76 private Result getResult(IItemCollection items, IPreferenceValueProvider valueProvider) { 77 EventAvailability eventAvailability = RulesToolkit.getEventAvailability(items, DMS_PATH); 78 79 // Not getting any is good, but only if the event was not unavailable or disabled 80 if (eventAvailability == EventAvailability.UNKNOWN || eventAvailability == EventAvailability.DISABLED) { 81 return RulesToolkit.getEventAvailabilityResult(this, items, eventAvailability, DMS_PATH); 82 } 83 84 IQuantity limit = valueProvider.getPreferenceValue(WARNING_LIMIT); 85 IQuantity incidents = items.getAggregate(INCIDENTS_COUNT); 86 if (incidents != null && incidents.compareTo(limit) >= 0) { 87 double score = RulesToolkit.mapExp100(incidents.doubleValue(), limit.doubleValueIn(incidents.getUnit())); 88 IItemQuery query = ItemQueryBuilder.fromWhere(FILTER).build(); 89 return new Result(this, score, Messages.getString(Messages.DMSIncidentRuleFactory_TEXT_WARN), 90 MessageFormat.format(Messages.getString(Messages.DMSIncidentRuleFactory_TEXT_WARN_LONG), incidents), 91 query); 92 } 93 return new Result(this, 0, Messages.getString(Messages.DMSIncidentRuleFactory_TEXT_OK)); 94 } 95 96 @Override 97 public RunnableFuture<Result> evaluate(final IItemCollection items, final IPreferenceValueProvider valueProvider) { 98 FutureTask<Result> evaluationTask = new FutureTask<>(new Callable<Result>() { 99 @Override 100 public Result call() throws Exception { 101 return getResult(items, valueProvider); 102 } 103 }); 104 return evaluationTask; 105 } 106 107 @Override 108 public Collection<TypedPreference<?>> getConfigurationAttributes() { 109 return CONFIG_ATTRIBUTES; 110 } 111 112 @Override 113 public String getId() { 114 return RESULT_ID; 115 } 116 117 @Override 118 public String getName() { 119 return Messages.getString(Messages.DMSIncidentRuleFactory_RULE_NAME); 120 } 121 122 @Override 123 public String getTopic() { 124 // FIXME: Create constant for path 125 return "DMS"; //$NON-NLS-1$ 126 } 127 128 }