Java Map Merge mergeAggCompStatsTopoPageSpout(Map accSpoutStats, Map spoutStats)

Here you can find the source of mergeAggCompStatsTopoPageSpout(Map accSpoutStats, Map spoutStats)

Description

merge accumulated bolt stats with new bolt stats

License

Apache License

Declaration

public static Map<String, Object> mergeAggCompStatsTopoPageSpout(Map<String, Object> accSpoutStats,
        Map<String, Object> spoutStats) 

Method Source Code

//package com.java2s;
/*//from   ww w .j a v a  2  s . c o  m
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.HashMap;

import java.util.Map;

public class Main {
    private static final String NUM_TASKS = "num-tasks";
    private static final String NUM_EXECUTORS = "num-executors";
    private static final String ACKED = "acked";
    private static final String FAILED = "failed";
    private static final String EMITTED = "emitted";
    private static final String TRANSFERRED = "transferred";
    private static final String COMP_LAT_TOTAL = "completeLatencyTotal";

    /**
     * merge accumulated bolt stats with new bolt stats
     */
    public static Map<String, Object> mergeAggCompStatsTopoPageSpout(Map<String, Object> accSpoutStats,
            Map<String, Object> spoutStats) {
        Map<String, Object> ret = new HashMap<>();

        Integer numExecutors = getByKeyOr0(accSpoutStats, NUM_EXECUTORS).intValue();
        putKV(ret, NUM_EXECUTORS, numExecutors + 1);
        putKV(ret, NUM_TASKS, sumOr0(getByKeyOr0(accSpoutStats, NUM_TASKS), getByKeyOr0(spoutStats, NUM_TASKS)));
        putKV(ret, EMITTED, sumOr0(getByKeyOr0(accSpoutStats, EMITTED), getByKeyOr0(spoutStats, EMITTED)));
        putKV(ret, TRANSFERRED,
                sumOr0(getByKeyOr0(accSpoutStats, TRANSFERRED), getByKeyOr0(spoutStats, TRANSFERRED)));
        putKV(ret, COMP_LAT_TOTAL,
                sumOr0(getByKeyOr0(accSpoutStats, COMP_LAT_TOTAL), getByKeyOr0(spoutStats, COMP_LAT_TOTAL)));
        putKV(ret, ACKED, sumOr0(getByKeyOr0(accSpoutStats, ACKED), getByKeyOr0(spoutStats, ACKED)));
        putKV(ret, FAILED, sumOr0(getByKeyOr0(accSpoutStats, FAILED), getByKeyOr0(spoutStats, FAILED)));

        return ret;
    }

    private static Number getByKeyOr0(Map m, String k) {
        if (m == null) {
            return 0;
        }

        Number n = (Number) m.get(k);
        if (n == null) {
            return 0;
        }
        return n;
    }

    public static void putKV(Map map, String k, Object v) {
        map.put(k, v);
    }

    private static Number sumOr0(Object a, Object b) {
        if (isValidNumber(a) && isValidNumber(b)) {
            if (a instanceof Long || a instanceof Integer) {
                return ((Number) a).longValue() + ((Number) b).longValue();
            } else {
                return ((Number) a).doubleValue() + ((Number) b).doubleValue();
            }
        }
        return 0;
    }

    /**
     * Returns true if x is a number that is not NaN or Infinity, false otherwise
     */
    private static boolean isValidNumber(Object x) {
        return x != null && x instanceof Number && !Double.isNaN(((Number) x).doubleValue())
                && !Double.isInfinite(((Number) x).doubleValue());
    }
}

Related

  1. merge(Map master, Map copy)
  2. merge(Map src, Map dest)
  3. merge(Map first, Map second)
  4. merge(Map map1, Map map2)
  5. merge(Map mapPriorityLeast, Map mapPriorityFirst)
  6. mergeBootArgumentsIntoMap(String[] args, Map map)
  7. mergeCompactedValue(Map obj, String key, Object value)
  8. mergeCompositions( Map target, Map source)
  9. mergeConfig(Map userConfig, Map referenceConfig)