001/* 002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * <a href="mailto:grenard@nuxeo.com">Guillaume Renard</a> 016 * 017 */ 018 019package org.nuxeo.ecm.platform.routing.core.audit; 020 021import java.util.Date; 022import java.util.HashMap; 023import java.util.List; 024import java.util.Map; 025 026import org.apache.commons.lang.StringUtils; 027import org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData; 028import org.nuxeo.ecm.platform.audit.api.FilterMapEntry; 029import org.nuxeo.ecm.platform.audit.api.LogEntry; 030import org.nuxeo.ecm.platform.audit.api.Logs; 031import org.nuxeo.ecm.platform.routing.api.DocumentRoutingConstants; 032import org.nuxeo.runtime.api.Framework; 033 034/** 035 * Helper method related to Routing Audit. 036 * 037 * @since 7.4 038 */ 039public final class RoutingAuditHelper { 040 041 public static final String TIME_SINCE_WF_STARTED = "timeSinceWfStarted"; 042 043 public static final String TIME_SINCE_TASK_STARTED = "timeSinceTaskStarted"; 044 045 public static final String TASK_ACTOR = "taskActor"; 046 047 public static final String WORKFLOW_INITATIOR = "workflowInitiator"; 048 049 public static final String WORKFLOW_VARIABLES = "workflowVariables"; 050 051 /** 052 * Query the audit for an entry of the Routing category matching the given event and returns the time elapsed since it is recorded. 053 * 054 * @param event 055 * @param elementId 056 * @return 057 * @since 7.4 058 */ 059 public static long computeElapsedTime(DocumentRoutingConstants.Events event, String elementId) { 060 Logs logs = Framework.getService(Logs.class); 061 if (logs != null && StringUtils.isNotBlank(elementId)) { 062 Map<String, FilterMapEntry> filterMap = new HashMap<String, FilterMapEntry>(); 063 064 FilterMapEntry categoryFilterMapEntry = new FilterMapEntry(); 065 categoryFilterMapEntry.setColumnName(BuiltinLogEntryData.LOG_CATEGORY); 066 categoryFilterMapEntry.setOperator("="); 067 categoryFilterMapEntry.setQueryParameterName(BuiltinLogEntryData.LOG_CATEGORY); 068 categoryFilterMapEntry.setObject(DocumentRoutingConstants.ROUTING_CATEGORY); 069 filterMap.put(BuiltinLogEntryData.LOG_CATEGORY, categoryFilterMapEntry); 070 071 FilterMapEntry eventIdFilterMapEntry = new FilterMapEntry(); 072 eventIdFilterMapEntry.setColumnName(BuiltinLogEntryData.LOG_EVENT_ID); 073 eventIdFilterMapEntry.setOperator("="); 074 eventIdFilterMapEntry.setQueryParameterName(BuiltinLogEntryData.LOG_EVENT_ID); 075 eventIdFilterMapEntry.setObject(event.name()); 076 filterMap.put(BuiltinLogEntryData.LOG_EVENT_ID, eventIdFilterMapEntry); 077 078 List<LogEntry> logEntries = logs.getLogEntriesFor(elementId, filterMap, true); 079 for (LogEntry logEntry : logEntries) { 080 Date start = logEntry.getEventDate(); 081 return new Date().getTime() - start.getTime(); 082 } 083 } 084 return -1; 085 } 086 087 /** 088 * Return the elapsed time since a workflow had started. 089 * 090 * @param workflowInstanceId the workflowInstanceId 091 * @return elapsed time in ms 092 * @since 7.4 093 */ 094 public static long computeDurationSinceWfStarted(String workflowInstanceId) { 095 return RoutingAuditHelper.computeElapsedTime(DocumentRoutingConstants.Events.afterWorkflowStarted, workflowInstanceId); 096 } 097 098 /** 099 * Return the elapsed time since a task had started. 100 * 101 * @param taskId the taskId 102 * @return elapsed time in ms 103 * @since 7.4 104 */ 105 public static long computeDurationSinceTaskStarted(String taskId) { 106 return RoutingAuditHelper.computeElapsedTime(DocumentRoutingConstants.Events.afterWorkflowTaskCreated, taskId); 107 } 108}