diff --git a/src/main/java/org/mitre/synthea/engine/Module.java b/src/main/java/org/mitre/synthea/engine/Module.java index 25018985ed..9a68d0e1c6 100644 --- a/src/main/java/org/mitre/synthea/engine/Module.java +++ b/src/main/java/org/mitre/synthea/engine/Module.java @@ -409,7 +409,7 @@ public boolean process(Person person, long time, boolean terminateOnDeath) { person.attributes.put(historyKey, person.history); /* TODO - determining whether or not this the first time a person has entered a submodule is currently not easily computed, so we use `true` below. */ - TransitionMetrics.getMetric(historyKey, initial.name).enter(true); + TransitionMetrics.enter(historyKey, initial.name, true); } person.history = (List) person.attributes.get(historyKey); State current = person.history.get(0); @@ -424,10 +424,10 @@ public boolean process(Person person, long time, boolean terminateOnDeath) { Long duration = (exited - entered); nextStateName = current.transition(person, time); boolean firstTime = !person.hadPriorState(nextStateName); - TransitionMetrics.getMetric(historyKey, current.name).exit(nextStateName, duration); + TransitionMetrics.exit(historyKey, current.name, nextStateName, duration); current = states.get(nextStateName).clone(); // clone the state so we don't dirty the original person.history.add(0, current); - TransitionMetrics.getMetric(historyKey, nextStateName).enter(firstTime); + TransitionMetrics.enter(historyKey, nextStateName, firstTime); if (exited != null && exited < time) { // stop if the patient died in the meantime... if (terminateOnDeath && !person.alive(exited)) { diff --git a/src/main/java/org/mitre/synthea/helpers/TransitionMetrics.java b/src/main/java/org/mitre/synthea/helpers/TransitionMetrics.java index 5b001beee8..9ac5049398 100644 --- a/src/main/java/org/mitre/synthea/helpers/TransitionMetrics.java +++ b/src/main/java/org/mitre/synthea/helpers/TransitionMetrics.java @@ -36,6 +36,34 @@ public abstract class TransitionMetrics { private static final Table metrics = Tables.synchronizedTable(HashBasedTable.create()); + public static boolean enabled = + Config.getAsBoolean("generate.track_detailed_transition_metrics", false); + + /** + * Track entering a state within a given module. + * @param module The name of the module. + * @param state The name of the state. + * @param firstTime Whether or not this state was previously entered. + */ + public static void enter(String module, String state, boolean firstTime) { + if (enabled) { + getMetric(module, state).enter(firstTime); + } + } + + /** + * Track exiting a state and the resulting destination. + * @param module The name of the module. + * @param state The name of the state. + * @param destination Target state that was transitioned to. + * @param duration The time in milliseconds spent within the state. + */ + public static void exit(String module, String state, String destination, long duration) { + if (enabled) { + getMetric(module, state).exit(destination, duration); + } + } + /** * Get the Metric object for the given State in the given Module. * @@ -43,7 +71,7 @@ public abstract class TransitionMetrics { * @param stateName Name of the state * @return Metric object */ - public static Metric getMetric(String moduleName, String stateName) { + static Metric getMetric(String moduleName, String stateName) { Metric metric = metrics.get(moduleName, stateName); if (metric == null) { @@ -79,7 +107,7 @@ public static void exportMetrics() { System.out.println("Saving metrics for " + metrics.rowKeySet().size() + " modules."); String baseDir = Config.get("exporter.baseDirectory", "./output/"); - String statsDir = "statistics"; + String statsDir = "metrics"; Path output = Paths.get(baseDir, statsDir); output.toFile().mkdirs(); @@ -145,7 +173,8 @@ public static class Metric { /** * Helper function to increment the count for a destination state. * - * @param destination Target state that was transitioned to + * @param destination Target state that was transitioned to. + * @param duration The time in milliseconds spent within the state. */ public void exit(String destination, long duration) { diff --git a/src/test/java/org/mitre/synthea/helpers/TransitionMetricsTest.java b/src/test/java/org/mitre/synthea/helpers/TransitionMetricsTest.java index 3b8009f62d..268add202a 100644 --- a/src/test/java/org/mitre/synthea/helpers/TransitionMetricsTest.java +++ b/src/test/java/org/mitre/synthea/helpers/TransitionMetricsTest.java @@ -30,6 +30,7 @@ public static void setup() { @Test public void testExampleModule() throws Exception { + TransitionMetrics.enabled = true; TransitionMetrics.clear(); Provider mockProvider = TestHelper.buildMockProvider(); @@ -96,6 +97,7 @@ public void testExampleModule() throws Exception { m = TransitionMetrics.getMetric(example.name, "Terminal"); assertEquals(3, m.entered.get()); assertEquals(3, m.current.get()); + TransitionMetrics.enabled = false; } private long run(Person person, Module singleModule, long start) {