View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.monitoring.reporting.web.plugin.report.format;
18  
19  import org.apache.commons.monitoring.counters.Counter;
20  import org.apache.commons.monitoring.counters.Unit;
21  import org.apache.commons.monitoring.repositories.Repository;
22  
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.Map;
26  import java.util.concurrent.CopyOnWriteArrayList;
27  
28  public abstract class MapFormat {
29      protected static final Collection<String> ATTRIBUTES_ORDERED_LIST = buildMetricDataHeader();
30  
31      protected static Collection<String> buildMetricDataHeader() {
32          final Collection<String> list = new CopyOnWriteArrayList<String>();
33          list.add("Counter");
34          list.add("Role");
35          for (final MetricData md : MetricData.values()) {
36              list.add(md.name());
37          }
38          return list;
39      }
40  
41      protected static Unit timeUnit(final Map<String, ?> params) {
42          final Object u = params.get("unit");
43          if (u != null) {
44              if (String.class.isInstance(u)) {
45                  final Unit unit = Unit.get(String.class.cast(u).toLowerCase());
46                  if (unit != null) {
47                      return unit;
48                  }
49              }
50              if (String[].class.isInstance(u)) {
51                  final String[] array = String[].class.cast(u);
52                  if (array.length > 0) {
53                      final Unit unit = Unit.get(array[0].toLowerCase());
54                      if (unit != null) {
55                          return unit;
56                      }
57                  }
58              }
59          }
60          return Unit.Time.MILLISECOND;
61      }
62  
63      protected static Collection<Collection<String>> snapshot(final Unit timeUnit) {
64          final Collection<Collection<String>> data = new ArrayList<Collection<String>>();
65          for (final Counter counter : Repository.INSTANCE) {
66              final Unit counterUnit = counter.getKey().getRole().getUnit();
67              final boolean compatible = timeUnit.isCompatible(counterUnit);
68  
69              final Collection<String> line = new ArrayList<String>();
70              data.add(line);
71  
72              line.add(counter.getKey().getName());
73  
74              if (compatible) {
75                  line.add(counter.getKey().getRole().getName() + " (" + timeUnit.getName() + ")");
76              } else {
77                  line.add(counter.getKey().getRole().getName() + " (" + counterUnit.getName() + ")");
78              }
79  
80              for (final MetricData md : MetricData.values()) {
81                  double value = md.value(counter);
82                  if (md.isTime() && compatible && timeUnit != counterUnit) {
83                      value = timeUnit.convert(value, counterUnit);
84                  }
85                  line.add(Double.toString(value));
86              }
87          }
88          return data;
89      }
90  }