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

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

package org.whitesource.statistics;

import org.apache.commons.lang.time.DurationFormatUtils;
import org.apache.commons.lang.time.StopWatch;
import org.slf4j.Logger;
import org.whitesource.statistics.StatisticsTypes.ScanOriginStatistics;
import org.whitesource.utils.Constants;
import org.whitesource.utils.Prints.PrintUtils;
import org.whitesource.utils.logger.LoggerFactory;

import java.io.Serializable;
import java.util.Collection;
import java.util.LinkedList;

/**
 * @author chen.luigi
 * Statistics map to StepInfo (agents api)
 */
public abstract class Statistics implements Serializable {

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

    private final Logger logger = LoggerFactory.getLogger(Statistics.class);

    private static final long serialVersionUID = -5972838738131528661L;
    private static final String START = "Start: ";
    private static final String END = "End: ";
    private static final int NUM_OF_LONG_SEPARATOR = 72;
    private static final int NUM_OF_SHORT_SEPARATOR = 20;
    private static final int COMPLETION_STATUS_COLUMN = 42;
    public static final String DEFAULT_COMMENTS = "--------";

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

    private StopWatch stopWatch;
    private String stepName;
    private CompletionStatus completionStatus;
    private boolean isSubStatistics;
    private boolean printToLog;
    private Collection subStatistics;
    private long elapsedTime;

    /* --- Protected Members --- */

    protected String comments;

    /* --- Abstract  --- */

    public abstract void createComments();

    /* --- Constructors --- */

    public Statistics(String stepName, boolean isSubStatistics, boolean printToLog) {
        this.stepName = stepName;
        this.isSubStatistics = isSubStatistics;
        this.comments = DEFAULT_COMMENTS;
        this.printToLog = printToLog;
        this.stopWatch = new StopWatch();
        this.subStatistics = new LinkedList<>();
    }

    /* --- Overridden Methods --- */

    @Override
    public String toString() {
        int stepMargin = !isSubStatistics ? COMPLETION_STATUS_COLUMN - stepName.length() :
                COMPLETION_STATUS_COLUMN - stepName.length() - SummaryStatistics.TAP_SPACE.length();
        int elapsedTimeMargin = 0;
        String elapsedTime = getElapsedTime() != 0 ? DurationFormatUtils.formatDuration(getElapsedTime(), SummaryStatistics.ELAPSED_RUNNING_TIME_FORMAT)
                : PrintUtils.printSpaces(2) + DEFAULT_COMMENTS + PrintUtils.printSpaces(2);

        if (comments.equals(DEFAULT_COMMENTS)) {
            elapsedTimeMargin = 8;
        }

        return stepName + PrintUtils.printSpaces(stepMargin - 2) +
                completionStatus.toString() + PrintUtils.printSpaces(SummaryStatistics.COLUMNS_TAB_SPACE - completionStatus.toString().length() + 12) +
                elapsedTime + PrintUtils.printSpaces(SummaryStatistics.COLUMNS_TAB_SPACE - String.valueOf(stopWatch.toString()).length() + elapsedTimeMargin) +
                comments;
    }

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

    public StopWatch getStopWatch() {
        return stopWatch;
    }

    public void setStopWatch(StopWatch stopWatch) {
        this.stopWatch = stopWatch;
    }

    public void setStepName(String stepName) {
        this.stepName = stepName;
    }

    public String getStepName() {
        return stepName;
    }

    public CompletionStatus getCompletionStatus() {
        return completionStatus;
    }

    public void setCompletionStatus(CompletionStatus completionStatus) {
        this.completionStatus = completionStatus;
    }

    public void setComments(String comments) {
        this.comments = comments;
    }

    public String getComments() {
        return comments;
    }

    public boolean isSubStatistics() {
        return isSubStatistics;
    }

    public void setSubStatistics(boolean subStatistics) {
        isSubStatistics = subStatistics;
    }

    public boolean isPrintToLog() {
        return printToLog;
    }

    public void setPrintToLog(boolean printToLog) {
        this.printToLog = printToLog;
    }

    public Collection getSubStatistics() {
        return subStatistics;
    }

    public void setSubStatistics(Collection subStatistics) {
        this.subStatistics = subStatistics;
    }

    public long getElapsedTime() {
        return elapsedTime;
    }

    public void setElapsedTime(long elapsedTime) {
        this.elapsedTime = elapsedTime;
    }

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

    public void getStartStatisticLog() {
        this.stopWatch.start();
        if (printToLog) {
            this.logger.info(PrintUtils.getStatisticFirstLastSection(START + stepName, NUM_OF_LONG_SEPARATOR, NUM_OF_SHORT_SEPARATOR, Constants.DASH));
        }
    }

    public void getEndStatisticLog(CompletionStatus completionStatus) {
        stopWatch.stop();
        if (elapsedTime == 0) {
            this.elapsedTime = stopWatch.getTime();
        }
        this.completionStatus = completionStatus;
        if (printToLog) {
            this.logger.info(PrintUtils.getStatisticFirstLastSection(END + stepName, NUM_OF_LONG_SEPARATOR, NUM_OF_SHORT_SEPARATOR, Constants.DASH));
        }
        if (!isSubStatistics) {
            if (!(this instanceof ScanOriginStatistics)) {
                SummaryStatistics.getInstance().getStatisticsList().add(this);
                long totalElapsedTime = SummaryStatistics.getInstance().getTotalElapsedTime() + this.getElapsedTime();
                SummaryStatistics.getInstance().setTotalElapsedTime(totalElapsedTime);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy