Skip to content

Commit

Permalink
Moved start/end messages for bundle process from igbundleprocessor to…
Browse files Browse the repository at this point in the history
… abstract class. Finished message won't appear if no resources exist for bundling.
  • Loading branch information
Evan Chicoine committed Dec 18, 2023
1 parent 0278789 commit 0825b15
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected Map<String, IBaseResource> getResources(FhirContext fhirContext) {
}

@Override
protected String getResourceProcessorType() {
protected String getResourceBundlerType() {
return TYPE_MEASURE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
import org.opencds.cqf.tooling.common.ThreadUtils;
import org.opencds.cqf.tooling.cql.exception.CqlTranslatorException;
import org.opencds.cqf.tooling.library.LibraryProcessor;
import org.opencds.cqf.tooling.measure.MeasureBundler;
import org.opencds.cqf.tooling.utilities.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executors;

/**
* An abstract base class for bundlers that handle the bundling of various types of resources within an ig.
Expand Down Expand Up @@ -82,7 +82,7 @@ protected List<Object> getIdentifiers() {
}

private String getResourcePrefix() {
return getResourceProcessorType().toLowerCase() + "-";
return getResourceBundlerType().toLowerCase() + "-";
}

protected abstract Set<String> getPaths(FhirContext fhirContext);
Expand All @@ -94,8 +94,11 @@ private String getResourcePrefix() {
*/
protected abstract Map<String, IBaseResource> getResources(FhirContext fhirContext);

protected abstract String getResourceProcessorType();
protected abstract String getResourceBundlerType();

private String getTime() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
}

/**
* Bundles resources within an Implementation Guide based on specified options.
Expand All @@ -115,6 +118,7 @@ private String getResourcePrefix() {
public void bundleResources(ArrayList<String> refreshedLibraryNames, String igPath, List<String> binaryPaths, Boolean includeDependencies,
Boolean includeTerminology, Boolean includePatientScenarios, Boolean includeVersion, Boolean addBundleTimestamp,
FhirContext fhirContext, String fhirUri, IOUtils.Encoding encoding, Boolean verboseMessaging) {
System.out.println("\r\n[Bundle " + getResourceBundlerType() + " has started - " + getTime() + "]\r\n");

final List<String> bundledResources = new CopyOnWriteArrayList<>();

Expand All @@ -140,7 +144,7 @@ public void bundleResources(ArrayList<String> refreshedLibraryNames, String igPa
final Map<String, String> libraryPathMap = new ConcurrentHashMap<>(IOUtils.getLibraryPathMap(fhirContext));

if (resourcesMap.isEmpty()) {
System.out.println("[INFO] No " + getResourceProcessorType() + "s found. Continuing...");
System.out.println("[INFO] No " + getResourceBundlerType() + "s found. Continuing...");
return;
}

Expand All @@ -167,19 +171,19 @@ public void bundleResources(ArrayList<String> refreshedLibraryNames, String igPa
tasks.add(() -> {
//check if resourceSourcePath has been processed before:
if (processedResources.contains(resourceSourcePath)) {
System.out.println(getResourceProcessorType() + " processed already: " + resourceSourcePath);
System.out.println(getResourceBundlerType() + " processed already: " + resourceSourcePath);
return null;
}
String resourceName = FilenameUtils.getBaseName(resourceSourcePath).replace(getResourcePrefix(), "");

try {
Map<String, IBaseResource> resources = new ConcurrentHashMap<>();
Boolean shouldPersist = ResourceUtils.safeAddResource(resourceSourcePath, resources, fhirContext);
if (!resources.containsKey(getResourceProcessorType() + "/" + resourceEntry.getKey())) {
throw new IllegalArgumentException(String.format("Could not retrieve base resource for " + getResourceProcessorType() + " %s", resourceName));
if (!resources.containsKey(getResourceBundlerType() + "/" + resourceEntry.getKey())) {
throw new IllegalArgumentException(String.format("Could not retrieve base resource for " + getResourceBundlerType() + " %s", resourceName));
}

IBaseResource resource = resources.get(getResourceProcessorType() + "/" + resourceEntry.getKey());
IBaseResource resource = resources.get(getResourceBundlerType() + "/" + resourceEntry.getKey());
String primaryLibraryUrl = ResourceUtils.getPrimaryLibraryUrl(resource, fhirContext);
IBaseResource primaryLibrary;
if (primaryLibraryUrl != null && primaryLibraryUrl.startsWith("http")) {
Expand Down Expand Up @@ -225,7 +229,7 @@ public void bundleResources(ArrayList<String> refreshedLibraryNames, String igPa
try {
libraryProcessor.bundleLibraryDependencies(primaryLibrarySourcePath, fhirContext, resources, encoding, includeVersion);
} catch (Exception bre) {
failedExceptionMessages.put(resourceSourcePath, getResourceProcessorType() + " will not be bundled because Library Dependency bundling failed: " + bre.getMessage());
failedExceptionMessages.put(resourceSourcePath, getResourceBundlerType() + " will not be bundled because Library Dependency bundling failed: " + bre.getMessage());
//exit from task:
return null;
}
Expand All @@ -235,7 +239,7 @@ public void bundleResources(ArrayList<String> refreshedLibraryNames, String igPa
try {
TestCaseProcessor.bundleTestCases(igPath, getResourceTestGroupName(), primaryLibraryName, fhirContext, resources);
} catch (Exception tce) {
failedExceptionMessages.put(resourceSourcePath, getResourceProcessorType() + " will not be bundled because Test Case bundling failed: " + tce.getMessage());
failedExceptionMessages.put(resourceSourcePath, getResourceBundlerType() + " will not be bundled because Test Case bundling failed: " + tce.getMessage());
//exit from task:
return null;
}
Expand Down Expand Up @@ -291,7 +295,7 @@ public void bundleResources(ArrayList<String> refreshedLibraryNames, String igPa
ThreadUtils.executeTasks(tasks);

} catch (Exception e) {
LogUtils.putException("bundleResources: " + getResourceProcessorType(), e);
LogUtils.putException("bundleResources: " + getResourceBundlerType(), e);
}

//Prepare final report:
Expand All @@ -300,7 +304,7 @@ public void bundleResources(ArrayList<String> refreshedLibraryNames, String igPa
//Give user a snapshot of the files each resource will have persisted to their FHIR server (if fhirUri is provided)
final int persistCount = persistedFileReport.size();
if (persistCount > 0) {
message.append(NEWLINE).append(persistCount).append(" ").append(getResourceProcessorType()).append("(s) have POST tasks in the queue for ").append(fhirUri).append(": ");
message.append(NEWLINE).append(persistCount).append(" ").append(getResourceBundlerType()).append("(s) have POST tasks in the queue for ").append(fhirUri).append(": ");
int totalQueueCount = 0;
for (String library : persistedFileReport.keySet()) {
totalQueueCount = totalQueueCount + persistedFileReport.get(library);
Expand All @@ -318,7 +322,7 @@ public void bundleResources(ArrayList<String> refreshedLibraryNames, String igPa

final int bundledCount = bundledResources.size();
if (bundledCount > 0) {
message.append(NEWLINE).append(bundledCount).append(" ").append(getResourceProcessorType()).append("(s) successfully bundled:");
message.append(NEWLINE).append(bundledCount).append(" ").append(getResourceBundlerType()).append("(s) successfully bundled:");
for (String bundledResource : bundledResources) {
message.append(NEWLINE_INDENT).append(bundledResource).append(" BUNDLED");
}
Expand All @@ -334,7 +338,7 @@ public void bundleResources(ArrayList<String> refreshedLibraryNames, String igPa

final int refreshedNotBundledCount = resourcePathLibraryNames.size();
if (refreshedNotBundledCount > 0) {
message.append(NEWLINE).append(refreshedNotBundledCount).append(" ").append(getResourceProcessorType()).append("(s) refreshed, but not bundled (due to issues):");
message.append(NEWLINE).append(refreshedNotBundledCount).append(" ").append(getResourceBundlerType()).append("(s) refreshed, but not bundled (due to issues):");
for (String notBundled : resourcePathLibraryNames) {
message.append(NEWLINE_INDENT).append(notBundled).append(" REFRESHED");
}
Expand All @@ -346,7 +350,7 @@ public void bundleResources(ArrayList<String> refreshedLibraryNames, String igPa

final int failedCount = failedResources.size();
if (failedCount > 0) {
message.append(NEWLINE).append(failedCount).append(" ").append(getResourceProcessorType()).append("(s) failed refresh:");
message.append(NEWLINE).append(failedCount).append(" ").append(getResourceBundlerType()).append("(s) failed refresh:");
for (String failed : failedResources) {
if (failedExceptionMessages.containsKey(failed) && verboseMessaging) {
message.append(NEWLINE_INDENT).append(failed).append(" FAILED: ").append(failedExceptionMessages.get(failed));
Expand All @@ -359,7 +363,7 @@ public void bundleResources(ArrayList<String> refreshedLibraryNames, String igPa
//Exceptions stemming from IOUtils.translate that did not prevent process from completing for file:
final int translateErrorCount = cqlTranslatorErrorMessages.size();
if (translateErrorCount > 0) {
message.append(NEWLINE).append(cqlTranslatorErrorMessages.size()).append(" ").append(getResourceProcessorType()).append("(s) encountered CQL translator errors:");
message.append(NEWLINE).append(cqlTranslatorErrorMessages.size()).append(" ").append(getResourceBundlerType()).append("(s) encountered CQL translator errors:");

for (String library : cqlTranslatorErrorMessages.keySet()) {
message.append(NEWLINE_INDENT).append(
Expand All @@ -369,16 +373,17 @@ public void bundleResources(ArrayList<String> refreshedLibraryNames, String igPa
}

System.out.println(message);
System.out.println("\r\n[Bundle " + getResourceBundlerType() + " has finished - " + getTime() + "]\r\n");
}


private void reportProgress(int count, int total) {
double percentage = (double) count / total * 100;
System.out.print("\rBundle " + getResourceProcessorType() + "s: " + String.format("%.2f%%", percentage) + " processed.");
System.out.print("\rBundle " + getResourceBundlerType() + "s: " + String.format("%.2f%%", percentage) + " processed.");
}

private String getResourceTestGroupName() {
return getResourceProcessorType().toLowerCase();
return getResourceBundlerType().toLowerCase();
}

private void persistBundle(String bundleDestPath, String libraryName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,18 @@ public void bundleIg(ArrayList<String> refreshedLibraryNames, String igPath, Lis
Boolean includeDependencies, Boolean includeTerminology, Boolean includePatientScenarios, Boolean versioned, Boolean addBundleTimestamp,
FhirContext fhirContext, String fhirUri) {

System.out.println("\n");

System.out.println("\r\n[Bundle Measures has started - " + getTime() + "]\r\n");
new MeasureBundler().bundleResources(refreshedLibraryNames,
igPath, binaryPaths, includeDependencies, includeTerminology,
includePatientScenarios, versioned, addBundleTimestamp, fhirContext,
fhirUri, encoding, verboseMessaging);

System.out.println("\r\n[Bundle Measures has finished - " + getTime() + "]\r\n");


System.out.println("\r\n[Bundle PlanDefinitions has started - " + getTime() + "]\r\n");
new PlanDefinitionBundler(this.libraryProcessor, this.cdsHooksProcessor).bundleResources(refreshedLibraryNames,
igPath, binaryPaths, includeDependencies, includeTerminology,
includePatientScenarios, versioned, addBundleTimestamp, fhirContext,
fhirUri, encoding, verboseMessaging);
System.out.println("\r\n[Bundle PlanDefinitions has finished - " + getTime() + "]\r\n");



System.out.println("\r\n[Bundle Questionnaires has started - " + getTime() + "]\r\n");
new QuestionnaireBundler(this.libraryProcessor).bundleResources(refreshedLibraryNames,
igPath, binaryPaths, includeDependencies, includeTerminology,
includePatientScenarios, versioned, addBundleTimestamp, fhirContext,
fhirUri, encoding, verboseMessaging);
System.out.println("\r\n[Bundle Questionnaires has finished - " + getTime() + "]\r\n");



//run collected post calls last:
if (HttpClientUtils.hasPostTasksInQueue()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected Map<String, IBaseResource> getResources(FhirContext fhirContext) {
}

@Override
protected String getResourceProcessorType() {
protected String getResourceBundlerType() {
return TYPE_PLAN_DEFINITION;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected Map<String, IBaseResource> getResources(FhirContext fhirContext) {
}

@Override
protected String getResourceProcessorType() {
protected String getResourceBundlerType() {
return TYPE_QUESTIONNAIRE;
}

Expand Down

0 comments on commit 0825b15

Please sign in to comment.