001/* 002 * (C) Copyright 2015-2018 Nuxeo (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.lang3.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 * @since 7.4 057 */ 058 public static long computeElapsedTime(DocumentRoutingConstants.Events event, String elementId) { 059 Logs logs = Framework.getService(Logs.class); 060 if (logs != null && StringUtils.isNotBlank(elementId)) { 061 Map<String, FilterMapEntry> filterMap = new HashMap<>(); 062 063 FilterMapEntry categoryFilterMapEntry = new FilterMapEntry(); 064 categoryFilterMapEntry.setColumnName(BuiltinLogEntryData.LOG_CATEGORY); 065 categoryFilterMapEntry.setOperator("="); 066 categoryFilterMapEntry.setQueryParameterName(BuiltinLogEntryData.LOG_CATEGORY); 067 categoryFilterMapEntry.setObject(DocumentRoutingConstants.ROUTING_CATEGORY); 068 filterMap.put(BuiltinLogEntryData.LOG_CATEGORY, categoryFilterMapEntry); 069 070 FilterMapEntry eventIdFilterMapEntry = new FilterMapEntry(); 071 eventIdFilterMapEntry.setColumnName(BuiltinLogEntryData.LOG_EVENT_ID); 072 eventIdFilterMapEntry.setOperator("="); 073 eventIdFilterMapEntry.setQueryParameterName(BuiltinLogEntryData.LOG_EVENT_ID); 074 eventIdFilterMapEntry.setObject(event.name()); 075 filterMap.put(BuiltinLogEntryData.LOG_EVENT_ID, eventIdFilterMapEntry); 076 077 List<LogEntry> logEntries = logs.getLogEntriesFor(elementId, filterMap, true); 078 if (logEntries.size() > 0) { 079 LogEntry logEntry = logEntries.get(0); 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}