...
/**
 * Actions queue statistics.
 */
public class ActionsQueueStats extends AbstractStats {

	/**
	 * Stats values definitions.
	 */
	public static enum StatDef {
		/**
		 * Requested actions.
		 */
		REQUESTED_ACTIONS("ReqAct"),
		/**
		 * Requested create actions.
		 */
		REQUESTED_CREATE_ACTIONS("ReqCreAct"),
		/**
		 * Requested update actions.
		 */
		REQUESTED_UPDATE_ACTIONS("ReqUpdAct"),
		/**
		 * Requested delete actions.
		 */
		REQUESTED_DELETE_ACTIONS("ReqDelAct"),
		/**
		 * Enqueued actions.
		 */
		ENQUEUED_ACTIONS("EnqAct"),
		/**
		 * Last (on last request) queue elements count.
		 */
		LAST_QUEUE_ELEMENTS("LastQEls"),
		/**
		 * Min queue elements count on request.
		 */
		MIN_QUEUE_ELEMENTS("MinQEls"),
		/**
		 * Max queue elements count on request.
		 */
		MAX_QUEUE_ELEMENTS("MaxQEls"),
		/**
		 * Average queue elements count on request.
		 */
		AVG_QUEUE_ELEMENTS("AvgQEls"),
		/**
		 * Processed actions.
		 */
		PROCESSED_ACTIONS("ProcAct"),
		/**
		 * Processed actions batches.
		 */
		PROCESSED_BATCHES("ProcBat"),
		/**
		 * Total processing time.
		 */
		PROCESSING_TIME("ProcTime"),
		/**
		 * Min batch processing time.
		 */
		MIN_BATCH_PROCESSING_TIME("MinBProcTime"),
		/**
		 * Max batch processing time.
		 */
		MAX_BATCH_PROCESSING_TIME("MaxBProcTime"),
		/**
		 * Average batch processing time.
		 */
		AVG_BATCH_PROCESSING_TIME("AvgBProcTime"),
		/**
		 * Min batch size.
		 */
		MIN_BATCH_SIZE("MinBSize"),
		/**
		 * Max batch size.
		 */
		MAX_BATCH_SIZE("MaxBSize"),
		/**
		 * Average batch size.
		 */
		AVG_BATCH_SIZE("AvgBSize"),
		/**
		 * Queue overflows count.
		 */
		QUEUE_OVERFLOWS("QFull"),
		/**
		 * Processing errors count.
		 */
		PROCESSING_ERRORS("ProcERR");

		/**
		 * Stats value name.
		 */
		private String statName;

		/**
		 * Constructor.
		 *
		 * @param aStatName stats value name.
		 */
		private StatDef(final String aStatName) {
			statName = aStatName;
		}

		/**
		 * Get stats value name.
		 *
		 * @return the stats value name.
		 */
		public String getStatName() {
			return statName;
		}

		/**
		 * Get stats label for print.
		 *
		 * @return stats label for print.
		 */
		public String getStatLabel() {
			return " " + statName + ": ";
		}

		/**
		 * Get all supported stat values names.
		 *
		 * @return stat values names list.
		 */
		public static List<String> getStatNames() {
			List<String> ret = new ArrayList<String>(StatDef.values().length);
			for (StatDef v : StatDef.values()) {
				ret.add(v.getStatName());
			}
			return ret;
		}
	}

	/**
	 * Min size value.
	 */
	public static final long MIN_SIZE_DEFAULT = Long.MAX_VALUE;
	/**
	 * Max size value.
	 */
	public static final long MAX_SIZE_DEFAULT = 0L;

