Skip to content

Commit

Permalink
Add SimulationResultManager#getMetricDefinition
Browse files Browse the repository at this point in the history
  • Loading branch information
mederly committed Jan 26, 2023
1 parent a3dc280 commit 837c5b4
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import com.evolveum.midpoint.model.api.ModelInteractionService;

import com.evolveum.midpoint.xml.ns._public.common.common_3.SimulationMetricDefinitionType;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.VisibleForTesting;
Expand Down Expand Up @@ -80,6 +82,9 @@ <X> X executeInSimulationMode(
@NotNull SimulatedFunctionCall<X> functionCall)
throws CommonException;

/** Returns the definition of a metric or `null` if there's none. */
@Nullable SimulationMetricDefinitionType getMetricDefinition(@NotNull String identifier);

interface SimulatedFunctionCall<X> {
X execute() throws CommonException;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static AggregatedMetricsComputation create(@NotNull OperationResult result) {
}

private void initialize(@NotNull OperationResult result) throws ConfigurationException {
List<SimulationMetricDefinitionType> definitions = simulationResultManager.getMetricDefinitions();
Collection<SimulationMetricDefinitionType> definitions = simulationResultManager.getMetricDefinitions();
LOGGER.trace("Processing {} global metric definitions", definitions.size());
for (SimulationMetricDefinitionType definition : definitions) {
SimulationMetricComputationType objectValueComputation = definition.getComputation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
Expand All @@ -47,13 +48,13 @@ class ObjectMetricsComputation<O extends ObjectType> {

@NotNull private final ProcessedObject<O> processedObject;
@NotNull private final LensElementContext<O> elementContext;
@NotNull private final List<SimulationMetricDefinitionType> metricDefinitions;
@NotNull private final Collection<SimulationMetricDefinitionType> metricDefinitions;
@NotNull private final Task task;

private ObjectMetricsComputation(
@NotNull ProcessedObject<O> processedObject,
@NotNull LensElementContext<O> elementContext,
@NotNull List<SimulationMetricDefinitionType> metricDefinitions,
@NotNull Collection<SimulationMetricDefinitionType> metricDefinitions,
@NotNull Task task) {
this.processedObject = processedObject;
this.elementContext = elementContext;
Expand All @@ -64,7 +65,7 @@ private ObjectMetricsComputation(
static <O extends ObjectType> List<SimulationProcessedObjectMetricValueType> computeAll(
ProcessedObject<O> processedObject,
LensElementContext<O> elementContext,
List<SimulationMetricDefinitionType> metricDefinitions,
Collection<SimulationMetricDefinitionType> metricDefinitions,
Task task,
OperationResult result) throws CommonException {
return new ObjectMetricsComputation<>(processedObject, elementContext, metricDefinitions, task)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public class SimulationResultManagerImpl implements SimulationResultManager, Sys
/** Global definitions provided by the system configuration. */
@NotNull private volatile List<SimulationDefinitionType> simulationDefinitions = new ArrayList<>();

/** Global metric definitions provided by the system configuration. */
@NotNull private volatile List<SimulationMetricDefinitionType> metricDefinitions = new ArrayList<>();
/** Global metric definitions provided by the system configuration. Keyed by identifier. Immutable. */
@NotNull private volatile Map<String, SimulationMetricDefinitionType> metricDefinitions = new HashMap<>();

/** Primitive way of checking we do not write to closed results. */
@VisibleForTesting
Expand Down Expand Up @@ -261,7 +261,11 @@ public void update(@Nullable SystemConfigurationType value) {
var configuration = value != null ? value.getSimulation() : null;
if (configuration != null) {
simulationDefinitions = CloneUtil.cloneCollectionMembers(configuration.getSimulation());
metricDefinitions = CloneUtil.cloneCollectionMembers(configuration.getMetric());
metricDefinitions = configuration.getMetric().stream()
.map(def -> CloneUtil.toImmutable(def))
.collect(Collectors.toMap(
def -> def.getIdentifier(),
def -> def));
}
}

Expand Down Expand Up @@ -331,8 +335,8 @@ public SimulationResultContext newSimulationContext(@NotNull String resultOid) {
return new SimulationResultContextImpl(this, resultOid);
}

@NotNull List<SimulationMetricDefinitionType> getMetricDefinitions() {
return metricDefinitions;
@NotNull Collection<SimulationMetricDefinitionType> getMetricDefinitions() {
return metricDefinitions.values();
}

@Override
Expand Down Expand Up @@ -371,6 +375,11 @@ public <X> X executeInSimulationMode(
return returnValue;
}

@Override
public @Nullable SimulationMetricDefinitionType getMetricDefinition(@NotNull String identifier) {
return metricDefinitions.get(identifier);
}

/**
* Checks that we do not write into closed {@link SimulationResultType}.
* Assumes {@link InternalsConfig#consistencyChecks} be `true`, i.e. usually not employed in production.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ public abstract class AbstractBasicSimulationExecutionTest extends AbstractSimul

abstract TaskExecutionMode getExecutionMode();

/** Checks whether we can obtain a definition for given metric. */
@Test
public void test010GetMetricDefinition() {
when("obtaining a definition for a known metric");
var def = simulationResultManager.getMetricDefinition(METRIC_ATTRIBUTE_MODIFICATIONS_ID);

then("it is OK");
assertThat(def).as("definition").isNotNull();
assertThat(def.getIdentifier()).as("identifier").isEqualTo(METRIC_ATTRIBUTE_MODIFICATIONS_ID);
assertThat(def.getComputation()).as("computation item").isNotNull();
assertThat(def.getDomain()).as("domain item").isNotNull();

when("obtaining a definition for unknown metric");
var def2 = simulationResultManager.getMetricDefinition("nonsense");

then("it's null");
assertThat(def2).isNull();
}

/**
* Creating a user without an account.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public class AbstractSimulationsTest extends AbstractEmptyModelIntegrationTest {
"6d8ba4fd-95ee-4d98-80c2-3a194b566f89",
"simple-development-source");

static final String METRIC_ATTRIBUTE_MODIFICATIONS_ID = "attribute-modifications";

@BeforeMethod
public void onNativeOnly() {
skipIfNotNativeRepository();
Expand Down

0 comments on commit 837c5b4

Please sign in to comment.