protected Number convertToNumber(Object o) { if (o == null) { return null; } else if (o instanceof MutableDouble || o instanceof MutableLong) { return (Number)o; } else if (o instanceof Double || o instanceof Float) { return new MutableDouble((Number)o); } else if (o instanceof Number) { return new MutableLong((Number)o); } else { return new MutableDouble(o.toString()); } }
protected Number convertToNumber(Object o) { if (o == null) { return null; } else if (o instanceof MutableDouble || o instanceof MutableLong) { return (Number)o; } else if (o instanceof Double || o instanceof Float) { return new MutableDouble((Number)o); } else if (o instanceof Number) { return new MutableLong((Number)o); } else { return new MutableDouble(o.toString()); } }
public SimpleMovingAverageObject() { sum = new MutableDouble(0); count = new MutableInt(0); }
@Override public void process(String timeBucket, String key, String field, Number value) { String finalKey = timeBucket + "|" + key; Map<String, Number> m = dataMap.get(finalKey); if (value == null) { return; } if (m == null) { m = new HashMap<String, Number>(); m.put(field, new MutableDouble(value)); dataMap.put(finalKey, m); } else { Number n = m.get(field); if (n == null) { m.put(field, new MutableDouble(value)); } else { ((MutableDouble)n).add(value); } } }
/** * Adds the value for each key. * @param tuple * @param map */ public void addTuple(Map<K, V> tuple, Map<K, MutableDouble> map) { for (Map.Entry<K, V> e: tuple.entrySet()) { if (!doprocessKey(e.getKey()) || (e.getValue() == null)) { continue; } MutableDouble val = map.get(e.getKey()); if (val == null) { val = new MutableDouble(0.0); map.put(cloneKey(e.getKey()), val); } val.add(e.getValue().doubleValue()); } }
@Override public void endWindow() { int totalWindowsOccupied = cacheOject.size(); for (Map.Entry<String, Map<String, KeyValPair<MutableDouble, Integer>>> e : outputMap.entrySet()) { for (Map.Entry<String, KeyValPair<MutableDouble, Integer>> dimensionValObj : e.getValue().entrySet()) { Map<String, DimensionObject<String>> outputData = new HashMap<String, DimensionObject<String>>(); KeyValPair<MutableDouble, Integer> keyVal = dimensionValObj.getValue(); if (operationType == AggregateOperation.SUM) { outputData.put(e.getKey(), new DimensionObject<String>(keyVal.getKey(), dimensionValObj.getKey())); } else if (operationType == AggregateOperation.AVERAGE) { if (keyVal.getValue() != 0) { double totalCount = ((double)(totalWindowsOccupied * applicationWindowSize)) / 1000; outputData.put(e.getKey(), new DimensionObject<String>(new MutableDouble(keyVal.getKey().doubleValue() / totalCount), dimensionValObj.getKey())); } } if (!outputData.isEmpty()) { output.emit(outputData); } } } currentWindow = (currentWindow + 1) % windowSize; }
@Override public void process(Map<String, DimensionObject<String>> tuple) { for (Map.Entry<String, DimensionObject<String>> e : tuple.entrySet()) { Map<String, MutableDouble> obj = dataMap.get(e.getKey()); DimensionObject<String> eObj = e.getValue(); if (obj == null) { obj = new HashMap<String, MutableDouble>(); obj.put(eObj.getVal(), new MutableDouble(eObj.getCount())); dataMap.put(e.getKey(), obj); } else { MutableDouble n = obj.get(eObj.getVal()); if (n == null) { obj.put(eObj.getVal(), new MutableDouble(eObj.getCount())); } else { n.add(eObj.getCount()); } } } } };
if (val == null) { if (countkey) { val = new MutableDouble(1.00); } else { val = new MutableDouble(value.doubleValue());
if (val == null) { if (countkey) { val = new MutableDouble(1.00); } else { val = new MutableDouble(value.doubleValue());
@Override public void endWindow() { for (AverageAlertData data : alerts) { try { avgAlertOutputPort.emit(JsonUtils.toJson(data)); } catch (IOException e) { logger.warn("Exception while converting object to JSON", e); } } alerts.clear(); for (Map.Entry<MerchantKey, MutableDouble> entry : currentSMAMap.entrySet()) { MerchantKey key = entry.getKey(); MutableDouble currentSma = entry.getValue(); MutableDouble lastSma = lastSMAMap.get(key); if (lastSma == null) { lastSma = new MutableDouble(currentSma.doubleValue()); lastSMAMap.put(key, lastSma); } else { lastSma.setValue(currentSma.getValue()); } } }
MutableDouble nval = numerators.get(e.getKey()); if (nval == null) { nval = new MutableDouble(0.0); } else {
/** * For each tuple (a key value pair) Adds the values for each key. */ @Override public void process(KeyValPair<K, V> tuple) { K key = tuple.getKey(); if (!doprocessKey(key)) { return; } SumEntry val = sums.get(key); if (val == null) { val = new SumEntry(new MutableDouble(tuple.getValue().doubleValue()), true); } else { val.sum.add(tuple.getValue().doubleValue()); val.changed = true; } sums.put(cloneKey(key), val); }
/** * Used to accumulate store metrics across multiple regions in a region * server. These metrics are not "persistent", i.e. we keep overriding them * on every update instead of incrementing, so we need to accumulate them in * a temporary map before pushing them to the global metric collection. * @param tmpMap a temporary map for accumulating store metrics * @param storeMetricType the store metric type to increment * @param val the value to add to the metric */ public void accumulateStoreMetric(final Map<String, MutableDouble> tmpMap, StoreMetricType storeMetricType, double val) { final String key = getStoreMetricName(storeMetricType); if (tmpMap.get(key) == null) { tmpMap.put(key, new MutableDouble(val)); } else { tmpMap.get(key).add(val); } if (this == ALL_SCHEMA_METRICS) { // also compute the max value across all Stores on this server final String maxKey = getStoreMetricNameMax(storeMetricType); MutableDouble cur = tmpMap.get(maxKey); if (cur == null) { tmpMap.put(maxKey, new MutableDouble(val)); } else if (cur.doubleValue() < val) { cur.setValue(val); } } else { ALL_SCHEMA_METRICS.accumulateStoreMetric(tmpMap, storeMetricType, val); } }
@Override public void process(KeyValPair<MerchantKey, Double> tuple) { MutableDouble currentSma = currentSMAMap.get(tuple.getKey()); if (currentSma == null) { // first sma for the given key double sma = tuple.getValue(); currentSMAMap.put(tuple.getKey(), new MutableDouble(sma)); //lastSMAMap.put(tuple.getKey(), new MutableDouble(sma)); } else { // move the current SMA value to the last SMA Map //lastSMAMap.get(tuple.getKey()).setValue(currentSma.getValue()); currentSma.setValue(tuple.getValue()); // update the current SMA value } }
MutableDouble nval = numerators.get(key); if (nval == null) { nval = new MutableDouble(0.0); } else {
/** * Process each key to store the value. If same key appears again update * with latest value. */ @Override public void process(KeyValPair<K, V> tuple) { if (tuple.getValue().doubleValue() != 0.0) { // Avoid divide by zero, Emit // an error tuple? MutableDouble val = basemap.get(tuple.getKey()); if (val == null) { val = new MutableDouble(0.0); basemap.put(cloneKey(tuple.getKey()), val); } val.setValue(tuple.getValue().doubleValue()); } } };
/** * Process each key to store the value. If same key appears again update * with latest value. */ @Override public void process(KeyValPair<K, V> tuple) { if (tuple.getValue().doubleValue() != 0.0) { // Avoid divide by zero, Emit // an error tuple? MutableDouble val = basemap.get(tuple.getKey()); if (val == null) { val = new MutableDouble(0.0); basemap.put(cloneKey(tuple.getKey()), val); } val.setValue(tuple.getValue().doubleValue()); } } };
/** * Adds the value for each key. * * @param tuple * @param map */ public void addTuple(KeyValPair<K, V> tuple, Map<K, MutableDouble> map) { K key = tuple.getKey(); if (!doprocessKey(key) || (tuple.getValue() == null)) { return; } MutableDouble val = map.get(key); if (val == null) { val = new MutableDouble(0.0); map.put(cloneKey(key), val); } val.add(tuple.getValue().doubleValue()); }
/** * Adds the values for each key, counts the number of occurrences of each * key and computes the average. */ @Override public void process(KeyValPair<K, ? extends Number> tuple) { K key = tuple.getKey(); if (!doprocessKey(key)) { return; } MutableDouble val = sums.get(key); if (val == null) { val = new MutableDouble(tuple.getValue().doubleValue()); } else { val.add(tuple.getValue().doubleValue()); } sums.put(cloneKey(key), val); MutableLong count = counts.get(key); if (count == null) { count = new MutableLong(0); counts.put(cloneKey(key), count); } count.increment(); } };
val = new MutableDouble(tval); basemap.put(cloneKey(key), val); return;