	/**
	 * Requested actions.
	 */
	private StatValue requestedActions;
	/**
	 * Requested create actions.
	 */
	private StatValue requestedCreateActions;
	/**
	 * Requested update actions.
	 */
	private StatValue requestedUpdateActions;
	/**
	 * Requested delete actions.
	 */
	private StatValue requestedDeleteActions;
	/**
	 * Enqueued actions.
	 */
	private StatValue enqueuedActions;
	/**
	 * Last (on last request) queue elements count.
	 */
	private StatValue lastQueueElements;
	/**
	 * Min queue elements count on request.
	 */
	private StatValue minQueueElements;
	/**
	 * Max queue elements count on request.
	 */
	private StatValue maxQueueElements;
	/**
	 * Queue elements sum. Used to calculate average value.
	 */
	private StatValue queueElementsSum;
	/**
	 * Processed actions.
	 */
	private StatValue processedActions;
	/**
	 * Processed actions batches.
	 */
	private StatValue processedBatches;
	/**
	 * Total processing time.
	 */
	private StatValue processingTime;
	/**
	 * Min batch processing time.
	 */
	private StatValue minBatchProcessingTime;
	/**
	 * Max batch processing time.
	 */
	private StatValue maxBatchProcessingTime;
	/**
	 * Min batch size.
	 */
	private StatValue minBatchSize;
	/**
	 * Max batch size.
	 */
	private StatValue maxBatchSize;
	/**
	 * Queue overflows count.
	 */
	private StatValue queueOverflows;
	/**
	 * Processing processingErrors count.
	 */
	private StatValue processingErrors;

	/**
	 * Name of the stats value.
	 */
	private String name;

	/**
	 * Constructor.
	 *
	 * @param aName stats name.
	 * @param selectedIntervals supported stats intervals.
	 */
	public ActionsQueueStats(final String aName,  final Interval[] selectedIntervals) {
		Long longPattern = Long.valueOf(0);
		name = aName;

		requestedActions = StatValueFactory.createStatValue(longPattern, "requestedActions", selectedIntervals);
		requestedCreateActions = StatValueFactory.createStatValue(longPattern, "requestedCreateActions", selectedIntervals);
		requestedUpdateActions = StatValueFactory.createStatValue(longPattern, "requestedUpdateActions", selectedIntervals);
		requestedDeleteActions = StatValueFactory.createStatValue(longPattern, "requestedDeleteActions", selectedIntervals);
		enqueuedActions = StatValueFactory.createStatValue(longPattern, "enqueuedActions", selectedIntervals);
		lastQueueElements = StatValueFactory.createStatValue(longPattern, "lastQueueElements", selectedIntervals);
		minQueueElements = StatValueFactory.createStatValue(longPattern, "minQueueElements", selectedIntervals);
		minQueueElements.setDefaultValueAsLong(MIN_SIZE_DEFAULT);
		minQueueElements.reset();
		maxQueueElements = StatValueFactory.createStatValue(longPattern, "maxQueueElements", selectedIntervals);
		maxQueueElements.setDefaultValueAsLong(MAX_SIZE_DEFAULT);
		maxQueueElements.reset();
		queueElementsSum = StatValueFactory.createStatValue(longPattern, "queueElementsSum", selectedIntervals);
		processedActions = StatValueFactory.createStatValue(longPattern, "processedActions", selectedIntervals);
		processedBatches = StatValueFactory.createStatValue(longPattern, "processedBatches", selectedIntervals);
		processingTime = StatValueFactory.createStatValue(longPattern, "processingTime", selectedIntervals);
		minBatchProcessingTime = StatValueFactory.createStatValue(longPattern, "minBatchProcessingTime", selectedIntervals);
		minBatchProcessingTime.setDefaultValueAsLong(MIN_SIZE_DEFAULT);
		minBatchProcessingTime.reset();
		maxBatchProcessingTime = StatValueFactory.createStatValue(longPattern, "maxBatchProcessingTime", selectedIntervals);
		maxBatchProcessingTime.setDefaultValueAsLong(MAX_SIZE_DEFAULT);
		maxBatchProcessingTime.reset();
		minBatchSize = StatValueFactory.createStatValue(longPattern, "minBatchSize", selectedIntervals);
		minBatchSize.setDefaultValueAsLong(MIN_SIZE_DEFAULT);
		minBatchSize.reset();
		maxBatchSize = StatValueFactory.createStatValue(longPattern, "maxBatchSize", selectedIntervals);
		maxBatchSize.setDefaultValueAsLong(MAX_SIZE_DEFAULT);
		maxBatchSize.reset();
		queueOverflows = StatValueFactory.createStatValue(longPattern, "queueOverflows", selectedIntervals);
		processingErrors = StatValueFactory.createStatValue(longPattern, "processingErrors", selectedIntervals);
	}

