java/sql-dk/src/info/globalcode/sql/dk/formatting/BarChartFormatter.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 06 Sep 2015 21:48:54 +0200
branchv_0
changeset 226 b40153eb7716
parent 224 36db9fd27436
child 228 3787c999d12c
permissions -rw-r--r--
BarChartFormatter: format null as an empty/missing bar + better logging
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
224
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * SQL-DK
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2015 František Kučera (frantovo.cz)
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 * (at your option) any later version.
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 *
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 * GNU General Public License for more details.
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 *
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
 */
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
package info.globalcode.sql.dk.formatting;
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
import info.globalcode.sql.dk.Functions;
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
import info.globalcode.sql.dk.configuration.PropertyDeclaration;
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
import info.globalcode.sql.dk.logging.LoggerProducer;
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
import java.math.BigDecimal;
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
import java.math.MathContext;
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
import java.math.RoundingMode;
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
import java.util.List;
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
import java.util.logging.Level;
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
import java.util.logging.Logger;
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
/**
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
 *
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
 * @author Ing. František Kučera (frantovo.cz)
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
 */
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
@PropertyDeclaration(name = BarChartFormatter.PROPERTY_PRECISION, type = Integer.class, defaultValue = BarChartFormatter.PROPERTY_PRECISION_DEFAULT, description = "number of characters representing 100 % in the bar chart")
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
public class BarChartFormatter extends TabularPrefetchingFormatter {
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
	public static final String NAME = "barchart"; // bash-completion:formatter
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	public static final String PROPERTY_PRECISION = "precision";
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
	protected static final String PROPERTY_PRECISION_DEFAULT = "100";
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	private static final MathContext mathContext = MathContext.DECIMAL128;
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	public static final Logger log = LoggerProducer.getLogger();
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
	private final BigDecimal chartPrecision;
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	private final char chartFull;
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
	private final char chartEmpty;
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	public BarChartFormatter(FormatterContext formatterContext) {
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
		super(formatterContext);
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
		chartPrecision = BigDecimal.valueOf(formatterContext.getProperties().getInteger(PROPERTY_PRECISION, Integer.parseInt(PROPERTY_PRECISION_DEFAULT)));
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
		chartFull = isAsciiNostalgia() ? '#' : '█';
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
		chartEmpty = isAsciiNostalgia() ? '~' : '░';
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
		// TODO: consider using partial blocks for more precision: https://en.wikipedia.org/wiki/Block_Elements
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
	}
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	@Override
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	protected void postprocessPrefetchedResultSet(ColumnsHeader currentHeader, List<Object[]> currentResultSet) {
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
		super.postprocessPrefetchedResultSet(currentHeader, currentResultSet);
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
		updateColumnWidth(currentHeader.getColumnCount(), chartPrecision.intValue());
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
		BigDecimal maximum = BigDecimal.ZERO;
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
		BigDecimal minimum = BigDecimal.ZERO;
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
		int lastIndex = currentHeader.getColumnCount() - 1;
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
226
b40153eb7716 BarChartFormatter: format null as an empty/missing bar + better logging
František Kučera <franta-hg@frantovo.cz>
parents: 224
diff changeset
    64
		Object valueObject = null;
224
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
		try {
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
			for (Object[] row : currentResultSet) {
226
b40153eb7716 BarChartFormatter: format null as an empty/missing bar + better logging
František Kučera <franta-hg@frantovo.cz>
parents: 224
diff changeset
    67
				valueObject = row[lastIndex];
224
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
				if (valueObject != null) {
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
					BigDecimal value = new BigDecimal(valueObject.toString());
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
					maximum = maximum.max(value);
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
					minimum = minimum.min(value);
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
				}
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
			}
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
			BigDecimal range = maximum.subtract(minimum);
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
			for (Object[] row : currentResultSet) {
226
b40153eb7716 BarChartFormatter: format null as an empty/missing bar + better logging
František Kučera <franta-hg@frantovo.cz>
parents: 224
diff changeset
    78
				valueObject = row[lastIndex];
b40153eb7716 BarChartFormatter: format null as an empty/missing bar + better logging
František Kučera <franta-hg@frantovo.cz>
parents: 224
diff changeset
    79
				if (valueObject == null) {
b40153eb7716 BarChartFormatter: format null as an empty/missing bar + better logging
František Kučera <franta-hg@frantovo.cz>
parents: 224
diff changeset
    80
					row[lastIndex] = "";
b40153eb7716 BarChartFormatter: format null as an empty/missing bar + better logging
František Kučera <franta-hg@frantovo.cz>
parents: 224
diff changeset
    81
				} else {
224
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
					BigDecimal value = new BigDecimal(valueObject.toString());
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
					BigDecimal valueFromMinimum = value.subtract(minimum);
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
					BigDecimal points = chartPrecision.divide(range, mathContext).multiply(valueFromMinimum, mathContext);
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
					int pointsRounded = points.setScale(0, RoundingMode.HALF_UP).intValue();
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
					row[lastIndex] = Functions.repeat(chartFull, pointsRounded) + Functions.repeat(chartEmpty, chartPrecision.intValue() - pointsRounded);
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
				}
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
			}
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
		} catch (NumberFormatException e) {
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
			// https://en.wiktionary.org/wiki/parsable
226
b40153eb7716 BarChartFormatter: format null as an empty/missing bar + better logging
František Kučera <franta-hg@frantovo.cz>
parents: 224
diff changeset
    93
			log.log(Level.SEVERE, "Last column must be number or an object with toString() value parsable to a number. But was „{0}“", valueObject);
224
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
			// FIXME: throw FormatterException
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
			throw e;
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
		}
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
	}
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
36db9fd27436 BarChartFormatter: first version
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
}