001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.monitoring.reporting.web.plugin.report.format;
018    
019    import org.apache.commons.monitoring.counters.Counter;
020    import org.apache.commons.monitoring.counters.Unit;
021    import org.apache.commons.monitoring.repositories.Repository;
022    
023    import java.util.ArrayList;
024    import java.util.Collection;
025    import java.util.Map;
026    import java.util.concurrent.CopyOnWriteArrayList;
027    
028    public abstract class MapFormat {
029        protected static final Collection<String> ATTRIBUTES_ORDERED_LIST = buildMetricDataHeader();
030    
031        protected static Collection<String> buildMetricDataHeader() {
032            final Collection<String> list = new CopyOnWriteArrayList<String>();
033            list.add("Counter");
034            list.add("Role");
035            for (final MetricData md : MetricData.values()) {
036                list.add(md.name());
037            }
038            return list;
039        }
040    
041        protected static Unit timeUnit(final Map<String, ?> params) {
042            final Object u = params.get("unit");
043            if (u != null) {
044                if (String.class.isInstance(u)) {
045                    final Unit unit = Unit.get(String.class.cast(u).toLowerCase());
046                    if (unit != null) {
047                        return unit;
048                    }
049                }
050                if (String[].class.isInstance(u)) {
051                    final String[] array = String[].class.cast(u);
052                    if (array.length > 0) {
053                        final Unit unit = Unit.get(array[0].toLowerCase());
054                        if (unit != null) {
055                            return unit;
056                        }
057                    }
058                }
059            }
060            return Unit.Time.MILLISECOND;
061        }
062    
063        protected static Collection<Collection<String>> snapshot(final Unit timeUnit) {
064            final Collection<Collection<String>> data = new ArrayList<Collection<String>>();
065            for (final Counter counter : Repository.INSTANCE) {
066                final Unit counterUnit = counter.getKey().getRole().getUnit();
067                final boolean compatible = timeUnit.isCompatible(counterUnit);
068    
069                final Collection<String> line = new ArrayList<String>();
070                data.add(line);
071    
072                line.add(counter.getKey().getName());
073    
074                if (compatible) {
075                    line.add(counter.getKey().getRole().getName() + " (" + timeUnit.getName() + ")");
076                } else {
077                    line.add(counter.getKey().getRole().getName() + " (" + counterUnit.getName() + ")");
078                }
079    
080                for (final MetricData md : MetricData.values()) {
081                    double value = md.value(counter);
082                    if (md.isTime() && compatible && timeUnit != counterUnit) {
083                        value = timeUnit.convert(value, counterUnit);
084                    }
085                    line.add(Double.toString(value));
086                }
087            }
088            return data;
089        }
090    }