	/* (non-Javadoc)
	 * @see net.java.dev.moskito.core.producers.AbstractStats#getName()
	 */
	@Override
	public final String getName() {
		return name;
	}

	/* (non-Javadoc)
	 * @see net.java.dev.moskito.core.producers.AbstractStats#getAvailableValueNames()
	 */
	@Override
	public final List<String> getAvailableValueNames() {
		return StatDef.getStatNames();
	}

	/**
	 * Add requested (planned to be enqueued) action to stats.
	 *
	 * @param action requested action.
	 * @param aCurrentQueueElements current queue elements count.
	 */
	public final void addRequestedAction(final CMProfileAction action, final long aCurrentQueueElements) {
		requestedActions.increase();

		switch (action.getType()) {
		case INSERT:
			requestedCreateActions.increase();
			break;
		case UPDATE:
			requestedUpdateActions.increase();
			break;
		case DELETE:
			requestedDeleteActions.increase();
			break;
		default:
			// do nothing
		}

		lastQueueElements.setValueAsLong(aCurrentQueueElements);
		minQueueElements.setValueIfLesserThanCurrentAsLong(aCurrentQueueElements);
		maxQueueElements.setValueIfGreaterThanCurrentAsLong(aCurrentQueueElements);
		queueElementsSum.increaseByLong(aCurrentQueueElements);
	}

	/**
	 * Add enqueued action to stats.
	 */
	public final void addEnqueuedAction() {
		enqueuedActions.increase();
	}

	/**
	 * Add processed actions batch to stats.
	 *
	 * @param actions processed actions.
	 * @param aProcessingTime processing time in mills.
	 */
	public final void addProcessedBatch(final List<CMProfileAction> actions, final long aProcessingTime) {
		processedActions.increaseByLong(actions.size());

		processedBatches.increase();
		processingTime.increaseByLong(aProcessingTime);
		minBatchProcessingTime.setValueIfLesserThanCurrentAsLong(aProcessingTime);
		maxBatchProcessingTime.setValueIfGreaterThanCurrentAsLong(aProcessingTime);
		minBatchSize.setValueIfLesserThanCurrentAsLong(actions.size());
		maxBatchSize.setValueIfGreaterThanCurrentAsLong(actions.size());
	}

	/**
	 * Add queue overflow (unrecoverable overflow) to stats.
	 */
	public final void addQueueOverflow() {
		queueOverflows.increase();
	}

	/**
	 * Add processing error to stats.
	 */
	public final void addProcessingError() {
		processingErrors.increase();
	}

	/**
	 * Get average queue elements.
	 *
	 * @param intervalName interval name.
	 * @return The average queue elements. Or 0 if no processed actions.
	 */
	public final long getAvgQueueElements(final String intervalName) {
		long processedActionsValue = processedActions.getValueAsLong(intervalName);
		return processedActionsValue != 0 ? queueElementsSum.getValueAsLong(intervalName) / processedActionsValue : 0;
	}

	/**
	 * Get average batch size stats.
	 *
	 * @param intervalName interval name.
	 * @return The average batch size. Or 0 if no processed batches.
	 */
	public final long getAvgBatchSize(final String intervalName) {
		long processedBatchesValue = processedBatches.getValueAsLong(intervalName);
		return processedBatchesValue != 0 ? processedActions.getValueAsLong(intervalName) / processedBatchesValue : 0;
	}

