public static SensorSummary sensorSummaryForCatalog(Sensor<?> sensor) { return new SensorSummary(sensor.getName(), sensor.getTypeName(), sensor.getDescription(), null); }
public <T> Map<Entity,T> getValues(Sensor<T> sensor) { Map<Entity, T> valuesCopy = copyValues(sensor); return coerceValues(valuesCopy, sensor.getType()); }
@Test public void testGetSensor() throws Exception { Sensor<?> sensor = entity.getEntityType().getSensor("entity.sensor.added"); assertEquals(sensor.getDescription(), "Sensor dynamically added to entity"); assertEquals(sensor.getName(), "entity.sensor.added"); assertNull(entity.getEntityType().getSensor("does.not.exist")); }
@Test public void testWindowSizeCulling() { averager.onEvent(intSensor.newEvent(producer, 10)); averager.onEvent(intSensor.newEvent(producer, 20)); averager.onEvent(intSensor.newEvent(producer, 30)); averager.onEvent(intSensor.newEvent(producer, 40)); averager.onEvent(intSensor.newEvent(producer, 50)); averager.onEvent(intSensor.newEvent(producer, 60)); assertEquals(averager.getAverage(), (30+40+50+60)/4d); } }
@Override public void onEvent(SensorEvent<T> event) { Number current = elvis(event.getValue(), 0); double newVal = current.doubleValue() - last.doubleValue(); entity.sensors().set((AttributeSensor<T>)target, TypeCoercions.coerce(newVal, target.getTypeToken())); last = current; } }
@Test public void testRebindPersistsDynamicAttribute() throws Exception { final String sensorName = "test.mydynamicsensor"; final String sensorDescription = "My description"; final AttributeSensor<String> MY_DYNAMIC_SENSOR = new BasicAttributeSensor<String>( String.class, sensorName, sensorDescription); origApp.sensors().set(MY_DYNAMIC_SENSOR, "myval"); assertEquals(origApp.getEntityType().getSensor(sensorName).getDescription(), sensorDescription); newApp = rebind(); assertEquals(newApp.getAttribute(MY_DYNAMIC_SENSOR), "myval"); assertEquals(newApp.getEntityType().getSensor(sensorName).getDescription(), sensorDescription); }
@Test public void testMonospacedAverage() { averager.onEvent(intSensor.newEvent(producer, 10), 1000); averager.onEvent(intSensor.newEvent(producer, 20), 1250); averager.onEvent(intSensor.newEvent(producer, 30), 1500); averager.onEvent(intSensor.newEvent(producer, 40), 1750); averager.onEvent(intSensor.newEvent(producer, 50), 2000); average = averager.getAverage(2000); assertEquals(average.value, (20+30+40+50)/4d); assertEquals(average.confidence, 1d); }
/** @deprecated since 0.7.0 only {@link AbstractEnricher} has emit convenience */ @Deprecated protected <T> void emit(Sensor<T> sensor, Object val) { checkState(entity != null, "entity must first be set"); if (val == Entities.UNCHANGED) { return; } if (val == Entities.REMOVE) { ((EntityInternal)entity).removeAttribute((AttributeSensor<T>) sensor); return; } T newVal = TypeCoercions.coerce(val, sensor.getTypeToken()); if (sensor instanceof AttributeSensor) { entity.sensors().set((AttributeSensor<T>)sensor, newVal); } else { entity.sensors().emit(sensor, newVal); } }
public EntitySensorToken(Entity e, Sensor<?> s) { this.e = e; this.s = s; this.sName = (s == null) ? null : checkNotNull(s.getName(), "sensor must have non-null name: %s", s); } @Override
public static SensorSummary sensorSummary(Entity entity, Sensor<?> sensor, UriBuilder ub) { URI applicationUri = serviceUriBuilder(ub, ApplicationApi.class, "get").build(entity.getApplicationId()); URI entityUri = serviceUriBuilder(ub, EntityApi.class, "get").build(entity.getApplicationId(), entity.getId()); URI selfUri = serviceUriBuilder(ub, SensorApi.class, "get").build(entity.getApplicationId(), entity.getId(), sensor.getName()); MutableMap.Builder<String, URI> lb = MutableMap.<String, URI>builder() .put("self", selfUri) .put("application", applicationUri) .put("entity", entityUri) .put("action:json", selfUri); if (sensor instanceof AttributeSensor) { Iterable<RendererHints.NamedAction> hints = Iterables.filter(RendererHints.getHintsFor((AttributeSensor<?>)sensor), RendererHints.NamedAction.class); for (RendererHints.NamedAction na : hints) addNamedAction(lb, na , entity, sensor); } return new SensorSummary(sensor.getName(), sensor.getTypeName(), sensor.getDescription(), lb.build()); }
@Test public void testWeightedAverage() { averager.onEvent(intSensor.newEvent(producer, 10), 1000); averager.onEvent(intSensor.newEvent(producer, 20), 1100); averager.onEvent(intSensor.newEvent(producer, 30), 1300); averager.onEvent(intSensor.newEvent(producer, 40), 1600); averager.onEvent(intSensor.newEvent(producer, 50), 2000); average = averager.getAverage(2000); assertEquals(average.value, (20*0.1d)+(30*0.2d)+(40*0.3d)+(50*0.4d)); assertEquals(average.confidence, 1d); }
@SuppressWarnings({ "rawtypes", "unchecked" }) protected Function<? super Collection<?>, ?> lookupTransformation(String t1) { //return MathAggregatorFunctions.computingAverage( TypeToken<U> targetType = targetSensor.getTypeToken(); Function<?, ?> result; if ("average".equalsIgnoreCase(t1)) { result = MathAggregatorFunctions.computingAverage(null, null, (TypeToken<? extends Number>) targetType); } else if ("sum".equalsIgnoreCase(t1)) { result = MathAggregatorFunctions.computingSum(null, null, (TypeToken<? extends Number>) targetType); } else if ("min".equalsIgnoreCase(t1)) { result = MathAggregatorFunctions.computingMin(null, null, (TypeToken<? extends Number>) targetType); } else if ("max".equalsIgnoreCase(t1)) { result = MathAggregatorFunctions.computingMax(null, null, (TypeToken<? extends Number>) targetType); } else if ("isQuorate".equalsIgnoreCase(t1)) { result = new Enrichers.ComputingIsQuorate(targetType, QuorumChecks.of(config().get(QUORUM_CHECK_TYPE)), config().get(QUORUM_TOTAL_SIZE)); } else if ("list".equalsIgnoreCase(t1)) { result = new ComputingList(); } else if ("first".equalsIgnoreCase(t1)) { result = new FirstOrNull(); } else { result = null; } return (Function<? super Collection<?>, ?>) result; }
@SuppressWarnings("unchecked") private U resolveImmediately(Object rawVal, Sensor<U> targetSensor) { if (rawVal == Entities.UNCHANGED || rawVal == Entities.REMOVE) { // If it's a special marker-object, then don't try to transform it return (U) rawVal; } // evaluate immediately, or return null. // For vals that implement ImmediateSupplier, we'll use that to get the value // (or Maybe.absent) without blocking. // Otherwise, the Tasks.resolving will give it its best shot at resolving without // blocking on external events (such as waiting for another entity's sensor). return (U) Tasks.resolving(rawVal).as(targetSensor.getType()) .context(entity) .description("Computing sensor "+targetSensor+" from "+rawVal) .immediately(true) .getMaybe().orNull(); } }
@Override public boolean apply(Sensor<?> other) { return this.sensor.equals(other.getName()); } }
@Test public void testMultipleValueAverage() { averager.onEvent(intSensor.newEvent(producer, 10)); averager.onEvent(intSensor.newEvent(producer, 20)); averager.onEvent(intSensor.newEvent(producer, 30)); averager.onEvent(intSensor.newEvent(producer, 40)); assertEquals(averager.getAverage(), (10+20+30+40)/4d); }
@Override protected <T> void emit(Sensor<T> sensor, Object val) { checkState(entity != null, "entity must first be set"); if (val == Entities.UNCHANGED) { return; } if (val == Entities.REMOVE) { ((EntityInternal)entity).sensors().remove((AttributeSensor<T>) sensor); return; } T newVal = TypeCoercions.coerce(val, sensor.getTypeToken()); Maybe<T> published = Maybe.of(newVal); if (sensor instanceof AttributeSensor) { AttributeSensor<T> attribute = (AttributeSensor<T>)sensor; if (Boolean.TRUE.equals(suppressDuplicates)) { DeduplicatingAttributeModifier<T> modifier = DeduplicatingAttributeModifier.create(newVal); entity.sensors().modify(attribute, modifier); published = modifier.getLastValue(); } else { entity.sensors().set(attribute, newVal); } } else { entity.sensors().emit(sensor, newVal); } if (published!=null && published.isPresent()) { highlightActionPublishSensor(sensor, published.get()); } }
@Override public boolean apply(Sensor<?> input) { return input != null && namePredicate.apply(input.getName()); } };
@Override public synchronized Set<SubscriptionHandle> getSubscriptionsForEntitySensor(Entity source, Sensor<?> sensor) { if (useDelegateForSubscribing) return delegate.getSubscriptionsForEntitySensor(source, sensor); Set<SubscriptionHandle> result = new LinkedHashSet<SubscriptionHandle>(); for (QueuedSubscription q: queuedSubscriptions) { // just match based on sensor name to facilitate yaml // (if subscription has no sensor, that means subscribe to all sensors) if ((q.s.sensor==null || (sensor!=null && Objects.equal(q.s.sensor.getName(), sensor.getName()))) && (q.s.producer==null || Objects.equal(q.s.producer, source))) result.add(q.s); } return result; }