001/* 002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * <a href="mailto:grenard@nuxeo.com">Guillaume Renard</a> 018 * 019 */ 020 021package org.nuxeo.ecm.platform.routing.core.audit; 022 023import java.util.Date; 024import java.util.HashMap; 025import java.util.List; 026import java.util.Map; 027 028import org.apache.commons.lang.StringUtils; 029import org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData; 030import org.nuxeo.ecm.platform.audit.api.FilterMapEntry; 031import org.nuxeo.ecm.platform.audit.api.LogEntry; 032import org.nuxeo.ecm.platform.audit.api.Logs; 033import org.nuxeo.ecm.platform.routing.api.DocumentRoutingConstants; 034import org.nuxeo.runtime.api.Framework; 035 036/** 037 * Helper method related to Routing Audit. 038 * 039 * @since 7.4 040 */ 041public final class RoutingAuditHelper { 042 043 public static final String TIME_SINCE_WF_STARTED = "timeSinceWfStarted"; 044 045 public static final String TIME_SINCE_TASK_STARTED = "timeSinceTaskStarted"; 046 047 public static final String TASK_ACTOR = "taskActor"; 048 049 public static final String WORKFLOW_INITATIOR = "workflowInitiator"; 050 051 public static final String WORKFLOW_VARIABLES = "workflowVariables"; 052 053 /** 054 * Query the audit for an entry of the Routing category matching the given event and returns the time elapsed since it is recorded. 055 * 056 * @param event 057 * @param elementId 058 * @return 059 * @since 7.4 060 */ 061 public static long computeElapsedTime(DocumentRoutingConstants.Events event, String elementId) { 062 Logs logs = Framework.getService(Logs.class); 063 if (logs != null && StringUtils.isNotBlank(elementId)) { 064 Map<String, FilterMapEntry> filterMap = new HashMap<String, FilterMapEntry>(); 065 066 FilterMapEntry categoryFilterMapEntry = new FilterMapEntry(); 067 categoryFilterMapEntry.setColumnName(BuiltinLogEntryData.LOG_CATEGORY); 068 categoryFilterMapEntry.setOperator("="); 069 categoryFilterMapEntry.setQueryParameterName(BuiltinLogEntryData.LOG_CATEGORY); 070 categoryFilterMapEntry.setObject(DocumentRoutingConstants.ROUTING_CATEGORY); 071 filterMap.put(BuiltinLogEntryData.LOG_CATEGORY, categoryFilterMapEntry); 072 073 FilterMapEntry eventIdFilterMapEntry = new FilterMapEntry(); 074 eventIdFilterMapEntry.setColumnName(BuiltinLogEntryData.LOG_EVENT_ID); 075 eventIdFilterMapEntry.setOperator("="); 076 eventIdFilterMapEntry.setQueryParameterName(BuiltinLogEntryData.LOG_EVENT_ID); 077 eventIdFilterMapEntry.setObject(event.name()); 078 filterMap.put(BuiltinLogEntryData.LOG_EVENT_ID, eventIdFilterMapEntry); 079 080 List<LogEntry> logEntries = logs.getLogEntriesFor(elementId, filterMap, true); 081 for (LogEntry logEntry : logEntries) { 082 Date start = logEntry.getEventDate(); 083 return new Date().getTime() - start.getTime(); 084 } 085 } 086 return -1; 087 } 088 089 /** 090 * Return the elapsed time since a workflow had started. 091 * 092 * @param workflowInstanceId the workflowInstanceId 093 * @return elapsed time in ms 094 * @since 7.4 095 */ 096 public static long computeDurationSinceWfStarted(String workflowInstanceId) { 097 return RoutingAuditHelper.computeElapsedTime(DocumentRoutingConstants.Events.afterWorkflowStarted, workflowInstanceId); 098 } 099 100 /** 101 * Return the elapsed time since a task had started. 102 * 103 * @param taskId the taskId 104 * @return elapsed time in ms 105 * @since 7.4 106 */ 107 public static long computeDurationSinceTaskStarted(String taskId) { 108 return RoutingAuditHelper.computeElapsedTime(DocumentRoutingConstants.Events.afterWorkflowTaskCreated, taskId); 109 } 110}