	/**
	 * Get average processing time stats.
	 *
	 * @param intervalName interval name.
	 * @return The average processing time. Or 0 if no processed batches.
	 */
	public final long getAvgBatchProcessingTime(final String intervalName) {
		long processedBatchesValue = processedBatches.getValueAsLong(intervalName);
		return processedBatchesValue != 0 ? processingTime.getValueAsLong(intervalName) / processedBatchesValue : 0;
	}

	/**
	 * Decorate min stat value.
	 *
	 * @param minValue value to decorate.
	 * @return minValue if it was set and 0 otherwise.
	 */
	public final long decorateMinValue(final long minValue) {
		return minValue < MIN_SIZE_DEFAULT ? minValue : 0;
	}

	@Override
	public final String toStatsString(final String intervalName, final TimeUnit timeUnit) {
		StringBuilder b = new StringBuilder();
		b.append(getName()).append(' ');

		b.append(StatDef.REQUESTED_ACTIONS.getStatLabel()).append(requestedActions.getValueAsLong(intervalName));
		b.append(StatDef.REQUESTED_CREATE_ACTIONS.getStatLabel()).append(requestedCreateActions.getValueAsLong(intervalName));
		b.append(StatDef.REQUESTED_UPDATE_ACTIONS.getStatLabel()).append(requestedUpdateActions.getValueAsLong(intervalName));
		b.append(StatDef.REQUESTED_DELETE_ACTIONS.getStatLabel()).append(requestedDeleteActions.getValueAsLong(intervalName));
		b.append(StatDef.ENQUEUED_ACTIONS.getStatLabel()).append(enqueuedActions.getValueAsLong(intervalName));

		b.append(StatDef.LAST_QUEUE_ELEMENTS.getStatLabel()).append(lastQueueElements.getValueAsLong(intervalName));
		b.append(StatDef.MIN_QUEUE_ELEMENTS.getStatLabel()).append(decorateMinValue(minQueueElements.getValueAsLong(intervalName)));
		b.append(StatDef.MAX_QUEUE_ELEMENTS.getStatLabel()).append(maxQueueElements.getValueAsLong(intervalName));
		b.append(StatDef.AVG_QUEUE_ELEMENTS.getStatLabel()).append(getAvgQueueElements(intervalName));

		b.append(StatDef.PROCESSED_ACTIONS.getStatLabel()).append(processedActions.getValueAsLong(intervalName));
		b.append(StatDef.PROCESSED_BATCHES.getStatLabel()).append(processedBatches.getValueAsLong(intervalName));
		b.append(StatDef.PROCESSING_TIME.getStatLabel()).append(processingTime.getValueAsLong(intervalName));
		b.append(StatDef.MIN_BATCH_PROCESSING_TIME.getStatLabel()).append(decorateMinValue(minBatchProcessingTime.getValueAsLong(intervalName)));
		b.append(StatDef.MAX_BATCH_PROCESSING_TIME.getStatLabel()).append(maxBatchProcessingTime.getValueAsLong(intervalName));
		b.append(StatDef.AVG_BATCH_PROCESSING_TIME.getStatLabel()).append(getAvgBatchProcessingTime(intervalName));
		b.append(StatDef.MIN_BATCH_SIZE.getStatLabel()).append(decorateMinValue(minBatchSize.getValueAsLong(intervalName)));
		b.append(StatDef.MAX_BATCH_SIZE.getStatLabel()).append(maxBatchSize.getValueAsLong(intervalName));
		b.append(StatDef.AVG_BATCH_SIZE.getStatLabel()).append(getAvgBatchSize(intervalName));
		b.append(StatDef.QUEUE_OVERFLOWS.getStatLabel()).append(queueOverflows.getValueAsLong(intervalName));
		b.append(StatDef.PROCESSING_ERRORS.getStatLabel()).append(processingErrors.getValueAsLong(intervalName));

		return b.toString();
	}
}
  • No labels