Skip to content

Commit

Permalink
chore(admin): add endpoint to count unique task workers
Browse files Browse the repository at this point in the history
Related to CAM-12486
Closes #320
  • Loading branch information
tasso94 committed Sep 24, 2020
1 parent 8324b85 commit 7ec4a8a
Show file tree
Hide file tree
Showing 5 changed files with 361 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Set;

import org.camunda.bpm.admin.impl.plugin.resources.AdminPluginsRootResource;
import org.camunda.bpm.admin.impl.plugin.resources.BaseRootResource;
import org.camunda.bpm.admin.plugin.spi.impl.AbstractAdminPlugin;

/**
Expand All @@ -40,6 +41,7 @@ public Set<Class<?>> getResourceClasses() {
HashSet<Class<?>> classes = new HashSet<Class<?>>();

classes.add(AdminPluginsRootResource.class);
classes.add(BaseRootResource.class);

return classes;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
* under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. Camunda licenses this file to you under the Apache License,
* Version 2.0; you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.camunda.bpm.admin.impl.plugin.resources;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.camunda.bpm.admin.resource.AbstractAdminPluginRootResource;
import org.camunda.bpm.engine.rest.util.ProvidersUtil;

import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.Providers;

@Path("plugin/base")
public class BaseRootResource extends AbstractAdminPluginRootResource {

@Context
protected Providers providers;

public BaseRootResource() {
super("base");
}

@Path("{engine}" + MetricsRestService.PATH)
public MetricsRestService getProcessDefinitionResource(@PathParam("engine") String engineName) {
MetricsRestService metricsRestService = new MetricsRestService(engineName);
metricsRestService.setObjectMapper(getObjectMapper());
return subResource(metricsRestService, engineName);
}

protected ObjectMapper getObjectMapper() {
if (providers != null) {
return ProvidersUtil
.resolveFromContext(providers, ObjectMapper.class, MediaType.APPLICATION_JSON_TYPE, this.getClass());
} else {
return null;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
* under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. Camunda licenses this file to you under the Apache License,
* Version 2.0; you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.camunda.bpm.admin.impl.plugin.resources;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.camunda.bpm.admin.Admin;
import org.camunda.bpm.admin.resource.AbstractAdminPluginResource;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.camunda.bpm.engine.rest.dto.converter.DateConverter;
import org.camunda.bpm.engine.rest.dto.metrics.MetricsResultDto;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import java.util.Date;

@Produces(MediaType.APPLICATION_JSON)
public class MetricsRestService extends AbstractAdminPluginResource {

public static final String PATH = "/metrics";

public static final String QUERY_PARAM_START_DATE = "startDate";
public static final String QUERY_PARAM_END_DATE = "endDate";

protected ObjectMapper objectMapper;

public MetricsRestService(String engineName) {
super(engineName);
}

@GET
@Path("/task-worker/sum")
@Produces(MediaType.APPLICATION_JSON)
public MetricsResultDto countUniqueTaskWorkers(@QueryParam(QUERY_PARAM_START_DATE) String startDateAsString,
@QueryParam(QUERY_PARAM_END_DATE) String endDateAsString) {
ProcessEngine processEngine = Admin.getRuntimeDelegate().getProcessEngine(engineName);
ProcessEngineConfigurationImpl engineConfig =
((ProcessEngineConfigurationImpl)processEngine.getProcessEngineConfiguration());
return engineConfig.getCommandExecutorTxRequired().execute(commandContext -> {
// analogous to metrics, no permission check is performed

DateConverter dateConverter = new DateConverter();
dateConverter.setObjectMapper(objectMapper);

Date startDate = convertToDate(dateConverter, startDateAsString);
Date endDate = convertToDate(dateConverter, endDateAsString);

MetricsResultDto result = new MetricsResultDto();
long count = commandContext.getHistoricTaskInstanceManager()
.findUniqueTaskWorkerCount(startDate, endDate);

result.setResult(count);

return result;
});
}

protected Date convertToDate(DateConverter dateConverter,
String dateAsString) {
return dateConverter.convertQueryParameterToType(dateAsString);
}

public void setObjectMapper(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
* under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. Camunda licenses this file to you under the Apache License,
* Version 2.0; you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.camunda.bpm.admin.plugin.base;

import org.apache.ibatis.logging.LogFactory;
import org.camunda.bpm.admin.Admin;
import org.camunda.bpm.admin.impl.DefaultAdminRuntimeDelegate;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.impl.util.LogUtil;
import org.camunda.bpm.engine.test.ProcessEngineRule;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;

public abstract class AbstractAdminPluginTest {

protected static final TestAdminRuntimeDelegate RUNTIME_DELEGATE = new TestAdminRuntimeDelegate();

static {
LogUtil.readJavaUtilLoggingConfigFromClasspath();
LogFactory.useJdkLogging();
}

@Rule
public ProcessEngineRule processEngineRule = new ProcessEngineRule(true);

@BeforeClass
public static void beforeClass() {
Admin.setAdminRuntimeDelegate(RUNTIME_DELEGATE);
}

@AfterClass
public static void afterClass() {
Admin.setAdminRuntimeDelegate(null);
}

@Before
public void before() {
RUNTIME_DELEGATE.ENGINE = getProcessEngine();
}

@After
public void after() {
RUNTIME_DELEGATE.ENGINE = null;
getProcessEngine().getIdentityService().clearAuthentication();
}

public ProcessEngine getProcessEngine() {
return processEngineRule.getProcessEngine();
}

private static class TestAdminRuntimeDelegate extends DefaultAdminRuntimeDelegate {

public ProcessEngine ENGINE;

@Override
public ProcessEngine getProcessEngine(String processEngineName) {

// always return default engine for plugin tests
return ENGINE;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
* under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. Camunda licenses this file to you under the Apache License,
* Version 2.0; you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.camunda.bpm.admin.plugin.base;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.camunda.bpm.admin.impl.plugin.resources.MetricsRestService;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.ProcessEngineConfiguration;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.impl.util.ClockUtil;
import org.camunda.bpm.engine.rest.dto.metrics.MetricsResultDto;
import org.camunda.bpm.engine.test.Deployment;
import org.camunda.bpm.engine.test.RequiredHistoryLevel;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import static org.assertj.core.api.Assertions.assertThat;

@RequiredHistoryLevel(ProcessEngineConfiguration.HISTORY_ACTIVITY)
public class MetricsRestServiceTest extends AbstractAdminPluginTest {

public static final SimpleDateFormat DATE_FORMAT =
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

protected MetricsRestService resource;
protected ProcessEngine processEngine;
protected RuntimeService runtimeService;
protected TaskService taskService;

@Before
public void setUp() {
super.before();
processEngine = getProcessEngine();
runtimeService = processEngine.getRuntimeService();
resource = new MetricsRestService(processEngine.getName());
ObjectMapper objectMapper = new ObjectMapper();
resource.setObjectMapper(objectMapper);
taskService = processEngine.getTaskService();
}

@After
public void reset() {
ClockUtil.reset();
}

@Test
@Deployment(resources = {
"processes/user-task-process.bpmn"
})
public void shouldCountUniqueTaskWorkers_StartTimeInBetween() throws ParseException {
// given
Date currentTime = DATE_FORMAT.parse("2020-01-02T00:00:00.000+0100");
ClockUtil.setCurrentTime(currentTime);

runtimeService.startProcessInstanceByKey("userTaskProcess");
String taskId = taskService.createTaskQuery().singleResult().getId();

taskService.setAssignee(taskId, "foo");

String startDateAsString = "2020-01-01T00:00:00.000+0100";
String endDateAsString = "2020-01-03T00:00:00.000+0100";

// when
MetricsResultDto metricsResultDto =
resource.countUniqueTaskWorkers(startDateAsString, endDateAsString);

// then
assertThat(metricsResultDto.getResult()).isEqualTo(1L);
}

@Test
@Deployment(resources = {
"processes/user-task-process.bpmn"
})
public void shouldCountUniqueTaskWorkers_StartTimeBefore() throws ParseException {
// given
Date currentTime = DATE_FORMAT.parse("2019-12-31T00:00:00.000+0100");
ClockUtil.setCurrentTime(currentTime);

runtimeService.startProcessInstanceByKey("userTaskProcess");
String taskId = taskService.createTaskQuery().singleResult().getId();

taskService.setAssignee(taskId, "foo");

String startDateAsString = "2020-01-01T00:00:00.000+0100";
String endDateAsString = "2020-01-03T00:00:00.000+0100";

// when
MetricsResultDto metricsResultDto =
resource.countUniqueTaskWorkers(startDateAsString, endDateAsString);

// then
assertThat(metricsResultDto.getResult()).isEqualTo(0L);
}

@Test
@Deployment(resources = {
"processes/user-task-process.bpmn"
})
public void shouldCountUniqueTaskWorkers_StartTimeAfter() throws ParseException {
// given
Date currentTime = DATE_FORMAT.parse("2020-01-04T00:00:00.000+0100");
ClockUtil.setCurrentTime(currentTime);

runtimeService.startProcessInstanceByKey("userTaskProcess");
String taskId = taskService.createTaskQuery().singleResult().getId();

taskService.setAssignee(taskId, "foo");

String startDateAsString = "2020-01-01T00:00:00.000+0100";
String endDateAsString = "2020-01-03T00:00:00.000+0100";

// when
MetricsResultDto metricsResultDto =
resource.countUniqueTaskWorkers(startDateAsString, endDateAsString);

// then
assertThat(metricsResultDto.getResult()).isEqualTo(0L);
}

}

0 comments on commit 7ec4a8a

Please sign in to comment.