WorkflowView.java 5.69 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.workflow.exec.Workflow
 *  com.day.crx.statistics.PathBuilder
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 */
package com.day.cq.workflow.impl.statistics;

import com.day.cq.workflow.exec.Workflow;
import com.day.cq.workflow.impl.statistics.AbstractWorkflowView;
import com.day.cq.workflow.impl.statistics.WorkflowPathViewBuilder;
import com.day.crx.statistics.PathBuilder;
import java.util.Date;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;

public class WorkflowView
extends AbstractWorkflowView {
    private static final String TOTAL_TIME = "totalTime";
    private static final String TOTAL_COUNT = "totalCount";
    private static final String ABORTED_WORKFLOWS = "abortedWorkflows";
    private static final String RUNNING_WORKFLOWS = "runningWorkflows";
    private static final String SUSPENDED_WORKFLOWS = "suspendedWorkflows";
    private static final String COMPLETED_WORKFLOWS = "completedWorkflows";
    private static final String MAX_THROUGHPUT_TIME = "maxThroughputTime";
    private static final String MIN_THROUGHPUT_TIME = "minThroughputTime";
    private Object eventType;
    private long executionTime = 0;

    public WorkflowView(String pathPrefix, Workflow workflow, Object eventType) {
        super(pathPrefix);
        this.eventType = eventType;
        if (workflow != null && eventType.equals("WorkflowCompleted")) {
            this.executionTime = this.getTime(workflow);
        }
    }

    protected PathBuilder getPathBuilder() {
        return new WorkflowPathViewBuilder();
    }

    public void write(Node node) throws RepositoryException {
        if (this.eventType.equals("WorkflowStarted")) {
            this.updateCount(node, "runningWorkflows", true);
        } else if (this.eventType.equals("WorkflowResumed")) {
            this.updateCount(node, "runningWorkflows", true);
            this.updateCount(node, "suspendedWorkflows", false);
        } else if (this.eventType.equals("WorkflowAborted")) {
            this.updateCount(node, "runningWorkflows", false);
            this.updateCount(node, "abortedWorkflows", true);
        } else if (this.eventType.equals("WorkflowCompleted")) {
            this.setMaxThroughput(node, this.executionTime);
            this.setMinThroughput(node, this.executionTime);
            this.updateCount(node, "runningWorkflows", false);
            this.updateCount(node, "completedWorkflows", true);
            this.setTotalTime(node, this.executionTime);
            this.updateCount(node, "totalCount", true);
        } else if (this.eventType.equals("WorkflowSuspended")) {
            this.updateCount(node, "runningWorkflows", false);
            this.updateCount(node, "suspendedWorkflows", true);
        }
    }

    public long getMinThroughputTime(Session session) {
        return this.get(session, "minThroughputTime");
    }

    public long getMaxThroughputTime(Session session) {
        return this.get(session, "maxThroughputTime");
    }

    public long getNumOfCompleted(Session session) {
        return this.get(session, "completedWorkflows");
    }

    public long getNumOfSuspended(Session session) {
        return this.get(session, "suspendedWorkflows");
    }

    public long getNumOfAborted(Session session) {
        return this.get(session, "abortedWorkflows");
    }

    public long getTotalCount(Session session) {
        return this.get(session, "totalCount");
    }

    public long getTotalTime(Session session) {
        return this.get(session, "totalTime");
    }

    public long getNumOfRunning(Session session) {
        return this.get(session, "runningWorkflows");
    }

    public long getAverageThroughputTime(Session session) {
        long count = this.get(session, "totalCount");
        long time = this.get(session, "totalTime");
        return count == 0 ? 0 : time / count;
    }

    private void setTotalTime(Node node, long executionTime) throws RepositoryException {
        long totalTime = 0;
        if (node.hasProperty("totalTime")) {
            totalTime = node.getProperty("totalTime").getLong();
        }
        node.setProperty("totalTime", totalTime + executionTime);
    }

    private void setMaxThroughput(Node node, long executionTime) throws RepositoryException {
        long maxThroughput = 0;
        if (node.hasProperty("maxThroughputTime")) {
            maxThroughput = node.getProperty("maxThroughputTime").getLong();
        }
        if (executionTime > maxThroughput) {
            node.setProperty("maxThroughputTime", executionTime);
        }
    }

    private void setMinThroughput(Node node, long executionTime) throws RepositoryException {
        long minThroughput = -1;
        if (node.hasProperty("minThroughputTime")) {
            minThroughput = node.getProperty("minThroughputTime").getLong();
        }
        if (executionTime < minThroughput || minThroughput < 0) {
            node.setProperty("minThroughputTime", executionTime);
        }
    }

    private void updateCount(Node node, String propName, boolean doIncrement) throws RepositoryException {
        long count = 0;
        if (node.hasProperty(propName)) {
            count = node.getProperty(propName).getLong();
        }
        count = doIncrement ? ++count : --count;
        node.setProperty(propName, count > 0 ? count : 0);
    }

    private long getTime(Workflow instance) {
        long startTime = instance.getTimeStarted() != null ? instance.getTimeStarted().getTime() : 0;
        long endTime = instance.getTimeEnded() != null ? instance.getTimeEnded().getTime() : startTime;
        return endTime - startTime;
    }
}