Skip to content

Commit

Permalink
Fix "Clean up operation statistics" button
Browse files Browse the repository at this point in the history
Now it erases also statistics in the activity state tree.
(In a single task, currently.)

To review: changed PageTask.createDeleteItemDelta method.
  • Loading branch information
mederly committed Jul 7, 2021
1 parent fab7dc9 commit 6009f48
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import com.evolveum.midpoint.gui.api.prism.ItemStatus;

import com.evolveum.midpoint.schema.statistics.ActivityStatisticsUtil;
import com.evolveum.midpoint.schema.util.task.ActivityStateUtil;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.wicket.Page;
import org.apache.wicket.ajax.AjaxRequestTarget;
Expand Down Expand Up @@ -381,7 +385,7 @@ public StringResourceModel getTitle() {
@Override
public void yesPerformed(AjaxRequestTarget target) {
try {
deleteItem(target, TaskType.F_OPERATION_STATS);
deleteStatistics(target);
} catch (Exception e) {
LOGGER.error("Cannot delete task operation statistics, {}", e.getMessage(), e);
getSession().error(PageTask.this.getString("PageTask.cleanup.operationStatistics.failed"));
Expand All @@ -396,12 +400,19 @@ public void yesPerformed(AjaxRequestTarget target) {
repeatingView.add(cleanupPerformance);
}

private void deleteItem(AjaxRequestTarget target, ItemName... itemName) throws SchemaException {
private void deleteStatistics(AjaxRequestTarget target) throws SchemaException {
List<ItemPath> statisticsPaths = new ArrayList<>();
statisticsPaths.add(TaskType.F_OPERATION_STATS);
statisticsPaths.addAll(ActivityStatisticsUtil.getAllStatisticsPaths(getTask()));
deleteItem(target, statisticsPaths.toArray(new ItemPath[0]));
}

private void deleteItem(AjaxRequestTarget target, ItemPath... paths) throws SchemaException {
Collection<ItemDelta<?, ?>> itemDeltas = new ArrayList<>();
for (ItemName item : itemName) {
ItemDelta<?, ?> delta = createDeleteItemDelta(item);
for (ItemPath path : paths) {
ItemDelta<?, ?> delta = createDeleteItemDelta(path);
if (delta == null) {
LOGGER.trace("Nothing to delete for {}", item);
LOGGER.trace("Nothing to delete for {}", path);
continue;
}
itemDeltas.add(delta);
Expand Down Expand Up @@ -446,27 +457,12 @@ public void yesPerformed(AjaxRequestTarget target) {
repeatingView.add(cleanupResults);
}

private ItemDelta<?, ?> createDeleteItemDelta(ItemName itemName) throws SchemaException {
ItemWrapper<?, ?> item = getObjectWrapper().findItem(itemName, ItemWrapper.class);
if (item == null) {
return null;
}

PrismValueWrapper<?> itemValue = item.getValue();
if (itemValue == null) {
return null;
}

PrismValue newValue = itemValue.getNewValue();
if (newValue == null || newValue.isEmpty()) {
return null;
}

private ItemDelta<?, ?> createDeleteItemDelta(ItemPath itemPath) throws SchemaException {
// Originally here was a code that looked at item wrapper - why?
// Removed because it didn't allow to remove values not covered by wrapper, like activityState/activity/statistics.
return getPrismContext().deltaFor(TaskType.class)
.item(itemName)
.replace()
.item(itemPath).replace()
.asItemDelta();

}

private void saveTaskChanges(AjaxRequestTarget target, ObjectDelta<TaskType> taskDelta) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package com.evolveum.midpoint.schema.statistics;

import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.schema.util.task.ActivityItemProcessingStatisticsUtil;
import com.evolveum.midpoint.schema.util.task.ActivityPath;

Expand All @@ -16,6 +17,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -134,4 +136,32 @@ private static Stream<ObjectActionsExecutedEntryType> getAllActionsExecuted(
state.getStatistics().getActionsExecuted() != null ?
state.getStatistics().getActionsExecuted().getObjectActionsEntry().stream() : Stream.empty();
}

/**
* Returns all paths in activity states that point to the statistics.
* Local to the current task!
*/
public static List<ItemPath> getAllStatisticsPaths(@NotNull TaskType task) {
return getStatePathsStream(task)
.map(path -> path.append(ActivityStateType.F_STATISTICS))
.collect(Collectors.toList());
}

public static Stream<ItemPath> getStatePathsStream(@NotNull TaskType task) {
ItemPath rootPath = ItemPath.create(TaskType.F_ACTIVITY_STATE, TaskActivityStateType.F_ACTIVITY);
ActivityStateType rootActivity = task.getActivityState() != null ? task.getActivityState().getActivity() : null;

if (rootActivity == null) {
return Stream.empty();
} else {
return Stream.concat(
Stream.of(rootPath),
getStatePathsStream(rootActivity.getActivity(), rootPath.append(ActivityStateType.F_ACTIVITY)));
}
}

private static Stream<ItemPath> getStatePathsStream(@NotNull List<ActivityStateType> states, @NotNull ItemPath path) {
return states.stream()
.map(state -> path.append(state.getId()));
}
}

0 comments on commit 6009f48

Please sign in to comment.