All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.whitesource.statistics.SummaryStatistics Maven / Gradle / Ivy

package org.whitesource.statistics;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DurationFormatUtils;
import org.whitesource.statistics.StatisticsTypes.ScanOriginStatistics;
import org.whitesource.utils.Constants;
import org.whitesource.utils.Prints.PrintUtils;
import org.whitesource.utils.StatusCode;

import java.util.LinkedList;

/**
 * @author chen.luigi
 */
public final class SummaryStatistics {

    /* --- Static Members --- */

    private static final SummaryStatistics instance = new SummaryStatistics();
    private static final String WHITESOURCE_SCAN_SUMMARY = "WhiteSource Scan Summary:";
    private static final String STEP = "Step";
    private static final String COMPLETION_STATUS = "Completion Status";
    private static final String ELAPSED = "Elapsed";
    private static final String COMMENTS = "Comments";
    private static final String ELAPSED_RUNNING_TIME = "Elapsed running time:";
    private static final String SCAN_ORIGIN = "Scan Origin: ";
    public static final String TAP_SPACE = "   ";

    // number of long separator
    private static final int NUM_OF_LONG_SEPARATOR = 150;

    // number of short separator
    private static final int NUM_OF_SHORT_SEPARATOR = 61;

    // divide the length of separator by the number of summary columns
    public static final int COLUMNS_TAB_SPACE = NUM_OF_LONG_SEPARATOR / 5;

    // divide the length of separator by the number of summary columns + extra 3 spaces
    private static final int STEP_TAB_SPACE = (NUM_OF_LONG_SEPARATOR / 5) + 3;

    public static final String ELAPSED_RUNNING_TIME_FORMAT = "HH:mm:ss.SS";

    /* --- Private Members --- */

    private String scanOriginName;
    private LinkedList statisticsList;
    private long totalElapsedTime;

    /* --- Constructors --- */

    public SummaryStatistics() {
        statisticsList = new LinkedList<>();
    }

    /* --- Getters / Setters --- */

    public static SummaryStatistics getInstance() {
        return instance;
    }

    public LinkedList getStatisticsList() {
        return statisticsList;
    }

    public void setStatisticsList(LinkedList statisticsList) {
        this.statisticsList = statisticsList;
    }

    public long getTotalElapsedTime() {
        return totalElapsedTime;
    }

    public void setTotalElapsedTime(long totalElapsedTime) {
        this.totalElapsedTime = totalElapsedTime;
    }

    public String getScanOriginName() {
        return scanOriginName;
    }

    public void setScanOriginName(String scanOriginName) {
        this.scanOriginName = scanOriginName;
    }

    /* --- Public Methods --- */

    // print statistics summary
    public String printSummary(StatusCode processExitCode) {
        return PrintUtils.getStatisticFirstLastSection(WHITESOURCE_SCAN_SUMMARY, NUM_OF_LONG_SEPARATOR, NUM_OF_SHORT_SEPARATOR, Constants.DASH) +
                Constants.DOWN_LINE +
                PrintUtils.getSeparator(NUM_OF_LONG_SEPARATOR, Constants.EQUALS) +
                Constants.DOWN_LINE +
                printScanOriginStatistics() +
                Constants.DOWN_LINE +
                PrintUtils.getSeparator(NUM_OF_LONG_SEPARATOR, Constants.EQUALS) +
                Constants.DOWN_LINE +
                getColumnsName() +
                Constants.DOWN_LINE +
                PrintUtils.getSeparator(NUM_OF_LONG_SEPARATOR, Constants.EQUALS) +
                Constants.DOWN_LINE +
                getSummaryInfo() +
                Constants.DOWN_LINE +
                PrintUtils.getSeparator(NUM_OF_LONG_SEPARATOR, Constants.EQUALS) +
                Constants.DOWN_LINE +
                ELAPSED_RUNNING_TIME +
                PrintUtils.printSpaces(82 - ELAPSED_RUNNING_TIME.length()) +
                DurationFormatUtils.formatDuration(totalElapsedTime, ELAPSED_RUNNING_TIME_FORMAT) +
                Constants.DOWN_LINE +
                PrintUtils.getSeparator(NUM_OF_LONG_SEPARATOR, Constants.EQUALS) +
                Constants.DOWN_LINE +
                "Process finished with exit code " + processExitCode.name() + " (" + processExitCode.getValue() + ")";
    }

    private String printScanOriginStatistics() {
        if (StringUtils.isNotBlank(scanOriginName)) {
            return SCAN_ORIGIN + scanOriginName;
        } else {
            return Constants.EMPTY_STRING;
        }
    }

    /* create all the summary info table by getting all the statistics from map */
    private String getSummaryInfo() {
        StringBuilder stringBuilder = new StringBuilder();
        for (Statistics statistics : statisticsList) {
            // scan origin statistics will join to the statistics list only when there is a
            // clear priority and clear combinations of the scan origins
            if (!(statistics instanceof ScanOriginStatistics)) {
                statistics.createComments();
                if (!statistics.getSubStatistics().isEmpty()) {
                    stringBuilder.append(statistics.toString());
                    stringBuilder.append(Constants.DOWN_LINE);
                    for (Statistics subStatistics : statistics.getSubStatistics()) {
                        subStatistics.createComments();
                        stringBuilder.append(TAP_SPACE);
                        stringBuilder.append(subStatistics.toString());
                        stringBuilder.append(Constants.DOWN_LINE);
                    }
                } else {
                    stringBuilder.append(statistics.toString());
                    stringBuilder.append(Constants.DOWN_LINE);
                }
            }
        }
        return stringBuilder.toString();
    }

    /* create columns of summary table*/
    private String getColumnsName() {
        return STEP +
                PrintUtils.printSpaces(STEP_TAB_SPACE) +
                COMPLETION_STATUS +
                PrintUtils.printSpaces(COLUMNS_TAB_SPACE) +
                ELAPSED +
                PrintUtils.printSpaces(COLUMNS_TAB_SPACE) +
                COMMENTS +
                PrintUtils.printSpaces(COLUMNS_TAB_SPACE);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy